From: Mathieu Baudier Date: Sat, 21 Jan 2017 14:53:27 +0000 (+0100) Subject: Fix issue with self relativize X-Git-Tag: argeo-commons-2.1.58~11 X-Git-Url: https://git.argeo.org/?a=commitdiff_plain;h=846e51f6df6b29222d5d83372705cb694db22ad7;hp=7cf338533865594652b72a81bdba5472745ce034;p=lgpl%2Fargeo-commons.git Fix issue with self relativize --- diff --git a/org.argeo.jcr/ext/test/org/argeo/jcr/fs/JcrFileSystemTest.java b/org.argeo.jcr/ext/test/org/argeo/jcr/fs/JcrFileSystemTest.java index 1cf7ef70e..2ad75a2aa 100644 --- a/org.argeo.jcr/ext/test/org/argeo/jcr/fs/JcrFileSystemTest.java +++ b/org.argeo.jcr/ext/test/org/argeo/jcr/fs/JcrFileSystemTest.java @@ -29,7 +29,18 @@ public class JcrFileSystemTest extends TestCase { FileSystemProvider fsProvider = new JackrabbitMemoryFsProvider(); // Simple file + Path rootPath = fsProvider.getPath(new URI("jcr+memory:/")); + log.debug("Got root " + rootPath); Path testPath = fsProvider.getPath(new URI("jcr+memory:/test.txt")); + log.debug("Test path"); + assertEquals("test.txt", testPath.getFileName().toString()); + assertEquals(rootPath, testPath.getParent()); + assertEquals(testPath.getFileName(), rootPath.relativize(testPath)); + // relativize self should be empty path + Path selfRelative = testPath.relativize(testPath); + assertEquals("", selfRelative.toString()); + + log.debug("Create file " + testPath); Files.createFile(testPath); BasicFileAttributes bfa = Files.readAttributes(testPath, BasicFileAttributes.class); FileTime ft = bfa.creationTime(); @@ -46,8 +57,6 @@ public class JcrFileSystemTest extends TestCase { assertTrue(Arrays.equals(arr, read)); assertEquals(txt, new String(read)); log.debug("Read " + testPath); - Path rootPath = fsProvider.getPath(new URI("jcr+memory:/")); - log.debug("Got root " + rootPath); Path testDir = rootPath.resolve("testDir"); log.debug("Resolved " + testDir); // Copy diff --git a/org.argeo.jcr/src/org/argeo/jcr/fs/JcrPath.java b/org.argeo.jcr/src/org/argeo/jcr/fs/JcrPath.java index 6a0c7adfe..1f7943484 100644 --- a/org.argeo.jcr/src/org/argeo/jcr/fs/JcrPath.java +++ b/org.argeo.jcr/src/org/argeo/jcr/fs/JcrPath.java @@ -41,6 +41,11 @@ public class JcrPath implements Path { this.absolute = true; this.hashCode = 0; return; + } else if (path.equals("")) {// empty path + this.path = new String[] { "" }; + this.absolute = false; + this.hashCode = "".hashCode(); + return; } this.absolute = path.charAt(0) == delimChar ? true : false; String trimmedPath = path.substring(absolute ? 1 : 0, @@ -234,7 +239,7 @@ public class JcrPath implements Path { String p2 = other.toString(); return new JcrPath(fs, p2.substring(p1.length(), p2.length())); } - throw new UnsupportedOperationException(); + throw new IllegalArgumentException(other + " cannot be realtivized against " + this); } @Override @@ -296,6 +301,17 @@ public class JcrPath implements Path { if (!(obj instanceof JcrPath)) return false; JcrPath other = (JcrPath) obj; + + if (path == null) {// root + if (other.path == null)// root + return true; + else + return false; + } else { + if (other.path == null)// root + return false; + } + // non root if (path.length != other.path.length) return false; for (int i = 0; i < path.length; i++) {