You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by th...@apache.org on 2011/08/19 07:29:42 UTC
svn commit: r1159504 - in /jackrabbit/sandbox/microkernel: pom.xml
src/main/java/org/apache/jackrabbit/mk/util/PathUtils.java
src/test/java/org/apache/jackrabbit/mk/PathTest.java
src/test/java/org/apache/jackrabbit/mk/util/PathTest.java
Author: thomasm
Date: Fri Aug 19 05:29:42 2011
New Revision: 1159504
URL: http://svn.apache.org/viewvc?rev=1159504&view=rev
Log:
Improve PathUtils using PIT mutation testing
Added:
jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/util/PathTest.java
- copied, changed from r1158695, jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/PathTest.java
Removed:
jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/PathTest.java
Modified:
jackrabbit/sandbox/microkernel/pom.xml
jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/PathUtils.java
Modified: jackrabbit/sandbox/microkernel/pom.xml
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/pom.xml?rev=1159504&r1=1159503&r2=1159504&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/pom.xml (original)
+++ jackrabbit/sandbox/microkernel/pom.xml Fri Aug 19 05:29:42 2011
@@ -52,16 +52,33 @@
</dependency>
</dependencies>
- <build>
- <plugins>
- <plugin>
- <artifactId>maven-compiler-plugin</artifactId>
- <configuration>
- <source>1.5</source>
- <target>1.5</target>
- </configuration>
- </plugin>
- </plugins>
- </build>
+ <build>
+ <plugins>
+ <plugin>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <configuration>
+ <source>1.5</source>
+ <target>1.5</target>
+ </configuration>
+ </plugin>
+ <!--
+ run mutation tests using:
+ mvn org.pitest:pitest-maven:mutationCoverage
+ -->
+ <plugin>
+ <groupId>org.pitest</groupId>
+ <artifactId>pitest-maven</artifactId>
+ <version>0.20</version>
+ <configuration>
+ <inScopeClasses>
+ <param>org.apache.jackrabbit.mk.util.Path*</param>
+ </inScopeClasses>
+ <targetClasses>
+ <param>org.apache.jackrabbit.mk.util.Path*</param>
+ </targetClasses>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
</project>
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=1159504&r1=1159503&r2=1159504&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 Fri Aug 19 05:29:42 2011
@@ -20,6 +20,11 @@ import java.util.ArrayList;
/**
* Utility methods to parse a JCR path.
+ * <p>
+ * Each method validates the input, except if the system property
+ * {packageName}.SKIP_VALIDATION is set, in which case only minimal validation
+ * takes place within this function, so when the parameter is an illegal path,
+ * the the result of this method is undefined.
*/
public class PathUtils {
@@ -33,10 +38,6 @@ public class PathUtils {
private static final String[] EMPTY_ARRAY = new String[0];
- private PathUtils() {
- // utility class
- }
-
/**
* Whether the path is the root path ("/").
*
@@ -71,9 +72,7 @@ public class PathUtils {
/**
* Get the parent of a path. The parent of the root path ("/") is the root
- * path. Only minimal validation takes place within this function, so when
- * the parameter is an illegal path, the the result of this method is
- * undefined.
+ * path.
*
* @param path the path
* @return the parent path
@@ -85,13 +84,13 @@ public class PathUtils {
return path;
}
int end = path.length() - 1;
- int pos = getPreviousSlash(path, end);
+ int pos = path.lastIndexOf('/', end);
if (pos > 0) {
return path.substring(0, pos);
- } else if (pos < 0 && !isAbsolutePath(path)) {
- return "";
+ } else if (pos == 0) {
+ return "/";
}
- return path.length() > 0 ? "/" : path;
+ return "";
}
/**
@@ -108,7 +107,7 @@ public class PathUtils {
return "";
}
int end = path.length() - 1;
- int pos = getPreviousSlash(path, end);
+ int pos = path.lastIndexOf('/', end);
if (pos != -1) {
return path.substring(pos + 1, end + 1);
}
@@ -133,7 +132,7 @@ public class PathUtils {
i++;
}
while (true) {
- i = getNextSlash(path, i) + 1;
+ i = path.indexOf('/', i) + 1;
if (i == 0) {
return count;
}
@@ -161,19 +160,13 @@ public class PathUtils {
}
ArrayList<String> list = new ArrayList<String>();
while(true) {
- int index = getNextSlash(path, 0);
+ int index = path.indexOf('/');
if (index < 0) {
- if (path.length() > 0) {
- list.add(path);
- }
+ list.add(path);
break;
}
- if (index >= 0) {
- String s = path.substring(0, index);
- if (s.length() > 0) {
- list.add(s);
- }
- }
+ String s = path.substring(0, index);
+ list.add(s);
path = path.substring(index + 1);
}
String[] array = new String[list.size()];
@@ -191,21 +184,16 @@ public class PathUtils {
public static String concat(String parentPath, String... relativePaths) {
assertValid(parentPath);
int parentLen = parentPath.length();
- int len = parentLen;
- for (String s : relativePaths) {
- if (isAbsolutePath(s)) {
- throw new IllegalArgumentException("Cannot append absolute path " + s);
- }
- int l = s.length();
- if (l > 0) {
- len += 1 + s.length();
- }
- }
- StringBuilder buff = new StringBuilder(len);
+ int size = relativePaths.length;
+ StringBuilder buff = new StringBuilder(parentLen + size * 5);
buff.append(parentPath);
boolean needSlash = parentLen > 0 && !denotesRootPath(parentPath);
- for (int i=0, size = relativePaths.length; i < size; i++) {
+ for (int i=0; i < size; i++) {
String s = relativePaths[i];
+ assertValid(s);
+ if (isAbsolutePath(s)) {
+ throw new IllegalArgumentException("Cannot append absolute path " + s);
+ }
if (s.length() > 0) {
if (needSlash) {
buff.append('/');
@@ -217,32 +205,31 @@ public class PathUtils {
return buff.toString();
}
-// /**
-// * Concatenate path elements.
-// *
-// * @param parentPath the parent path
-// * @param relativePath the relative path to add
-// * @return the concatenated path
-// */
-// public static String concat(String parentPath, String relativePath) {
-// assertValid(parentPath);
-// assertValid(relativePath);
-//
-//// if (isAbsolutePath(relativePath)) {
-//// throw new IllegalArgumentException("Cannot append absolute path " + relativePath);
-//// } else if (relativePath.length() == 0) {
-//// return parentPath;
-//// }
-//// int parentLen = parentPath.length();
-//// StringBuilder buff = new StringBuilder(
-//// parentLen + 1 + relativePath.length());
-//// buff.append(parentPath);
-//// if (parentLen > 0 && !denotesRootPath(parentPath)) {
-//// buff.append('/');
-//// }
-// buff.append(relativePath);
-// return buff.toString();
-// }
+ /**
+ * Concatenate path elements.
+ *
+ * @param parentPath the parent path
+ * @param relativePath the relative path to add
+ * @return the concatenated path
+ */
+ public static String concat(String parentPath, String relativePath) {
+ assertValid(parentPath);
+ assertValid(relativePath);
+ if (isAbsolutePath(relativePath)) {
+ throw new IllegalArgumentException("Cannot append absolute path " + relativePath);
+ } else if (relativePath.length() == 0) {
+ return parentPath;
+ }
+ int parentLen = parentPath.length();
+ StringBuilder buff = new StringBuilder(
+ parentLen + 1 + relativePath.length());
+ buff.append(parentPath);
+ if (parentLen > 0 && !denotesRootPath(parentPath)) {
+ buff.append('/');
+ }
+ buff.append(relativePath);
+ return buff.toString();
+ }
/**
* Relativize a path wrt. a parent path such that
@@ -272,8 +259,7 @@ public class PathUtils {
}
/**
- * Get the index of the next slash. This is similar to
- * String.indexOf, except that escaping with { and } is supported.
+ * Get the index of the next slash.
*
* @param path the path
* @param index the starting index
@@ -287,21 +273,6 @@ public class PathUtils {
}
/**
- * Get the index of the previous slash. This is similar to
- * String.lastIndexOf, except that escaping with { and } is supported.
- *
- * @param path the path
- * @param index the starting index
- * @return the index of the previous slash (possibly the starting index), or
- * -1 if not found
- */
- public static int getPreviousSlash(String path, int index) {
- assertValid(path);
-
- return path.lastIndexOf('/', index);
- }
-
- /**
* Check if the path is valid, and throw an IllegalArgumentException if not.
* 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
@@ -330,7 +301,7 @@ public class PathUtils {
//------------------------------------------< private >---
- public static void assertValid(String path) {
+ private static void assertValid(String path) {
if (!SKIP_VALIDATION) {
validate(path);
}
Copied: jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/util/PathTest.java (from r1158695, 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/util/PathTest.java?p2=jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/util/PathTest.java&p1=jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/PathTest.java&r1=1158695&r2=1159504&rev=1159504&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/util/PathTest.java Fri Aug 19 05:29:42 2011
@@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.jackrabbit.mk;
+package org.apache.jackrabbit.mk.util;
import org.apache.jackrabbit.mk.util.PathUtils;
import junit.framework.TestCase;
@@ -41,72 +41,6 @@ public class PathTest extends TestCase {
test("x", "y");
}
- public void testValidate() {
- for (String invalid : new String[] {
- "//",
- "//test",
- "/test/",
- "test/",
- "/test//",
- "/test//test",
- "//x",
- "/x/",
- "x/",
- "/x//",
- "/x//x",
- }) {
- try {
- PathUtils.validate(invalid);
- fail(invalid);
- } catch (IllegalArgumentException e) {
- // expected
- }
- }
- for (String valid : new String[] {
- "",
- "/",
- "test",
- "test/test",
- "/test",
- "/test/test",
- "x",
- "x/x",
- "/x",
- "/x/x",
- }) {
- PathUtils.validate(valid);
- }
- }
-
- public void testMore() {
- String[] paths = {
- "",
- "/",
- "foo",
- "/foo",
- "foo/bar",
- "/foo/bar",
- "foo/bar/baz",
- "/foo/bar/baz",
- "x",
- "/x",
- "x/y",
- "/x/y",
- "x/y/z",
- "/x/y/z",
- };
-
- for (String path : paths) {
- String parent = PathUtils.getParentPath(path);
- String name = PathUtils.getName(path);
- String concat = PathUtils.concat(parent, name);
-
- assertEquals("original: " + path + " parent: " + parent +
- " name: " + name + " concat: " + concat,
- path, concat);
- }
- }
-
private void test(String parent, String child) {
// split
@@ -116,6 +50,12 @@ public class PathTest extends TestCase {
assertEquals(2, PathUtils.split(parent + "/" + child).length);
assertEquals(1, PathUtils.split("/" + parent).length);
assertEquals(2, PathUtils.split("/" + parent + "/" + child).length);
+ assertEquals(3, PathUtils.split("/" + parent + "/" + child + "/" + child).length);
+ assertEquals(parent, PathUtils.split(parent)[0]);
+ assertEquals(parent, PathUtils.split(parent + "/" + child)[0]);
+ assertEquals(child, PathUtils.split(parent + "/" + child)[1]);
+ assertEquals(child, PathUtils.split(parent + "/" + child + "/" + child + "1")[1]);
+ assertEquals(child + "1", PathUtils.split(parent + "/" + child + "/" + child + "1")[2]);
// concat
assertEquals(parent + "/" + child, PathUtils.concat(parent, child));
@@ -132,6 +72,11 @@ public class PathTest extends TestCase {
// expected
}
assertEquals(parent + "/" + child + "/" + child, PathUtils.concat(parent, child, child));
+ assertEquals(parent + "/" + child, PathUtils.concat(parent, "", child));
+ assertEquals(parent + "/" + child, PathUtils.concat(parent, child, ""));
+ assertEquals(child + "/" + child, PathUtils.concat("", child, child));
+ assertEquals(child, PathUtils.concat("", child, ""));
+ assertEquals(child, PathUtils.concat("", "", child));
// denotesRoot
assertTrue(PathUtils.denotesRoot("/"));
@@ -174,7 +119,171 @@ public class PathTest extends TestCase {
// expected
}
+ }
+
+ public void testMore() {
+ String[] paths = {
+ "",
+ "/",
+ "foo",
+ "/foo",
+ "foo/bar",
+ "/foo/bar",
+ "foo/bar/baz",
+ "/foo/bar/baz",
+ "x",
+ "/x",
+ "x/y",
+ "/x/y",
+ "x/y/z",
+ "/x/y/z",
+ };
+
+ for (String path : paths) {
+ String parent = PathUtils.getParentPath(path);
+ String name = PathUtils.getName(path);
+ String concat = PathUtils.concat(parent, name);
+
+ assertEquals("original: " + path + " parent: " + parent +
+ " name: " + name + " concat: " + concat,
+ path, concat);
+ }
+ }
+
+ public void testNextSlash() {
+ String path = "/test/a";
+ int n = PathUtils.getNextSlash(path, 0);
+ assertEquals(0, n);
+ n = PathUtils.getNextSlash(path, n + 1);
+ assertEquals(5, n);
+ n = PathUtils.getNextSlash(path, n + 1);
+ assertEquals(-1, n);
+ }
+
+ public void testValidate() {
+ for (String invalid : new String[] {
+ "//",
+ "//test",
+ "/test/",
+ "test/",
+ "/test//",
+ "/test//test",
+ "//x",
+ "/x/",
+ "x/",
+ "/x//",
+ "/x//x",
+ }) {
+ try {
+ PathUtils.validate(invalid);
+ fail(invalid);
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ }
+ for (String valid : new String[] {
+ "",
+ "/",
+ "test",
+ "test/test",
+ "/test",
+ "/test/test",
+ "x",
+ "x/x",
+ "/x",
+ "/x/x",
+ }) {
+ PathUtils.validate(valid);
+ }
+ try {
+ PathUtils.concat("", "/test", "");
+ fail();
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+
+ }
+ public void testValidateEverything() {
+ String invalid = "/test/test//test/test";
+ try {
+ PathUtils.denotesRoot(invalid);
+ fail();
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ try {
+ PathUtils.concat(invalid, "x");
+ fail();
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ try {
+ PathUtils.concat("/x", invalid);
+ fail();
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ try {
+ PathUtils.concat("/x", "y", invalid);
+ fail();
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ try {
+ PathUtils.concat(invalid, "y", "z");
+ fail();
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ try {
+ PathUtils.getDepth(invalid);
+ fail();
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ try {
+ PathUtils.getName(invalid);
+ fail();
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ try {
+ PathUtils.getNextSlash(invalid, 0);
+ fail();
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ try {
+ PathUtils.getParentPath(invalid);
+ fail();
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ try {
+ PathUtils.isAbsolute(invalid);
+ fail();
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ try {
+ PathUtils.relativize(invalid, invalid);
+ fail();
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ try {
+ PathUtils.relativize("/test", invalid);
+ fail();
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ try {
+ PathUtils.split(invalid);
+ fail();
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
}
}