Merge branch 'master' of github.com:jclouds/jclouds

* 'master' of github.com:jclouds/jclouds:
  Solved issue 409: Filesystem provider treats PROPERTY_BASEDIR pointing to an absolute dir
This commit is contained in:
Adrian Cole 2010-11-24 10:59:57 +01:00
commit c77979c311
3 changed files with 81 additions and 15 deletions

View File

@ -335,12 +335,12 @@ public class FilesystemStorageStrategyImpl implements FilesystemStorageStrategy
* @return the resulting string * @return the resulting string
*/ */
protected String buildPathStartingFromBaseDir(String...pathTokens) { protected String buildPathStartingFromBaseDir(String...pathTokens) {
String normalizedToken = removeFileSeparatorFromBorders(normalize(baseDirectory)); String normalizedToken = removeFileSeparatorFromBorders(normalize(baseDirectory), true);
StringBuilder completePath = new StringBuilder(normalizedToken); StringBuilder completePath = new StringBuilder(normalizedToken);
if(pathTokens!=null && pathTokens.length>0) { if(pathTokens!=null && pathTokens.length>0) {
for(int i=0; i<pathTokens.length; i++) { for(int i=0; i<pathTokens.length; i++) {
if(pathTokens[i]!=null) { if(pathTokens[i]!=null) {
normalizedToken = removeFileSeparatorFromBorders(normalize(pathTokens[i])); normalizedToken = removeFileSeparatorFromBorders(normalize(pathTokens[i]), false);
completePath.append(File.separator).append(normalizedToken); completePath.append(File.separator).append(normalizedToken);
} }
} }
@ -367,16 +367,19 @@ public class FilesystemStorageStrategyImpl implements FilesystemStorageStrategy
* Remove leading and trailing {@link File.separator} character from the * Remove leading and trailing {@link File.separator} character from the
* string. * string.
* @param pathToBeCleaned * @param pathToBeCleaned
* @param remove only trailing separator char from path
* @return * @return
*/ */
private String removeFileSeparatorFromBorders(String pathToBeCleaned) { private String removeFileSeparatorFromBorders(String pathToBeCleaned, boolean onlyTrailing) {
if (null == pathToBeCleaned || pathToBeCleaned.equals("")) return pathToBeCleaned; if (null == pathToBeCleaned || pathToBeCleaned.equals("")) return pathToBeCleaned;
int beginIndex = 0; int beginIndex = 0;
int endIndex = pathToBeCleaned.length(); int endIndex = pathToBeCleaned.length();
//search for separator chars //search for separator chars
if (pathToBeCleaned.substring(0, 1).equals(File.separator)) beginIndex = 1; if (!onlyTrailing) {
if (pathToBeCleaned.substring(0, 1).equals(File.separator)) beginIndex = 1;
}
if (pathToBeCleaned.substring(pathToBeCleaned.length() - 1).equals(File.separator)) endIndex--; if (pathToBeCleaned.substring(pathToBeCleaned.length() - 1).equals(File.separator)) endIndex--;
return pathToBeCleaned.substring(beginIndex, endIndex); return pathToBeCleaned.substring(beginIndex, endIndex);

View File

@ -185,16 +185,8 @@ public class FilesystemAsyncBlobStoreTest {
/** /**
* Test of list method, of class FilesystemAsyncBlobStore. * Test of list method, of class FilesystemAsyncBlobStore.
*/ */
public void testList_NoOptionSingleContainer() throws IOException { public void testList_NoOptionSingleContainer()
throws IOException {
// Testing list for a not existing container
try {
blobStore.list(CONTAINER_NAME);
fail("Found a not existing container");
} catch(ContainerNotFoundException e) {
}
blobStore.createContainerInLocation(null, CONTAINER_NAME); blobStore.createContainerInLocation(null, CONTAINER_NAME);
// Testing list for an empty container // Testing list for an empty container
checkForContainerContent(CONTAINER_NAME, null); checkForContainerContent(CONTAINER_NAME, null);
@ -268,6 +260,29 @@ public class FilesystemAsyncBlobStoreTest {
} }
public void testList_Subdirectory()
throws IOException {
blobStore.createContainerInLocation(null, CONTAINER_NAME);
// Testing list for an empty container
checkForContainerContent(CONTAINER_NAME, null);
//creates blobs in first container
Set<String> 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" }
);
//remove not expected values
blobsExpected.remove("bbb" + File.separator + "ccc" + File.separator + "ddd" + File.separator + "1234.jpg");
blobsExpected.remove("4rrr.jpg");
checkForContainerContent(CONTAINER_NAME, "rrr", blobsExpected);
}
/** /**
* TODO * TODO
* Should throws an exception? * Should throws an exception?
@ -809,12 +824,17 @@ public class FilesystemAsyncBlobStoreTest {
* @param expectedBlobKeys * @param expectedBlobKeys
*/ */
private void checkForContainerContent(final String containerName, Set<String> expectedBlobKeys) { private void checkForContainerContent(final String containerName, Set<String> expectedBlobKeys) {
checkForContainerContent(containerName, null, expectedBlobKeys);
}
private void checkForContainerContent(final String containerName, String inDirectory, Set<String> expectedBlobKeys) {
ListContainerOptions options = ListContainerOptions.Builder.recursive(); ListContainerOptions options = ListContainerOptions.Builder.recursive();
if (null != inDirectory && !"".equals(inDirectory)) options.inDirectory(inDirectory);
PageSet<? extends StorageMetadata> blobsRetrieved = blobStore.list(containerName, options); PageSet<? extends StorageMetadata> blobsRetrieved = blobStore.list(containerName, options);
//nothing expected //nothing expected
if (null == expectedBlobKeys) { if (null == expectedBlobKeys || 0 == expectedBlobKeys.size()) {
assertTrue(blobsRetrieved.isEmpty(), "Wrong blob number retrieved in the containter [" + containerName + "]"); assertTrue(blobsRetrieved.isEmpty(), "Wrong blob number retrieved in the containter [" + containerName + "]");
return; return;
} }

View File

@ -417,6 +417,38 @@ public class FilesystemStorageStrategyImplTest {
} }
public void testGetFileForBlobKey_AbsolutePath()
throws IOException {
String absoluteBasePath = (new File(getAbsoluteDirectory(), "basedir")).getAbsolutePath() + FS;
String absoluteContainerPath = absoluteBasePath + CONTAINER_NAME + FS;
//create storageStrategy with an absolute path
FilesystemStorageStrategy storageStrategyAbsolute = new FilesystemStorageStrategyImpl(
new Blob.Factory() {
@Override
public Blob create(MutableBlobMetadata metadata) {
return new BlobImpl(metadata != null ? metadata : new MutableBlobMetadataImpl());
}
},
absoluteBasePath,
new FilesystemContainerNameValidatorImpl(),
new FilesystemBlobKeyValidatorImpl());
TestUtils.cleanDirectoryContent(absoluteContainerPath);
String blobKey;
File fileForPayload;
blobKey = TestUtils.createRandomBlobKey("getFileForBlobKey-", ".img");
fileForPayload = storageStrategyAbsolute.getFileForBlobKey(CONTAINER_NAME, blobKey);
assertNotNull(fileForPayload, "Result File object is null");
assertEquals(fileForPayload.getAbsolutePath(), absoluteContainerPath + blobKey, "Wrong file path");
blobKey = TestUtils.createRandomBlobKey("asd" + FS + "vmad" + FS + "andsnf" + FS + "getFileForBlobKey-", ".img");
fileForPayload = storageStrategyAbsolute.getFileForBlobKey(CONTAINER_NAME, blobKey);
assertEquals(fileForPayload.getAbsolutePath(), absoluteContainerPath + blobKey, "Wrong file path");
}
public void testBlobExists() throws IOException { public void testBlobExists() throws IOException {
String[] sourceBlobKeys = new String[]{ String[] sourceBlobKeys = new String[]{
TestUtils.createRandomBlobKey("blobExists-", ".jpg"), TestUtils.createRandomBlobKey("blobExists-", ".jpg"),
@ -530,5 +562,16 @@ public class FilesystemStorageStrategyImplTest {
//---------------------------------------------------------- Private methods //---------------------------------------------------------- Private methods
/**
* Calculates an absolute directory path that depends on operative system
* @return
*/
private String getAbsoluteDirectory() throws IOException {
File tempFile = File.createTempFile("prefix", "suffix");
String tempAbsolutePath = tempFile.getParent();
return tempAbsolutePath;
}
} }