You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by tr...@apache.org on 2005/01/21 12:51:31 UTC

svn commit: r125921 - incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/core

Author: tripod
Date: Fri Jan 21 03:51:29 2005
New Revision: 125921

URL: http://svn.apache.org/viewcvs?view=rev&rev=125921
Log:
- added tests for Path and QName
Added:
   incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/core/
   incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/core/PathTest.java   (contents, props changed)
   incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/core/QNameTest.java   (contents, props changed)
   incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/core/TestAll.java   (contents, props changed)

Added: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/core/PathTest.java
Url: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/core/PathTest.java?view=auto&rev=125921
==============================================================================
--- (empty file)
+++ incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/core/PathTest.java	Fri Jan 21 03:51:29 2005
@@ -0,0 +1,303 @@
+/*
+ * Copyright 2004-2005 The Apache Software Foundation or its licensors,
+ *                     as applicable.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.core;
+
+import junit.framework.TestCase;
+
+import java.util.ArrayList;
+
+import org.apache.jackrabbit.core.util.Text;
+
+/**
+ * This Class implements a test case for the 'Path' class.
+ *
+ * Actually, this should be below the {@link org.apache.jackrabbit.test} package,
+ * but it needs package protected methods of that class.
+ */
+public class PathTest extends TestCase {
+
+    private final NamespaceResolver resolver;
+
+    private Test[] tests;
+
+    private static final int ABS = 1;
+    private static final int NOR = 2;
+    private static final int VAL = 4;
+
+    private static int NUM_TESTS = 1;
+
+    public PathTest() {
+
+        // create dummy namespace resolver
+        resolver = new NamespaceResolver(){
+            public String getURI(String prefix) {
+                return prefix;
+            }
+
+            public String getPrefix(String uri) {
+                return uri;
+            }
+        };
+
+        // create tests
+        ArrayList list = new ArrayList();
+        // absolute paths
+        list.add(new Test("/", NOR|VAL));
+        list.add(new Test("/", NOR|VAL));
+        list.add(new Test("/", NOR|VAL));
+        list.add(new Test("/a/b/c", NOR|VAL));
+        list.add(new Test("/prefix:name/prefix:name", NOR|VAL));
+        list.add(new Test("/name[2]/name[2]", NOR|VAL));
+        list.add(new Test("/prefix:name[2]/prefix:name[2]", NOR|VAL));
+
+        // relative paths
+        list.add(new Test("a/b/c", NOR|VAL));
+        list.add(new Test("prefix:name/prefix:name", NOR|VAL));
+        list.add(new Test("name[2]/name[2]", NOR|VAL));
+        list.add(new Test("prefix:name[2]/prefix:name[2]", NOR|VAL));
+
+        // invalid paths
+        list.add(new Test(""));
+        list.add(new Test("/a/b/c/"));
+        list.add(new Test(" /a/b/c/"));
+        list.add(new Test("/a/b/c/ "));
+        list.add(new Test("/:name/prefix:name"));
+        list.add(new Test("/prefix:name "));
+        list.add(new Test("/prefix: name"));
+        list.add(new Test("/ prefix:name"));
+        list.add(new Test("/prefix : name"));
+        list.add(new Test("/name[0]/name[2]"));
+        list.add(new Test("/prefix:name[2]foo/prefix:name[2]"));
+        list.add(new Test("a/b/c/"));
+        list.add(new Test(":name/prefix:name"));
+        list.add(new Test("name[0]/name[2]"));
+        list.add(new Test("prefix:name[2]foo/prefix:name[2]"));
+
+        // not normalized paths
+        list.add(new Test("/a/../b", "/b", VAL));
+        list.add(new Test("/a/./b", "/a/b", VAL));
+        list.add(new Test("/a/b/../..", "/", VAL));
+        list.add(new Test("/a/b/c/../d/..././f", "/a/b/d/.../f", VAL));
+        list.add(new Test("../a/b/../../../../f", "../../../f", VAL));
+        list.add(new Test("a/../..", "..", VAL));
+        list.add(new Test("../../a/.", "../../a", VAL));
+
+        // invalid normalized paths
+        list.add(new Test("/..", "/..", 0));
+        list.add(new Test("/a/b/../../..", "/a/b/../../..", 0));
+
+        tests = (Test[]) list.toArray(new Test[list.size()]);
+    }
+
+    public void testCreate() throws Exception {
+        for (int i=0; i<tests.length; i++) {
+            Test t = tests[i];
+            long t1 = System.currentTimeMillis();
+            for (int j=0; j<NUM_TESTS; j++) {
+                try {
+                    if (t.normalizedPath==null) {
+                        // check just creation
+                        Path p = Path.create(t.path, resolver, false);
+                        if (!t.isValid()) {
+                            fail("Should throw MalformedPathException: " + t.path);
+                        }
+                        assertEquals("\"" + t.path + "\".create(false)", t.path,  p.toJCRPath(resolver));
+                        assertEquals("\"" + t.path + "\".isNormalized()", t.isNormalized(), p.isNormalized());
+                        assertEquals("\"" + t.path + "\".isAbsolute()", t.isAbsolute(), p.isAbsolute());
+                    } else {
+                        // check with normalization
+                        Path p = Path.create(t.path, resolver, true);
+                        if (!t.isValid()) {
+                            fail("Should throw MalformedPathException: " + t.path);
+                        }
+                        assertEquals("\"" + t.path + "\".create(true)", t.normalizedPath, p.toJCRPath(resolver));
+                        assertEquals("\"" + t.path + "\".isAbsolute()", t.isAbsolute(), p.isAbsolute());
+                    }
+                } catch (MalformedPathException e) {
+                    if (t.isValid()) {
+                        throw e;
+                    }
+                }
+            }
+            long t2 = System.currentTimeMillis();
+            if (NUM_TESTS>1) {
+                System.out.println("testCreate():\t" + t + "\t" + (t2-t1) + "\tms");
+            }
+        }
+    }
+
+    public void testCheckFormat() throws Exception {
+        for (int i=0; i<tests.length; i++) {
+            Test t = tests[i];
+            long t1 = System.currentTimeMillis();
+            for (int j=0; j<NUM_TESTS; j++) {
+                if (t.normalizedPath==null) {
+                    // check just creation
+                    boolean isValid = true;
+                    try {
+                        Path.checkFormat(t.path);
+                    } catch (MalformedPathException e) {
+                        isValid = false;
+                    }
+                    assertEquals("\"" + t.path + "\".checkFormat()", t.isValid(),  isValid);
+                }
+            }
+            long t2 = System.currentTimeMillis();
+            if (NUM_TESTS>1) {
+                System.out.println("testCheckFormat():\t" + t + "\t" + (t2-t1) + "\tms");
+            }
+        }
+    }
+
+    public void testBuilder() throws Exception {
+        for (int i=0; i<tests.length; i++) {
+            Test t = tests[i];
+            if (t.isValid()) {
+                if (t.normalizedPath==null) {
+                    // check just creation
+                    Path p = build(t.path, resolver, false);
+                    assertEquals("\"" + t.path + "\".create(false)", t.path,  p.toJCRPath(resolver));
+                    assertEquals("\"" + t.path + "\".isNormalized()", t.isNormalized(), p.isNormalized());
+                    assertEquals("\"" + t.path + "\".isAbsolute()", t.isAbsolute(), p.isAbsolute());
+                } else {
+                    // check with normalization
+                    Path p = build(t.path, resolver, true);
+                    assertEquals("\"" + t.path + "\".create(true)", t.normalizedPath, p.toJCRPath(resolver));
+                    assertEquals("\"" + t.path + "\".isAbsolute()", t.isAbsolute(), p.isAbsolute());
+                }
+            }
+        }
+    }
+
+    public void testBuilderReverse() throws Exception {
+        for (int i=0; i<tests.length; i++) {
+            Test t = tests[i];
+            if (t.isValid()) {
+                if (t.normalizedPath==null) {
+                    // check just creation
+                    Path p = buildReverse(t.path, resolver, false);
+                    assertEquals("\"" + t.path + "\".create(false)", t.path,  p.toJCRPath(resolver));
+                    assertEquals("\"" + t.path + "\".isNormalized()", t.isNormalized(), p.isNormalized());
+                    assertEquals("\"" + t.path + "\".isAbsolute()", t.isAbsolute(), p.isAbsolute());
+                } else {
+                    // check with normalization
+                    Path p = buildReverse(t.path, resolver, true);
+                    assertEquals("\"" + t.path + "\".create(true)", t.normalizedPath, p.toJCRPath(resolver));
+                    assertEquals("\"" + t.path + "\".isAbsolute()", t.isAbsolute(), p.isAbsolute());
+                }
+            }
+        }
+    }
+
+    private Path build(String path, NamespaceResolver resolver, boolean normalize)
+            throws Exception {
+        Path.PathBuilder builder = new Path.PathBuilder();
+        String[] elems = Text.explode(path, '/', false);
+        if (path.startsWith("/")) {
+            builder.addRoot();
+        }
+        for (int i=0; i<elems.length; i++) {
+            int pos = elems[i].indexOf('[');
+            if (pos<0) {
+                builder.addLast(QName.fromJCRName(elems[i], resolver));
+            } else {
+                int index = Integer.parseInt(elems[i].substring(pos+1, elems[i].length()-1));
+                builder.addLast(QName.fromJCRName(elems[i].substring(0, pos), resolver), index);
+            }
+        }
+        return normalize ? builder.getPath().getNormalizedPath() : builder.getPath();
+    }
+
+    private Path buildReverse(String path, NamespaceResolver resolver, boolean normalize)
+            throws Exception {
+        Path.PathBuilder builder = new Path.PathBuilder();
+        String[] elems = Text.explode(path, '/', false);
+        for (int i=elems.length-1; i>=0; i--) {
+            int pos = elems[i].indexOf('[');
+            if (pos<0) {
+                builder.addFirst(QName.fromJCRName(elems[i], resolver));
+            } else {
+                int index = Integer.parseInt(elems[i].substring(pos+1, elems[i].length()-1));
+                builder.addFirst(QName.fromJCRName(elems[i].substring(0, pos), resolver), index);
+            }
+        }
+        if (path.startsWith("/")) {
+            builder.addRoot();
+        }
+        return normalize ? builder.getPath().getNormalizedPath() : builder.getPath();
+    }
+
+    private static class Test {
+
+        private final String path;
+
+        private final String normalizedPath;
+
+        private final int flags;
+
+        /**
+         * creates an invalid path test
+         * @param path
+         */
+        public Test(String path) {
+            this(path, null, 0);
+        }
+
+        /**
+         * @param path
+         * @param flags
+         */
+        public Test(String path, int flags) {
+            this(path, null, flags);
+        }
+
+        public Test(String path, String normalizedPath, int flags) {
+            this.path = path;
+            this.normalizedPath = normalizedPath;
+            this.flags = flags | ((path.length()>0 && path.charAt(0)=='/') ? ABS : 0);
+        }
+
+        public boolean isAbsolute() {
+            return (flags&ABS) > 0;
+        }
+
+        public boolean isNormalized() {
+            return (flags&NOR) > 0;
+        }
+
+        public boolean isValid() {
+            return (flags&VAL) > 0;
+        }
+
+        public String toString() {
+            StringBuffer b = new StringBuffer(path);
+            if (normalizedPath!=null) {
+                b.append(" -> " + normalizedPath);
+            }
+            if (isAbsolute()) {
+                b.append(",ABS");
+            }
+            if (isNormalized()) {
+                b.append(",NOR");
+            }
+            if (isValid()) {
+                b.append(",VAL");
+            }
+            return b.toString();
+        }
+    }
+}

