Improve use of InputSupplier helpers

This commit is contained in:
Andrew Gaul 2012-10-02 13:28:10 -07:00 committed by Andrew Gaul
parent 418097d4e6
commit ceb203c10a
6 changed files with 20 additions and 22 deletions

View File

@ -58,13 +58,13 @@ import org.jclouds.io.InputSuppliers;
import org.jclouds.io.Payload; import org.jclouds.io.Payload;
import org.jclouds.io.payloads.PhantomPayload; import org.jclouds.io.payloads.PhantomPayload;
import org.jclouds.io.payloads.StringPayload; import org.jclouds.io.payloads.StringPayload;
import org.jclouds.util.Strings2;
import org.testng.annotations.AfterMethod; import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod; import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.common.base.Charsets; import com.google.common.base.Charsets;
import com.google.common.io.ByteStreams; import com.google.common.io.ByteStreams;
import com.google.common.io.CharStreams;
import com.google.common.io.Closeables; import com.google.common.io.Closeables;
import com.google.common.io.Files; import com.google.common.io.Files;
import com.google.common.io.InputSupplier; import com.google.common.io.InputSupplier;
@ -742,7 +742,7 @@ public class FilesystemAsyncBlobStoreTest {
Blob blobRangeStartAt = blobStore.getBlob(CONTAINER_NAME, blob.getMetadata().getName(), getOptionsRangeStartAt); Blob blobRangeStartAt = blobStore.getBlob(CONTAINER_NAME, blob.getMetadata().getName(), getOptionsRangeStartAt);
payload = blobRangeStartAt.getPayload(); payload = blobRangeStartAt.getPayload();
try { try {
assertEquals(input.substring(1), CharStreams.toString(CharStreams.newReaderSupplier(payload, Charsets.UTF_8))); assertEquals(input.substring(1), Strings2.toString(payload));
} finally { } finally {
Closeables.closeQuietly(payload); Closeables.closeQuietly(payload);
} }
@ -752,7 +752,7 @@ public class FilesystemAsyncBlobStoreTest {
Blob blobRangeTail = blobStore.getBlob(CONTAINER_NAME, blob.getMetadata().getName(), getOptionsRangeTail); Blob blobRangeTail = blobStore.getBlob(CONTAINER_NAME, blob.getMetadata().getName(), getOptionsRangeTail);
payload = blobRangeTail.getPayload(); payload = blobRangeTail.getPayload();
try { try {
assertEquals(input.substring(5), CharStreams.toString(CharStreams.newReaderSupplier(payload, Charsets.UTF_8))); assertEquals(input.substring(5), Strings2.toString(payload));
} finally { } finally {
Closeables.closeQuietly(payload); Closeables.closeQuietly(payload);
} }
@ -762,7 +762,7 @@ public class FilesystemAsyncBlobStoreTest {
Blob blobFragment = blobStore.getBlob(CONTAINER_NAME, blob.getMetadata().getName(), getOptionsFragment); Blob blobFragment = blobStore.getBlob(CONTAINER_NAME, blob.getMetadata().getName(), getOptionsFragment);
payload = blobFragment.getPayload(); payload = blobFragment.getPayload();
try { try {
assertEquals(input.substring(4, 7), CharStreams.toString(CharStreams.newReaderSupplier(payload, Charsets.UTF_8))); assertEquals(input.substring(4, 7), Strings2.toString(payload));
} finally { } finally {
Closeables.closeQuietly(payload); Closeables.closeQuietly(payload);
} }

View File

@ -93,7 +93,7 @@ public class SwiftBlobIntegrationLiveTest extends BaseBlobIntegrationTest {
public void testMultipartChunkedFileStream() throws IOException, InterruptedException { public void testMultipartChunkedFileStream() throws IOException, InterruptedException {
FileOutputStream fous = new FileOutputStream(new File("target/const.txt")); FileOutputStream fous = new FileOutputStream(new File("target/const.txt"));
ByteStreams.copy(oneHundredOneConstitutions.getInput(), fous); ByteStreams.copy(oneHundredOneConstitutions, fous);
fous.flush(); fous.flush();
fous.close(); fous.close();
String containerName = getContainerName(); String containerName = getContainerName();

View File

@ -115,7 +115,7 @@ public abstract class BaseJettyTest {
response.setContentType("text/plain"); response.setContentType("text/plain");
response.setHeader("Content-MD5", md5); response.setHeader("Content-MD5", md5);
response.setStatus(HttpServletResponse.SC_OK); response.setStatus(HttpServletResponse.SC_OK);
copy(oneHundredOneConstitutions.getInput(), response.getOutputStream()); copy(oneHundredOneConstitutions, response.getOutputStream());
} else if (request.getMethod().equals("PUT")) { } else if (request.getMethod().equals("PUT")) {
if (request.getContentLength() > 0) { if (request.getContentLength() > 0) {
response.setStatus(HttpServletResponse.SC_OK); response.setStatus(HttpServletResponse.SC_OK);

View File

@ -43,6 +43,7 @@ import org.jclouds.snia.cdmi.v1.domain.DataObject;
import org.jclouds.snia.cdmi.v1.internal.BaseCDMIApiLiveTest; import org.jclouds.snia.cdmi.v1.internal.BaseCDMIApiLiveTest;
import org.jclouds.snia.cdmi.v1.options.CreateContainerOptions; import org.jclouds.snia.cdmi.v1.options.CreateContainerOptions;
import org.jclouds.snia.cdmi.v1.queryparams.DataObjectQueryParams; import org.jclouds.snia.cdmi.v1.queryparams.DataObjectQueryParams;
import org.jclouds.util.Strings2;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.common.base.Charsets; import com.google.common.base.Charsets;
@ -100,7 +101,7 @@ public class DataNonCDMIContentTypeApiLiveTest extends BaseCDMIApiLiveTest {
dataNonCDMIContentTypeApi.create(dataObjectNameIn, value); dataNonCDMIContentTypeApi.create(dataObjectNameIn, value);
payloadOut = dataNonCDMIContentTypeApi.getValue(dataObjectNameIn); payloadOut = dataNonCDMIContentTypeApi.getValue(dataObjectNameIn);
assertNotNull(payloadOut); assertNotNull(payloadOut);
assertEquals(CharStreams.toString(new InputStreamReader(payloadOut.getInput(), "UTF-8")), value); assertEquals(Strings2.toString(payloadOut), value);
payloadIn = new StringPayload(value); payloadIn = new StringPayload(value);
payloadIn.setContentMetadata(BaseMutableContentMetadata.fromContentMetadata(payloadIn.getContentMetadata() payloadIn.setContentMetadata(BaseMutableContentMetadata.fromContentMetadata(payloadIn.getContentMetadata()
@ -109,7 +110,7 @@ public class DataNonCDMIContentTypeApiLiveTest extends BaseCDMIApiLiveTest {
payloadOut = dataNonCDMIContentTypeApi.getValue(dataObjectNameIn); payloadOut = dataNonCDMIContentTypeApi.getValue(dataObjectNameIn);
assertNotNull(payloadOut); assertNotNull(payloadOut);
assertEquals(CharStreams.toString(new InputStreamReader(payloadOut.getInput(), "UTF-8")), value); assertEquals(Strings2.toString(payloadOut), value);
dataObject = dataNonCDMIContentTypeApi.get(dataObjectNameIn, DataObjectQueryParams.Builder.field("parentURI")); dataObject = dataNonCDMIContentTypeApi.get(dataObjectNameIn, DataObjectQueryParams.Builder.field("parentURI"));
assertNotNull(dataObject); assertNotNull(dataObject);
@ -149,7 +150,7 @@ public class DataNonCDMIContentTypeApiLiveTest extends BaseCDMIApiLiveTest {
assertEquals(containerApi.get(containerName).getChildren().contains(dataObjectNameIn), true); assertEquals(containerApi.get(containerName).getChildren().contains(dataObjectNameIn), true);
payloadOut = dataNonCDMIContentTypeApi.getValue(dataObjectNameIn); payloadOut = dataNonCDMIContentTypeApi.getValue(dataObjectNameIn);
assertNotNull(payloadOut); assertNotNull(payloadOut);
assertEquals(CharStreams.toString(new InputStreamReader(payloadOut.getInput(), "UTF-8")), value); assertEquals(Strings2.toString(payloadOut), value);
dataNonCDMIContentTypeApi.delete(dataObjectNameIn); dataNonCDMIContentTypeApi.delete(dataObjectNameIn);
assertEquals(containerApi.get(containerName).getChildren().contains(dataObjectNameIn), false); assertEquals(containerApi.get(containerName).getChildren().contains(dataObjectNameIn), false);
@ -181,12 +182,11 @@ public class DataNonCDMIContentTypeApiLiveTest extends BaseCDMIApiLiveTest {
payloadOut = dataNonCDMIContentTypeApi.getValue(dataObjectNameIn); payloadOut = dataNonCDMIContentTypeApi.getValue(dataObjectNameIn);
assertNotNull(payloadOut); assertNotNull(payloadOut);
// assertEquals(CharStreams.toString(new InputStreamReader(payloadOut.getInput(), // assertEquals(Strings2.toString(payloadOut), value);
// "UTF-8")),value); // byte[] _bytes = ByteStreams.toByteArray(payloadOut);
// byte[] _bytes = ByteStreams.toByteArray(payloadOut.getInput());
tmpFileOut = new File(Files.createTempDir(), "temp.txt"); tmpFileOut = new File(Files.createTempDir(), "temp.txt");
fos = new FileOutputStream(tmpFileOut); fos = new FileOutputStream(tmpFileOut);
ByteStreams.copy(payloadOut.getInput(), fos); ByteStreams.copy(payloadOut, fos);
fos.flush(); fos.flush();
fos.close(); fos.close();
assertEquals(Files.equal(tmpFileOut, tmpFileIn), true); assertEquals(Files.equal(tmpFileOut, tmpFileIn), true);
@ -237,7 +237,7 @@ public class DataNonCDMIContentTypeApiLiveTest extends BaseCDMIApiLiveTest {
assertNotNull(payloadOut); assertNotNull(payloadOut);
tmpFileOut = new File(Files.createTempDir(), "temp.jpg"); tmpFileOut = new File(Files.createTempDir(), "temp.jpg");
fos = new FileOutputStream(tmpFileOut); fos = new FileOutputStream(tmpFileOut);
ByteStreams.copy(payloadOut.getInput(), fos); ByteStreams.copy(payloadOut, fos);
fos.flush(); fos.flush();
fos.close(); fos.close();
assertEquals(Files.equal(tmpFileOut, inFile), true); assertEquals(Files.equal(tmpFileOut, inFile), true);
@ -289,7 +289,7 @@ public class DataNonCDMIContentTypeApiLiveTest extends BaseCDMIApiLiveTest {
assertNotNull(payloadOut); assertNotNull(payloadOut);
tmpFileOut = new File(Files.createTempDir(), "temp.jpg"); tmpFileOut = new File(Files.createTempDir(), "temp.jpg");
fos = new FileOutputStream(tmpFileOut); fos = new FileOutputStream(tmpFileOut);
ByteStreams.copy(payloadOut.getInput(), fos); ByteStreams.copy(payloadOut, fos);
fos.flush(); fos.flush();
fos.close(); fos.close();
assertEquals(Files.equal(tmpFileOut, inFile), true); assertEquals(Files.equal(tmpFileOut, inFile), true);
@ -308,14 +308,12 @@ public class DataNonCDMIContentTypeApiLiveTest extends BaseCDMIApiLiveTest {
payloadOut = dataNonCDMIContentTypeApi.getValue(dataObjectNameIn, "bytes=0-10"); payloadOut = dataNonCDMIContentTypeApi.getValue(dataObjectNameIn, "bytes=0-10");
assertNotNull(payloadOut); assertNotNull(payloadOut);
assertEquals(CharStreams.toString(new InputStreamReader(payloadOut.getInput(), "UTF-8")), assertEquals(Strings2.toString(payloadOut), value.substring(0, 11));
value.substring(0, 11));
assertEquals(payloadOut.getContentMetadata().getContentLength(), new Long(11)); assertEquals(payloadOut.getContentMetadata().getContentLength(), new Long(11));
payloadOut = dataNonCDMIContentTypeApi.getValue(dataObjectNameIn, "bytes=11-20"); payloadOut = dataNonCDMIContentTypeApi.getValue(dataObjectNameIn, "bytes=11-20");
assertNotNull(payloadOut); assertNotNull(payloadOut);
assertEquals(CharStreams.toString(new InputStreamReader(payloadOut.getInput(), "UTF-8")), assertEquals(Strings2.toString(payloadOut), value.substring(11, 21));
value.substring(11, 21));
assertEquals(payloadOut.getContentMetadata().getContentLength(), new Long(10)); assertEquals(payloadOut.getContentMetadata().getContentLength(), new Long(10));
dataNonCDMIContentTypeApi.delete(dataObjectNameIn); dataNonCDMIContentTypeApi.delete(dataObjectNameIn);

View File

@ -37,7 +37,7 @@ public class ReturnPayloadBytes implements Function<HttpResponse, byte[]> {
@Override @Override
public byte[] apply(HttpResponse from) { public byte[] apply(HttpResponse from) {
try { try {
return ByteStreams.toByteArray(from.getPayload().getInput()); return ByteStreams.toByteArray(from.getPayload());
} catch (IOException e) { } catch (IOException e) {
throw Throwables.propagate(e); throw Throwables.propagate(e);
} }

View File

@ -111,7 +111,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.getInput()); byte[] buffer = toByteArray(oneHundredOneConstitutions);
assertEquals(oneHundredOneConstitutionsLength, (long) buffer.length); assertEquals(oneHundredOneConstitutionsLength, (long) buffer.length);
Payload part1 = newByteArrayPayload(buffer); Payload part1 = newByteArrayPayload(buffer);
@ -153,7 +153,7 @@ public class AWSS3ClientLiveTest extends S3ClientLiveTest {
public void testMultipartChunkedFileStream() throws IOException, InterruptedException { public void testMultipartChunkedFileStream() throws IOException, InterruptedException {
FileOutputStream fous = new FileOutputStream(new File("target/const.txt")); FileOutputStream fous = new FileOutputStream(new File("target/const.txt"));
ByteStreams.copy(oneHundredOneConstitutions.getInput(), fous); ByteStreams.copy(oneHundredOneConstitutions, fous);
fous.flush(); fous.flush();
fous.close(); fous.close();
String containerName = getContainerName(); String containerName = getContainerName();