jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mreut...@apache.org
Subject svn commit: r155900 [1/2] - in incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit: init/ test/ test/api/ test/api/nodetype/ test/api/query/ test/api/version/
Date Wed, 02 Mar 2005 09:07:10 GMT
Author: mreutegg
Date: Wed Mar  2 01:06:54 2005
New Revision: 155900

URL: http://svn.apache.org/viewcvs?view=rev&rev=155900
Log:
More test cases for packages javax.jcr and javax.jcr.nodetype

Added:
    incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/NodeDiscoveringNodeTypesTest.java
    incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/nodetype/NodeDefTest.java   (with props)
    incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/nodetype/NodeTypeManagerTest.java   (with props)
    incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/nodetype/NodeTypeTest.java   (with props)
    incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/nodetype/PropertyDefTest.java   (with props)
Removed:
    incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/GetAncestorTest.java
    incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/ItemDefTest.java
    incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/ItemReadMethodsTest.java
Modified:
    incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/init/NodeTestData.java
    incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/AbstractJCRTest.java
    incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/NodeIteratorTest.java
    incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/NodeReadMethodsTest.java
    incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/PropertyReadMethodsTest.java
    incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/PropertyUtil.java
    incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/RootNodeTest.java
    incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/TestAll.java
    incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/nodetype/TestAll.java
    incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/query/AbstractQueryTest.java
    incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/query/SQLQueryLevel2Test.java
    incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/query/XPathQueryLevel2Test.java
    incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/version/AbstractVersionTest.java

Modified: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/init/NodeTestData.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/init/NodeTestData.java?view=diff&r1=155899&r2=155900
==============================================================================
--- incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/init/NodeTestData.java (original)
+++ incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/init/NodeTestData.java Wed Mar  2 01:06:54 2005
@@ -100,6 +100,8 @@
 
         Node resReference = dataRoot.addNode("reference");
         resReference.setProperty("ref", resource);
+        // make this node itself referenceable
+        resReference.addMixin(mixReferenceable);
         log.println("Adding node: " + resReference.getPath());
 
         superuser.save();

Modified: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/AbstractJCRTest.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/AbstractJCRTest.java?view=diff&r1=155899&r2=155900
==============================================================================
--- incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/AbstractJCRTest.java (original)
+++ incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/AbstractJCRTest.java Wed Mar  2 01:06:54 2005
@@ -23,6 +23,7 @@
 import javax.jcr.NamespaceRegistry;
 import javax.jcr.Repository;
 import javax.jcr.NamespaceException;
+import javax.jcr.RangeIterator;
 import java.util.StringTokenizer;
 
 /**
@@ -61,6 +62,11 @@
     protected String jcrPrimaryType;
 
     /**
+     * JCR Name jcr:mixinTypes using the namespace resolver of the current session.
+     */
+    protected String jcrMixinTypes;
+
+    /**
      * JCR Name jcr:predecessors using the namespace resolver of the current session.
      */
     protected String jcrPredecessors;
@@ -219,6 +225,7 @@
 
         // setup some common names
         jcrPrimaryType = superuser.getNamespacePrefix(NS_JCR_URI) + ":primaryType";
+        jcrMixinTypes = superuser.getNamespacePrefix(NS_JCR_URI) + ":mixinTypes";
         jcrPredecessors = superuser.getNamespacePrefix(NS_JCR_URI) + ":predecessors";
         jcrBaseVersion = superuser.getNamespacePrefix(NS_JCR_URI) + ":baseVersion";
         jcrUUID = superuser.getNamespacePrefix(NS_JCR_URI) + ":uuid";
@@ -337,6 +344,26 @@
 
         // finally try global property
         return helper.getProperty(RepositoryStub.PROP_PREFIX + "." + name);
+    }
+
+    /**
+     * Returns the size of the <code>RangeIterator</code> <code>it</code>.
+     * Note, that the <code>RangeIterator</code> might get consumed, because
+     * {@link RangeIterator#getSize()} might return -1 (information unavailable).
+     * @param it a <code>RangeIterator</code>.
+     * @return the size of the iterator (number of elements).
+     */
+    protected long getSize(RangeIterator it) {
+        long size = it.getSize();
+        if (size != -1) {
+            return size;
+        }
+        size = 0;
+        while (it.hasNext()) {
+            it.next();
+            size++;
+        }
+        return size;
     }
 
 }

Added: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/NodeDiscoveringNodeTypesTest.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/NodeDiscoveringNodeTypesTest.java?view=auto&rev=155900
==============================================================================
--- incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/NodeDiscoveringNodeTypesTest.java (added)
+++ incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/NodeDiscoveringNodeTypesTest.java Wed Mar  2 01:06:54 2005
@@ -0,0 +1,218 @@
+/*
+ * 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.test.api;
+
+import org.apache.jackrabbit.test.AbstractJCRTest;
+import org.apache.jackrabbit.test.NotExecutableException;
+
+import javax.jcr.Session;
+import javax.jcr.Node;
+import javax.jcr.NodeIterator;
+import javax.jcr.Value;
+import javax.jcr.RepositoryException;
+import javax.jcr.nodetype.NodeType;
+import java.util.NoSuchElementException;
+
+/**
+ * <code>NodeDiscoveringNodeTypesTest</code>...
+ * <p/>
+ * All test cases in this class rely on content in the repository. That is the
+ * root node of the default workspace must at least contain one child node,
+ * otherwise a {@link NotExecutableException} is thrown.
+ *
+ * @test
+ * @sources NodeDiscoveringNodeTypesTest.java
+ * @executeClass org.apache.jackrabbit.test.api.NodeDiscoveringNodeTypesTest
+ * @keywords level1
+ */
+public class NodeDiscoveringNodeTypesTest extends AbstractJCRTest {
+
+    /**
+     * The session we use for the tests
+     */
+    private Session session;
+
+    /**
+     * The root node of the default workspace
+     */
+    private Node rootNode;
+
+    /**
+     * A child node of the root node in the default workspace.
+     */
+    private Node childNode;
+
+    /**
+     * Sets up the fixture for the test cases.
+     */
+    protected void setUp() throws Exception {
+        isReadOnly = true;
+        super.setUp();
+
+        session = helper.getReadOnlySession();
+        rootNode = session.getRootNode();
+        NodeIterator nodes = rootNode.getNodes();
+        try {
+            childNode = nodes.nextNode();
+        } catch (NoSuchElementException e) {
+        }
+    }
+
+    /**
+     * Releases the session aquired in {@link #setUp()}.
+     */
+    protected void tearDown() throws Exception {
+        if (session != null) {
+            session.logout();
+        }
+        super.tearDown();
+    }
+
+    /**
+     * Test if getPrimaryNodeType() returns the node type according to the
+     * property "jcr:primaryType"
+     */
+    public void testGetPrimaryNodeType()
+            throws NotExecutableException, RepositoryException {
+
+        if (childNode == null) {
+            throw new NotExecutableException("Workspace does not have sufficient content for this test. " +
+                    "Root node must have at least one child node.");
+        }
+
+        NodeType type = childNode.getPrimaryNodeType();
+        String name = childNode.getProperty(jcrPrimaryType).getString();
+
+        assertEquals("getPrimaryNodeType() must return the node type stored " +
+                "as property \"jcr:primaryType\"",
+                name, type.getName());
+
+        assertFalse("getPrimaryNodeType() must return a primary node type",
+                type.isMixin());
+    }
+
+
+    /**
+     * Test if getMixinNodeType returns the node types according to the property
+     * "jcr:mixinTypes". Therefor a node with mixin types is located recursively
+     * in the entire repository. A NotExecutableException is thrown when no such
+     * node is found.
+     */
+    public void testGetMixinNodeTypes()
+            throws NotExecutableException, RepositoryException {
+
+        if (childNode == null) {
+            throw new NotExecutableException("Workspace does not have sufficient content for this test. " +
+                    "Root node must have at least one child node.");
+        }
+
+        Node node = locateNodeWithMixinNodeTypes(rootNode);
+
+        if (node == null) {
+            throw new NotExecutableException("Workspace does not contain a node with mixin node types defined");
+        }
+
+        Value names[] = node.getProperty(jcrMixinTypes).getValues();
+        NodeType types[] = node.getMixinNodeTypes();
+
+        assertEquals("getMixinNodeTypes() does not return the same number of " +
+                "node types as " +
+                "getProperty(\"jcr:mixinTypes\").getValues()",
+                types.length,
+                names.length);
+
+        StringBuffer namesString = new StringBuffer();
+        for (int i = 0; i < names.length; i++) {
+            namesString.append("|" + names[i].getString() + "|");
+        }
+
+        for (int i = 0; i < types.length; i++) {
+            String pattern = "|" + types[i].getName() + "|";
+
+            assertTrue("getMixinNodeTypes() does not return the same node" +
+                    "types as getProperty(\"jcr:mixinTypes\").getValues()",
+                    namesString.indexOf(pattern) != -1);
+
+            assertTrue("All nodes returned by getMixinNodeTypes() must be" +
+                    "mixin",
+                    types[i].isMixin());
+        }
+    }
+
+
+    /**
+     * Test if isNodeTye(String nodeTypeName) returns true if nodeTypeName is the
+     * name of the primary node type, the name of a mixin node type and the name
+     * of a supertype.
+     */
+    public void testIsNodeType()
+            throws NotExecutableException, RepositoryException {
+
+        String nodeTypeName;
+
+        // test with primary node's name
+        nodeTypeName = rootNode.getPrimaryNodeType().getName();
+        assertTrue("isNodeType(String nodeTypeName) must return true if " +
+                "nodeTypeName is the name of the primary node type",
+                rootNode.isNodeType(nodeTypeName));
+
+        // test with mixin node's name
+        // (if such a node is available)
+        Node nodeWithMixin = locateNodeWithMixinNodeTypes(rootNode);
+        if (nodeWithMixin != null) {
+            NodeType types[] = nodeWithMixin.getMixinNodeTypes();
+            nodeTypeName = types[0].getName();
+            assertTrue("isNodeType(String nodeTypeName) must return true if " +
+                    "nodeTypeName is the name of one of the " +
+                    "mixin node types",
+                    nodeWithMixin.isNodeType(nodeTypeName));
+        }
+
+        // test with the name of predefined supertype "nt:base"
+        assertTrue("isNodeType(String nodeTypeName) must return true if " +
+                "nodeTypeName is the name of a node type of a supertype",
+                rootNode.isNodeType(ntBase));
+    }
+
+    //-----------------------< internal >---------------------------------------
+
+    /**
+     * Returns the first descendant of <code>node</code> which defines mixin
+     * node type(s).
+     *
+     * @param node <code>Node</code> to start traversal.
+     * @return first node with mixin node type(s)
+     */
+    private Node locateNodeWithMixinNodeTypes(Node node)
+            throws RepositoryException {
+
+        if (node.getMixinNodeTypes().length != 0) {
+            return node;
+        }
+
+        NodeIterator nodes = node.getNodes();
+        while (nodes.hasNext()) {
+            Node returnedNode = this.locateNodeWithMixinNodeTypes(nodes.nextNode());
+            if (returnedNode != null) {
+                return returnedNode;
+            }
+        }
+        return null;
+    }
+
+
+}
\ No newline at end of file

