diff --git a/src/main/java/com/upplication/s3fs/S3FileSystemProvider.java b/src/main/java/com/upplication/s3fs/S3FileSystemProvider.java index e136cd3..757dab3 100644 --- a/src/main/java/com/upplication/s3fs/S3FileSystemProvider.java +++ b/src/main/java/com/upplication/s3fs/S3FileSystemProvider.java @@ -229,6 +229,10 @@ public boolean overloadPropertiesWithSystemProps(Properties props, String key) { } /** + * The system envs have preference over the properties files. + * So we overload it + * @param props Properties + * @param key String * @return true if the key are overloaded by a system property */ public boolean overloadPropertiesWithSystemEnv(Properties props, String key) { @@ -239,6 +243,11 @@ public boolean overloadPropertiesWithSystemEnv(Properties props, String key) { return false; } + /** + * Get the system env with the key param + * @param key String + * @return String or null + */ public String systemGetEnv(String key) { return System.getenv(key); } diff --git a/src/main/java/com/upplication/s3fs/S3Path.java b/src/main/java/com/upplication/s3fs/S3Path.java index 5dadc9a..4ca813f 100644 --- a/src/main/java/com/upplication/s3fs/S3Path.java +++ b/src/main/java/com/upplication/s3fs/S3Path.java @@ -116,6 +116,8 @@ public S3FileStore getFileStore() { /** * key for amazon without final slash. * note: the final slash need to be added to save a directory (Amazon s3 spec) + * + * @return the key for AmazonS3Client */ public String getKey() { @@ -125,12 +127,6 @@ public String getKey() { key = key.substring(1, key.length()); } - // TODO: review this... :S - /* - if (key.endsWith("/")) { - key = key.substring(0, key.length()-1); - } - */ return key; } diff --git a/src/test/java/com/upplication/s3fs/FileSystemProvider/CreateDirectoryTest.java b/src/test/java/com/upplication/s3fs/FileSystemProvider/CreateDirectoryTest.java index 392beb3..b882cdb 100644 --- a/src/test/java/com/upplication/s3fs/FileSystemProvider/CreateDirectoryTest.java +++ b/src/test/java/com/upplication/s3fs/FileSystemProvider/CreateDirectoryTest.java @@ -14,9 +14,7 @@ import java.nio.file.*; import java.util.Properties; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.*; import static org.mockito.Mockito.*; public class CreateDirectoryTest extends S3UnitTestBase { @@ -51,6 +49,7 @@ public void createDirectoryInNewBucket() throws IOException { S3Path root = createNewS3FileSystem().getPath("/newer-bucket"); Path resolve = root.resolve("folder"); Path path = Files.createDirectories(resolve); + assertEquals("s3://s3.test.amazonaws.com/newer-bucket/folder", path.toAbsolutePath().toString()); // assert assertTrue(Files.exists(root)); @@ -89,8 +88,7 @@ private S3FileSystem createNewS3FileSystem() throws IOException { try { return s3fsProvider.getFileSystem(S3EndpointConstant.S3_GLOBAL_URI_TEST); } catch (FileSystemNotFoundException e) { - return (S3FileSystem) FileSystems.newFileSystem(S3EndpointConstant.S3_GLOBAL_URI_TEST, null); + return (S3FileSystem) s3fsProvider.newFileSystem(S3EndpointConstant.S3_GLOBAL_URI_TEST, null); } - } } \ No newline at end of file diff --git a/src/test/java/com/upplication/s3fs/Path/GetFilenameTest.java b/src/test/java/com/upplication/s3fs/Path/GetFileNameTest.java similarity index 100% rename from src/test/java/com/upplication/s3fs/Path/GetFilenameTest.java rename to src/test/java/com/upplication/s3fs/Path/GetFileNameTest.java diff --git a/src/test/java/com/upplication/s3fs/Path/ToUriTest.java b/src/test/java/com/upplication/s3fs/Path/ToUriTest.java index 209a795..4f34fba 100644 --- a/src/test/java/com/upplication/s3fs/Path/ToUriTest.java +++ b/src/test/java/com/upplication/s3fs/Path/ToUriTest.java @@ -12,9 +12,12 @@ import java.io.IOException; import java.net.URI; import java.nio.file.FileSystem; +import java.nio.file.FileSystemNotFoundException; import java.nio.file.FileSystems; import java.nio.file.Path; +import java.util.HashMap; import java.util.Map; +import java.util.Properties; import static com.upplication.s3fs.AmazonS3Factory.ACCESS_KEY; import static com.upplication.s3fs.AmazonS3Factory.SECRET_KEY; @@ -22,13 +25,23 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; public class ToUriTest extends S3UnitTestBase { + private S3FileSystemProvider s3fsProvider; + @Before - public void setup() throws IOException { - FileSystems - .newFileSystem(S3EndpointConstant.S3_GLOBAL_URI_TEST, null); + public void setup() { + s3fsProvider = spy(new S3FileSystemProvider()); + // stub the possibility to add system envs var + doReturn(false).when(s3fsProvider).overloadPropertiesWithSystemEnv(any(Properties.class), anyString()); + doReturn(new Properties()).when(s3fsProvider).loadAmazonProperties(); + + s3fsProvider.newFileSystem(S3EndpointConstant.S3_GLOBAL_URI_TEST, null); } @Test @@ -67,8 +80,7 @@ public void toUriWithNotEndSlash() { @Test public void toUriRelative() { - S3FileSystem fileSystem = new S3FileSystemProvider() - .getFileSystem(S3_GLOBAL_URI_TEST); + S3FileSystem fileSystem = s3fsProvider.getFileSystem(S3_GLOBAL_URI_TEST); S3Path path = new S3Path(fileSystem, "bla"); assertEquals(URI.create("bla"), path.toUri()); @@ -84,17 +96,32 @@ public void toUriBucketWithoutEndSlash() { @Test public void toUriWithCredentials() { Map envs = ImmutableMap.builder().put(ACCESS_KEY, "access").put(SECRET_KEY, "secret").build(); - FileSystem fileSystem = new S3FileSystemProvider() - .newFileSystem(S3_GLOBAL_URI_TEST, envs); + FileSystem fileSystem = s3fsProvider.newFileSystem(S3_GLOBAL_URI_TEST, envs); Path path = fileSystem.getPath("/bla/file"); assertEquals(URI.create("s3://access@s3.test.amazonaws.com/bla/file"), path.toUri()); } + @Test + public void toUriWithCredentialBySystemProperty() { + + System.setProperty(ACCESS_KEY, "accessKeywii"); + System.setProperty(SECRET_KEY, "secretKey"); + + FileSystem fileSystem = s3fsProvider.newFileSystem(S3_GLOBAL_URI_TEST, null); + + Path path = fileSystem.getPath("/bla/file"); + + assertEquals(URI.create("s3://accessKeywii@s3.test.amazonaws.com/bla/file"), path.toUri()); + + System.clearProperty(ACCESS_KEY); + System.clearProperty(SECRET_KEY); + } + @Test public void toUriWithEndpoint() throws IOException { - try (FileSystem fs = FileSystems.newFileSystem(URI.create("s3://endpoint/"), null)) { + try (FileSystem fs = s3fsProvider.newFileSystem(URI.create("s3://endpoint/"), null)) { Path path = fs.getPath("/bucket/path/to/file"); URI uri = path.toUri(); // the scheme is s3 @@ -104,7 +131,7 @@ public void toUriWithEndpoint() throws IOException { } } - private static S3Path getPath(String path) { - return (S3Path) FileSystems.getFileSystem(S3_GLOBAL_URI_TEST).getPath(path); + private S3Path getPath(String path) { + return s3fsProvider.getFileSystem(S3_GLOBAL_URI_TEST).getPath(path); } } diff --git a/src/test/java/com/upplication/s3fs/S3UnitTestBase.java b/src/test/java/com/upplication/s3fs/S3UnitTestBase.java index b9ade2e..e227a9e 100644 --- a/src/test/java/com/upplication/s3fs/S3UnitTestBase.java +++ b/src/test/java/com/upplication/s3fs/S3UnitTestBase.java @@ -1,5 +1,7 @@ package com.upplication.s3fs; +import static com.upplication.s3fs.AmazonS3Factory.ACCESS_KEY; +import static com.upplication.s3fs.AmazonS3Factory.SECRET_KEY; import static com.upplication.s3fs.S3FileSystemProvider.AMAZON_S3_FACTORY_CLASS; import org.junit.After; @@ -12,7 +14,13 @@ public class S3UnitTestBase { @BeforeClass public static void setProperties() { + + System.clearProperty(S3FileSystemProvider.AMAZON_S3_FACTORY_CLASS); + System.clearProperty(ACCESS_KEY); + System.clearProperty(SECRET_KEY); + System.setProperty(AMAZON_S3_FACTORY_CLASS, "com.upplication.s3fs.util.AmazonS3MockFactory"); + } @After