Search in sources :

Example 36 with NodeIterator

use of javax.jcr.NodeIterator in project jackrabbit by apache.

the class NodeReadMethodsTest method locateNodeWithReference.

/**
     * Returns the first descendant of <code>node</code> which has a property of
     * type {@link javax.jcr.PropertyType#REFERENCE} set and is <b>not</b>
     * multivalued.
     *
     * @param node <code>Node</code> to start traversal.
     * @return first node with a property of PropertType.REFERENCE
     */
private Node locateNodeWithReference(Node node) throws RepositoryException {
    PropertyIterator properties = node.getProperties();
    while (properties.hasNext()) {
        Property p = properties.nextProperty();
        if (p.getType() == PropertyType.REFERENCE && !p.getDefinition().isMultiple()) {
            return node;
        }
    }
    NodeIterator nodes = node.getNodes();
    while (nodes.hasNext()) {
        Node returnedNode = locateNodeWithReference(nodes.nextNode());
        if (returnedNode != null) {
            return returnedNode;
        }
    }
    return null;
}
Also used : NodeIterator(javax.jcr.NodeIterator) Node(javax.jcr.Node) PropertyIterator(javax.jcr.PropertyIterator) Property(javax.jcr.Property)

Example 37 with NodeIterator

use of javax.jcr.NodeIterator in project jackrabbit by apache.

the class NodeReadMethodsTest method testGetNodesNamePattern.

/**
     * Test getNodes(String namePattern) with all possible patterns. Tested
     * node: root - NotExecutableException is thrown when root node has no sub
     * nodes.
     */
