Search in sources :

Example 61 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 62 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)

Example 63 with NodeIterator

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

the class NodeReadMethodsTest method locateNodeWithSameNameSiblings.

/**
     * 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();
        NodeIterator nodes2 = node.getNodes(n.getName());
        int i = 0;
        while (nodes2.hasNext()) {
            nodes2.next();
            i++;
        }
        if (i > 1) {
            // node has same name siblings
            return n;
        } else {
            Node returnedNode = locateNodeWithSameNameSiblings(n);
            if (returnedNode != null) {
                return returnedNode;
            }
        }
    }
    return null;
}
Also used : NodeIterator(javax.jcr.NodeIterator) Node(javax.jcr.Node)

Example 64 with NodeIterator

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

the class NodeIteratorTest method testGetSize.

/**
     * Tests if {@link javax.jcr.NodeIterator#getSize()} returns the correct
     * size.
     * @throws NotExecutableException if getSize() returns -1 (unavailable).
     */
public void testGetSize() throws RepositoryException, NotExecutableException {
    NodeIterator iter = testRootNode.getNodes();
    long size = testRootNode.getNodes().getSize();
    if (size != -1) {
        long count = 0;
        while (iter.hasNext()) {
            iter.nextNode();
            count++;
        }
        assertEquals("NodeIterator.getSize does not return correct number.", size, count);
    } else {
        throw new NotExecutableException("NodeIterator.getSize() does not return size information.");
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) NotExecutableException(org.apache.jackrabbit.test.NotExecutableException)

Example 65 with NodeIterator

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

the class NodeIteratorTest method testNoSuchElementException.

/**
     * Tests if a {@link java.util.NoSuchElementException} is thrown when {@link
     * javax.jcr.NodeIterator#nextNode()} is called and there are no more nodes
     * available.
     */
public void testNoSuchElementException() throws RepositoryException {
    NodeIterator iter = testRootNode.getNodes();
    while (iter.hasNext()) {
        iter.nextNode();
    }
    try {
        iter.nextNode();
        fail("nextNode() must throw a NoSuchElementException when no nodes are available");
    } catch (NoSuchElementException e) {
    // success
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

NodeIterator (javax.jcr.NodeIterator)309 Node (javax.jcr.Node)216 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