Added: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/core/QNameTest.java
Url: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/core/QNameTest.java?view=auto&rev=125921
==============================================================================
--- (empty file)
+++ incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/core/QNameTest.java	Fri Jan 21 03:51:29 2005
@@ -0,0 +1,151 @@
+/*
+ * Copyright 2004-2005 The Apache Software Foundation or its licensors,
+ *                     as applicable.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.core;
+
+import junit.framework.TestCase;
+
+import java.util.ArrayList;
+
+/**
+ * This Class implements a test case for the 'Path' class.
+ *
+ * Actually, this should be below the {@link org.apache.jackrabbit.test} package,
+ * but it needs package protected methods of that class.
+ */
+public class QNameTest extends TestCase {
+
+    private final NamespaceResolver resolver;
+
+    private Test[] tests;
+
+    private static int NUM_TESTS = 1;
+
+    public QNameTest() {
+
+        // create dummy namespace resolver
+        resolver = new NamespaceResolver(){
+            public String getURI(String prefix) {
+                return prefix;
+            }
+
+            public String getPrefix(String uri) {
+                return uri;
+            }
+        };
+
+        // create tests
+        ArrayList list = new ArrayList();
+
+        // valid names
+        list.add(new Test("name", "", "name"));
+        list.add(new Test("prefix:name", "prefix", "name"));
+        list.add(new Test("pre fix:na me", "pre fix", "na me"));
+
+        // invalid names
+        list.add(new Test(":name"));
+        list.add(new Test(""));
+        list.add(new Test(" name"));
+        list.add(new Test(" prefix: name"));
+        list.add(new Test("prefix: name"));
+        list.add(new Test("prefix:name "));
+        list.add(new Test("prefix :name"));
+        list.add(new Test("name/name"));
+        list.add(new Test("name[name"));
+        list.add(new Test("name]name"));
+        list.add(new Test("name*name"));
+        list.add(new Test("prefix:name:name"));
+
+        tests = (Test[]) list.toArray(new Test[list.size()]);
+    }
+
+    public void testCreate() throws Exception {
+        for (int i=0; i<tests.length; i++) {
+            Test t = tests[i];
+            long t1 = System.currentTimeMillis();
+            for (int j=0; j<NUM_TESTS; j++) {
+                try {
+                    QName n = QName.fromJCRName(t.jcrName, resolver);
+                    if (!t.isValid()) {
+                        fail("Should throw IllegalNameException: " + t.jcrName);
+                    }
+                    assertEquals("\"" + t.jcrName + "\".uri", t.prefix, n.namespaceURI);
+                    assertEquals("\"" + t.jcrName + "\".localName", t.name, n.localName);
+                } catch (IllegalNameException e) {
+                    if (t.isValid()) {
+                        throw e;
+                    }
+                }
+            }
+            long t2 = System.currentTimeMillis();
+            if (NUM_TESTS>1) {
+                System.out.println("testCreate():\t" + t + "\t" + (t2-t1) + "\tms");
+            }
+        }
+    }
+
+    public void testCheckFormat() throws Exception {
+        for (int i=0; i<tests.length; i++) {
+            Test t = tests[i];
+            long t1 = System.currentTimeMillis();
+            for (int j=0; j<NUM_TESTS; j++) {
+                // check just creation
+                boolean isValid = true;
+                try {
+                    QName.checkFormat(t.jcrName);
+                } catch (IllegalNameException e) {
+                    isValid = false;
+                }
+                assertEquals("\"" + t.jcrName + "\".checkFormat()", t.isValid(),  isValid);
+            }
+            long t2 = System.currentTimeMillis();
+            if (NUM_TESTS>1) {
+                System.out.println("testCheckFormat():\t" + t + "\t" + (t2-t1) + "\tms");
+            }
+        }
+    }
+
+    private static class Test {
+
+        private final String jcrName;
+
+        private final String prefix;
+
+        private final String name;
+
+        public Test(String jcrName) {
+            this(jcrName, null, null);
+        }
+
+        public Test(String jcrName, String prefix, String name) {
+            this.jcrName = jcrName;
+            this.prefix = prefix;
+            this.name = name;
+        }
+
+        public boolean isValid() {
+            return name!=null;
+        }
+
+        public String toString() {
+            StringBuffer b = new StringBuffer(jcrName);
+            if (isValid()) {
+                b.append(",VAL");
+            }
+            return b.toString();
+        }
+    }
+}

Added: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/core/TestAll.java
Url: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/core/TestAll.java?view=auto&rev=125921
==============================================================================
--- (empty file)
+++ incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/core/TestAll.java	Fri Jan 21 03:51:29 2005
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2004-2005 The Apache Software Foundation or its licensors,
+ *                     as applicable.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.core;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+/**
+ * Test suite that includes all testcases for the Search module.
+ */
+public class TestAll extends TestCase {
+
+    /**
+     * Returns a <code>Test</code> suite that executes all tests inside this
+     * package.
+     *
+     * @return a <code>Test</code> suite that executes all tests inside this
+     *         package.
+     */
+    public static Test suite() {
+        TestSuite suite = new TestSuite("Core tests");
+
+        suite.addTestSuite(PathTest.class);
+        suite.addTestSuite(QNameTest.class);
+
+        return suite;
+    }
+}