From fb1bce8e364d28d54971845529f36bb1be344b7d Mon Sep 17 00:00:00 2001 From: Enno Ruijters Date: Tue, 28 Sep 2021 10:16:00 +0200 Subject: [PATCH 1/5] Add unit test --- .../cloud/storage/s3fs/path/ResolveTest.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/test/java/org/carlspring/cloud/storage/s3fs/path/ResolveTest.java b/src/test/java/org/carlspring/cloud/storage/s3fs/path/ResolveTest.java index 64dc15f1..c5262b48 100644 --- a/src/test/java/org/carlspring/cloud/storage/s3fs/path/ResolveTest.java +++ b/src/test/java/org/carlspring/cloud/storage/s3fs/path/ResolveTest.java @@ -6,12 +6,14 @@ import org.carlspring.cloud.storage.s3fs.util.S3EndpointConstant; import java.io.IOException; +import java.nio.file.Paths; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.carlspring.cloud.storage.s3fs.util.S3EndpointConstant.S3_GLOBAL_URI_TEST; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; class ResolveTest extends S3UnitTestBase @@ -64,4 +66,30 @@ void resolve() assertEquals(getPath("/bucket2/other/child"), getPath("/bucket/path/to/file").resolve("/bucket2/other/child")); } + + @Test + void nonS3Paths() + { + S3Path parent = getPath("/bucket"); + S3Path child = getPath("/bucket/rabbit"); + S3Path resolved = (S3Path) parent.resolve(child); + + assertEquals(child, resolved); + + resolved = (S3Path) parent.resolve("rabbit"); + assertEquals(child, resolved); + + resolved = (S3Path) parent.resolve(Paths.get("rabbit")); //unixPath + assertEquals(child, resolved); + + resolved = (S3Path) parent.resolve(Paths.get("./rabbit")); //unixPath + assertEquals("s3://s3.test.amazonaws.com/bucket/./rabbit", resolved.toString()); + + resolved = (S3Path) parent.resolve(Paths.get("./rabbit in space")); //unixPath + assertEquals("s3://s3.test.amazonaws.com/bucket/./rabbit%20in%20space", resolved.toString()); + + IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> + parent.resolve(Paths.get("/tmp"))); + assertEquals("other must be an instance of org.carlspring.cloud.storage.s3fs.S3Path or a relative Path", e.getMessage()); + } } From 1ec951a516da6fc4ccdfb6fca0335ffc082e9381 Mon Sep 17 00:00:00 2001 From: Enno Ruijters Date: Tue, 28 Sep 2021 10:16:52 +0200 Subject: [PATCH 2/5] Support resolving against Strings and non-S3Path Paths --- .../carlspring/cloud/storage/s3fs/S3Path.java | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/carlspring/cloud/storage/s3fs/S3Path.java b/src/main/java/org/carlspring/cloud/storage/s3fs/S3Path.java index a1ad2485..cbea7783 100644 --- a/src/main/java/org/carlspring/cloud/storage/s3fs/S3Path.java +++ b/src/main/java/org/carlspring/cloud/storage/s3fs/S3Path.java @@ -32,6 +32,7 @@ public class S3Path public static final String PATH_SEPARATOR = "/"; public static final String PATH_OTHER_INSTANCE_MESSAGE = "other must be an instance of %s"; + public static final String PATH_OTHER_INSTANCE_OR_RELATIVE_MESSAGE = "other must be an instance of %s or a relative Path"; /** * S3FileStore which represents the Bucket this path resides in. @@ -444,16 +445,31 @@ public Path normalize() @Override public Path resolve(Path other) { + String otherUri = ""; if (other.isAbsolute()) { Preconditions.checkArgument(other instanceof S3Path, - PATH_OTHER_INSTANCE_MESSAGE, + PATH_OTHER_INSTANCE_OR_RELATIVE_MESSAGE, S3Path.class.getName()); return other; } + else if (other instanceof S3Path) + { + S3Path otherS3Path = (S3Path) other; + otherUri = otherS3Path.uri; + } + else + { + int nameCount = other.getNameCount(); + for (int i = 0; i < nameCount; i++) + { + if (i > 0) + otherUri += PATH_SEPARATOR; + otherUri += other.getName(i); + } + } - S3Path otherS3Path = (S3Path) other; StringBuilder pathBuilder = new StringBuilder(); if (this.isAbsolute()) @@ -463,9 +479,9 @@ public Path resolve(Path other) pathBuilder.append(this.uri); - if (!otherS3Path.uri.isEmpty()) + if (!otherUri.isEmpty()) { - pathBuilder.append(PATH_SEPARATOR + otherS3Path.uri); + pathBuilder.append(PATH_SEPARATOR + otherUri); } return new S3Path(this.fileSystem, pathBuilder.toString()); From 8b37b0830f99094960bc32c1d1c688273ccd7ce6 Mon Sep 17 00:00:00 2001 From: Martin Todorov Date: Tue, 5 Oct 2021 00:07:59 +0100 Subject: [PATCH 3/5] Code style fixes --- src/main/java/org/carlspring/cloud/storage/s3fs/S3Path.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/org/carlspring/cloud/storage/s3fs/S3Path.java b/src/main/java/org/carlspring/cloud/storage/s3fs/S3Path.java index cbea7783..41058667 100644 --- a/src/main/java/org/carlspring/cloud/storage/s3fs/S3Path.java +++ b/src/main/java/org/carlspring/cloud/storage/s3fs/S3Path.java @@ -465,7 +465,10 @@ else if (other instanceof S3Path) for (int i = 0; i < nameCount; i++) { if (i > 0) + { otherUri += PATH_SEPARATOR; + } + otherUri += other.getName(i); } } From 3d1e8964e3853ba388debbb5d3278e2c1f28aae8 Mon Sep 17 00:00:00 2001 From: Enno Ruijters Date: Tue, 5 Oct 2021 09:44:33 +0200 Subject: [PATCH 4/5] Fix unit test to work on Windows (and other backslash-using filesystems) --- .../org/carlspring/cloud/storage/s3fs/path/ResolveTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/org/carlspring/cloud/storage/s3fs/path/ResolveTest.java b/src/test/java/org/carlspring/cloud/storage/s3fs/path/ResolveTest.java index c5262b48..01a98524 100644 --- a/src/test/java/org/carlspring/cloud/storage/s3fs/path/ResolveTest.java +++ b/src/test/java/org/carlspring/cloud/storage/s3fs/path/ResolveTest.java @@ -89,7 +89,7 @@ void nonS3Paths() assertEquals("s3://s3.test.amazonaws.com/bucket/./rabbit%20in%20space", resolved.toString()); IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> - parent.resolve(Paths.get("/tmp"))); + parent.resolve(Paths.get("tempDirectory").toAbsolutePath())); assertEquals("other must be an instance of org.carlspring.cloud.storage.s3fs.S3Path or a relative Path", e.getMessage()); } } From dfc606f8b0572a55152fc9cc2bbb18e0b28a3140 Mon Sep 17 00:00:00 2001 From: Enno Ruijters Date: Tue, 5 Oct 2021 09:57:01 +0200 Subject: [PATCH 5/5] More testing for windows --- .../org/carlspring/cloud/storage/s3fs/path/ResolveTest.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/test/java/org/carlspring/cloud/storage/s3fs/path/ResolveTest.java b/src/test/java/org/carlspring/cloud/storage/s3fs/path/ResolveTest.java index 01a98524..d06dc587 100644 --- a/src/test/java/org/carlspring/cloud/storage/s3fs/path/ResolveTest.java +++ b/src/test/java/org/carlspring/cloud/storage/s3fs/path/ResolveTest.java @@ -72,6 +72,7 @@ void nonS3Paths() { S3Path parent = getPath("/bucket"); S3Path child = getPath("/bucket/rabbit"); + S3Path deepChild = getPath("/bucket/rabbit/in/space"); S3Path resolved = (S3Path) parent.resolve(child); assertEquals(child, resolved); @@ -82,6 +83,9 @@ void nonS3Paths() resolved = (S3Path) parent.resolve(Paths.get("rabbit")); //unixPath assertEquals(child, resolved); + resolved = (S3Path) parent.resolve(Paths.get("rabbit").resolve("in").resolve("space")); + assertEquals(deepChild, resolved); + resolved = (S3Path) parent.resolve(Paths.get("./rabbit")); //unixPath assertEquals("s3://s3.test.amazonaws.com/bucket/./rabbit", resolved.toString());