11package com .upplication .s3fs .Path ;
22
3+ import com .upplication .s3fs .AmazonS3Factory ;
34import com .upplication .s3fs .S3FileSystem ;
45import com .upplication .s3fs .S3FileSystemProvider ;
56import com .upplication .s3fs .S3Path ;
910import org .junit .Test ;
1011
1112import java .io .IOException ;
13+ import java .net .URI ;
1214import java .nio .file .FileSystems ;
1315import java .nio .file .Path ;
16+ import java .nio .file .Paths ;
1417import java .nio .file .WatchEvent ;
1518import java .util .HashMap ;
1619
@@ -232,4 +235,55 @@ public void registerWatchService() throws IOException {
232235 S3Path path = forPath ("/buck/file" );
233236 path .register (null , new WatchEvent .Kind <?>[0 ], new WatchEvent .Modifier [0 ]);
234237 }
238+ @ Test
239+ public void testResolve () throws IOException {
240+ S3FileSystem fileSystem2 = null ;
241+ try {
242+ HashMap <String , String > environments = new HashMap <>();
243+ environments .put (AmazonS3Factory .ACCESS_KEY , "accesskey" );
244+ environments .put (AmazonS3Factory .SECRET_KEY , "secretaccesskey" );
245+
246+ fileSystem2 = (S3FileSystem ) FileSystems .newFileSystem (URI .create ("s3://accesskey:secretaccesskey@s3.test.amazonaws.com/bucket" ), environments );
247+
248+ //good
249+ S3Path parent = (S3Path ) fileSystem2 .provider ().getPath (URI .create ("s3://accesskey:secretaccesskey@s3.test.amazonaws.com/bucket" ));
250+ S3Path child = (S3Path ) fileSystem2 .provider ().getPath (URI .create ("s3://accesskey:secretaccesskey@s3.test.amazonaws.com/bucket/rabbit" ));
251+ S3Path resolved = (S3Path ) parent .resolve (child );
252+
253+ assertEquals (child , resolved );
254+
255+ resolved = (S3Path ) parent .resolve ("s3://accesskey:secretaccesskey@s3.test.amazonaws.com/bucket/rabbit" );
256+ assertEquals (child , resolved );
257+
258+ resolved = (S3Path ) parent .resolve ("rabbit" );
259+ assertEquals (child , resolved );
260+
261+ resolved = (S3Path ) parent .resolve (Paths .get ("rabbit" )); //unixPath
262+ assertEquals (child , resolved );
263+
264+ resolved = (S3Path ) parent .resolve (Paths .get ("./rabbit" )); //unixPath
265+ assertEquals ("s3://accesskey:secretaccesskey@s3.test.amazonaws.com/bucket/./rabbit" , resolved .toString ());
266+
267+ resolved = (S3Path ) parent .resolve (Paths .get ("./rabbit in space" )); //unixPath
268+ assertEquals ("s3://accesskey:secretaccesskey@s3.test.amazonaws.com/bucket/./rabbit%20in%20space" , resolved .toString ());
269+
270+ try {
271+ parent .resolve (Paths .get ("/tmp" ));
272+ fail ("expect IllegalArgumentException" );
273+ } catch (IllegalArgumentException e ) {
274+ //ignore
275+ assertEquals ("other must be an instance of com.upplication.s3fs.S3Path or be relative" , e .getMessage ());
276+ }
277+
278+ //bad
279+ S3Path parent2 = fileSystem2 .getPath ("s3://accesskey:secretaccesskey@s3.test.amazonaws.com/bucket" );
280+ S3Path child2 = fileSystem2 .getPath ("s3://accesskey:secretaccesskey@s3.test.amazonaws.com/bucket/rabbit" );
281+ S3Path resolved2 = (S3Path ) parent2 .resolve (child2 );
282+ assertEquals ("s3:/accesskey:secretaccesskey@s3.test.amazonaws.com/bucket/s3:/accesskey:secretaccesskey@s3.test.amazonaws.com/bucket/rabbit" , resolved2 .toString ());
283+
284+ } finally {
285+ if (fileSystem2 != null )
286+ fileSystem2 .close ();
287+ }
288+ }
235289}
0 commit comments