Modified: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/NodeIteratorTest.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/NodeIteratorTest.java?view=diff&r1=155899&r2=155900
==============================================================================
--- incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/NodeIteratorTest.java (original)
+++ incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/NodeIteratorTest.java Wed Mar  2 01:06:54 2005
@@ -17,6 +17,7 @@
 package org.apache.jackrabbit.test.api;
 
 import org.apache.jackrabbit.test.AbstractJCRTest;
+import org.apache.jackrabbit.test.NotExecutableException;
 
 import javax.jcr.RepositoryException;
 import javax.jcr.NodeIterator;
@@ -40,8 +41,9 @@
     /**
      * Tests if {@link javax.jcr.NodeIterator#getSize()} returns the correct
      * size.
+     * @throws NotExecutableException if getSize() returns -1 (unavailable).
      */
-    public void testGetSize() throws RepositoryException {
+    public void testGetSize() throws RepositoryException, NotExecutableException {
         NodeIterator iter = testRootNode.getNodes();
         long size = testRootNode.getNodes().getSize();
         if (size != -1) {
@@ -52,7 +54,7 @@
             }
             assertEquals("NodeIterator.getSize does not return correct number.", size, count);
         } else {
-            // NodeIterator does not return size information
+            throw new NotExecutableException("NodeIterator.getSize() does not return size information.");
         }
     }
 

Modified: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/NodeReadMethodsTest.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/NodeReadMethodsTest.java?view=diff&r1=155899&r2=155900
==============================================================================
--- incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/NodeReadMethodsTest.java (original)
+++ incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/NodeReadMethodsTest.java Wed Mar  2 01:06:54 2005
@@ -19,26 +19,27 @@
 import org.apache.jackrabbit.test.AbstractJCRTest;
 import org.apache.jackrabbit.test.NotExecutableException;
 
-import javax.jcr.Node;
 import javax.jcr.Session;
-import javax.jcr.RepositoryException;
+import javax.jcr.Node;
 import javax.jcr.NodeIterator;
+import javax.jcr.RepositoryException;
 import javax.jcr.PathNotFoundException;
-import javax.jcr.PropertyIterator;
-import javax.jcr.Property;
+import javax.jcr.ItemNotFoundException;
 import javax.jcr.Item;
+import javax.jcr.ItemVisitor;
+import javax.jcr.Property;
+import javax.jcr.PropertyIterator;
 import javax.jcr.PropertyType;
 import javax.jcr.UnsupportedRepositoryOperationException;
-import javax.jcr.ItemNotFoundException;
 import java.util.ArrayList;
 import java.util.NoSuchElementException;
 
 /**
- * Tests the 'read' methods specified in the {@link javax.jcr.Node} interface
- * on a level 1 repository.
+ * Tests the 'read' methods specified in the {@link javax.jcr.Node} interface on
+ * a level 1 repository.
  * <p/>
- * Most tests require at least one child node under the root node, otherwise
- * a {@link org.apache.jackrabbit.test.NotExecutableException} is thrown.
+ * Most tests require at least one child node under the root node, otherwise a
+ * {@link org.apache.jackrabbit.test.NotExecutableException} is thrown.
  *
  * @test
  * @sources NodeReadMethodsTest.java
@@ -48,9 +49,19 @@
 public class NodeReadMethodsTest extends AbstractJCRTest {
 
     /**
+     * Session to access the workspace
+     */
+    private Session session;
+
+    /**
      * The root node of the default workspace
      */
-    Node rootNode;
+    private Node rootNode;
+
+    /**
+     * The first child node of the rootNode
+     */
+    private Node childNode;
 
     /**
      * Sets up the fixture for this test.
@@ -58,11 +69,198 @@
     protected void setUp() throws Exception {
         isReadOnly = true;
         super.setUp();
-        Session session = helper.getReadOnlySession();
+
+        session = helper.getReadOnlySession();
         rootNode = session.getRootNode();
+
+        NodeIterator nodes = rootNode.getNodes();
+        try {
+            childNode = nodes.nextNode();
+        } catch (NoSuchElementException e) {
+        }
+    }
+
+    /**
+     * Releases the session aquired in {@link #setUp()}.
+     */
+    protected void tearDown() throws Exception {
+        if (session != null) {
+            session.logout();
+        }
+        super.tearDown();
+    }
+
+    // -----------< tests of methods inherited of Item >------------------------
+
+    /**
+     * Tests if getPath() returns the correct path.
+     */
+    public void testGetPath()
+            throws NotExecutableException, RepositoryException {
+
+        if (childNode == null) {
+            throw new NotExecutableException("Workspace does not have sufficient content to run this test.");
+        }
+
+        String path = "/" + childNode.getName();
+        if (getSize(rootNode.getNodes(childNode.getName())) > 1) {
+            // is a same-name-sibling, append index
+            path += "[" + childNode.getIndex() + "]";
+        }
+
+        log.println("path: " + childNode.getPath());
+        assertEquals("getPath returns wrong result",
+                path,
+                childNode.getPath());
+    }
+
+    /**
+     * Tests if getName() returns same as last name returned by getPath()
+     */
+    public void testGetName() throws RepositoryException {
+        assertEquals("getName() of root must be an empty string",
+                "",
+                rootNode.getName());
+
+        // build name from path
+        String path = childNode.getPath();
+        String name = path.substring(path.lastIndexOf("/") + 1);
+        if (name.indexOf("[") != -1) {
+            name = name.substring(0, name.indexOf("["));
+        }
+        assertEquals("getName() must be the same as the last item in the path",
+                name,
+                childNode.getName());
+    }
+
+    /**
+     * Test if the ancestor at depth = n, where n is the depth of this
+     * <code>Item</code>, returns this <code>Node</code> itself.
+     */
+    public void testGetAncestorOfNodeDepth() throws RepositoryException {
+        Node nodeAtDepth = (Node) rootNode.getAncestor(rootNode.getDepth());
+        assertTrue("The ancestor of depth = n, where n is the depth of this " +
+                "Node must be the item itself.", rootNode.isSame(nodeAtDepth));
+    }
+
+    /**
+     * Test if getting the ancestor of depth = n, where n is greater than depth
+     * of this <code>Node</code>, throws an <code>ItemNotFoundException</code>.
+     */
+    public void testGetAncestorOfGreaterDepth() throws RepositoryException {
+        try {
+            int greaterDepth = rootNode.getDepth() + 1;
+            rootNode.getAncestor(greaterDepth);
+            fail("Getting ancestor of depth n, where n is greater than depth of" +
+                    "this Node must throw an ItemNotFoundException");
+        } catch (ItemNotFoundException e) {
+            // success
+        }
+    }
+
+    /**
+     * Test if getting the ancestor of negative depth throws an
+     * <code>ItemNotFoundException</code>.
+     */
+    public void testGetAncestorOfNegativeDepth() throws RepositoryException {
+        try {
+            rootNode.getAncestor(-1);
+            fail("Getting ancestor of depth < 0 must throw an ItemNotFoundException.");
+        } catch (ItemNotFoundException e) {
+            // success
+        }
+    }
+
+    /**
+     * Tests if getParent() returns parent node
+     */
+    public void testGetParent()
+            throws NotExecutableException, RepositoryException {
+
+        if (childNode == null) {
+            throw new NotExecutableException("Workspace does not have sufficient content to run this test.");
+        }
+
+        assertTrue("getParent() of a child node return the parent node.",
+                rootNode.isSame(childNode.getParent()));
+    }
+
+    /**
+     * Tests if getParent() of root throws an ItemNotFoundException
+     */
+    public void testGetParentOfRoot() throws RepositoryException {
+        try {
+            rootNode.getParent();
+            fail("getParent() of root must throw an ItemNotFoundException.");
+        } catch (ItemNotFoundException e) {
+            // success
+        }
+    }
+
+    /**
+     * Tests if depth of root is 0 and depth of a sub node of root is 1
+     */
+    public void testGetDepth() throws RepositoryException {
+        assertEquals("getDepth() of root must be 0", 0, rootNode.getDepth());
+        if (childNode != null) {
+            assertEquals("getDepth() of child node of root must be 1", 1,
+                    childNode.getDepth());
+        }
+    }
+
+    /**
+     * Tests if getSession() is same as through which the Item was acquired
+     */
+    public void testGetSession() throws RepositoryException {
+        assertSame("getSession must return the Session through which " +
+                "the Node was acquired.",
+                rootNode.getSession(),
+                session);
     }
 
     /**
+     * Tests if isNode() returns true
+     */
+    public void testIsNode() {
+        assertTrue("isNode() must return true.",
+                rootNode.isNode());
+    }
+
+    /**
+     * Tests if isSame() returns true when retrieving an item through different
+     * sessions
+     */
+    public void testIsSame() throws RepositoryException {
+        // access same node through different session
+        Item otherRootNode = helper.getReadOnlySession().getRootNode();
+        assertTrue("isSame(Item item) must return true for the same " +
+                "item retrieved through different sessions.",
+                rootNode.isSame(otherRootNode));
+    }
+
+    /**
+     *
+     */
+    public void testAccept() throws RepositoryException {
+        final Node n = rootNode;
+
+        ItemVisitor itemVisitor = new ItemVisitor() {
+            public void visit(Property property) {
+                fail("Wrong accept method executed.");
+            }
+
+            public void visit(Node node) {
+                assertTrue("Visited node is not the same as the one passed in method visit(Node)",
+                        n.isSame(node));
+            }
+        };
+
+        n.accept(itemVisitor);
+    }
+
+    // -----------< tests of Node specific methods >----------------------------
+
+    /**
      * Test if getNode(String relPath) returns the correct node and if a
      * PathNotFoundException is thrown when Node at relPath does not exist
      */
