Fix issue with self relativize
authorMathieu Baudier <mbaudier@argeo.org>
Sat, 21 Jan 2017 14:53:27 +0000 (15:53 +0100)
committerMathieu Baudier <mbaudier@argeo.org>
Sat, 21 Jan 2017 14:53:27 +0000 (15:53 +0100)
org.argeo.jcr/ext/test/org/argeo/jcr/fs/JcrFileSystemTest.java
org.argeo.jcr/src/org/argeo/jcr/fs/JcrPath.java

index 1cf7ef70e9573e0a4c99e91c2d59175eaea50026..2ad75a2aa2fceee280106182428e1427f988a5ca 100644 (file)
@@ -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
index 6a0c7adfe2d0ebbab10dd1ccf0b648df83ab464c..1f79434845eb84c54ce2aa9ab08bbdafb5d76113 100644 (file)
@@ -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++) {