Search in sources :

Example 1 with TestUtils.genFixedLengthFile

use of com.aliyun.oss.integrationtests.TestUtils.genFixedLengthFile in project aliyun-oss-java-sdk by aliyun.

the class CRCChecksumTest method testEmpytObjectCRC.

@Test
public void testEmpytObjectCRC() {
    String key = "empty-object-crc";
    try {
        // put
        PutObjectResult putObjectResult = ossClient.putObject(bucketName, key, new ByteArrayInputStream(new String("").getBytes()));
        Assert.assertTrue(putObjectResult.getClientCRC() == 0L);
        Assert.assertTrue(putObjectResult.getServerCRC() == 0L);
        Assert.assertEquals(putObjectResult.getRequestId().length(), REQUEST_ID_LEN);
        String localFile = TestUtils.genFixedLengthFile(0);
        putObjectResult = ossClient.putObject(bucketName, key, new FileInputStream(localFile));
        Assert.assertEquals(putObjectResult.getClientCRC(), putObjectResult.getServerCRC());
        Assert.assertTrue(putObjectResult.getClientCRC() == 0L);
        Assert.assertTrue(putObjectResult.getServerCRC() == 0L);
        Assert.assertEquals(putObjectResult.getRequestId().length(), REQUEST_ID_LEN);
        putObjectResult = ossClient.putObject(bucketName, key, new File(localFile));
        Assert.assertEquals(putObjectResult.getClientCRC(), putObjectResult.getServerCRC());
        Assert.assertTrue(putObjectResult.getClientCRC() == 0L);
        Assert.assertTrue(putObjectResult.getServerCRC() == 0L);
        Assert.assertEquals(putObjectResult.getRequestId().length(), REQUEST_ID_LEN);
        // get
        OSSObject ossObject = ossClient.getObject(bucketName, key);
        Assert.assertNull(ossObject.getClientCRC());
        Assert.assertNotNull(ossObject.getServerCRC());
        Assert.assertEquals(ossObject.getRequestId().length(), REQUEST_ID_LEN);
        Assert.assertTrue(IOUtils.getCRCValue(ossObject.getObjectContent()) == 0L);
        InputStream content = ossObject.getObjectContent();
        while (content.read() != -1) {
        }
        ossObject.setClientCRC(IOUtils.getCRCValue(content));
        Assert.assertTrue(putObjectResult.getClientCRC() == 0L);
        Assert.assertTrue(putObjectResult.getServerCRC() == 0L);
        content.close();
        ossClient.deleteObject(bucketName, key);
        TestUtils.removeFile(localFile);
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : OSSObject(com.aliyun.oss.model.OSSObject) PutObjectResult(com.aliyun.oss.model.PutObjectResult) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) TestUtils.genFixedLengthFile(com.aliyun.oss.integrationtests.TestUtils.genFixedLengthFile) File(java.io.File) FileInputStream(java.io.FileInputStream) InconsistentException(com.aliyun.oss.InconsistentException) Test(org.junit.Test)

Aggregations

InconsistentException (com.aliyun.oss.InconsistentException)1 TestUtils.genFixedLengthFile (com.aliyun.oss.integrationtests.TestUtils.genFixedLengthFile)1 OSSObject (com.aliyun.oss.model.OSSObject)1 PutObjectResult (com.aliyun.oss.model.PutObjectResult)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 Test (org.junit.Test)1