@@ -88,9 +286,11 @@
         try {
             NodeIterator nodes2 = rootNode.getNodes();
             Node node = nodes2.nextNode();
-            assertSame(rootNode.getNode(node.getName()), node);
+            assertTrue("Node from Iterator is not the same as the Node from getNode()",
+                    rootNode.getNode(node.getName()).isSame(node));
         } catch (NoSuchElementException e) {
-            throw new NotExecutableException("Workspace does not have sufficient content for this test. Root node must have at least one child node.");
+            throw new NotExecutableException("Workspace does not have sufficient content for this test. " +
+                    "Root node must have at least one child node.");
         }
     }
 
@@ -116,7 +316,8 @@
         // get root node and build an ArrayList of its sub nodes
         Node node = rootNode;
         if (!node.hasNodes()) {
-            throw new NotExecutableException("Workspace does not have sufficient content for this test. Root node must have at least one child node.");
+            throw new NotExecutableException("Workspace does not have sufficient content for this test. " +
+                    "Root node must have at least one child node.");
         }
         NodeIterator allNodesIt = node.getNodes();
         ArrayList allNodes = new ArrayList();
@@ -125,7 +326,6 @@
             allNodes.add(n);
         }
 
-
         // test if an empty NodeIterator is returned
         // when the pattern is not matching any child node
         String pattern0 = "";
@@ -138,30 +338,21 @@
             // success
         }
 
-
         // all further tests are using root's first sub node
         Node firstNode = (Node) allNodes.get(0);
 
-
         // test pattern "*"
         String pattern1 = "*";
         String assertString1 = "node.getNodes(\"" + pattern1 + "\"): ";
         NodeIterator nodes1 = node.getNodes(pattern1);
-        int numOfNodes1 = 0;
         // test if the number of found nodes is correct
-        while (nodes1.hasNext()) {
-            nodes1.nextNode();
-            numOfNodes1++;
-        }
         assertEquals(assertString1 + "number of nodes found: ",
                 allNodes.size(),
-                numOfNodes1);
-
+                getSize(nodes1));
 
         // test pattern "nodeName"
         String pattern2 = firstNode.getName();
         String assertString2 = "node.getNodes(\"" + pattern2 + "\"): ";
-        int numOfNodes2 = 0;
         // test if the names of the found nodes are matching the pattern
         NodeIterator nodes2 = node.getNodes(pattern2);
         while (nodes2.hasNext()) {
@@ -169,7 +360,6 @@
             assertEquals(assertString2 + "name comparison failed: ",
                     firstNode.getName(),
                     n.getName());
-            numOfNodes2++;
         }
         // test if the number of found nodes is correct
         int numExpected2 = 0;
@@ -179,15 +369,15 @@
                 numExpected2++;
             }
         }
+        nodes2 = node.getNodes(pattern2);
         assertEquals(assertString2 + "number of nodes found: ",
                 numExpected2,
-                numOfNodes2);
+                getSize(nodes2));
 
 
-        // test pattern "nodeName | nodeName"
-        String pattern3 = firstNode.getName() + " | " + firstNode.getName();
+        // test pattern "nodeName|nodeName"
+        String pattern3 = firstNode.getName() + "|" + firstNode.getName();
         String assertString3 = "node.getNodes(\"" + pattern3 + "\"): ";
-        int numOfNodes3 = 0;
         // test if the names of the found nodes are matching the pattern
         NodeIterator nodes3 = node.getNodes(pattern3);
         while (nodes3.hasNext()) {
@@ -195,7 +385,6 @@
             assertEquals(assertString2 + "name comparison failed: ",
                     firstNode.getName(),
                     n.getName());
-            numOfNodes3++;
         }
         // test if the number of found nodes is correct
         int numExpected3 = 0;
@@ -205,9 +394,10 @@
                 numExpected3++;
             }
         }
+        nodes3 = node.getNodes(pattern3);
         assertEquals(assertString3 + "number of nodes found: ",
                 numExpected3,
-                numOfNodes3);
+                getSize(nodes3));
 
 
         // test pattern "*odeNam*"
@@ -216,7 +406,6 @@
             String shortenName = name.substring(1, name.length() - 1);
             String pattern4 = "*" + shortenName + "*";
             String assertString4 = "node.getNodes(\"" + pattern4 + "\"): ";
-            int numOfNodes4 = 0;
             // test if the names of the found nodes are matching the pattern
             NodeIterator nodes4 = node.getNodes(pattern4);
             while (nodes4.hasNext()) {
@@ -224,7 +413,6 @@
                 assertTrue(assertString4 + "name comparison failed: *" +
                         shortenName + "* not found in " + n.getName(),
                         n.getName().indexOf(shortenName) != -1);
-                numOfNodes4++;
             }
             // test if the number of found nodes is correct
             int numExpected4 = 0;
@@ -234,9 +422,10 @@
                     numExpected4++;
                 }
             }
+            nodes4 = node.getNodes(pattern4);
             assertEquals(assertString4 + "number of nodes found: ",
                     numExpected4,
-                    numOfNodes4);
+                    getSize(nodes4));
         }
     }
 
@@ -265,7 +454,8 @@
         try {
             PropertyIterator properties2 = rootNode.getProperties();
             Property property = properties2.nextProperty();
-            assertSame(rootNode.getProperty(property.getName()), property);
+            assertTrue("Property returned by getProperties() is not the same as returned by getProperty(String).",
+                    rootNode.getProperty(property.getName()).isSame(property));
         } catch (NoSuchElementException e) {
             fail("Root node must always have at least one property: jcr:primaryType");
         }
@@ -324,19 +514,13 @@
         String pattern1 = "*";
         String assertString1 = "node.getProperties(\"" + pattern1 + "\"): ";
         PropertyIterator properties1 = node.getProperties(pattern1);
-        int numOfProperties1 = 0;
-        while (properties1.hasNext()) {
-            properties1.nextProperty();
-            numOfProperties1++;
-        }
         assertEquals(assertString1 + "number of properties found: ",
                 allProperties.size(),
-                numOfProperties1);
+                getSize(properties1));
 
         // test: getProperties("propertyName")
         String pattern2 = firstProperty.getName();
         String assertString2 = "node.getProperties(\"" + pattern2 + "\"): ";
-        int numOfProperties2 = 0;
         // test if the names of the found properties are matching the pattern
         PropertyIterator properties2 = node.getProperties(pattern2);
         while (properties2.hasNext()) {
@@ -344,7 +528,6 @@
             assertEquals(assertString2 + "name comparison failed: ",
                     firstProperty.getName(),
                     p.getName());
-            numOfProperties2++;
         }
         // test if the number of found properties is correct
         int numExpected2 = 0;
@@ -354,15 +537,15 @@
                 numExpected2++;
             }
         }
+        properties2 = node.getProperties(pattern2);
         assertEquals(assertString2 + "number of properties found: ",
                 numExpected2,
-                numOfProperties2);
+                getSize(properties2));
 
 
-        // test: getProperties("propertyName | propertyName")
-        String pattern3 = firstProperty.getName() + " | " + firstProperty.getName();
+        // test: getProperties("propertyName|propertyName")
+        String pattern3 = firstProperty.getName() + "|" + firstProperty.getName();
         String assertString3 = "node.getProperties(\"" + pattern3 + "\"): ";
-        int numOfProperties3 = 0;
         // test if the names of the found properties are matching the pattern
         PropertyIterator properties3 = node.getProperties(pattern3);
         while (properties3.hasNext()) {
@@ -370,7 +553,6 @@
             assertEquals(assertString2 + "name comparison failed: ",
                     firstProperty.getName(),
                     p.getName());
-            numOfProperties3++;
         }
         // test if the number of found properties is correct
         int numExpected3 = 0;
@@ -380,9 +562,10 @@
                 numExpected3++;
             }
         }
+        properties3 = node.getProperties(pattern3);
         assertEquals(assertString3 + "number of properties found: ",
                 numExpected3,
-                numOfProperties3);
+                getSize(properties3));
 
 
         // test: getProperties("*opertyNam*")
@@ -391,7 +574,6 @@
             String shortenName = name.substring(1, name.length() - 1);
             String pattern4 = "*" + shortenName + "*";
             String assertString4 = "node.getProperties(\"" + pattern4 + "\"): ";
-            int numOfProperties4 = 0;
             // test if the names of the found properties are matching the pattern
             PropertyIterator properties4 = node.getProperties(pattern4);
             while (properties4.hasNext()) {
@@ -399,7 +581,6 @@
                 assertTrue(assertString4 + "name comparison failed: *" +
                         shortenName + "* not found in " + p.getName(),
                         p.getName().indexOf(shortenName) != -1);
-                numOfProperties4++;
             }
             // test if the number of found properties is correct
             int numExpected4 = 0;
@@ -409,9 +590,10 @@
                     numExpected4++;
                 }
             }
+            properties4 = node.getProperties(pattern4);
             assertEquals(assertString4 + "number of properties found: ",
                     numExpected4,
-                    numOfProperties4);
+                    getSize(properties4));
         }
     }
 
