jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdue...@apache.org
Subject svn commit: r1342342 - in /jackrabbit/oak/trunk: oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/ oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/ oak-it/mk/src/main/java/org/apache/jackrabbit/mk/test/
Date Thu, 24 May 2012 17:00:57 GMT
Author: mduerig
Date: Thu May 24 17:00:57 2012
New Revision: 1342342

URL: http://svn.apache.org/viewvc?rev=1342342&view=rev
Log:
OAK-96: PathUtils should use assertions to enable validation instead of system property

Modified:
    jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/PathUtils.java
    jackrabbit/oak/trunk/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/PathTest.java
    jackrabbit/oak/trunk/oak-it/mk/src/main/java/org/apache/jackrabbit/mk/test/MicroKernelIT.java

Modified: jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/PathUtils.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/PathUtils.java?rev=1342342&r1=1342341&r2=1342342&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/PathUtils.java
(original)
+++ jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/PathUtils.java
Thu May 24 17:00:57 2012
@@ -29,15 +29,6 @@ import java.util.NoSuchElementException;
  * the the result of this method is undefined.
  */
 public class PathUtils {
-
-    /**
-     * Controls whether paths passed into methods of this class are validated or
-     * not. By default, paths are validated for each method call, which
-     * potentially slows down processing. To disable validation, set the system
-     * property org.apache.jackrabbit.mk.util.PathUtils.SKIP_VALIDATION.
-     */
-    private static final boolean SKIP_VALIDATION = Boolean.getBoolean(PathUtils.class.getName()
+ ".SKIP_VALIDATION");
-
     private static final String[] EMPTY_ARRAY = new String[0];
 
     private PathUtils() {
@@ -50,7 +41,7 @@ public class PathUtils {
      * @return whether this is the root
      */
     public static boolean denotesRoot(String path) {
-        assertValid(path);
+        assert isValid(path);
 
         return denotesRootPath(path);
     }
@@ -66,7 +57,7 @@ public class PathUtils {
      * @return true if it starts with a slash
      */
     public static boolean isAbsolute(String path) {
-        assertValid(path);
+        assert isValid(path);
 
         return isAbsolutePath(path);
     }
@@ -96,7 +87,7 @@ public class PathUtils {
      * @return the ancestor path
      */
     public static String getAncestorPath(String path, int nth) {
-        assertValid(path);
+        assert isValid(path);
 
         if (path.isEmpty() || denotesRootPath(path)
                 || nth <= 0) {
@@ -127,7 +118,7 @@ public class PathUtils {
      * @return the last element
      */
     public static String getName(String path) {
-        assertValid(path);
+        assert isValid(path);
 
         if (path.isEmpty() || denotesRootPath(path)) {
             return "";
@@ -148,7 +139,7 @@ public class PathUtils {
      * @return the number of elements
      */
     public static int getDepth(String path) {
-        assertValid(path);
+        assert isValid(path);
 
         int count = 1, i = 0;
         if (isAbsolutePath(path)) {
@@ -174,7 +165,7 @@ public class PathUtils {
      * @return the path elements
      */
     public static String[] split(String path) {
-        assertValid(path);
+        assert isValid(path);
 
         if (path.isEmpty()) {
             return EMPTY_ARRAY;
@@ -208,7 +199,7 @@ public class PathUtils {
      * @return an Iterable for the path elements
      */
     public static Iterable<String> elements(final String path) {
-        assertValid(path);
+        assert isValid(path);
 
         final Iterator<String> it = new Iterator<String>() {
             int pos = PathUtils.isAbsolute(path) ? 1 : 0;
@@ -272,14 +263,14 @@ public class PathUtils {
      * @return the concatenated path
      */
     public static String concat(String parentPath, String... relativePaths) {
-        assertValid(parentPath);
+        assert isValid(parentPath);
         int parentLen = parentPath.length();
         int size = relativePaths.length;
         StringBuilder buff = new StringBuilder(parentLen + size * 5);
         buff.append(parentPath);
         boolean needSlash = parentLen > 0 && !denotesRootPath(parentPath);
         for (String s : relativePaths) {
-            assertValid(s);
+            assert isValid(s);
             if (isAbsolutePath(s)) {
                 throw new IllegalArgumentException("Cannot append absolute path " + s);
             }
@@ -302,8 +293,8 @@ public class PathUtils {
      * @return the concatenated path
      */
     public static String concat(String parentPath, String subPath) {
-        assertValid(parentPath);
-        assertValid(subPath);
+        assert isValid(parentPath);
+        assert isValid(subPath);
         // special cases
         if (parentPath.isEmpty()) {
             return subPath;
@@ -328,8 +319,8 @@ public class PathUtils {
      * @return true if the path is an offspring of the ancestor
      */
     public static boolean isAncestor(String ancestor, String path) {
-        assertValid(ancestor);
-        assertValid(path);
+        assert isValid(ancestor);
+        assert isValid(path);
         if (ancestor.isEmpty() || path.isEmpty()) {
             return false;
         }
@@ -349,8 +340,8 @@ public class PathUtils {
      * @return relativized path
      */
     public static String relativize(String parentPath, String path) {
-        assertValid(parentPath);
-        assertValid(path);
+        assert isValid(parentPath);
+        assert isValid(path);
 
         if (parentPath.equals(path)) {
             return "";
@@ -375,7 +366,7 @@ public class PathUtils {
      *         if not found
      */
     public static int getNextSlash(String path, int index) {
-        assertValid(path);
+        assert isValid(path);
 
         return path.indexOf('/', index);
     }
@@ -407,12 +398,32 @@ public class PathUtils {
         }
     }
 
-    //------------------------------------------< private >---
-
-    private static void assertValid(String path) {
-        if (!SKIP_VALIDATION) {
-            validate(path);
+    /**
+     * Check if the path is valid. A valid path is absolute (starts with a '/')
+     * or relative (doesn't start with '/'), and contains none or more elements.
+     * A path may not end with '/', except for the root path. Elements itself must
+     * be at least one character long.
+     *
+     * @param path the path
+     * @return {@code true} iff the path is valid.
+     */
+    public static boolean isValid(String path) {
+        if (path.isEmpty() || denotesRootPath(path)) {
+            return true;
+        } else if (path.charAt(path.length() - 1) == '/') {
+            return false;
+        }
+        char last = 0;
+        for (int index = 0, len = path.length(); index < len; index++) {
+            char c = path.charAt(index);
+            if (c == '/') {
+                if (last == '/') {
+                    return false;
+                }
+            }
+            last = c;
         }
+        return true;
     }
 
 }

Modified: jackrabbit/oak/trunk/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/PathTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/PathTest.java?rev=1342342&r1=1342341&r2=1342342&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/PathTest.java
(original)
+++ jackrabbit/oak/trunk/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/PathTest.java
Thu May 24 17:00:57 2012
@@ -16,25 +16,39 @@
  */
 package org.apache.jackrabbit.oak.commons;
 
+import junit.framework.AssertionFailedError;
 import junit.framework.TestCase;
 
 import java.util.Iterator;
 
 public class PathTest extends TestCase {
+    static boolean assertsEnabled;
+
+    static {
+        assert assertsEnabled = true;
+    }
 
     public void test() {
 
         try {
             PathUtils.getParentPath("invalid/path/");
-            fail();
-        } catch (IllegalArgumentException e) {
+            if (assertsEnabled) {
+                fail();
+            }
+        } catch (AssertionFailedError e) {
+            throw e;
+        } catch (AssertionError e) {
             // expected
         }
 
         try {
             PathUtils.getName("invalid/path/");
-            fail();
-        } catch (IllegalArgumentException e) {
+            if (assertsEnabled) {
+                fail();
+            }
+        } catch (AssertionFailedError e) {
+            throw e;
+        } catch (AssertionError e) {
             // expected
         }
 
@@ -62,9 +76,19 @@ public class PathTest extends TestCase {
         assertEquals(parent + "/" + child, PathUtils.concat(parent, child));
         try {
             assertEquals(parent + "/" + child, PathUtils.concat(parent + "/", "/" + child));
-            fail();
+            if (assertsEnabled) {
+                fail();
+            }
+        } catch (AssertionFailedError e) {
+            throw e;
         } catch (IllegalArgumentException e) {
-            // expected
+            if (assertsEnabled) {
+                throw e;
+            }
+        } catch (AssertionError e) {
+            if (!assertsEnabled) {
+                throw e;
+            }
         }
         try {
             assertEquals(parent + "/" + child, PathUtils.concat(parent, "/" + child));
@@ -90,13 +114,21 @@ public class PathTest extends TestCase {
         }
         try {
             PathUtils.concat("", "//");
-            fail();
-        } catch (IllegalArgumentException e) {
+            if (assertsEnabled) {
+                fail();
+            }
+        } catch (AssertionFailedError e) {
+            throw e;
+        } catch (AssertionError e) {
             // expected
         }
         try {
             PathUtils.concat("/", "/");
-            fail();
+            if (assertsEnabled) {
+                fail();
+            }
+        } catch (AssertionFailedError e) {
+            throw e;
         } catch (IllegalArgumentException e) {
             // expected
         }
@@ -252,80 +284,144 @@ public class PathTest extends TestCase {
         String invalid = "/test/test//test/test";
         try {
             PathUtils.denotesRoot(invalid);
-            fail();
-        } catch (IllegalArgumentException e) {
+            if (assertsEnabled) {
+                fail();
+            }
+        } catch (AssertionFailedError e) {
+            throw e;
+        } catch (AssertionError e) {
             // expected
         }
         try {
             PathUtils.concat(invalid, "x");
-            fail();
-        } catch (IllegalArgumentException e) {
+            if (assertsEnabled) {
+                fail();
+            }
+        } catch (AssertionFailedError e) {
+            throw e;
+        } catch (AssertionError e) {
             // expected
         }
         try {
             PathUtils.concat("/x", invalid);
-            fail();
+            if (assertsEnabled) {
+                fail();
+            }
+        } catch (AssertionFailedError e) {
+            throw e;
         } catch (IllegalArgumentException e) {
-            // expected
+            if (assertsEnabled) {
+                throw e;
+            }
+        } catch (AssertionError e) {
+            if (!assertsEnabled) {
+                throw e;
+            }
         }
         try {
             PathUtils.concat("/x", "y", invalid);
-            fail();
+            if (assertsEnabled) {
+                fail();
+            }
+        } catch (AssertionFailedError e) {
+            throw e;
         } catch (IllegalArgumentException e) {
-            // expected
+            if (assertsEnabled) {
+                throw e;
+            }
+        } catch (AssertionError e) {
+            if (!assertsEnabled) {
+                throw e;
+            }
         }
         try {
             PathUtils.concat(invalid, "y", "z");
-            fail();
-        } catch (IllegalArgumentException e) {
+            if (assertsEnabled) {
+                fail();
+            }
+        } catch (AssertionFailedError e) {
+            throw e;
+        } catch (AssertionError e) {
             // expected
         }
         try {
             PathUtils.getDepth(invalid);
-            fail();
-        } catch (IllegalArgumentException e) {
+            if (assertsEnabled) {
+                fail();
+            }
+        } catch (AssertionFailedError e) {
+            throw e;
+        } catch (AssertionError e) {
             // expected
         }
         try {
             PathUtils.getName(invalid);
-            fail();
-        } catch (IllegalArgumentException e) {
+            if (assertsEnabled) {
+                fail();
+            }
+        } catch (AssertionFailedError e) {
+            throw e;
+        } catch (AssertionError e) {
             // expected
         }
         try {
             PathUtils.getNextSlash(invalid, 0);
-            fail();
-        } catch (IllegalArgumentException e) {
+            if (assertsEnabled) {
+                fail();
+            }
+        } catch (AssertionFailedError e) {
+            throw e;
+        } catch (AssertionError e) {
             // expected
         }
         try {
             PathUtils.getParentPath(invalid);
-            fail();
-        } catch (IllegalArgumentException e) {
+            if (assertsEnabled) {
+                fail();
+            }
+        } catch (AssertionFailedError e) {
+            throw e;
+        } catch (AssertionError e) {
             // expected
         }
         try {
             PathUtils.isAbsolute(invalid);
-            fail();
-        } catch (IllegalArgumentException e) {
+            if (assertsEnabled) {
+                fail();
+            }
+        } catch (AssertionFailedError e) {
+            throw e;
+        } catch (AssertionError e) {
             // expected
         }
         try {
             PathUtils.relativize(invalid, invalid);
-            fail();
-        } catch (IllegalArgumentException e) {
+            if (assertsEnabled) {
+                fail();
+            }
+        } catch (AssertionFailedError e) {
+            throw e;
+        } catch (AssertionError e) {
             // expected
         }
         try {
             PathUtils.relativize("/test", invalid);
-            fail();
-        } catch (IllegalArgumentException e) {
+            if (assertsEnabled) {
+                fail();
+            }
+        } catch (AssertionFailedError e) {
+            throw e;
+        } catch (AssertionError e) {
             // expected
         }
         try {
             PathUtils.split(invalid);
-            fail();
-        } catch (IllegalArgumentException e) {
+            if (assertsEnabled) {
+                fail();
+            }
+        } catch (AssertionFailedError e) {
+            throw e;
+        } catch (AssertionError e) {
             // expected
         }
     }
@@ -347,7 +443,7 @@ public class PathTest extends TestCase {
             try {
                 PathUtils.elements(path);
                 fail();
-            } catch (IllegalArgumentException e) {
+            } catch (AssertionError e) {
                 // expected
             }
         }

Modified: jackrabbit/oak/trunk/oak-it/mk/src/main/java/org/apache/jackrabbit/mk/test/MicroKernelIT.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-it/mk/src/main/java/org/apache/jackrabbit/mk/test/MicroKernelIT.java?rev=1342342&r1=1342341&r2=1342342&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-it/mk/src/main/java/org/apache/jackrabbit/mk/test/MicroKernelIT.java
(original)
+++ jackrabbit/oak/trunk/oak-it/mk/src/main/java/org/apache/jackrabbit/mk/test/MicroKernelIT.java
Thu May 24 17:00:57 2012
@@ -21,7 +21,6 @@ import org.apache.jackrabbit.mk.test.uti
 import org.apache.jackrabbit.mk.util.MicroKernelInputStream;
 import org.json.simple.JSONArray;
 import org.json.simple.JSONObject;
-import org.junit.Ignore;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.junit.runners.Parameterized;
@@ -604,7 +603,7 @@ public class MicroKernelIT extends Abstr
             String path = "/test/";
             mk.getNodes(path, head);
             fail("Success with invalid path: " + path);
-        } catch (IllegalArgumentException e) {
+        } catch (AssertionError e) {
             // expected
         } catch (MicroKernelException e) {
             // expected



Mime
View raw message