public void testGetNodesNamePattern() throws NotExecutableException, RepositoryException {
    // get root node and build an ArrayList of its sub nodes
    Node node = testRootNode;
    if (!node.hasNodes()) {
        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();
    List<Node> allNodes = new ArrayList<Node>();
    while (allNodesIt.hasNext()) {
        Node n = allNodesIt.nextNode();
        allNodes.add(n);
    }
    // test if an empty NodeIterator is returned
    // when the pattern is not matching any child node
    String pattern0 = "";
    NodeIterator nodes0 = node.getNodes(pattern0);
    try {
        nodes0.nextNode();
        fail("An empty NodeIterator must be returned if pattern does" + "not match any child node.");
    } catch (NoSuchElementException e) {
    // success
    }
    // all further tests are using root's first sub node
    Node firstNode = allNodes.get(0);
    // test pattern "*"
    String pattern1 = "*";
    String assertString1 = "node.getNodes(\"" + pattern1 + "\"): ";
    NodeIterator nodes1 = node.getNodes(pattern1);
    // test if the number of found nodes is correct
    assertEquals(assertString1 + "number of nodes found: ", allNodes.size(), getSize(nodes1));
    // test pattern "nodeName"
    String pattern2 = firstNode.getName();
    String assertString2 = "node.getNodes(\"" + pattern2 + "\"): ";
    // test if the names of the found nodes are matching the pattern
    NodeIterator nodes2 = node.getNodes(pattern2);
    while (nodes2.hasNext()) {
        Node n = nodes2.nextNode();
        assertEquals(assertString2 + "name comparison failed: ", firstNode.getName(), n.getName());
    }
    // test if the number of found nodes is correct
    int numExpected2 = 0;
    for (int i = 0; i < allNodes.size(); i++) {
        Node n = allNodes.get(i);
        if (n.getName().equals(firstNode.getName())) {
            numExpected2++;
        }
    }
    nodes2 = node.getNodes(pattern2);
    assertEquals(assertString2 + "number of nodes found: ", numExpected2, getSize(nodes2));
    // test pattern "nodeName|nodeName"
    String pattern3 = firstNode.getName() + "|" + firstNode.getName();
    String assertString3 = "node.getNodes(\"" + pattern3 + "\"): ";
    // test if the names of the found nodes are matching the pattern
    NodeIterator nodes3 = node.getNodes(pattern3);
    while (nodes3.hasNext()) {
        Node n = nodes3.nextNode();
        assertEquals(assertString2 + "name comparison failed: ", firstNode.getName(), n.getName());
    }
    // test if the number of found nodes is correct
    int numExpected3 = 0;
    for (int i = 0; i < allNodes.size(); i++) {
        Node n = allNodes.get(i);
        if (n.getName().equals(firstNode.getName())) {
            numExpected3++;
        }
    }
    nodes3 = node.getNodes(pattern3);
    assertEquals(assertString3 + "number of nodes found: ", numExpected3, getSize(nodes3));
    // test pattern "*odeNam*"
    if (firstNode.getName().length() > 2) {
        String name = firstNode.getName();
        String shortenName = name.substring(1, name.length() - 1);
        String pattern4 = "*" + shortenName + "*";
        String assertString4 = "node.getNodes(\"" + pattern4 + "\"): ";
        // test if the names of the found nodes are matching the pattern
        NodeIterator nodes4 = node.getNodes(pattern4);
        while (nodes4.hasNext()) {
            Node n = nodes4.nextNode();
            assertTrue(assertString4 + "name comparison failed: *" + shortenName + "* not found in " + n.getName(), n.getName().indexOf(shortenName) != -1);
        }
        // test if the number of found nodes is correct
        int numExpected4 = 0;
        for (int i = 0; i < allNodes.size(); i++) {
            Node n = allNodes.get(i);
            if (n.getName().indexOf(shortenName) != -1) {
                numExpected4++;
            }
        }
        nodes4 = node.getNodes(pattern4);
        assertEquals(assertString4 + "number of nodes found: ", numExpected4, getSize(nodes4));
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) Node(javax.jcr.Node) ArrayList(java.util.ArrayList) NoSuchElementException(java.util.NoSuchElementException)

Example 38 with NodeIterator

use of javax.jcr.NodeIterator in project jackrabbit by apache.

the class NodeReadMethodsTest method setUp.

/**
     * Sets up the fixture for this test.
     */
protected void setUp() throws Exception {
    isReadOnly = true;
    super.setUp();
    session = getHelper().getReadOnlySession();
    testRootNode = session.getRootNode().getNode(testPath);
    NodeIterator nodes = testRootNode.getNodes();
    try {
        childNode = nodes.nextNode();
    } catch (NoSuchElementException e) {
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) NoSuchElementException(java.util.NoSuchElementException)

Example 39 with NodeIterator

use of javax.jcr.NodeIterator in project jackrabbit by apache.

the class NodeReadMethodsTest method testHasNodes.

/**
     * Test if hasNodes() returns true if any sub node exists or false if not.
     * Tested node: root
     */
public void testHasNodes() throws RepositoryException {
    Node node = testRootNode;
    NodeIterator nodes = node.getNodes();
    int i = 0;
    while (nodes.hasNext()) {
        nodes.nextNode();
        i++;
    }
    if (i == 0) {
        assertFalse("node.hasNodes() returns true although " + "no sub nodes existing", node.hasNodes());
    } else {
        assertTrue("node.hasNodes() returns false althuogh " + "sub nodes are existing", node.hasNodes());
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) Node(javax.jcr.Node)

Example 40 with NodeIterator

use of javax.jcr.NodeIterator in project jackrabbit by apache.

the class NodeReadMethodsTest method testHasNode.

/**
     * Test if hasNode(String relPath) returns true if the required node exists
     * and false if it doesn't. Tested node: root
     */
public void testHasNode() throws NotExecutableException, RepositoryException {
    Node node = testRootNode;
    NodeIterator nodes = node.getNodes();
    StringBuffer notExistingNodeName = new StringBuffer();
    while (nodes.hasNext()) {
        Node n = nodes.nextNode();
        assertTrue("hasNode(String relPath) returns false although " + "node at relPath is existing", node.hasNode(n.getName()));
        notExistingNodeName.append(n.getName() + "X");
    }
    if (notExistingNodeName.toString().equals("")) {
        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 " + "node at relPath is not existing", node.hasNode(notExistingNodeName.toString().replaceAll(":", "")));
}
Also used : NodeIterator(javax.jcr.NodeIterator) NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) Node(javax.jcr.Node)

Aggregations

NodeIterator (javax.jcr.NodeIterator)307 Node (javax.jcr.Node)214 Session (javax.jcr.Session)55 QueryResult (javax.jcr.query.QueryResult)52 RepositoryException (javax.jcr.RepositoryException)40 Query (javax.jcr.query.Query)40 Test (org.junit.Test)36 QueryManager (javax.jcr.query.QueryManager)34 PropertyIterator (javax.jcr.PropertyIterator)30 ArrayList (java.util.ArrayList)26 Property (javax.jcr.Property)24 Version (javax.jcr.version.Version)23 NoSuchElementException (java.util.NoSuchElementException)19 Value (javax.jcr.Value)19 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)19 HashSet (java.util.HashSet)13 PathNotFoundException (javax.jcr.PathNotFoundException)12 JackrabbitSession (org.apache.jackrabbit.api.JackrabbitSession)11 NodeImpl (org.apache.jackrabbit.core.NodeImpl)11 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)11