jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdue...@apache.org
Subject svn commit: r1143360 - in /jackrabbit/sandbox/microkernel/src: main/java/org/apache/jackrabbit/mk/util/PathUtils.java test/java/org/apache/jackrabbit/mk/PathTest.java
Date Wed, 06 Jul 2011 11:59:55 GMT
Author: mduerig
Date: Wed Jul  6 11:59:55 2011
New Revision: 1143360

URL: http://svn.apache.org/viewvc?rev=1143360&view=rev
Log:
- fixed relativize
- added assertions for valid paths

Modified:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/PathUtils.java
    jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/PathTest.java

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/PathUtils.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/PathUtils.java?rev=1143360&r1=1143359&r2=1143360&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/PathUtils.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/PathUtils.java
Wed Jul  6 11:59:55 2011
@@ -34,6 +34,8 @@ public class PathUtils {
      * @return whether this is the root
      */
     public static boolean denotesRoot(String path) {
+        assertValid(path);
+
         return "/".equals(path);
     }
 
@@ -44,6 +46,8 @@ public class PathUtils {
      * @return true if it starts with a slash
      */
     public static boolean isAbsolute(String path) {
+        assertValid(path);
+
         return path.startsWith("/");
     }
 
@@ -57,6 +61,8 @@ public class PathUtils {
      * @return the parent path
      */
     public static String getParentPath(String path) {
+        assertValid(path);
+
         if (path.equals("/") || path.length() == 0) {
             return path;
         }
@@ -81,12 +87,11 @@ public class PathUtils {
      * @return the last element
      */
     public static String getName(String path) {
+        assertValid(path);
+
         if (path.equals("/") || path.length() == 0) {
             return "";
         }
-        if (path.endsWith("/")) {
-            throw new IllegalArgumentException(path + " (ends with '/')");
-        }
         int end = path.length() - 1;
         int pos = getPreviousSlash(path, end);
         if (pos != -1) {
@@ -103,6 +108,8 @@ public class PathUtils {
      * @return the number of elements
      */
     public static int getDepth(String path) {
+        assertValid(path);
+
         int count = 1, i = 0;
         if (path.startsWith("/")) {
             if (PathUtils.denotesRoot(path)) {
@@ -120,6 +127,8 @@ public class PathUtils {
     }
 
     public static String[] split(String path) {
+        assertValid(path);
+
         ArrayList<String> list = new ArrayList<String>();
         if (path.startsWith("/")) {
             if (path.length() == 1) {
@@ -149,6 +158,9 @@ public class PathUtils {
     }
 
     public static String concat(String parentPath, String relativePath) {
+        assertValid(parentPath);
+        assertValid(relativePath);
+
         if (relativePath.startsWith("/")) {
             throw new IllegalArgumentException("can't append absolute path");
         } else if (relativePath.length() == 0) {
@@ -175,6 +187,13 @@ public class PathUtils {
      * @return  relativized path
      */
     public static String relativize(String parentPath, String path) {
+        assertValid(parentPath);
+        assertValid(path);
+
+        if (parentPath.equals(path)) {
+            return "";
+        }
+        
         String prefix = denotesRoot(parentPath)
             ? parentPath
             : parentPath + '/';
@@ -195,6 +214,8 @@ public class PathUtils {
      *         -1 if not found
      */
     public static int getNextSlash(String path, int index) {
+        assertValid(path);
+
         for (int len = path.length(); index < len; index++) {
             char c = path.charAt(index);
             if (c == '/') {
@@ -222,6 +243,8 @@ public class PathUtils {
      *         -1 if not found
      */
     public static int getPreviousSlash(String path, int index) {
+        assertValid(path);
+
         for (; index >= 0; index--) {
             char c = path.charAt(index);
             if (c == '/') {
@@ -284,4 +307,14 @@ public class PathUtils {
         }
     }
 
+    //------------------------------------------< private >---
+
+    public static void assertValid(String path) {
+        try {
+            validate(path);
+        } catch (IllegalArgumentException e) {
+            assert false;
+        }
+    }
+
 }

Modified: jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/PathTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/PathTest.java?rev=1143360&r1=1143359&r2=1143360&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/PathTest.java (original)
+++ jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/PathTest.java Wed
Jul  6 11:59:55 2011
@@ -26,28 +26,28 @@ public class PathTest extends TestCase {
         try {
             PathUtils.getNextSlash("/{error", 1);
             fail();
-        } catch (IllegalArgumentException e) {
+        } catch (AssertionError e) {
             // expected
         }
 
         try {
             PathUtils.getPreviousSlash("error}", "error}".length() - 1);
             fail();
-        } catch (IllegalArgumentException e) {
+        } catch (AssertionError e) {
             // expected
         }
 
         try {
             PathUtils.getParentPath("invalid/path/");
             fail();
-        } catch (IllegalArgumentException e) {
+        } catch (AssertionError e) {
             // expected
         }
 
         try {
             PathUtils.getName("invalid/path/");
             fail();
-        } catch (IllegalArgumentException e) {
+        } catch (AssertionError e) {
             // expected
         }
 
@@ -133,11 +133,10 @@ public class PathTest extends TestCase {
 
         // concat
         assertEquals(parent + "/" + child, PathUtils.concat(parent , child ));
-        assertEquals(parent + "/" + child, PathUtils.concat(parent + "/", child ));
         try {
             assertEquals(parent + "/" + child, PathUtils.concat(parent + "/", "/" + child));
             fail();
-        } catch (IllegalArgumentException e) {
+        } catch (AssertionError e) {
             // expected
         }
         try {
@@ -178,7 +177,7 @@ public class PathTest extends TestCase {
 
         // relativize
         assertEquals("", PathUtils.relativize("/", "/"));
-        assertEquals("", PathUtils.relativize("/" + parent, "/" + parent + "/"));
+        assertEquals("", PathUtils.relativize("/" + parent, "/" + parent));
         assertEquals(child , PathUtils.relativize("/" + parent, "/" + parent + "/" + child));
         assertEquals(parent + "/" + child, PathUtils.relativize("/", "/" + parent + "/" +
child));
         try {



Mime
View raw message