1212import java .io .IOException ;
1313import java .net .URI ;
1414import java .nio .file .FileSystem ;
15+ import java .nio .file .FileSystemNotFoundException ;
1516import java .nio .file .FileSystems ;
1617import java .nio .file .Path ;
18+ import java .util .HashMap ;
1719import java .util .Map ;
20+ import java .util .Properties ;
1821
1922import static com .upplication .s3fs .AmazonS3Factory .ACCESS_KEY ;
2023import static com .upplication .s3fs .AmazonS3Factory .SECRET_KEY ;
2124import static com .upplication .s3fs .util .S3EndpointConstant .*;
2225
2326import static org .junit .Assert .assertEquals ;
2427import static org .junit .Assert .assertTrue ;
28+ import static org .mockito .Matchers .any ;
29+ import static org .mockito .Matchers .anyString ;
30+ import static org .mockito .Mockito .doReturn ;
31+ import static org .mockito .Mockito .spy ;
2532
2633public class ToUriTest extends S3UnitTestBase {
2734
35+ private S3FileSystemProvider s3fsProvider ;
36+
2837 @ Before
29- public void setup () throws IOException {
30- FileSystems
31- .newFileSystem (S3EndpointConstant .S3_GLOBAL_URI_TEST , null );
38+ public void setup () {
39+ s3fsProvider = spy (new S3FileSystemProvider ());
40+ // stub the possibility to add system envs var
41+ doReturn (false ).when (s3fsProvider ).overloadPropertiesWithSystemEnv (any (Properties .class ), anyString ());
42+ doReturn (new Properties ()).when (s3fsProvider ).loadAmazonProperties ();
43+
44+ s3fsProvider .newFileSystem (S3EndpointConstant .S3_GLOBAL_URI_TEST , null );
3245 }
3346
3447 @ Test
@@ -67,8 +80,7 @@ public void toUriWithNotEndSlash() {
6780
6881 @ Test
6982 public void toUriRelative () {
70- S3FileSystem fileSystem = new S3FileSystemProvider ()
71- .getFileSystem (S3_GLOBAL_URI_TEST );
83+ S3FileSystem fileSystem = s3fsProvider .getFileSystem (S3_GLOBAL_URI_TEST );
7284
7385 S3Path path = new S3Path (fileSystem , "bla" );
7486 assertEquals (URI .create ("bla" ), path .toUri ());
@@ -84,17 +96,32 @@ public void toUriBucketWithoutEndSlash() {
8496 @ Test
8597 public void toUriWithCredentials () {
8698 Map <String , String > envs = ImmutableMap .<String , String >builder ().put (ACCESS_KEY , "access" ).put (SECRET_KEY , "secret" ).build ();
87- FileSystem fileSystem = new S3FileSystemProvider ()
88- .newFileSystem (S3_GLOBAL_URI_TEST , envs );
99+ FileSystem fileSystem = s3fsProvider .newFileSystem (S3_GLOBAL_URI_TEST , envs );
89100
90101 Path path = fileSystem .getPath ("/bla/file" );
91102
92103 assertEquals (
URI .
create (
"s3://[email protected] /bla/file" ),
path .
toUri ());
93104 }
94105
106+ @ Test
107+ public void toUriWithCredentialBySystemProperty () {
108+
109+ System .setProperty (ACCESS_KEY , "accessKeywii" );
110+ System .setProperty (SECRET_KEY , "secretKey" );
111+
112+ FileSystem fileSystem = s3fsProvider .newFileSystem (S3_GLOBAL_URI_TEST , null );
113+
114+ Path path = fileSystem .getPath ("/bla/file" );
115+
116+ assertEquals (
URI .
create (
"s3://[email protected] /bla/file" ),
path .
toUri ());
117+
118+ System .clearProperty (ACCESS_KEY );
119+ System .clearProperty (SECRET_KEY );
120+ }
121+
95122 @ Test
96123 public void toUriWithEndpoint () throws IOException {
97- try (FileSystem fs = FileSystems .newFileSystem (URI .create ("s3://endpoint/" ), null )) {
124+ try (FileSystem fs = s3fsProvider .newFileSystem (URI .create ("s3://endpoint/" ), null )) {
98125 Path path = fs .getPath ("/bucket/path/to/file" );
99126 URI uri = path .toUri ();
100127 // the scheme is s3
@@ -104,7 +131,7 @@ public void toUriWithEndpoint() throws IOException {
104131 }
105132 }
106133
107- private static S3Path getPath (String path ) {
108- return ( S3Path ) FileSystems .getFileSystem (S3_GLOBAL_URI_TEST ).getPath (path );
134+ private S3Path getPath (String path ) {
135+ return s3fsProvider .getFileSystem (S3_GLOBAL_URI_TEST ).getPath (path );
109136 }
110137}
0 commit comments