Prefer ByteSource over deprecated InputSupplier

This commit is contained in:
Andrew Gaul 2013-12-01 22:15:36 -08:00
parent 076e31b076
commit 266d7f847b
9 changed files with 58 additions and 106 deletions

View File

@ -26,7 +26,6 @@ import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail; import static org.testng.Assert.fail;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.util.Iterator; import java.util.Iterator;
@ -60,9 +59,9 @@ import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import com.google.common.io.ByteSource;
import com.google.common.io.ByteStreams; import com.google.common.io.ByteStreams;
import com.google.common.io.Files; import com.google.common.io.Files;
import com.google.common.io.InputSupplier;
import com.google.inject.CreationException; import com.google.inject.CreationException;
/** /**
@ -585,9 +584,7 @@ public class FilesystemAsyncBlobStoreTest {
assertNotNull(resultBlob, "Blob exists"); assertNotNull(resultBlob, "Blob exists");
// checks file content // checks file content
InputSupplier<FileInputStream> expectedFile = ByteSource expectedFile = Files.asByteSource(new File(TARGET_CONTAINER_NAME, blobKey));
Files.newInputStreamSupplier(new File(
TARGET_CONTAINER_NAME, blobKey));
assertTrue(ByteStreams.equal(expectedFile, resultBlob.getPayload()), assertTrue(ByteStreams.equal(expectedFile, resultBlob.getPayload()),
"Blob payload differs from file content"); "Blob payload differs from file content");
// metadata are verified in the test for blobMetadata, so no need to // metadata are verified in the test for blobMetadata, so no need to

View File

@ -47,9 +47,9 @@ import org.testng.annotations.Test;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import com.google.common.io.ByteSource;
import com.google.common.io.ByteStreams; import com.google.common.io.ByteStreams;
import com.google.common.io.Files; import com.google.common.io.Files;
import com.google.common.io.InputSupplier;
/** /**
* Test class for {@link FilesystemStorageStrategyImpl } class * Test class for {@link FilesystemStorageStrategyImpl } class
@ -354,10 +354,8 @@ public class FilesystemStorageStrategyImplTest {
// verify that the files is equal // verify that the files is equal
File blobFullPath = new File(TARGET_CONTAINER_NAME, blobKey); File blobFullPath = new File(TARGET_CONTAINER_NAME, blobKey);
InputSupplier<FileInputStream> expectedInput = ByteSource expectedInput = Files.asByteSource(sourceFile);
Files.newInputStreamSupplier(sourceFile); ByteSource actualInput = Files.asByteSource(blobFullPath);
InputSupplier<FileInputStream> actualInput =
Files.newInputStreamSupplier(blobFullPath);
assertTrue(ByteStreams.equal(expectedInput, actualInput), assertTrue(ByteStreams.equal(expectedInput, actualInput),
"Files are not equal"); "Files are not equal");
} }
@ -375,10 +373,8 @@ public class FilesystemStorageStrategyImplTest {
// verify that the files is equal // verify that the files is equal
File blobFullPath = new File(TARGET_CONTAINER_NAME, blobKey); File blobFullPath = new File(TARGET_CONTAINER_NAME, blobKey);
InputSupplier<FileInputStream> expectedInput = ByteSource expectedInput = Files.asByteSource(sourceFile);
Files.newInputStreamSupplier(sourceFile); ByteSource actualInput = Files.asByteSource(blobFullPath);
InputSupplier<FileInputStream> actualInput =
Files.newInputStreamSupplier(blobFullPath);
assertTrue(ByteStreams.equal(expectedInput, actualInput), assertTrue(ByteStreams.equal(expectedInput, actualInput),
"Files are not equal"); "Files are not equal");
} }

View File

@ -40,9 +40,8 @@ import org.testng.annotations.DataProvider;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.common.hash.Hashing; import com.google.common.hash.Hashing;
import com.google.common.io.ByteStreams; import com.google.common.io.ByteSource;
import com.google.common.io.Files; import com.google.common.io.Files;
import com.google.common.io.InputSupplier;
/** /**
* *
@ -65,7 +64,7 @@ public class SwiftBlobIntegrationLiveTest extends BaseBlobIntegrationTest {
return props; return props;
} }
private InputSupplier<InputStream> oneHundredOneConstitutions; private ByteSource oneHundredOneConstitutions;
public SwiftBlobIntegrationLiveTest() { public SwiftBlobIntegrationLiveTest() {
provider = System.getProperty("test.swift.provider", "swift"); provider = System.getProperty("test.swift.provider", "swift");
@ -178,10 +177,10 @@ public class SwiftBlobIntegrationLiveTest extends BaseBlobIntegrationTest {
private File createFileBiggerThan(long partSize) throws IOException { private File createFileBiggerThan(long partSize) throws IOException {
long copiesNeeded = (partSize / getOneHundredOneConstitutionsLength()) + 1; long copiesNeeded = (partSize / getOneHundredOneConstitutionsLength()) + 1;
InputSupplier<InputStream> temp = ByteStreams.join(oneHundredOneConstitutions); ByteSource temp = ByteSource.concat(oneHundredOneConstitutions);
for (int i = 0; i < copiesNeeded; i++) { for (int i = 0; i < copiesNeeded; i++) {
temp = ByteStreams.join(temp, oneHundredOneConstitutions); temp = ByteSource.concat(temp, oneHundredOneConstitutions);
} }
File fileToUpload = new File("target/lots-of-const.txt"); File fileToUpload = new File("target/lots-of-const.txt");

View File

@ -29,7 +29,6 @@ import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull; import static org.testng.Assert.assertNull;
import static org.testng.Assert.fail; import static org.testng.Assert.fail;
import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -80,6 +79,7 @@ import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.google.common.hash.HashCode; import com.google.common.hash.HashCode;
import com.google.common.io.ByteSource;
import com.google.common.io.ByteStreams; import com.google.common.io.ByteStreams;
import com.google.common.io.Files; import com.google.common.io.Files;
import com.google.common.io.InputSupplier; import com.google.common.io.InputSupplier;
@ -91,7 +91,7 @@ import com.google.common.util.concurrent.Uninterruptibles;
* @author Adrian Cole * @author Adrian Cole
*/ */
public class BaseBlobIntegrationTest extends BaseBlobStoreIntegrationTest { public class BaseBlobIntegrationTest extends BaseBlobStoreIntegrationTest {
private InputSupplier<InputStream> oneHundredOneConstitutions; private ByteSource oneHundredOneConstitutions;
private byte[] oneHundredOneConstitutionsMD5; private byte[] oneHundredOneConstitutionsMD5;
private static long oneHundredOneConstitutionsLength; private static long oneHundredOneConstitutionsLength;
@ -100,23 +100,23 @@ public class BaseBlobIntegrationTest extends BaseBlobStoreIntegrationTest {
public void setUpResourcesOnThisThread(ITestContext testContext) throws Exception { public void setUpResourcesOnThisThread(ITestContext testContext) throws Exception {
super.setUpResourcesOnThisThread(testContext); super.setUpResourcesOnThisThread(testContext);
oneHundredOneConstitutions = getTestDataSupplier(); oneHundredOneConstitutions = getTestDataSupplier();
oneHundredOneConstitutionsMD5 = md5Supplier(oneHundredOneConstitutions); oneHundredOneConstitutionsMD5 = oneHundredOneConstitutions.hash(md5()).asBytes();
} }
protected static byte[] md5Supplier(InputSupplier<? extends InputStream> supplier) throws IOException { private static byte[] md5Supplier(InputSupplier<? extends InputStream> supplier) throws IOException {
return ByteStreams.hash(supplier, md5()).asBytes(); return ByteStreams.hash(supplier, md5()).asBytes();
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static InputSupplier<InputStream> getTestDataSupplier() throws IOException { public static ByteSource getTestDataSupplier() throws IOException {
byte[] oneConstitution = ByteStreams.toByteArray(new GZIPInputStream(BaseJettyTest.class byte[] oneConstitution = ByteStreams.toByteArray(new GZIPInputStream(BaseJettyTest.class
.getResourceAsStream("/const.txt.gz"))); .getResourceAsStream("/const.txt.gz")));
InputSupplier<ByteArrayInputStream> constitutionSupplier = ByteStreams.newInputStreamSupplier(oneConstitution); ByteSource constitutionSupplier = ByteSource.wrap(oneConstitution);
InputSupplier<InputStream> temp = ByteStreams.join(constitutionSupplier); ByteSource temp = ByteSource.concat(constitutionSupplier);
for (int i = 0; i < 100; i++) { for (int i = 0; i < 100; i++) {
temp = ByteStreams.join(temp, constitutionSupplier); temp = ByteSource.concat(temp, constitutionSupplier);
} }
oneHundredOneConstitutionsLength = oneConstitution.length * 101l; oneHundredOneConstitutionsLength = oneConstitution.length * 101l;
return temp; return temp;
@ -181,7 +181,7 @@ public class BaseBlobIntegrationTest extends BaseBlobStoreIntegrationTest {
@Test(groups = { "integration", "live" }) @Test(groups = { "integration", "live" })
public void testFileGetParallel() throws Exception { public void testFileGetParallel() throws Exception {
final InputSupplier<? extends InputStream> supplier = createTestInput(32 * 1024); final ByteSource supplier = createTestInput(32 * 1024);
final String expectedContentDisposition = "attachment; filename=constit.txt"; final String expectedContentDisposition = "attachment; filename=constit.txt";
final String container = getContainerName(); final String container = getContainerName();
try { try {
@ -198,7 +198,7 @@ public class BaseBlobIntegrationTest extends BaseBlobStoreIntegrationTest {
public Void apply(Blob from) { public Void apply(Blob from) {
try { try {
validateMetadata(from.getMetadata(), container, name); validateMetadata(from.getMetadata(), container, name);
assertEquals(md5Supplier(from.getPayload()), md5Supplier(supplier)); assertEquals(md5Supplier(from.getPayload()), supplier.hash(md5()).asBytes());
checkContentDisposition(from, expectedContentDisposition); checkContentDisposition(from, expectedContentDisposition);
} catch (IOException e) { } catch (IOException e) {
Throwables.propagate(e); Throwables.propagate(e);
@ -221,12 +221,12 @@ public class BaseBlobIntegrationTest extends BaseBlobStoreIntegrationTest {
} }
private void uploadInputSupplier(String container, String name, String contentDisposition, private void uploadInputSupplier(String container, String name, String contentDisposition,
InputSupplier<? extends InputStream> supplier) throws IOException { ByteSource supplier) throws IOException {
BlobStore blobStore = view.getBlobStore(); BlobStore blobStore = view.getBlobStore();
blobStore.putBlob(container, blobStore.blobBuilder(name) blobStore.putBlob(container, blobStore.blobBuilder(name)
.payload(new InputStreamSupplierPayload(supplier)) .payload(new InputStreamSupplierPayload(supplier))
.contentType("text/plain") .contentType("text/plain")
.contentMD5(md5Supplier(supplier)) .contentMD5(supplier.hash(md5()).asBytes())
.contentLength(ByteStreams.length(supplier)) .contentLength(ByteStreams.length(supplier))
.contentDisposition(contentDisposition) .contentDisposition(contentDisposition)
.build()); .build());
@ -667,12 +667,12 @@ public class BaseBlobIntegrationTest extends BaseBlobStoreIntegrationTest {
assertEquals(metadata.getContentMetadata().getContentMD5(), md5().hashString(TEST_STRING, UTF_8).asBytes()); assertEquals(metadata.getContentMetadata().getContentMD5(), md5().hashString(TEST_STRING, UTF_8).asBytes());
} }
/** @return InputSupplier containing a random length 0..length of random bytes. */ /** @return ByteSource containing a random length 0..length of random bytes. */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private static InputSupplier<? extends InputStream> createTestInput(int length) { private static ByteSource createTestInput(int length) {
Random random = new Random(); Random random = new Random();
byte[] buffer = new byte[random.nextInt(length)]; byte[] buffer = new byte[random.nextInt(length)];
random.nextBytes(buffer); random.nextBytes(buffer);
return ByteStreams.newInputStreamSupplier(buffer); return ByteSource.wrap(buffer);
} }
} }

View File

@ -33,8 +33,9 @@ import java.security.spec.RSAPrivateCrtKeySpec;
import java.security.spec.RSAPublicKeySpec; import java.security.spec.RSAPublicKeySpec;
import java.util.Map; import java.util.Map;
import com.google.common.io.Resources;
import org.jclouds.crypto.Pems; import org.jclouds.crypto.Pems;
import org.jclouds.io.Payloads;
import org.jclouds.util.Strings2; import org.jclouds.util.Strings2;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@ -118,7 +119,7 @@ public class SshKeysTest {
@Test @Test
public void testEncodeAsOpenSSH() throws IOException, InvalidKeySpecException, NoSuchAlgorithmException { public void testEncodeAsOpenSSH() throws IOException, InvalidKeySpecException, NoSuchAlgorithmException {
String encoded = SshKeys.encodeAsOpenSSH((RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic( String encoded = SshKeys.encodeAsOpenSSH((RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(
SshKeys.publicKeySpecFromOpenSSH(Payloads.newPayload(getClass().getResourceAsStream("/test.pub"))))); SshKeys.publicKeySpecFromOpenSSH(Resources.asByteSource(Resources.getResource(getClass(), "/test.pub")))));
assertEquals(encoded, Strings2.toStringAndClose(getClass().getResourceAsStream("/test.pub")).trim()); assertEquals(encoded, Strings2.toStringAndClose(getClass().getResourceAsStream("/test.pub")).trim());
} }

View File

@ -18,8 +18,6 @@ package org.jclouds.http;
import static com.google.common.hash.Hashing.md5; import static com.google.common.hash.Hashing.md5;
import static com.google.common.io.BaseEncoding.base64; import static com.google.common.io.BaseEncoding.base64;
import static com.google.common.io.ByteStreams.join;
import static com.google.common.io.ByteStreams.newInputStreamSupplier;
import static com.google.common.io.ByteStreams.toByteArray; import static com.google.common.io.ByteStreams.toByteArray;
import static com.google.common.io.Closeables.close; import static com.google.common.io.Closeables.close;
import static com.google.common.io.Files.asByteSource; import static com.google.common.io.Files.asByteSource;
@ -31,7 +29,6 @@ import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue; import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail; import static org.testng.Assert.fail;
import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -55,7 +52,6 @@ import com.google.common.collect.Multimap;
import com.google.common.io.ByteSource; import com.google.common.io.ByteSource;
import com.google.common.io.CharSink; import com.google.common.io.CharSink;
import com.google.common.io.Files; import com.google.common.io.Files;
import com.google.common.io.InputSupplier;
import com.squareup.okhttp.mockwebserver.Dispatcher; import com.squareup.okhttp.mockwebserver.Dispatcher;
import com.squareup.okhttp.mockwebserver.MockResponse; import com.squareup.okhttp.mockwebserver.MockResponse;
import com.squareup.okhttp.mockwebserver.MockWebServer; import com.squareup.okhttp.mockwebserver.MockWebServer;
@ -77,12 +73,12 @@ public abstract class BaseHttpCommandExecutorServiceIntegrationTest extends Base
private String constitutionsMd5; private String constitutionsMd5;
private long constitutionsLength; private long constitutionsLength;
private InputSupplier<InputStream> oneHundredOneConstitutions; private ByteSource oneHundredOneConstitutions;
@BeforeClass(groups = "integration") @BeforeClass(groups = "integration")
public void setup() throws IOException { public void setup() throws IOException {
oneHundredOneConstitutions = getTestDataSupplier(); oneHundredOneConstitutions = getTestDataSupplier();
constitutionsMd5 = base64().encode(asByteSource(oneHundredOneConstitutions.getInput()).hash(md5()).asBytes()); constitutionsMd5 = base64().encode(oneHundredOneConstitutions.hash(md5()).asBytes());
} }
protected IntegrationTestClient client(String url) { protected IntegrationTestClient client(String url) {
@ -647,13 +643,13 @@ public abstract class BaseHttpCommandExecutorServiceIntegrationTest extends Base
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private InputSupplier<InputStream> getTestDataSupplier() throws IOException { private ByteSource getTestDataSupplier() throws IOException {
byte[] oneConstitution = toByteArray(new GZIPInputStream( byte[] oneConstitution = toByteArray(new GZIPInputStream(
BaseHttpCommandExecutorServiceIntegrationTest.class.getResourceAsStream("/const.txt.gz"))); BaseHttpCommandExecutorServiceIntegrationTest.class.getResourceAsStream("/const.txt.gz")));
InputSupplier<ByteArrayInputStream> constitutionSupplier = newInputStreamSupplier(oneConstitution); ByteSource constitutionSupplier = ByteSource.wrap(oneConstitution);
InputSupplier<InputStream> temp = join(constitutionSupplier); ByteSource temp = ByteSource.concat(constitutionSupplier);
for (int i = 0; i < 100; i++) { for (int i = 0; i < 100; i++) {
temp = join(temp, constitutionSupplier); temp = ByteSource.concat(temp, constitutionSupplier);
} }
constitutionsLength = oneConstitution.length * 101; constitutionsLength = oneConstitution.length * 101;
return temp; return temp;

View File

@ -20,8 +20,6 @@ import static com.google.common.base.Throwables.getStackTraceAsString;
import static com.google.common.hash.Hashing.md5; import static com.google.common.hash.Hashing.md5;
import static com.google.common.io.BaseEncoding.base64; import static com.google.common.io.BaseEncoding.base64;
import static com.google.common.io.ByteStreams.copy; import static com.google.common.io.ByteStreams.copy;
import static com.google.common.io.ByteStreams.join;
import static com.google.common.io.ByteStreams.newInputStreamSupplier;
import static com.google.common.io.ByteStreams.toByteArray; import static com.google.common.io.ByteStreams.toByteArray;
import static com.google.common.net.HttpHeaders.CONTENT_DISPOSITION; import static com.google.common.net.HttpHeaders.CONTENT_DISPOSITION;
import static com.google.common.net.HttpHeaders.CONTENT_ENCODING; import static com.google.common.net.HttpHeaders.CONTENT_ENCODING;
@ -35,7 +33,6 @@ import static org.jclouds.io.ByteSources.asByteSource;
import static org.jclouds.util.Closeables2.closeQuietly; import static org.jclouds.util.Closeables2.closeQuietly;
import static org.jclouds.util.Strings2.toStringAndClose; import static org.jclouds.util.Strings2.toStringAndClose;
import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.Enumeration; import java.util.Enumeration;
@ -70,8 +67,7 @@ import com.google.common.collect.ImmutableMap.Builder;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.LinkedHashMultimap; import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Multimap; import com.google.common.collect.Multimap;
import com.google.common.io.ByteStreams; import com.google.common.io.ByteSource;
import com.google.common.io.InputSupplier;
import com.google.inject.Injector; import com.google.inject.Injector;
import com.google.inject.Module; import com.google.inject.Module;
@ -94,8 +90,8 @@ public abstract class BaseJettyTest {
public void setUpJetty(@Optional("8123") final int testPort) throws Exception { public void setUpJetty(@Optional("8123") final int testPort) throws Exception {
this.testPort = testPort; this.testPort = testPort;
final InputSupplier<InputStream> oneHundredOneConstitutions = getTestDataSupplier(); final ByteSource oneHundredOneConstitutions = getTestDataSupplier();
md5 = base64().encode(ByteStreams.hash(oneHundredOneConstitutions, md5()).asBytes()); md5 = base64().encode(oneHundredOneConstitutions.hash(md5()).asBytes());
Handler server1Handler = new AbstractHandler() { Handler server1Handler = new AbstractHandler() {
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
@ -251,14 +247,14 @@ public abstract class BaseJettyTest {
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static InputSupplier<InputStream> getTestDataSupplier() throws IOException { public static ByteSource getTestDataSupplier() throws IOException {
byte[] oneConstitution = toByteArray(new GZIPInputStream(BaseJettyTest.class.getResourceAsStream("/const.txt.gz"))); byte[] oneConstitution = toByteArray(new GZIPInputStream(BaseJettyTest.class.getResourceAsStream("/const.txt.gz")));
InputSupplier<ByteArrayInputStream> constitutionSupplier = newInputStreamSupplier(oneConstitution); ByteSource constitutionSupplier = ByteSource.wrap(oneConstitution);
InputSupplier<InputStream> temp = join(constitutionSupplier); ByteSource temp = ByteSource.concat(constitutionSupplier);
for (int i = 0; i < 100; i++) { for (int i = 0; i < 100; i++) {
temp = join(temp, constitutionSupplier); temp = ByteSource.concat(temp, constitutionSupplier);
} }
return temp; return temp;
} }

View File

@ -17,8 +17,6 @@
package org.jclouds.aws.s3; package org.jclouds.aws.s3;
import static com.google.common.hash.Hashing.md5; import static com.google.common.hash.Hashing.md5;
import static com.google.common.io.ByteStreams.join;
import static com.google.common.io.ByteStreams.newInputStreamSupplier;
import static com.google.common.io.ByteStreams.toByteArray; import static com.google.common.io.ByteStreams.toByteArray;
import static org.jclouds.aws.s3.blobstore.options.AWSS3PutOptions.Builder.storageClass; import static org.jclouds.aws.s3.blobstore.options.AWSS3PutOptions.Builder.storageClass;
import static org.jclouds.io.Payloads.newByteArrayPayload; import static org.jclouds.io.Payloads.newByteArrayPayload;
@ -28,10 +26,8 @@ import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue; import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail; import static org.testng.Assert.fail;
import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.zip.GZIPInputStream; import java.util.zip.GZIPInputStream;
@ -61,9 +57,8 @@ import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.io.ByteStreams; import com.google.common.io.ByteSource;
import com.google.common.io.Files; import com.google.common.io.Files;
import com.google.common.io.InputSupplier;
/** /**
* Tests behavior of {@code S3Client} * Tests behavior of {@code S3Client}
@ -75,7 +70,7 @@ public class AWSS3ClientLiveTest extends S3ClientLiveTest {
public AWSS3ClientLiveTest() { public AWSS3ClientLiveTest() {
provider = "aws-s3"; provider = "aws-s3";
} }
private InputSupplier<InputStream> oneHundredOneConstitutions; private ByteSource oneHundredOneConstitutions;
private byte[] oneHundredOneConstitutionsMD5; private byte[] oneHundredOneConstitutionsMD5;
private static long oneHundredOneConstitutionsLength; private static long oneHundredOneConstitutionsLength;
@ -89,18 +84,18 @@ public class AWSS3ClientLiveTest extends S3ClientLiveTest {
public void setUpResourcesOnThisThread(ITestContext testContext) throws Exception { public void setUpResourcesOnThisThread(ITestContext testContext) throws Exception {
super.setUpResourcesOnThisThread(testContext); super.setUpResourcesOnThisThread(testContext);
oneHundredOneConstitutions = getTestDataSupplier(); oneHundredOneConstitutions = getTestDataSupplier();
oneHundredOneConstitutionsMD5 = ByteStreams.hash(oneHundredOneConstitutions, md5()).asBytes(); oneHundredOneConstitutionsMD5 = oneHundredOneConstitutions.hash(md5()).asBytes();
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static InputSupplier<InputStream> getTestDataSupplier() throws IOException { public static ByteSource getTestDataSupplier() throws IOException {
byte[] oneConstitution = toByteArray(new GZIPInputStream(BaseJettyTest.class.getResourceAsStream("/const.txt.gz"))); byte[] oneConstitution = toByteArray(new GZIPInputStream(BaseJettyTest.class.getResourceAsStream("/const.txt.gz")));
InputSupplier<ByteArrayInputStream> constitutionSupplier = newInputStreamSupplier(oneConstitution); ByteSource constitutionSupplier = ByteSource.wrap(oneConstitution);
InputSupplier<InputStream> temp = join(constitutionSupplier); ByteSource temp = ByteSource.concat(constitutionSupplier);
// we have to go beyond 5MB per part // we have to go beyond 5MB per part
for (oneHundredOneConstitutionsLength = oneConstitution.length; oneHundredOneConstitutionsLength < 5 * 1024 * 1024; oneHundredOneConstitutionsLength += oneConstitution.length) { for (oneHundredOneConstitutionsLength = oneConstitution.length; oneHundredOneConstitutionsLength < 5 * 1024 * 1024; oneHundredOneConstitutionsLength += oneConstitution.length) {
temp = join(temp, constitutionSupplier); temp = ByteSource.concat(temp, constitutionSupplier);
} }
return temp; return temp;
} }
@ -112,7 +107,7 @@ public class AWSS3ClientLiveTest extends S3ClientLiveTest {
String key = "constitution.txt"; String key = "constitution.txt";
String uploadId = getApi().initiateMultipartUpload(containerName, String uploadId = getApi().initiateMultipartUpload(containerName,
ObjectMetadataBuilder.create().key(key).contentMD5(oneHundredOneConstitutionsMD5).build()); ObjectMetadataBuilder.create().key(key).contentMD5(oneHundredOneConstitutionsMD5).build());
byte[] buffer = toByteArray(oneHundredOneConstitutions); byte[] buffer = oneHundredOneConstitutions.read();
assertEquals(oneHundredOneConstitutionsLength, (long) buffer.length); assertEquals(oneHundredOneConstitutionsLength, (long) buffer.length);
Payload part1 = newByteArrayPayload(buffer); Payload part1 = newByteArrayPayload(buffer);

View File

@ -18,13 +18,11 @@ package org.jclouds.azureblob.blobstore.integration;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import com.google.common.io.ByteStreams; import com.google.common.collect.Iterables;
import com.google.common.io.ByteSource;
import com.google.common.io.Files; import com.google.common.io.Files;
import com.google.common.io.InputSupplier;
import org.jclouds.azureblob.blobstore.strategy.MultipartUploadStrategy; import org.jclouds.azureblob.blobstore.strategy.MultipartUploadStrategy;
import org.jclouds.blobstore.BlobStore; import org.jclouds.blobstore.BlobStore;
import org.jclouds.blobstore.domain.Blob; import org.jclouds.blobstore.domain.Blob;
@ -42,7 +40,7 @@ import static com.google.common.hash.Hashing.md5;
*/ */
@Test(groups = "live") @Test(groups = "live")
public class AzureBlobIntegrationLiveTest extends BaseBlobIntegrationTest { public class AzureBlobIntegrationLiveTest extends BaseBlobIntegrationTest {
private InputSupplier<InputStream> oneHundredOneConstitutions; private ByteSource oneHundredOneConstitutions;
private byte[] oneHundredOneConstitutionsMD5; private byte[] oneHundredOneConstitutionsMD5;
public AzureBlobIntegrationLiveTest() { public AzureBlobIntegrationLiveTest() {
@ -81,7 +79,7 @@ public class AzureBlobIntegrationLiveTest extends BaseBlobIntegrationTest {
*/ */
public void testMultipartChunkedFileStream() throws IOException, InterruptedException { public void testMultipartChunkedFileStream() throws IOException, InterruptedException {
oneHundredOneConstitutions = getTestDataSupplier(); oneHundredOneConstitutions = getTestDataSupplier();
oneHundredOneConstitutionsMD5 = ByteStreams.hash(oneHundredOneConstitutions, md5()).asBytes(); oneHundredOneConstitutionsMD5 = oneHundredOneConstitutions.hash(md5()).asBytes();
File file = new File("target/const.txt"); File file = new File("target/const.txt");
Files.copy(oneHundredOneConstitutions, file); Files.copy(oneHundredOneConstitutions, file);
String containerName = getContainerName(); String containerName = getContainerName();
@ -100,12 +98,7 @@ public class AzureBlobIntegrationLiveTest extends BaseBlobIntegrationTest {
public void testMultipartChunkedFileStreamPowerOfTwoSize() throws IOException, InterruptedException { public void testMultipartChunkedFileStreamPowerOfTwoSize() throws IOException, InterruptedException {
final long limit = MultipartUploadStrategy.MAX_BLOCK_SIZE; final long limit = MultipartUploadStrategy.MAX_BLOCK_SIZE;
InputSupplier<InputStream> input = new InputSupplier<InputStream>() { ByteSource input = repeatingArrayByteSource(new byte[1024]).slice(0, limit);
@Override
public InputStream getInput() throws IOException {
return ByteStreams.limit(ZERO_INPUT_STREAM, limit);
}
};
File file = new File("target/const.txt"); File file = new File("target/const.txt");
Files.copy(input, file); Files.copy(input, file);
String containerName = getContainerName(); String containerName = getContainerName();
@ -122,28 +115,7 @@ public class AzureBlobIntegrationLiveTest extends BaseBlobIntegrationTest {
} }
} }
/** An infinite-length zero byte InputStream. */ private static ByteSource repeatingArrayByteSource(final byte[] input) {
// Guava feature request: return ByteSource.concat(Iterables.cycle(ByteSource.wrap(input)));
// https://code.google.com/p/guava-libraries/issues/detail?id=1370
private static final InputStream ZERO_INPUT_STREAM = new InputStream() {
@Override
public int read() {
return 0;
} }
@Override
public int read(final byte[] b) {
return read(b, 0, b.length);
}
@Override
public int read(final byte[] b, final int off, final int len) {
if (off < 0 || len < 0 || len > b.length - off) {
throw new IndexOutOfBoundsException();
}
int length = Math.min(len, b.length - off);
Arrays.fill(b, off, length, (byte) 0);
return length;
}
};
} }