Merge pull request #638 from andrewgaul/filesystem-is-same

Remove quirky isSame method
This commit is contained in:
Adrian Cole 2012-05-15 12:32:58 -07:00
commit bc0e512c81
3 changed files with 22 additions and 56 deletions

View File

@ -57,6 +57,7 @@ import org.jclouds.crypto.CryptoStreams;
import org.jclouds.filesystem.reference.FilesystemConstants;
import org.jclouds.filesystem.utils.TestUtils;
import org.jclouds.http.HttpRequest;
import org.jclouds.io.InputSuppliers;
import org.jclouds.io.payloads.PhantomPayload;
import org.jclouds.io.payloads.StringPayload;
import org.testng.annotations.AfterMethod;
@ -64,7 +65,10 @@ import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import com.google.common.io.ByteStreams;
import com.google.common.io.Closeables;
import com.google.common.io.Files;
import com.google.common.io.InputSupplier;
import com.google.inject.CreationException;
/**
@ -620,9 +624,13 @@ public class FilesystemAsyncBlobStoreTest {
assertNotNull(resultBlob, "Blob exists");
// checks file content
InputStream expectedFile = new FileInputStream(TARGET_CONTAINER_NAME + File.separator + blobKey);
InputStream currentFile = resultBlob.getPayload().getInput();
assertTrue(TestUtils.isSame(expectedFile, currentFile), "Blob payload differs from file content");
InputSupplier<FileInputStream> expectedFile =
Files.newInputStreamSupplier(new File(
TARGET_CONTAINER_NAME, blobKey));
InputSupplier<? extends InputStream> actualFile =
InputSuppliers.of(resultBlob.getPayload().getInput());
assertTrue(ByteStreams.equal(expectedFile, actualFile),
"Blob payload differs from file content");
// metadata are verified in the test for blobMetadata, so no need to
// perform a complete test here
assertNotNull(resultBlob.getMetadata(), "Metadata null");

View File

@ -51,6 +51,10 @@ import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import com.google.common.io.ByteStreams;
import com.google.common.io.Files;
import com.google.common.io.InputSupplier;
/**
* Test class for {@link FilesystemStorageStrategyImpl } class
*
@ -371,10 +375,13 @@ public class FilesystemStorageStrategyImplTest {
// write files
storageStrategy.writePayloadOnFile(CONTAINER_NAME, blobKey, filePayload);
// verify that the files is equal
String blobFullPath = TARGET_CONTAINER_NAME + FS + blobKey;
InputStream expectedInput = new FileInputStream(sourceFile);
InputStream currentInput = new FileInputStream(blobFullPath);
assertTrue(TestUtils.isSame(expectedInput, currentInput), "Files aren't equals");
File blobFullPath = new File(TARGET_CONTAINER_NAME, blobKey);
InputSupplier<FileInputStream> expectedInput =
Files.newInputStreamSupplier(sourceFile);
InputSupplier<FileInputStream> actualInput =
Files.newInputStreamSupplier(blobFullPath);
assertTrue(ByteStreams.equal(expectedInput, actualInput),
"Files are not equal");
}
public void testWritePayloadOnFile_SourceFileDoesntExist() {

View File

@ -200,53 +200,4 @@ public class TestUtils {
if (imageResourceIndex >= imageResource.length) imageResourceIndex = 0;
return new File(fileName);
}
/**
* Compare two input stream
*
* @param input1 the first stream
* @param input2 the second stream
* @return true if the streams contain the same content, or false otherwise
* @throws IOException
* @throws IllegalArgumentException if the stream is null
*/
public static boolean isSame(InputStream input1, InputStream input2 ) throws IOException {
boolean error = false;
try {
byte[] buffer1 = new byte[1024];
byte[] buffer2 = new byte[1024];
try {
int numRead1 = 0;
int numRead2 = 0;
while (true) {
numRead1 = input1.read(buffer1);
numRead2 = input2.read(buffer2);
if (numRead1 > -1) {
if (numRead2 != numRead1) return false;
// Otherwise same number of bytes read
if (!Arrays.equals(buffer1, buffer2)) return false;
// Otherwise same bytes read, so continue ...
} else {
// Nothing more in stream 1 ...
return numRead2 < 0;
}
}
} finally {
input1.close();
}
} catch (IOException e) {
error = true; // this error should be thrown, even if there is an error closing stream 2
throw e;
} catch (RuntimeException e) {
error = true; // this error should be thrown, even if there is an error closing stream 2
throw e;
} finally {
try {
input2.close();
} catch (IOException e) {
if (!error) throw e;
}
}
}
}