@@ -423,18 +605,24 @@
      */
     public void testGetPrimaryItem()
             throws NotExecutableException, RepositoryException {
+
         Node node = locateNodeWithPrimaryItem(rootNode);
-        String primaryItemName = node.getPrimaryNodeType().getPrimaryItemName();
 
-        if (primaryItemName == null) {
+        if (node == null) {
             throw new NotExecutableException("Workspace does not contain a node with primary item defined");
         }
 
+        String primaryItemName = node.getPrimaryNodeType().getPrimaryItemName();
+
         Item primaryItem = node.getPrimaryItem();
         if (primaryItem.isNode()) {
-            assertSame(primaryItem, node.getNode(primaryItemName));
+            assertTrue("Node returned by getPrimaryItem() is not the same as " +
+                    "the one aquired by getNode(String)",
+                    node.getNode(primaryItemName).isSame(primaryItem));
         } else {
-            assertSame(primaryItem, node.getProperty(primaryItemName));
+            assertTrue("Property returned by getPrimaryItem() is not the same as " +
+                    "the one aquired by getProperty(String)",
+                    node.getProperty(primaryItemName).isSame(primaryItem));
         }
     }
 
@@ -449,9 +637,7 @@
 
         Node node = locateNodeWithoutPrimaryItem(rootNode);
 
-        String primaryItemName = node.getPrimaryNodeType().getPrimaryItemName();
-
-        if (primaryItemName != null) {
+        if (node == null) {
             throw new NotExecutableException("Workspace does not contain a node with primary item defined");
         }
 
@@ -474,9 +660,9 @@
 
         Node node = locateNodeWithSameNameSiblings(rootNode);
 
-        if (node == rootNode) {
+        if (node == null) {
             assertEquals("getIndex() of a node without same name siblings " +
-                    "must return 1", node.getIndex(), 1);
+                    "must return 1", rootNode.getIndex(), 1);
         } else {
             NodeIterator nodes = node.getParent().getNodes(node.getName());
             int i = 1;
@@ -494,7 +680,7 @@
 
         Node node = locateNodeWithReference(rootNode);
 
-        if (node == rootNode) {
+        if (node == null) {
             throw new NotExecutableException("Workspace does not contain a node with a reference property set");
         }
 
@@ -530,14 +716,14 @@
         // find a node of type mix:referenceable
         Node node = locateReferenceableNode(rootNode);
 
-        if (!node.isNodeType(mixReferenceable)) {
+        if (node == null) {
             throw new NotExecutableException("Workspace does not contain a referencable node");
         }
 
         try {
             assertEquals("node.getUUID() does not match " +
                     "node.getProperty(\"jcr:uuid\").getString()",
-                    node.getProperty("jcr:uuid").getString(), node.getUUID());
+                    node.getProperty(jcrUUID).getString(), node.getUUID());
         } catch (PathNotFoundException e) {
             fail("Property UUID expected for " +
                     "node of type \"" + mixReferenceable + "\"");
@@ -554,7 +740,7 @@
         // find a node NOT of type mix:referenceable
         Node node = locateNonReferenceableNode(rootNode);
 
-        if (node.isNodeType(mixReferenceable)) {
+        if (node == null) {
             throw new NotExecutableException("Workspace does not contain a non referenceable node");
         }
 
@@ -585,7 +771,8 @@
             notExistingNodeName.append(n.getName() + "X");
         }
         if (notExistingNodeName.equals("")) {
-            throw new NotExecutableException("Workspace does not have sufficient content for this test. Root node must have at least one child node.");
+            throw new NotExecutableException("Workspace does not have sufficient content for this test. " +
+                    "Root node must have at least one child node.");
         }
 
         assertFalse("hasNode(String relPath) returns true although " +
@@ -676,137 +863,137 @@
     /**
      * Returns the first descendant of <code>node</code> which is of type
      * mix:referencable.
+     *
      * @param node <code>Node</code> to start traversal.
      * @return first node of type mix:referenceable
      */
     private Node locateReferenceableNode(Node node)
             throws RepositoryException {
 
+        if (node.isNodeType(mixReferenceable)) {
+            return node;
+        }
+
         NodeIterator nodes = node.getNodes();
         while (nodes.hasNext()) {
-            Node n = nodes.nextNode();
-            if (n.isNodeType(mixReferenceable)) {
-                return n;
-            } else {
-                Node returnedNode = locateReferenceableNode(n);
-                if (n != returnedNode) {
-                    return returnedNode;
-                }
+            Node returnedNode = locateReferenceableNode(nodes.nextNode());
+            if (returnedNode != null) {
+                return returnedNode;
             }
         }
-        // no node of type "mix:referenceable" found - return passed node
-        return node;
+        return null;
     }
 
     /**
-     * Returns the first descendant of <code>node</code> which is not of
-     * type mix:referenceable.
+     * Returns the first descendant of <code>node</code> which is not of type
+     * mix:referenceable.
+     *
      * @param node <code>Node</code> to start traversal.
      * @return first node which is not of type mix:referenceable
      */
     private Node locateNonReferenceableNode(Node node)
             throws RepositoryException {
 
+        if (!node.isNodeType(mixReferenceable)) {
+            return node;
+        }
+
         NodeIterator nodes = node.getNodes();
         while (nodes.hasNext()) {
-            Node n = nodes.nextNode();
-            if (!n.isNodeType(mixReferenceable)) {
-                return n;
-            } else {
-                Node returnedNode = locateNonReferenceableNode(n);
-                if (n != returnedNode) {
-                    return returnedNode;
-                }
+            Node returnedNode = locateNonReferenceableNode(nodes.nextNode());
+            if (returnedNode != null) {
+                return returnedNode;
             }
         }
-        // all nodes are of type "mix:referenceable" - return passed node
-        return node;
+        return null;
     }
 
     /**
-     * Returns the first descendant of <code>node</code> which has a property
-     * of type {@link javax.jcr.PropertyType#REFERENCE} set.
+     * Returns the first descendant of <code>node</code> which has a property of
+     * type {@link javax.jcr.PropertyType#REFERENCE} set.
+     *
      * @param node <code>Node</code> to start traversal.
      * @return first node with a property of PropertType.REFERENCE
      */
     private Node locateNodeWithReference(Node node)
             throws RepositoryException {
-        NodeIterator nodes = node.getNodes();
-        while (nodes.hasNext()) {
-            Node n = nodes.nextNode();
-            PropertyIterator properties = n.getProperties();
-            while (properties.hasNext()) {
-                Property p = properties.nextProperty();
-                if (p.getType() == PropertyType.REFERENCE) {
-                    return n;
-                }
+
+        PropertyIterator properties = node.getProperties();
+        while (properties.hasNext()) {
+            Property p = properties.nextProperty();
+            if (p.getType() == PropertyType.REFERENCE) {
+                return node;
             }
+        }
 
-            Node returnedNode = locateNodeWithReference(n);
-            if (n != returnedNode) {
+        NodeIterator nodes = node.getNodes();
+        while (nodes.hasNext()) {
+            Node returnedNode = locateNodeWithReference(nodes.nextNode());
+            if (returnedNode != null) {
                 return returnedNode;
             }
         }
-        // no node of type "mix:referenceable" found - return passed node
-        return node;
+        return null;
     }
 
     /**
-     * Returns the first descendant of <code>node</code> which defines a
-     * primary item.
+     * Returns the first descendant of <code>node</code> which defines a primary
+     * item.
+     *
      * @param node <code>Node</code> to start traversal.
      * @return first node with a primary item
      */
     private Node locateNodeWithPrimaryItem(Node node)
             throws RepositoryException {
+
+        if (node.getPrimaryNodeType().getPrimaryItemName() != null) {
+            return node;
+        }
+
         NodeIterator nodes = node.getNodes();
         while (nodes.hasNext()) {
-            Node n = nodes.nextNode();
-            if (n.getPrimaryNodeType().getPrimaryItemName() != null) {
-                return n;
-            } else {
-                Node returnedNode = locateNodeWithPrimaryItem(n);
-                if (n != returnedNode) {
-                    return returnedNode;
-                }
+            Node returnedNode = locateNodeWithPrimaryItem(nodes.nextNode());
+            if (returnedNode != null) {
+                return returnedNode;
             }
         }
-        // no node with primary item found - return passed node
-        return node;
+        return null;
     }
 
     /**
-     * Returns the first descendant of <code>node</code> which does not define
-     * a primary item.
+     * Returns the first descendant of <code>node</code> which does not define a
+     * primary item.
+     *
      * @param node <code>Node</code> to start traversal.
      * @return first node without a primary item
      */
     private Node locateNodeWithoutPrimaryItem(Node node)
             throws RepositoryException {
+
+        if (node.getPrimaryNodeType().getPrimaryItemName() == null) {
+            return node;
+        }
+
         NodeIterator nodes = node.getNodes();
         while (nodes.hasNext()) {
-            Node n = nodes.nextNode();
-            if (n.getPrimaryNodeType().getPrimaryItemName() == null) {
+            Node n = locateNodeWithPrimaryItem(nodes.nextNode());
+            if (n != null) {
                 return n;
-            } else {
-                Node returnedNode = locateNodeWithoutPrimaryItem(n);
-                if (n != returnedNode) {
-                    return returnedNode;
-                }
             }
         }
-        // no node with primary item found - return passed node
-        return node;
+        return null;
     }
 
     /**
-     * Returns the first descendant of <code>node</code> which has same
-     * name siblings.
+     * Returns the first descendant of <code>node</code> which has same name
+     * siblings.
+     *
      * @param node <code>Node</code> to start traversal.
      * @return first node with same name siblings
      */
     private Node locateNodeWithSameNameSiblings(Node node)
             throws RepositoryException {
+
         NodeIterator nodes = node.getNodes();
         while (nodes.hasNext()) {
             Node n = nodes.nextNode();
@@ -821,12 +1008,11 @@
                 return n;
             } else {
                 Node returnedNode = locateNodeWithSameNameSiblings(n);
-                if (n != returnedNode) {
+                if (n != null) {
                     return returnedNode;
                 }
             }
         }
-        // no node with same name siblings found - return passed node
-        return node;
+        return null;
     }
 }

Modified: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/PropertyReadMethodsTest.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/PropertyReadMethodsTest.java?view=diff&r1=155899&r2=155900
==============================================================================
--- incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/PropertyReadMethodsTest.java (original)
+++ incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/PropertyReadMethodsTest.java Wed Mar  2 01:06:54 2005
@@ -19,10 +19,17 @@
 import org.apache.jackrabbit.test.AbstractJCRTest;
 import org.apache.jackrabbit.test.NotExecutableException;
 
-import javax.jcr.*;
+import javax.jcr.Session;
+import javax.jcr.Node;
+import javax.jcr.Property;
+import javax.jcr.PropertyIterator;
+import javax.jcr.RepositoryException;
+import javax.jcr.ItemNotFoundException;
+import javax.jcr.ItemVisitor;
+import java.util.NoSuchElementException;
 
 /**
- * Tests general aspects of multi valued properties.
+ * <code>PropertyReadMethodsTest</code>...
  *
  * @test
  * @sources PropertyReadMethodsTest.java
@@ -31,11 +38,20 @@
  */
 public class PropertyReadMethodsTest extends AbstractJCRTest {
 
-    /** A single value property */
-    private Property singleProp;
+    /**
+     * Session to access the workspace
+     */
+    private Session session;
+
+    /**
+     * The root node of the default workspace
+     */
+    private Node rootNode;
 
-    /** A multi value property */
-    private Property multiValProp;
+    /**
+     * A property of the root node
+     */
+    private Property property;
 
     /**
      * Sets up the fixture for this test.
@@ -43,65 +59,163 @@
     protected void setUp() throws Exception {
         isReadOnly = true;
         super.setUp();
-        multiValProp = PropertyUtil.searchMultivalProp(testRootNode);
-        singleProp = testRootNode.getProperty(jcrPrimaryType);
+
+        session = helper.getReadOnlySession();
+        rootNode = session.getRootNode();
+
+        PropertyIterator properties = rootNode.getProperties();
+        try {
+            property = properties.nextProperty();
+        } catch (NoSuchElementException e) {
+            fail("Any node must have at least one property set: jcr:primaryType");
+        }
+
     }
 
     /**
-     * Tests that no null value property exists in a given node tree.
+     * Releases the session aquired in {@link #setUp()}.
      */
-    public void testNoNullValue() throws RepositoryException {
-        assertFalse("Single property with null value found.",
-                PropertyUtil.nullValues(testRootNode));
+    protected void tearDown() throws Exception {
+        if (session != null) {
+            session.logout();
+        }
+        super.tearDown();
     }
 
+    // -----------< tests of methods inherited of Item >------------------------
+
     /**
-     * Tests that all values of a multivalue property have the same property
-     * type.
+     * Tests if getPath() returns the correct path.
      */
-    public void testMultiValueType() throws RepositoryException, NotExecutableException {
-        if (multiValProp != null) {
-            Value[] vals = multiValProp.getValues();
-            if (vals.length > 0) {
-                int type = vals[0].getType();
-                for (int i = 1; i < vals.length; i++) {
-                    assertEquals("Multivalue property has values with different types.",
-                            type, vals[i].getType());
-                }
-            }
-        } else {
-            throw new NotExecutableException("Workspace does not contain a node with a multi valued property");
-        }
+    public void testGetPath()
+            throws NotExecutableException, RepositoryException {
+
+        assertEquals("getPath returns wrong result",
+                "/" + property.getName(),
+                property.getPath());
     }
 
     /**
-     * Tests failure of Property.getValue() method for a multivalue property.
+     * Tests if getName() returns same as last name returned by getPath()
      */
-    public void testGetValue() throws RepositoryException, NotExecutableException {
-        if (multiValProp != null) {
-            try {
-                multiValProp.getValue();
-                fail("Property.getValue() called on a multivalue property " +
-                        "should throw a ValueFormatException.");
-            } catch (ValueFormatException vfe) {
-                // ok
-            }
-        } else {
-            throw new NotExecutableException("Workspace does not contain a node with a multi valued property");
+    public void testGetName() throws RepositoryException {
+        String path = property.getPath();
+        String name = path.substring(path.lastIndexOf("/") + 1);
+        assertEquals("getName() must be the same as the last item in the path",
+                name,
+                property.getName());
+    }
+
+    /**
+     * Test if the ancestor at depth = n, where n is the depth of this
+     * <code>Item</code>, returns this <code>Property</code> itself.
+     *
+     * @throws RepositoryException
+     */
+    public void testGetAncestorOfItemDepth() throws RepositoryException {
+        Property propertyAtDepth = (Property) property.getAncestor(property.getDepth());
+        assertTrue("The ancestor of depth = n, where n is the depth of this " +
+                "Property must be the item itself.", property.isSame(propertyAtDepth));
+    }
+
+    /**
+     * Test if getting the ancestor of depth = n, where n is greater than depth
+     * of this <code>Property</code>, throws an <code>ItemNotFoundException</code>.
+     *
+     * @throws RepositoryException
+     */
+    public void testGetAncestorOfGreaterDepth() throws RepositoryException {
+        try {
+            int greaterDepth = property.getDepth() + 1;
+            property.getAncestor(greaterDepth);
+            fail("Getting ancestor of depth n, where n is greater than depth of" +
+                    "this Property must throw an ItemNotFoundException");
+        } catch (ItemNotFoundException e) {
+            // success
         }
     }
 
     /**
-     * Tests failure of Property.getValues() method for a single value
-     * property.
+     * Test if getting the ancestor of negative depth throws an
+     * <code>ItemNotFoundException</code>.
+     *
+     * @throws RepositoryException
      */
-    public void testGetValues() throws RepositoryException {
+    public void testGetAncestorOfNegativeDepth() throws RepositoryException {
         try {
-            singleProp.getValues();
-            fail("Property.getValues() called on a single property " +
-                    "should throw a ValueFormatException.");
-        } catch (ValueFormatException vfe) {
-            // ok
+            property.getAncestor(-1);
+            fail("Getting ancestor of depth < 0 must throw an ItemNotFoundException.");
+        } catch (ItemNotFoundException e) {
+            // success
         }
     }
+
+    /**
+     * Tests if getParent() returns parent node
+     */
+    public void testGetParent() throws RepositoryException {
+        assertTrue("getParent() of a property must return the parent node.",
+                rootNode.isSame(property.getParent()));
+    }
+
+    /**
+     * Tests if depth of a property of root is 1
+     */
+    public void testGetDepth() throws RepositoryException {
+        assertEquals("getDepth() of a property of root must be 1", 1,
+                property.getDepth());
+    }
+
+    /**
+     * Tests if getSession() is same as through which the Property was acquired
+     */
+    public void testGetSession() throws RepositoryException {
+        assertSame("getSession must return the Session through which " +
+                "the Property was acquired.",
+                property.getSession(),
+                session);
+    }
+
+    /**
+     * Tests if isNode() returns false
+     */
+    public void testIsNode() {
+        assertFalse("isNode() must return false.",
+                property.isNode());
+    }
+
+    /**
+     * Tests if isSame() returns true when retrieving a property through
+     * different sessions
+     */
+    public void testIsSame() throws RepositoryException {
+        // access same property through different session
+        PropertyIterator properties = helper.getReadOnlySession().getRootNode().getProperties();
+        Property otherProperty = properties.nextProperty();
+        assertTrue("isSame must return true for the same " +
+                "property retrieved through different sessions.",
+                property.isSame(otherProperty));
+    }
+
+    /**
+     * Tests if a Property calls the correct visit method on an {@link
+     * ItemVisitor}.
+     */
+    public void testAccept() throws RepositoryException {
+        final Property p = property;
+
+        ItemVisitor itemVisitor = new ItemVisitor() {
+            public void visit(Property property) {
+                assertTrue("Visited Property is not the same as the one returned by visit(Property).",
+                        p.isSame(property));
+            }
+
+            public void visit(Node node) {
+                fail("Wrong accept method executed.");
+            }
+        };
+
+        p.accept(itemVisitor);
+    }
+
 }

Modified: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/PropertyUtil.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/PropertyUtil.java?view=diff&r1=155899&r2=155900
==============================================================================
--- incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/PropertyUtil.java (original)
+++ incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/PropertyUtil.java Wed Mar  2 01:06:54 2005
@@ -35,15 +35,107 @@
  * This class provides various utility methods that are used by the property
  * test cases.
  */
-class PropertyUtil {
+public class PropertyUtil {
 
-    private static final Pattern PATH_PATTERN = Pattern.compile("(\\.)|(\\.\\.)|(([^ /:\\[\\]*'\"|](?:[^/:\\[\\]*'\"|]*[^ /:\\[\\]*'\"|])?):)?([^ /:\\[\\]*'\"|](?:[^/:\\[\\]*'\"|]*[^ /:\\[\\]*'\"|])?)(\\[([1-9]\\d*)\\])?");
-    private static final Pattern NAME_PATTERN = Pattern.compile("(([^ /:\\[\\]*'\"|](?:[^/:\\[\\]*'\"|]*[^ /:\\[\\]*'\"|])?):)?([^ /:\\[\\]*'\"|](?:[^/:\\[\\]*'\"|]*[^ /:\\[\\]*'\"|])?)");
 
-    private static final String dateFormatPattern = "[0-9][0-9][0-9][0-9]-(0[1-9]|1[0-2])-(0[1-9]|1[0-9]||2[0-9]|3[01])T([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9].[0-9][0-9][0-9](Z|[+-]([0-1][0-9]|2[0-3]):[0-5][0-9])";
-    private static final Pattern DATE_PATTERN = Pattern.compile(dateFormatPattern);
+    public static final	String BASE_CHAR =
+        "\\u0041-\\u005A|\\u0061-\\u007A|\\u00C0-\\u00D6|\\u00D8-\\u00F6|\\u00F8-\\u00FF|\\u0100-\\u0131|\\u0134-\\u013E|" +
+        "\\u0141-\\u0148|\\u014A-\\u017E|\\u0180-\\u01C3|\\u01CD-\\u01F0|\\u01F4-\\u01F5|\\u01FA-\\u0217|\\u0250-\\u02A8|" +
+        "\\u02BB-\\u02C1|\\u0386|\\u0388-\\u038A|\\u038C|\\u038E-\\u03A1|\\u03A3-\\u03CE|\\u03D0-\\u03D6|\\u03DA|\\u03DC|" +
+        "\\u03DE|\\u03E0|\\u03E2-\\u03F3|\\u0401-\\u040C|\\u040E-\\u044F|\\u0451-\\u045C|\\u045E-\\u0481|\\u0490-\\u04C4|" +
+        "\\u04C7-\\u04C8|\\u04CB-\\u04CC|\\u04D0-\\u04EB|\\u04EE-\\u04F5|\\u04F8-\\u04F9|\\u0531-\\u0556|\\u0559|" +
+        "\\u0561-\\u0586|\\u05D0-\\u05EA|\\u05F0-\\u05F2|\\u0621-\\u063A|\\u0641-\\u064A|\\u0671-\\u06B7|\\u06BA-\\u06BE|" +
+        "\\u06C0-\\u06CE|\\u06D0-\\u06D3|\\u06D5|\\u06E5-\\u06E6|\\u0905-\\u0939|\\u093D|\\u0958-\\u0961|\\u0985-\\u098C|" +
+        "\\u098F-\\u0990|\\u0993-\\u09A8|\\u09AA-\\u09B0|\\u09B2|\\u09B6-\\u09B9|\\u09DC-\\u09DD|\\u09DF-\\u09E1|" +
+        "\\u09F0-\\u09F1|\\u0A05-\\u0A0A|\\u0A0F-\\u0A10|\\u0A13-\\u0A28|\\u0A2A-\\u0A30|\\u0A32-\\u0A33|\\u0A35-\\u0A36|" +
+        "\\u0A38-\\u0A39|\\u0A59-\\u0A5C|\\u0A5E|\\u0A72-\\u0A74|\\u0A85-\\u0A8B|\\u0A8D|\\u0A8F-\\u0A91|\\u0A93-\\u0AA8|" +
+        "\\u0AAA-\\u0AB0|\\u0AB2-\\u0AB3|\\u0AB5-\\u0AB9|\\u0ABD|\\u0AE0|\\u0B05-\\u0B0C|\\u0B0F-\\u0B10|\\u0B13-\\u0B28|" +
+        "\\u0B2A-\\u0B30|\\u0B32-\\u0B33|\\u0B36-\\u0B39|\\u0B3D|\\u0B5C-\\u0B5D|\\u0B5F-\\u0B61|\\u0B85-\\u0B8A|" +
+        "\\u0B8E-\\u0B90|\\u0B92-\\u0B95|\\u0B99-\\u0B9A|\\u0B9C|\\u0B9E-\\u0B9F|\\u0BA3-\\u0BA4|\\u0BA8-\\u0BAA|" +
+        "\\u0BAE-\\u0BB5|\\u0BB7-\\u0BB9|\\u0C05-\\u0C0C|\\u0C0E-\\u0C10|\\u0C12-\\u0C28|\\u0C2A-\\u0C33|\\u0C35-\\u0C39|" +
+        "\\u0C60-\\u0C61|\\u0C85-\\u0C8C|\\u0C8E-\\u0C90|\\u0C92-\\u0CA8|\\u0CAA-\\u0CB3|\\u0CB5-\\u0CB9|\\u0CDE|" +
+        "\u0CE0-\\u0CE1|\\u0D05-\\u0D0C|\\u0D0E-\\u0D10|\\u0D12-\\u0D28|\\u0D2A-\\u0D39|\\u0D60-\\u0D61|\\u0E01-\\u0E2E|" +
+        "\\u0E30|\\u0E32-\\u0E33|\\u0E40-\\u0E45|\\u0E81-\\u0E82|\\u0E84|\\u0E87-\\u0E88|\\u0E8A|\\u0E8D|\\u0E94-\\u0E97|" +
+        "\\u0E99-\\u0E9F|\\u0EA1-\\u0EA3|\\u0EA5|\\u0EA7|\\u0EAA-\\u0EAB|\\u0EAD-\\u0EAE|\\u0EB0|\\u0EB2-\\u0EB3|\\u0EBD|" +
+        "\\u0EC0-\\u0EC4|\\u0F40-\\u0F47|\\u0F49-\\u0F69|\\u10A0-\\u10C5|\\u10D0-\\u10F6|\\u1100|\\u1102-\\u1103|" +
+        "\\u1105-\\u1107|\\u1109|\\u110B-\\u110C|\\u110E-\\u1112|\\u113C|\\u113E|\\u1140|\\u114C|\\u114E|\\u1150|" +
+        "\\u1154-\\u1155|\\u1159|\\u115F-\\u1161|\\u1163|\\u1165|\\u1167|\\u1169|\\u116D-\\u116E|\\u1172-\\u1173|\\u1175|" +
+        "\\u119E|\\u11A8|\\u11AB|\\u11AE-\\u11AF|\\u11B7-\\u11B8|\\u11BA|\\u11BC-\\u11C2|\\u11EB|\\u11F0|\\u11F9|" +
+        "\\u1E00-\\u1E9B|\\u1EA0-\\u1EF9|\\u1F00-\\u1F15|\\u1F18-\\u1F1D|\\u1F20-\\u1F45|\\u1F48-\\u1F4D|\\u1F50-\\u1F57|" +
+        "\\u1F59|\\u1F5B|\\u1F5D|\\u1F5F-\\u1F7D|\\u1F80-\\u1FB4|\\u1FB6-\\u1FBC|\\u1FBE|\\u1FC2-\\u1FC4|\\u1FC6-\\u1FCC|" +
+        "\\u1FD0-\\u1FD3|\\u1FD6-\\u1FDB|\\u1FE0-\\u1FEC|\\u1FF2-\\u1FF4|\\u1FF6-\\u1FFC|\\u2126|\\u212A-\\u212B|\\u212E|" +
+        "\\u2180-\\u2182|\\u3041-\\u3094|\\u30A1-\\u30FA|\\u3105-\\u312C|\\uAC00-\\uD7A3";
+
+    public static final String IDEOGRAPHIC = "\\u4E00-\\u9FA5|\\u3007|\\u3021-\\u3029";
+
+    public static final String COMBINING_CHAR =
+        "\\u0300-\\u0345|\\u0360-\\u0361|\\u0483-\\u0486|\\u0591-\\u05A1|\\u05A3-\\u05B9|\\u05BB-\\u05BD|\\u05BF|" +
+        "\\u05C1-\\u05C2|\\u05C4|\\u064B-\\u0652|\\u0670|\\u06D6-\\u06DC|\\u06DD-\\u06DF|\\u06E0-\\u06E4|\\u06E7-\\u06E8|" +
+        "\\u06EA-\\u06ED|\\u0901-\\u0903|\\u093C|\\u093E-\\u094C|\\u094D|\\u0951-\\u0954|\\u0962-\\u0963|\\u0981-\\u0983|" +
+        "\\u09BC|\\u09BE|\\u09BF|\\u09C0-\\u09C4|\\u09C7-\\u09C8|\\u09CB-\\u09CD|\\u09D7|\\u09E2-\\u09E3|\\u0A02|\\u0A3C|" +
+        "\\u0A3E|\\u0A3F|\\u0A40-\\u0A42|\\u0A47-\\u0A48|\\u0A4B-\\u0A4D|\\u0A70-\\u0A71|\\u0A81-\\u0A83|\\u0ABC|" +
+        "\\u0ABE-\\u0AC5|\\u0AC7-\\u0AC9|\\u0ACB-\\u0ACD|\\u0B01-\\u0B03|\\u0B3C|\\u0B3E-\\u0B43|\\u0B47-\\u0B48|" +
+        "\\u0B4B-\\u0B4D|\\u0B56-\\u0B57|\\u0B82-\\u0B83|\\u0BBE-\\u0BC2|\\u0BC6-\\u0BC8|\\u0BCA-\\u0BCD|\\u0BD7|" +
+        "\\u0C01-\\u0C03|\\u0C3E-\\u0C44|\\u0C46-\\u0C48|\\u0C4A-\\u0C4D|\\u0C55-\\u0C56|\\u0C82-\\u0C83|\\u0CBE-\\u0CC4|" +
+        "\\u0CC6-\\u0CC8|\\u0CCA-\\u0CCD|\\u0CD5-\\u0CD6|\\u0D02-\\u0D03|\\u0D3E-\\u0D43|\\u0D46-\\u0D48|\\u0D4A-\\u0D4D|" +
+        "\\u0D57|\\u0E31|\\u0E34-\\u0E3A|\\u0E47-\\u0E4E|\\u0EB1|\\u0EB4-\\u0EB9|\\u0EBB-\\u0EBC|\\u0EC8-\\u0ECD|" +
+        "\\u0F18-\\u0F19|\\u0F35|\\u0F37|\\u0F39|\\u0F3E|\\u0F3F|\\u0F71-\\u0F84|\\u0F86-\\u0F8B|\\u0F90-\\u0F95|\\u0F97|" +
+        "\\u0F99-\\u0FAD|\\u0FB1-\\u0FB7|\\u0FB9|\\u20D0-\\u20DC|\\u20E1|\\u302A-\\u302F|\\u3099|\\u309A";
+    
+    public static final String DIGIT = 
+        "\\u0030-\\u0039|\\u0660-\\u0669|\\u06F0-\\u06F9|\\u0966-\\u096F|\\u09E6-\\u09EF|\\u0A66-\\u0A6F|\\u0AE6-\\u0AEF|" +
+        "\\u0B66-\\u0B6F|\\u0BE7-\\u0BEF|\\u0C66-\\u0C6F|\\u0CE6-\\u0CEF|\\u0D66-\\u0D6F|\\u0E50-\\u0E59|\\u0ED0-\\u0ED9|" +
+        "\\u0F20-\\u0F29";
+
+    public static final String EXTENDER =
+        "\\u00B7|\\u02D0|\\u02D1|\\u0387|\\u0640|\\u0E46|\\u0EC6|\\u3005|\\u3031-\\u3035|\\u309D-\\u309E|\\u30FC-\\u30FE";
+
+    /*
+    name's prefix must be a valid xml name:
+    http://www.w3.org/TR/REC-xml-names
+    [4] 	NCName 	 ::= 	(Letter | '_') (NCNameChar)*	  // An XML Name, minus the ":"
+    [5] 	NCNameChar 	::= 	Letter | Digit | '.' | '-' | '_' | CombiningChar | Extender
+    [84]   	Letter	   ::=   	BaseChar | Ideographic
+    */
+
+    public static final String LETTER = BASE_CHAR + "|" + IDEOGRAPHIC;
+
+    public static final String NC_NAME =
+        "[" + LETTER + "|_]" +
+        "[" +  LETTER + "|" + DIGIT + "|.|\\-|_|" + COMBINING_CHAR + "|" + EXTENDER +"]*";
+
+    public static final String SIMPLENAME_CHAR =
+        "[^/:\\[\\]\\*'\"\\s]";
+    
+    public static final String PATTERNSTRING_NAME =
+        "((" + NC_NAME + "):)?" + // prefix
+        SIMPLENAME_CHAR + "([" + SIMPLENAME_CHAR + "| ]*" + SIMPLENAME_CHAR +")?";
+
+    public static final Pattern PATTERN_NAME = Pattern.compile(PATTERNSTRING_NAME);
+
+    public static final String PATTERNSTRING_PATH_ELEMENT =
+       PATTERNSTRING_NAME +
+       "(\\[[1-9]\\d*\\])?";
+
+    public static final String PATTERNSTRING_PATH_WITHOUT_LAST_SLASH =
+        "(\\./|\\.\\./|/)?" +
+        "(" + PropertyUtil.PATTERNSTRING_PATH_ELEMENT + "/)*" +
+        PropertyUtil.PATTERNSTRING_PATH_ELEMENT;
+
+    public static final String PATTERNSTRING_PATH =
+        PATTERNSTRING_PATH_WITHOUT_LAST_SLASH + "/?";
+    public static final Pattern PATTERN_PATH = Pattern.compile(PATTERNSTRING_PATH);
+
+    public static final String PATTERNSTRING_DATE =
+        "[0-9][0-9][0-9][0-9]-(0[1-9]|1[0-2])-(0[1-9]|1[0-9]||2[0-9]|3[01])" +
+        "T([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9].[0-9][0-9][0-9]" +
+        "(Z|[+-]([0-1][0-9]|2[0-3]):[0-5][0-9])";
+    public static final Pattern PATTERN_DATE = Pattern.compile(PATTERNSTRING_DATE);
+
+    public static final String PATTERNSTRING_UUID =
+        "\\p{XDigit}{8}-\\p{XDigit}{4}-\\p{XDigit}{4}-\\p{XDigit}{4}-\\p{XDigit}{12}";
+    public static final Pattern PATTERN_UUID = Pattern.compile(PATTERNSTRING_UUID);
 
-    private static final Pattern UUID_PATTERN = Pattern.compile("\\p{XDigit}{8}-\\p{XDigit}{4}-\\p{XDigit}{4}-\\p{XDigit}{4}-\\p{XDigit}{12}");
 
     /**
      * Private constructor to disable instantiation.
@@ -121,7 +213,7 @@
             NamespaceRegistry nsr = session.getWorkspace().getNamespaceRegistry();
             boolean prefixOk = true;
             // validate name element
-            Matcher matcher = NAME_PATTERN.matcher(name);
+            Matcher matcher = PATTERN_NAME.matcher(name);
             // validate namespace prefixes if present
             String[] split = name.split(":");
             if (split.length > 1) {
@@ -155,7 +247,7 @@
             for (int i = jcrPath.startsWith("/") ? 1 : 0; i < elems.length; i++) {
                 // validate path element
                 String elem = elems[i];
-                Matcher matcher = PATH_PATTERN.matcher(elem);
+                Matcher matcher = PATTERN_PATH.matcher(elem);
                 match = matcher.matches();
                 if (!match) {
                     break;
@@ -183,7 +275,7 @@
      * @return <code>true</code> if <code>str</code> is a valid date format.
      */
     public static boolean isDateFormat(String str) {
-        return DATE_PATTERN.matcher(str).matches();
+        return PATTERN_DATE.matcher(str).matches();
     }
 
     /**
@@ -193,7 +285,7 @@
      * @return <code>true</code> if <code>str</code> is a UUID.
      */
     public static boolean isUUID(String str) {
-        return UUID_PATTERN.matcher(str).matches();
+        return PATTERN_UUID.matcher(str).matches();
     }
 
     /**
@@ -305,4 +397,7 @@
         }
         return multiVal;
     }
+
+
+
 }

Modified: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/RootNodeTest.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/RootNodeTest.java?view=diff&r1=155899&r2=155900
==============================================================================
--- incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/RootNodeTest.java (original)
+++ incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/RootNodeTest.java Wed Mar  2 01:06:54 2005
@@ -21,6 +21,7 @@
 import javax.jcr.Node;
 import javax.jcr.RepositoryException;
 import javax.jcr.ItemNotFoundException;
+import javax.jcr.Session;
 
 /**
  * Test cases for the root node.
@@ -32,12 +33,30 @@
  */
 public class RootNodeTest extends AbstractJCRTest {
 
-    Node rootNode;
+    /** A readonly session for the default workspace */
+    private Session session;
 
+    /** The root node of the default workspace */
+    private Node rootNode;
+
+    /**
+     * Sets up the fixture for the test cases.
+     */
     protected void setUp() throws Exception {
         isReadOnly = true;
         super.setUp();
-        rootNode = helper.getReadOnlySession().getRootNode();
+        session = helper.getReadOnlySession();
+        rootNode = session.getRootNode();
+    }
+
+    /**
+     * Releases the session aquired in {@link #setUp()}.
+     */
+    protected void tearDown() throws Exception {
+        if (session != null) {
+            session.logout();
+        }
+        super.tearDown();
     }
 
     /**

Modified: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/TestAll.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/TestAll.java?view=diff&r1=155899&r2=155900
==============================================================================
--- incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/TestAll.java (original)
+++ incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/TestAll.java Wed Mar  2 01:06:54 2005
@@ -38,14 +38,12 @@
 
         // ADD TEST CLASSES HERE:
         suite.addTestSuite(AddNodeTest.class);
-        suite.addTestSuite(GetAncestorTest.class);
         suite.addTestSuite(RootNodeTest.class);
         suite.addTestSuite(NamespaceRegistryTest.class);
         suite.addTestSuite(ReferencesTest.class);
-        suite.addTestSuite(ItemDefTest.class);
-        suite.addTestSuite(ItemReadMethodsTest.class);
         suite.addTestSuite(NodeReadMethodsTest.class);
         suite.addTestSuite(PropertyTypeTest.class);
+        suite.addTestSuite(NodeDiscoveringNodeTypesTest.class);
 
         suite.addTestSuite(BinaryPropertyTest.class);
         suite.addTestSuite(BooleanPropertyTest.class);

Added: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/nodetype/NodeDefTest.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/nodetype/NodeDefTest.java?view=auto&rev=155900
==============================================================================
--- incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/nodetype/NodeDefTest.java (added)
+++ incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/nodetype/NodeDefTest.java Wed Mar  2 01:06:54 2005
@@ -0,0 +1,301 @@
+/*
+ * 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.test.api.nodetype;
+
+import org.apache.jackrabbit.test.AbstractJCRTest;
+import org.apache.jackrabbit.test.NotExecutableException;
+
+import javax.jcr.Session;
+import javax.jcr.Node;
+import javax.jcr.RepositoryException;
+import javax.jcr.PathNotFoundException;
+import javax.jcr.NodeIterator;
+import javax.jcr.nodetype.NodeTypeManager;
+import javax.jcr.nodetype.NodeTypeIterator;
+import javax.jcr.nodetype.NodeType;
+import javax.jcr.nodetype.NodeDef;
+
+/**
+ * Tests if node definitions are respected in node instances in the workspace.
+ *
+ * @test
+ * @sources NodeDefTest.java
+ * @executeClass org.apache.jackrabbit.test.api.NodeDefTest
+ * @keywords level1
+ */
+public class NodeDefTest extends AbstractJCRTest {
+
+    /**
+     * The session we use for the tests
+     */
+    private Session session;
+
+    /**
+     * The node type manager of the session
+     */
+    private NodeTypeManager manager;
+
+    /**
+     * The root node of the default workspace
+     */
+    private Node rootNode;
+
+    /**
+     * If <code>true</code> indicates that the test found a mandatory node
+     */
+    private boolean foundMandatoryNode = false;
+
+    /**
+     * Sets up the fixture for the test cases.
+     */
+    protected void setUp() throws Exception {
+        isReadOnly = true;
+        super.setUp();
+
+        session = helper.getReadOnlySession();
+        manager = session.getWorkspace().getNodeTypeManager();
+        rootNode = session.getRootNode();
+    }
+
+    /**
+     * Releases the session aquired in {@link #setUp()}.
+     */
+    protected void tearDown() throws Exception {
+        if (session != null) {
+            session.logout();
+        }
+        super.tearDown();
+    }
+
+    /**
+     * Test getDeclaringNodeType() returns the node type which is defining the
+     * requested child node def. Test runs for all existing node types.
+     */
+    public void testGetDeclaringNodeType()
+            throws RepositoryException {
+
+        NodeTypeIterator types = manager.getAllNodeTypes();
+        // loop all node types
+        while (types.hasNext()) {
+            NodeType currentType = types.nextNodeType();
+            NodeDef defsOfCurrentType[] =
+                    currentType.getChildNodeDefs();
+
+            // loop all child node defs of each node type
+            for (int i = 0; i < defsOfCurrentType.length; i++) {
+                NodeDef def = defsOfCurrentType[i];
+                NodeType type = def.getDeclaringNodeType();
+
+                // check if def is part of the child node defs of the
+                // declaring node type
+                NodeDef defs[] = type.getChildNodeDefs();
+                boolean hasType = false;
+                for (int j = 0; j < defs.length; j++) {
+                    if (defs[j].getName().equals(def.getName())) {
+                        hasType = true;
+                        break;
+                    }
+                }
+                assertTrue("getDeclaringNodeType() must return the node " +
+                        "which defines the corresponding child node def.",
+                        hasType);
+            }
+        }
+    }
+
+
+    /**
+     * Tests if auto create nodes are not a residual set definition (getName()
+     * does not return "*")
+     */
+    public void testIsAutoCreate()
+            throws RepositoryException {
+
+        NodeTypeIterator types = manager.getAllNodeTypes();
+        // loop all node types
+        while (types.hasNext()) {
+            NodeType type = types.nextNodeType();
+            NodeDef defs[] = type.getChildNodeDefs();
+            for (int i = 0; i < defs.length; i++) {
+                if (defs[i].isAutoCreate()) {
+                    assertFalse("An auto create node must not be a " +
+                            "residual set definition.",
+                            defs[i].getName().equals("*"));
+                }
+            }
+        }
+    }
+
+
+    /**
+     * This test checks if item definitions with mandatory constraints are
+     * respected.
+     * <p/>
+     * If the default workspace does not contain a node with a node type
+     * definition that specifies a mandatory child node a {@link
+     * org.apache.jackrabbit.test.NotExecutableException} is thrown.
+     */
+    public void testIsMandatory() throws RepositoryException, NotExecutableException {
+        traverse(rootNode);
+        if (!foundMandatoryNode) {
+            throw new NotExecutableException("Workspace does not contain any node with a mandatory child node definition");
+        }
+    }
+
+
+    /**
+     * Tests if getRequiredPrimaryTypes() does not return an empty array. Test
+     * runs for all existing node types.
+     */
+    public void testGetRequiredPrimaryTypes()
+            throws RepositoryException {
+
+        NodeTypeIterator types = manager.getAllNodeTypes();
+        // loop all node types
+        while (types.hasNext()) {
+            NodeType type = types.nextNodeType();
+            NodeDef defs[] = type.getChildNodeDefs();
+
+            for (int i = 0; i < defs.length; i++) {
+                assertTrue("getRequiredPrimaryTypes() must never return an " +
+                        "empty array.",
+                        defs[i].getRequiredPrimaryTypes().length > 0);
+            }
+        }
+    }
+
+
+    /**
+     * Tests if the default primary type is of the same or a sub node type as the
+     * the required primary types. Test runs for all existing node types.
+     */
+    public void testGetDefaultPrimaryTypes()
+            throws RepositoryException {
+
+        NodeTypeIterator types = manager.getAllNodeTypes();
+        // loop all node types
+        while (types.hasNext()) {
+            NodeType type = types.nextNodeType();
+            NodeDef defs[] = type.getChildNodeDefs();
+
+            for (int i = 0; i < defs.length; i++) {
+
+                NodeDef def = defs[i];
+                NodeType defaultType = def.getDefaultPrimaryType();
+                if (defaultType != null) {
+
+                    NodeType requiredTypes[] =
+                            def.getRequiredPrimaryTypes();
+
+                    for (int j = 0; j < requiredTypes.length; j++) {
+                        NodeType requiredType = requiredTypes[j];
+
+                        boolean isSubType = false;
+                        isSubType = compareWithRequiredType(requiredType,
+                                defaultType);
+
+                        assertTrue("The NodeType returned by " +
+                                "getDefaultPrimaryType or one of its " +
+                                "supertypes must match all NodeTypes " +
+                                "returned by getRequiredPrimaryTypes()",
+                                isSubType);
+                    }
+                }
+            }
+        }
+    }
+
+
+    //-----------------------< internal >---------------------------------------
+
+    /**
+     * Traverses the node hierarchy and applies
+     * {@link #checkMandatoryConstraint(javax.jcr.Node, javax.jcr.nodetype.NodeType)}
+     * to all descendant nodes of <code>parentNode</code>.
+     */
+    private void traverse(Node parentNode)
+            throws RepositoryException {
+
+        NodeIterator nodes = parentNode.getNodes();
+        while (nodes.hasNext()) {
+            Node node = nodes.nextNode();
+
+            NodeType primaryType = node.getPrimaryNodeType();
+            checkMandatoryConstraint(node, primaryType);
+
+            NodeType mixins[] = node.getMixinNodeTypes();
+            for (int i = 0; i < mixins.length; i++) {
+                checkMandatoryConstraint(node, mixins[i]);
+            }
+
+            traverse(node);
+        }
+    }
+
+
+    /**
+     * Checks if mandatory node definitions are respected.
+     */
+    private void checkMandatoryConstraint(Node node, NodeType type)
+            throws RepositoryException {
+
+        // test if node contains all mandatory nodes of current type
+        NodeDef nodeDefs[] = type.getChildNodeDefs();
+        for (int i = 0; i < nodeDefs.length; i++) {
+            NodeDef nodeDef = nodeDefs[i];
+            if (nodeDef.isMandatory()) {
+                foundMandatoryNode = true;
+                try {
+                    node.getNode(nodeDef.getName());
+                } catch (PathNotFoundException e) {
+                    fail("Mandatory child " + nodeDef.getName() + " for " +
+                            node.getPath() + " does not exist.");
+                }
+            }
+        }
+    }
+
+
+    /**
+     * Returns true if defaultType or one of its supertypes is of the same
+     * NodeType as requiredType.
+     *
+     * @param requiredType: one of the required primary types of a NodeDef
+     * @param defaultType:  the default primary type of a NodeDef
+     */
+    private boolean compareWithRequiredType(NodeType requiredType,
+                                            NodeType defaultType) {
+
+        // if (defaultType == requiredType) return true;
+        // rather use:
+        if (defaultType.getName().equals(requiredType.getName())) {
+            return true;
+        }
+
+        NodeType superTypes[] = defaultType.getSupertypes();
+        for (int i = 0; i < superTypes.length; i++) {
+            // if (superTypes[i] == requiredType) return true;
+            // rather use:
+            if (superTypes[i].getName().equals(requiredType.getName())) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+
+}
\ No newline at end of file

Propchange: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/nodetype/NodeDefTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/nodetype/NodeTypeManagerTest.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/nodetype/NodeTypeManagerTest.java?view=auto&rev=155900
==============================================================================
--- incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/nodetype/NodeTypeManagerTest.java (added)
+++ incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/nodetype/NodeTypeManagerTest.java Wed Mar  2 01:06:54 2005
@@ -0,0 +1,140 @@
+/*
+ * 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.test.api.nodetype;
+
+import org.apache.jackrabbit.test.AbstractJCRTest;
+
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+import javax.jcr.nodetype.NodeTypeManager;
+import javax.jcr.nodetype.NodeTypeIterator;
+import javax.jcr.nodetype.NodeType;
+import javax.jcr.nodetype.NoSuchNodeTypeException;
+
+/**
+ * Tests if the {@link NodeTypeManager} properly returns primary types an mixin
+ * types.
+ *
+ * @test
+ * @sources NodeTypeManagerTest.java
+ * @executeClass org.apache.jackrabbit.test.api.NodeTypeManagerTest
+ * @keywords level1
+ */
+public class NodeTypeManagerTest extends AbstractJCRTest {
+
+    /**
+     * The node type manager we use for the test cases
+     */
+    private NodeTypeManager manager;
+
+    /**
+     * The session for reading from the default workspace
+     */
+    private Session session;
+
+    /**
+     * Sets up the fixture for the test cases.
+     */
+    protected void setUp() throws Exception {
+        isReadOnly = true;
+        super.setUp();
+
+        session = helper.getReadOnlySession();
+        manager = session.getWorkspace().getNodeTypeManager();
+    }
+
+    /**
+     * Releases the session aquired in {@link #setUp()}.
+     */
+    protected void tearDown() throws Exception {
+        if (session != null) {
+            session.logout();
+        }
+        super.tearDown();
+    }
+
+    /**
+     * Test if getNodeType(String nodeTypeName) returns the expected NodeType and
+     * if a NoSuchTypeException is thrown if no according node type is existing
+     */
+    public void testGetNodeType() throws RepositoryException {
+        NodeType type = manager.getAllNodeTypes().nextNodeType();
+        assertEquals("getNodeType(String nodeTypeName) does not return correct " +
+                "NodeType",
+                manager.getNodeType(type.getName()).getName(),
+                type.getName());
+
+
+        StringBuffer notExistingName = new StringBuffer("X");
+        NodeTypeIterator types = manager.getAllNodeTypes();
+        while (types.hasNext()) {
+            // build a name which is for sure not existing
+            // (":" of namespace prefix will be replaced later on)
+            notExistingName.append(types.nextNodeType().getName());
+        }
+        try {
+            manager.getNodeType(notExistingName.toString().replaceAll(":", ""));
+            fail("getNodeType(String nodeTypeName) must throw a " +
+                    "NoSuchNodeTypeException if no according NodeType " +
+                    "does exist");
+        } catch (NoSuchNodeTypeException e) {
+            // success
+        }
+    }
+
+
+    /**
+     * Test if getAllNodeTypes() returns all primary and mixin node types
+     */
+    public void testGetAllNodeTypes() throws RepositoryException {
+        long sizeAll = getSize(manager.getAllNodeTypes());
+        long sizePrimary = getSize(manager.getPrimaryNodeTypes());
+        long sizeMixin = getSize(manager.getMixinNodeTypes());
+
+        assertEquals("sizeAll() must return all primary and mixin node types:",
+                sizePrimary + sizeMixin,
+                sizeAll);
+    }
+
+
+    /**
+     * Test if getPrimaryNodeTypes does not return any mixin node types
+     */
+    public void testGetPrimaryNodeTypes() throws RepositoryException {
+        NodeTypeIterator types = manager.getPrimaryNodeTypes();
+        while (types.hasNext()) {
+            assertFalse("getPrimaryNodeTypes() must not return mixin " +
+                    "node types",
+                    types.nextNodeType().isMixin());
+        }
+    }
+
+
+    /**
+     * Test if getMixinNodeTypes does return exclusively mixin node types
+     */
+    public void testGetMixinNodeTypes() throws RepositoryException {
+        NodeTypeIterator types = manager.getMixinNodeTypes();
+        while (types.hasNext()) {
+            assertTrue("getMixinNodeTypes() must return exclusively mixin " +
+                    "node types",
+                    types.nextNodeType().isMixin());
+        }
+    }
+
+
+}
\ No newline at end of file

Propchange: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/nodetype/NodeTypeManagerTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message