From 6b9c68039b460cf8bec937b11f5a6acbc87232e8 Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Sat, 23 Jul 2011 23:21:06 +1000 Subject: [PATCH] Issue 635:filesystem provider does not return md5 or etag on getBlob --- .../filesystem/FilesystemAsyncBlobStore.java | 147 ++++++++---------- .../strategy/FilesystemStorageStrategy.java | 2 +- .../FilesystemStorageStrategyImpl.java | 81 +++------- .../FilesystemAsyncBlobStoreTest.java | 112 ++++++------- .../jclouds/filesystem/utils/TestUtils.java | 2 +- 5 files changed, 143 insertions(+), 201 deletions(-) diff --git a/apis/filesystem/src/main/java/org/jclouds/filesystem/FilesystemAsyncBlobStore.java b/apis/filesystem/src/main/java/org/jclouds/filesystem/FilesystemAsyncBlobStore.java index 83290db0cf..d650064d4a 100644 --- a/apis/filesystem/src/main/java/org/jclouds/filesystem/FilesystemAsyncBlobStore.java +++ b/apis/filesystem/src/main/java/org/jclouds/filesystem/FilesystemAsyncBlobStore.java @@ -48,10 +48,10 @@ import java.util.Collection; import java.util.Date; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; -import java.util.Map.Entry; import java.util.concurrent.ExecutorService; import javax.annotation.Nullable; @@ -66,14 +66,13 @@ import org.jclouds.blobstore.BlobStoreContext; import org.jclouds.blobstore.ContainerNotFoundException; import org.jclouds.blobstore.KeyNotFoundException; import org.jclouds.blobstore.domain.Blob; +import org.jclouds.blobstore.domain.BlobBuilder; import org.jclouds.blobstore.domain.BlobMetadata; import org.jclouds.blobstore.domain.MutableBlobMetadata; import org.jclouds.blobstore.domain.MutableStorageMetadata; import org.jclouds.blobstore.domain.PageSet; import org.jclouds.blobstore.domain.StorageMetadata; import org.jclouds.blobstore.domain.StorageType; -import org.jclouds.blobstore.domain.Blob.Factory; -import org.jclouds.blobstore.domain.internal.MutableBlobMetadataImpl; import org.jclouds.blobstore.domain.internal.MutableStorageMetadataImpl; import org.jclouds.blobstore.domain.internal.PageSetImpl; import org.jclouds.blobstore.functions.HttpGetOptionsListToGetOptions; @@ -96,10 +95,8 @@ import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpResponse; import org.jclouds.http.HttpResponseException; import org.jclouds.http.options.HttpRequestOptions; -import org.jclouds.io.Payload; import org.jclouds.io.Payloads; import org.jclouds.io.payloads.BaseMutableContentMetadata; -import org.jclouds.io.payloads.FilePayload; import org.jclouds.logging.Logger; import org.jclouds.rest.annotations.ParamValidators; @@ -126,18 +123,15 @@ public class FilesystemAsyncBlobStore extends BaseAsyncBlobStore { protected final Crypto crypto; protected final HttpGetOptionsListToGetOptions httpGetOptionsConverter; protected final IfDirectoryReturnNameStrategy ifDirectoryReturnName; - protected final Factory blobFactory; protected final FilesystemStorageStrategy storageStrategy; @Inject protected FilesystemAsyncBlobStore(BlobStoreContext context, DateService dateService, Crypto crypto, - HttpGetOptionsListToGetOptions httpGetOptionsConverter, - IfDirectoryReturnNameStrategy ifDirectoryReturnName, Factory blobFactory, BlobUtils blobUtils, - @Named(Constants.PROPERTY_USER_THREADS) ExecutorService service, Supplier defaultLocation, - @Memoized Supplier> locations, FilesystemStorageStrategy storageStrategy) { + HttpGetOptionsListToGetOptions httpGetOptionsConverter, IfDirectoryReturnNameStrategy ifDirectoryReturnName, + BlobUtils blobUtils, @Named(Constants.PROPERTY_USER_THREADS) ExecutorService service, + Supplier defaultLocation, @Memoized Supplier> locations, + FilesystemStorageStrategy storageStrategy) { super(context, blobUtils, service, defaultLocation, locations); - // super(context, blobUtils, service, null, null); - this.blobFactory = blobFactory; this.dateService = dateService; this.crypto = crypto; this.httpGetOptionsConverter = httpGetOptionsConverter; @@ -166,22 +160,22 @@ public class FilesystemAsyncBlobStore extends BaseAsyncBlobStore { } SortedSet contents = newTreeSet(transform(blobBelongingToContainer, - new Function() { - public StorageMetadata apply(String key) { - Blob oldBlob = loadFileBlob(container, key); + new Function() { + public StorageMetadata apply(String key) { + Blob oldBlob = loadFileBlob(container, key); - checkState(oldBlob != null, "blob " + key + " is not present although it was in the list of " - + container); - checkState(oldBlob.getMetadata() != null, "blob " + container + "/" + key + " has no metadata"); - MutableBlobMetadata md = copy(oldBlob.getMetadata()); - String directoryName = ifDirectoryReturnName.execute(md); - if (directoryName != null) { - md.setName(directoryName); - md.setType(StorageType.RELATIVE_PATH); - } - return md; + checkState(oldBlob != null, "blob " + key + " is not present although it was in the list of " + + container); + checkState(oldBlob.getMetadata() != null, "blob " + container + "/" + key + " has no metadata"); + MutableBlobMetadata md = copy(oldBlob.getMetadata()); + String directoryName = ifDirectoryReturnName.execute(md); + if (directoryName != null) { + md.setName(directoryName); + md.setType(StorageType.RELATIVE_PATH); } - })); + return md; + } + })); String marker = null; if (options != null) { @@ -221,21 +215,21 @@ public class FilesystemAsyncBlobStore extends BaseAsyncBlobStore { if (delimiter != null) { SortedSet commonPrefixes = null; Iterable iterable = transform(contents, new CommonPrefixes(prefix != null ? prefix : null, - delimiter)); + delimiter)); commonPrefixes = iterable != null ? newTreeSet(iterable) : new TreeSet(); commonPrefixes.remove(CommonPrefixes.NO_PREFIX); contents = newTreeSet(filter(contents, new DelimiterFilter(prefix != null ? prefix : null, delimiter))); - Iterables. addAll(contents, transform(commonPrefixes, - new Function() { - public StorageMetadata apply(String o) { - MutableStorageMetadata md = new MutableStorageMetadataImpl(); - md.setType(StorageType.RELATIVE_PATH); - md.setName(o); - return md; - } - })); + Iterables. addAll(contents, + transform(commonPrefixes, new Function() { + public StorageMetadata apply(String o) { + MutableStorageMetadata md = new MutableStorageMetadataImpl(); + md.setType(StorageType.RELATIVE_PATH); + md.setName(o); + return md; + } + })); } // trim metadata, if the response isn't supposed to be detailed. @@ -247,11 +241,11 @@ public class FilesystemAsyncBlobStore extends BaseAsyncBlobStore { } return Futures.> immediateFuture(new PageSetImpl(contents, - marker)); + marker)); } - private ContainerNotFoundException cnfe(final String name) { + private ContainerNotFoundException cnfe(String name) { return new ContainerNotFoundException(name, String.format("container %s not in filesystem", name)); } @@ -265,7 +259,7 @@ public class FilesystemAsyncBlobStore extends BaseAsyncBlobStore { MutableBlobMetadata metadata = (MutableBlobMetadata) is.readObject(); convertUserMetadataKeysToLowercase(metadata); metadata.setContentMetadata(BaseMutableContentMetadata.fromContentMetadata(in.getContentMetadata().toBuilder() - .build())); + .build())); return metadata; } catch (Exception e) { propagate(e); @@ -292,7 +286,7 @@ public class FilesystemAsyncBlobStore extends BaseAsyncBlobStore { * {@inheritDoc} */ @Override - public ListenableFuture removeBlob(final String container, final String key) { + public ListenableFuture removeBlob(String container, String key) { storageStrategy.removeBlob(container, key); return immediateFuture(null); } @@ -301,7 +295,7 @@ public class FilesystemAsyncBlobStore extends BaseAsyncBlobStore { * {@inheritDoc} */ @Override - public ListenableFuture containerExists(final String containerName) { + public ListenableFuture containerExists(String containerName) { boolean exists = containerExistsSyncImpl(containerName); return immediateFuture(exists); } @@ -314,14 +308,14 @@ public class FilesystemAsyncBlobStore extends BaseAsyncBlobStore { Iterable containers = storageStrategy.getAllContainerNames(); return Futures.> immediateFuture(new PageSetImpl(transform( - containers, new Function() { - public StorageMetadata apply(String name) { - MutableStorageMetadata cmd = create(); - cmd.setName(name); - cmd.setType(StorageType.CONTAINER); - return cmd; - } - }), null)); + containers, new Function() { + public StorageMetadata apply(String name) { + MutableStorageMetadata cmd = create(); + cmd.setName(name); + cmd.setType(StorageType.CONTAINER); + return cmd; + } + }), null)); } protected MutableStorageMetadata create() { @@ -334,7 +328,7 @@ public class FilesystemAsyncBlobStore extends BaseAsyncBlobStore { @Path("{container}") @Override public ListenableFuture createContainerInLocation(final Location location, - @PathParam("container") @ParamValidators( { FilesystemContainerNameValidator.class }) String name) { + @PathParam("container") @ParamValidators({ FilesystemContainerNameValidator.class }) String name) { boolean result = storageStrategy.createContainer(name); return immediateFuture(result); } @@ -347,9 +341,9 @@ public class FilesystemAsyncBlobStore extends BaseAsyncBlobStore { } /** - * Load the blob with the given key belonging to the container with the given name. There must - * exist a resource on the file system whose complete name is given concatenating the container - * name and the key + * Load the blob with the given key belonging to the container with the given + * name. There must exist a resource on the file system whose complete name + * is given concatenating the container name and the key * * @param container * it's the name of the container the blob belongs to @@ -358,30 +352,20 @@ public class FilesystemAsyncBlobStore extends BaseAsyncBlobStore { * * @return the blob belonging to the given container with the given key */ - private Blob loadFileBlob(final String container, final String key) { + private Blob loadFileBlob(String container, String key) { logger.debug("Opening blob in container: %s - %s", container, key); - File blobPayload = storageStrategy.getFileForBlobKey(container, key); - - Payload payload = new FilePayload(blobPayload); - // Loading object metadata - MutableBlobMetadata metadata = new MutableBlobMetadataImpl(); - metadata.setName(key); - metadata.setLastModified(new Date(blobPayload.lastModified())); - metadata.getContentMetadata().setContentLength(blobPayload.length()); - // TODO What about the MD5? are we supposed to calculate it each time we load - // the file? + BlobBuilder builder = blobUtils.blobBuilder(); + builder.name(key); + File file = storageStrategy.getFileForBlobKey(container, key); try { - payload = Payloads.calculateMD5(payload); + builder.payload(file).calculateMD5(); } catch (IOException e) { logger.error("An error occurred calculating MD5 for blob %s from container ", key, container); Throwables.propagateIfPossible(e); } - metadata.getContentMetadata().setContentType(""); - String eTag = CryptoStreams.hex(payload.getContentMetadata().getContentMD5()); - metadata.setETag(eTag); - // Creating new blob object - Blob blob = blobFactory.create(metadata); - blob.setPayload(blobPayload); + Blob blob = builder.build(); + if (blob.getPayload().getContentMetadata().getContentMD5() != null) + blob.getMetadata().setETag(CryptoStreams.hex(blob.getPayload().getContentMetadata().getContentMD5())); return blob; } @@ -504,7 +488,7 @@ public class FilesystemAsyncBlobStore extends BaseAsyncBlobStore { storageStrategy.writePayloadOnFile(containerName, blobKey, object.getPayload()); } catch (IOException e) { logger.error(e, "An error occurred storing the new object with name [%s] to container [%s].", blobKey, - containerName); + containerName); Throwables.propagate(e); } return immediateFuture(eTag); @@ -551,7 +535,7 @@ public class FilesystemAsyncBlobStore extends BaseAsyncBlobStore { if (blob.getMetadata().getLastModified().before(modifiedSince)) { HttpResponse response = new HttpResponse(304, null, null); return immediateFailedFuture(new HttpResponseException(String.format("%1$s is before %2$s", blob - .getMetadata().getLastModified(), modifiedSince), null, response)); + .getMetadata().getLastModified(), modifiedSince), null, response)); } } @@ -560,7 +544,7 @@ public class FilesystemAsyncBlobStore extends BaseAsyncBlobStore { if (blob.getMetadata().getLastModified().after(unmodifiedSince)) { HttpResponse response = new HttpResponse(412, null, null); return immediateFailedFuture(new HttpResponseException(String.format("%1$s is after %2$s", blob - .getMetadata().getLastModified(), unmodifiedSince), null, response)); + .getMetadata().getLastModified(), unmodifiedSince), null, response)); } } @@ -602,10 +586,10 @@ public class FilesystemAsyncBlobStore extends BaseAsyncBlobStore { * {@inheritDoc} */ @Override - public ListenableFuture blobMetadata(final String container, final String key) { + public ListenableFuture blobMetadata(String container, String key) { try { Blob blob = getBlob(container, key).get(); - return immediateFuture(blob != null ? (BlobMetadata) copy(blob.getMetadata()) : null); + return Futures. immediateFuture(blob != null ? blob.getMetadata() : null); } catch (Exception e) { if (size(filter(getCausalChain(e), KeyNotFoundException.class)) >= 1) return immediateFuture(null); @@ -620,8 +604,9 @@ public class FilesystemAsyncBlobStore extends BaseAsyncBlobStore { } /** - * Override parent method because it uses strange futures and listenables that creates problem in - * the test if more than one test that deletes the container is executed + * Override parent method because it uses strange futures and listenables + * that creates problem in the test if more than one test that deletes the + * container is executed * * @param container * @return @@ -633,8 +618,8 @@ public class FilesystemAsyncBlobStore extends BaseAsyncBlobStore { } /** - * Each container is a directory, so in order to check if a container exists the corresponding - * directory must exists. Synchronous implementation + * Each container is a directory, so in order to check if a container exists + * the corresponding directory must exists. Synchronous implementation * * @param containerName * @return @@ -670,7 +655,7 @@ public class FilesystemAsyncBlobStore extends BaseAsyncBlobStore { @Override public ListenableFuture createContainerInLocation(Location location, String container, - CreateContainerOptions options) { + CreateContainerOptions options) { if (options.isPublicRead()) throw new UnsupportedOperationException("publicRead"); return createContainerInLocation(location, container); diff --git a/apis/filesystem/src/main/java/org/jclouds/filesystem/strategy/FilesystemStorageStrategy.java b/apis/filesystem/src/main/java/org/jclouds/filesystem/strategy/FilesystemStorageStrategy.java index 15a7cdaff6..8fdd35fda2 100644 --- a/apis/filesystem/src/main/java/org/jclouds/filesystem/strategy/FilesystemStorageStrategy.java +++ b/apis/filesystem/src/main/java/org/jclouds/filesystem/strategy/FilesystemStorageStrategy.java @@ -152,7 +152,7 @@ public interface FilesystemStorageStrategy { * @param container * @param key */ - void removeBlob(final String container, final String key); + void removeBlob(String container, String key); /** * Write a {@link Blob} {@link Payload} into a file diff --git a/apis/filesystem/src/main/java/org/jclouds/filesystem/strategy/internal/FilesystemStorageStrategyImpl.java b/apis/filesystem/src/main/java/org/jclouds/filesystem/strategy/internal/FilesystemStorageStrategyImpl.java index 45ed8c60ae..095cca9e4f 100644 --- a/apis/filesystem/src/main/java/org/jclouds/filesystem/strategy/internal/FilesystemStorageStrategyImpl.java +++ b/apis/filesystem/src/main/java/org/jclouds/filesystem/strategy/internal/FilesystemStorageStrategyImpl.java @@ -24,13 +24,13 @@ import java.io.File; import java.io.FileFilter; import java.io.FileOutputStream; import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; import java.util.HashSet; import java.util.Iterator; import java.util.Set; import javax.annotation.Resource; +import javax.inject.Inject; +import javax.inject.Named; import javax.inject.Provider; import org.apache.commons.io.FileUtils; @@ -47,8 +47,8 @@ import org.jclouds.logging.Logger; import org.jclouds.rest.annotations.ParamValidators; import com.google.common.base.Throwables; -import com.google.inject.Inject; -import com.google.inject.name.Named; +import com.google.common.io.Closeables; +import com.google.common.io.Files; /** * @@ -57,8 +57,6 @@ import com.google.inject.name.Named; public class FilesystemStorageStrategyImpl implements FilesystemStorageStrategy { private static final String BACK_SLASH = "\\"; - /** The buffer size used to copy an InputStream to an OutputStream */ - private static final int COPY_BUFFER_SIZE = 1024; @Resource protected Logger logger = Logger.NULL; @@ -126,9 +124,8 @@ public class FilesystemStorageStrategyImpl implements FilesystemStorageStrategy File containerFile = openFolder(container); File[] children = containerFile.listFiles(); if (null != children) { - for (File child : children) { + for (File child : children) FileUtils.forceDelete(child); - } } } catch (IOException e) { logger.error(e, "An error occurred while clearing container %s", container); @@ -201,41 +198,26 @@ public class FilesystemStorageStrategyImpl implements FilesystemStorageStrategy public void writePayloadOnFile(String container, String blobKey, Payload payload) throws IOException { filesystemContainerNameValidator.validate(container); filesystemBlobKeyValidator.validate(blobKey); - File outputFile = null; - OutputStream output = null; - InputStream input = null; + File outputFile = getFileForBlobKey(container, blobKey); + if (payload.getRawContent().equals(outputFile)){ + // we shouldn't re-copy the same contents + return; + } + FileOutputStream output = null; try { - outputFile = getFileForBlobKey(container, blobKey); - File parentDirectory = outputFile.getParentFile(); - if (!parentDirectory.exists()) { - if (!parentDirectory.mkdirs()) { - throw new IOException("An error occurred creating directory [" + parentDirectory.getName() + "]."); - } - } - output = new FileOutputStream(outputFile); - input = payload.getInput(); - copy(input, output); - + Files.createParentDirs(outputFile); + if (payload.getRawContent() instanceof File) + Files.copy(File.class.cast(payload.getRawContent()), outputFile); + else + payload.writeTo(new FileOutputStream(outputFile)); } catch (IOException ex) { if (outputFile != null) { outputFile.delete(); } throw ex; } finally { - if (input != null) { - try { - input.close(); - } catch (IOException ex) { - // Does nothing - } - } - if (output != null) { - try { - output.close(); - } catch (IOException ex) { - // Does nothing - } - } + Closeables.closeQuietly(output); + payload.release(); } } @@ -494,31 +476,4 @@ public class FilesystemStorageStrategyImpl implements FilesystemStorageStrategy return result; } - /** - * Copy from an InputStream to an OutputStream. - * - * @param input - * The InputStream - * @param output - * The OutputStream - * @return the number of bytes copied - * @throws IOException - * if an error occurs - */ - private long copy(InputStream input, OutputStream output) throws IOException { - byte[] buffer = new byte[COPY_BUFFER_SIZE]; - long count = 0; - while (true) { - int read = input.read(buffer); - if (read < 0) { - break; - } - count += read; - - output.write(buffer, 0, read); - } - output.flush(); - return count; - } - } diff --git a/apis/filesystem/src/test/java/org/jclouds/filesystem/FilesystemAsyncBlobStoreTest.java b/apis/filesystem/src/test/java/org/jclouds/filesystem/FilesystemAsyncBlobStoreTest.java index 5b4a24c61e..e0ab114c55 100644 --- a/apis/filesystem/src/test/java/org/jclouds/filesystem/FilesystemAsyncBlobStoreTest.java +++ b/apis/filesystem/src/test/java/org/jclouds/filesystem/FilesystemAsyncBlobStoreTest.java @@ -48,6 +48,7 @@ import org.jclouds.blobstore.domain.StorageMetadata; import org.jclouds.blobstore.domain.StorageType; import org.jclouds.blobstore.options.GetOptions; import org.jclouds.blobstore.options.ListContainerOptions; +import org.jclouds.crypto.CryptoStreams; import org.jclouds.filesystem.reference.FilesystemConstants; import org.jclouds.filesystem.utils.TestUtils; import org.testng.annotations.AfterMethod; @@ -179,9 +180,9 @@ public class FilesystemAsyncBlobStoreTest { checkForContainerContent(CONTAINER_NAME, null); // creates blobs in first container - Set blobsExpected = TestUtils.createBlobsInContainer(CONTAINER_NAME, new String[] { - "bbb" + File.separator + "ccc" + File.separator + "ddd" + File.separator + "1234.jpg", "4rrr.jpg", - "rrr" + File.separator + "sss" + File.separator + "788.jpg", "xdc" + File.separator + "wert.kpg" }); + Set blobsExpected = TestUtils.createBlobsInContainer(CONTAINER_NAME, "bbb" + File.separator + "ccc" + + File.separator + "ddd" + File.separator + "1234.jpg", "4rrr.jpg", "rrr" + File.separator + "sss" + + File.separator + "788.jpg", "xdc" + File.separator + "wert.kpg"); checkForContainerContent(CONTAINER_NAME, blobsExpected); } @@ -214,17 +215,17 @@ public class FilesystemAsyncBlobStoreTest { // creates blobs in first container - Set blobNamesCreatedInContainer1 = TestUtils.createBlobsInContainer(CONTAINER_NAME, - new String[] { "bbb" + File.separator + "ccc" + File.separator + "ddd" + File.separator + "1234.jpg", - TestUtils.createRandomBlobKey(), "rrr" + File.separator + "sss" + File.separator + "788.jpg", - "xdc" + File.separator + "wert.kpg" }); + Set blobNamesCreatedInContainer1 = TestUtils.createBlobsInContainer(CONTAINER_NAME, "bbb" + + File.separator + "ccc" + File.separator + "ddd" + File.separator + "1234.jpg", + TestUtils.createRandomBlobKey(), "rrr" + File.separator + "sss" + File.separator + "788.jpg", "xdc" + + File.separator + "wert.kpg"); // creates blobs in second container blobStore.createContainerInLocation(null, CONTAINER_NAME2); - Set blobNamesCreatedInContainer2 = TestUtils.createBlobsInContainer(CONTAINER_NAME2, new String[] { - "asd" + File.separator + "bbb" + File.separator + "ccc" + File.separator + "ddd" + File.separator - + "1234.jpg", TestUtils.createRandomBlobKey(), - "rrr" + File.separator + "sss" + File.separator + "788.jpg", "xdc" + File.separator + "wert.kpg" }); + Set blobNamesCreatedInContainer2 = TestUtils.createBlobsInContainer(CONTAINER_NAME2, "asd" + + File.separator + "bbb" + File.separator + "ccc" + File.separator + "ddd" + File.separator + "1234.jpg", + TestUtils.createRandomBlobKey(), "rrr" + File.separator + "sss" + File.separator + "788.jpg", "xdc" + + File.separator + "wert.kpg"); // test blobs in first container checkForContainerContent(CONTAINER_NAME, blobNamesCreatedInContainer1); @@ -238,9 +239,9 @@ public class FilesystemAsyncBlobStoreTest { checkForContainerContent(CONTAINER_NAME, null); // creates blobs in first container - Set blobsExpected = TestUtils.createBlobsInContainer(CONTAINER_NAME, new String[] { - "bbb" + File.separator + "ccc" + File.separator + "ddd" + File.separator + "1234.jpg", "4rrr.jpg", - "rrr" + File.separator + "sss" + File.separator + "788.jpg", "rrr" + File.separator + "wert.kpg" }); + Set blobsExpected = TestUtils.createBlobsInContainer(CONTAINER_NAME, "bbb" + File.separator + "ccc" + + File.separator + "ddd" + File.separator + "1234.jpg", "4rrr.jpg", "rrr" + File.separator + "sss" + + File.separator + "788.jpg", "rrr" + File.separator + "wert.kpg"); // remove not expected values blobsExpected.remove("bbb" + File.separator + "ccc" + File.separator + "ddd" + File.separator + "1234.jpg"); @@ -257,8 +258,8 @@ public class FilesystemAsyncBlobStoreTest { } /** - * Integration test, because clearContainer is not redefined in {@link FilesystemAsyncBlobStore} - * class + * Integration test, because clearContainer is not redefined in + * {@link FilesystemAsyncBlobStore} class */ public void testClearContainer_NoOptions() throws IOException { final String CONTAINER_NAME2 = "containerToClear"; @@ -268,17 +269,17 @@ public class FilesystemAsyncBlobStoreTest { blobStore.createContainerInLocation(null, CONTAINER_NAME2); // creates blobs in first container - Set blobNamesCreatedInContainer1 = TestUtils.createBlobsInContainer(CONTAINER_NAME, - new String[] { "bbb" + File.separator + "ccc" + File.separator + "ddd" + File.separator + "1234.jpg", - TestUtils.createRandomBlobKey(), "rrr" + File.separator + "sss" + File.separator + "788.jpg", - "xdc" + File.separator + "wert.kpg" }); + Set blobNamesCreatedInContainer1 = TestUtils.createBlobsInContainer(CONTAINER_NAME, "bbb" + + File.separator + "ccc" + File.separator + "ddd" + File.separator + "1234.jpg", + TestUtils.createRandomBlobKey(), "rrr" + File.separator + "sss" + File.separator + "788.jpg", "xdc" + + File.separator + "wert.kpg"); // creates blobs in second container blobStore.createContainerInLocation(null, CONTAINER_NAME2); - Set blobNamesCreatedInContainer2 = TestUtils.createBlobsInContainer(CONTAINER_NAME2, new String[] { - "asd" + File.separator + "bbb" + File.separator + "ccc" + File.separator + "ddd" + File.separator - + "1234.jpg", TestUtils.createRandomBlobKey(), - "rrr" + File.separator + "sss" + File.separator + "788.jpg", "xdc" + File.separator + "wert.kpg" }); + Set blobNamesCreatedInContainer2 = TestUtils.createBlobsInContainer(CONTAINER_NAME2, "asd" + + File.separator + "bbb" + File.separator + "ccc" + File.separator + "ddd" + File.separator + "1234.jpg", + TestUtils.createRandomBlobKey(), "rrr" + File.separator + "sss" + File.separator + "788.jpg", "xdc" + + File.separator + "wert.kpg"); // test blobs in containers checkForContainerContent(CONTAINER_NAME, blobNamesCreatedInContainer1); @@ -294,8 +295,8 @@ public class FilesystemAsyncBlobStoreTest { } /** - * Integration test, because countBlobs is not redefined in {@link FilesystemAsyncBlobStore} - * class + * Integration test, because countBlobs is not redefined in + * {@link FilesystemAsyncBlobStore} class */ public void testCountBlobs_NotExistingContainer() { try { @@ -306,8 +307,8 @@ public class FilesystemAsyncBlobStoreTest { } /** - * Integration test, because countBlobs is not redefined in {@link FilesystemAsyncBlobStore} - * class + * Integration test, because countBlobs is not redefined in + * {@link FilesystemAsyncBlobStore} class */ public void testCountBlobs_NoOptionsEmptyContainer() { blobStore.createContainerInLocation(null, CONTAINER_NAME); @@ -319,8 +320,8 @@ public class FilesystemAsyncBlobStoreTest { } /** - * Integration test, because countBlobs is not redefined in {@link FilesystemAsyncBlobStore} - * class + * Integration test, because countBlobs is not redefined in + * {@link FilesystemAsyncBlobStore} class */ public void testCountBlobs_NoOptions() { blobStore.createContainerInLocation(null, CONTAINER_NAME); @@ -342,7 +343,7 @@ public class FilesystemAsyncBlobStoreTest { assertFalse(result, "Blob exists"); // create the blob - TestUtils.createBlobsInContainer(CONTAINER_NAME, new String[] { BLOB_KEY }); + TestUtils.createBlobsInContainer(CONTAINER_NAME, BLOB_KEY); result = blobStore.blobExists(CONTAINER_NAME, BLOB_KEY); assertTrue(result, "Blob exists"); @@ -366,7 +367,7 @@ public class FilesystemAsyncBlobStoreTest { assertFalse(result, "Blob2 exists"); // create the blob - TestUtils.createBlobsInContainer(CONTAINER_NAME, new String[] { BLOB_KEY1, BLOB_KEY2 }); + TestUtils.createBlobsInContainer(CONTAINER_NAME, BLOB_KEY1, BLOB_KEY2); result = blobStore.blobExists(CONTAINER_NAME, BLOB_KEY1); assertTrue(result, "Blob " + BLOB_KEY1 + " doesn't exist"); result = blobStore.blobExists(CONTAINER_NAME, BLOB_KEY2); @@ -401,7 +402,7 @@ public class FilesystemAsyncBlobStoreTest { TestUtils.fileExists(TARGET_CONTAINER_NAME + File.separator + BLOB_KEY, false); // create the blob - TestUtils.createBlobsInContainer(CONTAINER_NAME, new String[] { BLOB_KEY }); + TestUtils.createBlobsInContainer(CONTAINER_NAME, BLOB_KEY); result = blobStore.blobExists(CONTAINER_NAME, BLOB_KEY); assertTrue(result, "Blob doesn't exist"); @@ -416,8 +417,9 @@ public class FilesystemAsyncBlobStoreTest { } /** - * Test of removeBlob method, with two blobs with a complex path as key and when first blob is - * removed, not all of its key's path is removed, because it is shared with the second blob's key + * Test of removeBlob method, with two blobs with a complex path as key and + * when first blob is removed, not all of its key's path is removed, because + * it is shared with the second blob's key */ public void testRemoveBlob_TwoComplexBlobKeys() throws IOException { final String BLOB_KEY1 = TestUtils.createRandomBlobKey("aa/bb/cc/dd/", null); @@ -433,7 +435,7 @@ public class FilesystemAsyncBlobStoreTest { assertFalse(result, "Blob2 exists"); // create the blobs - TestUtils.createBlobsInContainer(CONTAINER_NAME, new String[] { BLOB_KEY1, BLOB_KEY2 }); + TestUtils.createBlobsInContainer(CONTAINER_NAME, BLOB_KEY1, BLOB_KEY2); result = blobStore.blobExists(CONTAINER_NAME, BLOB_KEY1); assertTrue(result, "Blob " + BLOB_KEY1 + " doesn't exist"); result = blobStore.blobExists(CONTAINER_NAME, BLOB_KEY2); @@ -476,7 +478,8 @@ public class FilesystemAsyncBlobStoreTest { } /** - * Test of createContainerInLocation method, of class FilesystemAsyncBlobStore. + * Test of createContainerInLocation method, of class + * FilesystemAsyncBlobStore. */ public void testCreateContainerInLocation() throws IOException { final String CONTAINER_NAME2 = "funambol-test-2"; @@ -505,8 +508,8 @@ public class FilesystemAsyncBlobStoreTest { } /** - * Test of putBlob method, of class FilesystemAsyncBlobStore. with a simple filename - no path in - * the filename, eg filename.jpg + * Test of putBlob method, of class FilesystemAsyncBlobStore. with a simple + * filename - no path in the filename, eg filename.jpg */ public void testPutBlobSimpleName() { blobStore.createContainerInLocation(null, CONTAINER_NAME); @@ -515,7 +518,8 @@ public class FilesystemAsyncBlobStoreTest { } /** - * Test of putBlob method with a complex key, with path in the filename, eg picture/filename.jpg + * Test of putBlob method with a complex key, with path in the filename, eg + * picture/filename.jpg */ public void testPutBlobComplexName1() { blobStore.createContainerInLocation(null, CONTAINER_NAME); @@ -526,7 +530,8 @@ public class FilesystemAsyncBlobStoreTest { } /** - * Test of putBlob method with a complex key, with path in the filename, eg picture/filename.jpg + * Test of putBlob method with a complex key, with path in the filename, eg + * picture/filename.jpg */ public void testPutBlobComplexName2() { blobStore.createContainerInLocation(null, CONTAINER_NAME); @@ -589,7 +594,7 @@ public class FilesystemAsyncBlobStoreTest { assertNull(resultBlob, "Blob exists"); // create blob - TestUtils.createBlobsInContainer(CONTAINER_NAME, new String[] { blobKey }); + TestUtils.createBlobsInContainer(CONTAINER_NAME, blobKey); resultBlob = blobStore.getBlob(CONTAINER_NAME, blobKey, options); @@ -608,7 +613,7 @@ public class FilesystemAsyncBlobStoreTest { public void testBlobMetadata_withDefaultMetadata() throws IOException { String BLOB_KEY = TestUtils.createRandomBlobKey(null, null); // create the blob - TestUtils.createBlobsInContainer(CONTAINER_NAME, new String[] { BLOB_KEY }); + TestUtils.createBlobsInContainer(CONTAINER_NAME, BLOB_KEY); BlobMetadata metadata = blobStore.blobMetadata(CONTAINER_NAME, BLOB_KEY); assertNotNull(metadata, "Metadata null"); @@ -616,7 +621,8 @@ public class FilesystemAsyncBlobStoreTest { assertEquals(metadata.getName(), BLOB_KEY, "Wrong blob name"); assertEquals(metadata.getType(), StorageType.BLOB, "Wrong blob type"); assertEquals(metadata.getContentMetadata().getContentType(), "application/unknown", "Wrong blob content-type"); - assertEquals(metadata.getContentMetadata().getContentMD5(), null, "Wrong blob MD5"); + assertEquals(CryptoStreams.hex(metadata.getContentMetadata().getContentMD5()), metadata.getETag(), + "Wrong blob MD5"); assertEquals(metadata.getLocation(), null, "Wrong blob location"); assertEquals(metadata.getProviderId(), null, "Wrong blob provider id"); assertEquals(metadata.getUri(), null, "Wrong blob URI"); @@ -625,8 +631,6 @@ public class FilesystemAsyncBlobStoreTest { // metadata.getLastModified() File file = new File(TARGET_CONTAINER_NAME + File.separator + BLOB_KEY); assertEquals(metadata.getContentMetadata().getContentLength(), new Long(file.length()), "Wrong blob size"); - // don't know how to calculate ETAG - // assertEquals(metadata.getETag(), "105cf4e6c052d65352dabd20028ff102", "Wrong blob ETag"); } public void testDeleteContainer_NotExistingContainer() { @@ -667,15 +671,13 @@ public class FilesystemAsyncBlobStoreTest { TestUtils.directoryExists(TARGET_CONTAINER_NAME2, true); // create blobs inside container - TestUtils.createBlobsInContainer(CONTAINER_NAME, new String[] { - TestUtils.createRandomBlobKey("testutils-", null), TestUtils.createRandomBlobKey("testutils-", null), - TestUtils.createRandomBlobKey("ab123s" + File.separator + "testutils-", null), }); - TestUtils.createBlobsInContainer( - CONTAINER_NAME, - new String[] { TestUtils.createRandomBlobKey("testutils-", null), - TestUtils.createRandomBlobKey("testutils-", null), - TestUtils.createRandomBlobKey("asda123s" + File.separator + "testutils-", null), - TestUtils.createRandomBlobKey("123-_3s" + File.separator + "testutils-", null), }); + TestUtils.createBlobsInContainer(CONTAINER_NAME, TestUtils.createRandomBlobKey("testutils-", null), + TestUtils.createRandomBlobKey("testutils-", null), + TestUtils.createRandomBlobKey("ab123s" + File.separator + "testutils-", null)); + TestUtils.createBlobsInContainer(CONTAINER_NAME, TestUtils.createRandomBlobKey("testutils-", null), + TestUtils.createRandomBlobKey("testutils-", null), + TestUtils.createRandomBlobKey("asda123s" + File.separator + "testutils-", null), + TestUtils.createRandomBlobKey("123-_3s" + File.separator + "testutils-", null)); // delete first container blobStore.deleteContainer(CONTAINER_NAME); diff --git a/apis/filesystem/src/test/java/org/jclouds/filesystem/utils/TestUtils.java b/apis/filesystem/src/test/java/org/jclouds/filesystem/utils/TestUtils.java index 8ec538adf3..049afa0f46 100644 --- a/apis/filesystem/src/test/java/org/jclouds/filesystem/utils/TestUtils.java +++ b/apis/filesystem/src/test/java/org/jclouds/filesystem/utils/TestUtils.java @@ -76,7 +76,7 @@ public class TestUtils { * @return a Set with all blobs created * @throws IOException */ - public static Set createBlobsInContainer(String containerName, String[] blobNames) throws IOException { + public static Set createBlobsInContainer(String containerName, String... blobNames) throws IOException { Set blobNamesCreatedInContainer = new HashSet(); for (String blobName : blobNames) { createBlobAsFile(containerName, blobName, getImageForBlobPayload());