Search in sources :

Example 46 with NodeIterator

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

the class NodeOrderableChildNodesTest method prepareTest.

/**
     * Sets up the test content needed for the test cases.
     */
private void prepareTest() throws RepositoryException {
    // get root node
    Node defaultRootNode = (Node) superuser.getItem(testRootNode.getPath());
    // create testNode
    parentNode = defaultRootNode.addNode(nodeName1, getProperty("nodetype2"));
    // add child node
    Node firstNode = parentNode.addNode(nodeName2, getProperty("nodetype3"));
    // add a second child node
    Node secondNode = parentNode.addNode(nodeName3, getProperty("nodetype3"));
    // save the new nodes
    superuser.save();
    // get child node refs
    NodeIterator it = parentNode.getNodes();
    initialFirstNode = it.nextNode();
    initialSecondNode = it.nextNode();
    // first lets test if the nodes have been added in the right order
    assertTrue("Child nodes are not added in proper order ", firstNode.isSame(initialFirstNode));
    assertTrue("Child nodes are not added in proper order ", secondNode.isSame(initialSecondNode));
}
Also used : NodeIterator(javax.jcr.NodeIterator) Node(javax.jcr.Node)

Example 47 with NodeIterator

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

the class XPathDocOrderTest method docOrderTest.

//-----------------------------< internal >---------------------------------
/**
     * Executes a statement, checks if the Result contains exactly one node with
     * <code>path</code>.
     *
     * @param stmt to be executed
     * @param path the path of the node in the query result.
     */
private void docOrderTest(Statement stmt, String path) throws RepositoryException, NotExecutableException {
    if (!isSupported(Repository.QUERY_XPATH_DOC_ORDER)) {
        throw new NotExecutableException("Repository does not support document order on result set.");
    }
    int count = 0;
    // check precondition: at least 3 nodes
    for (NodeIterator it = testRootNode.getNodes(); it.hasNext(); it.nextNode()) {
        count++;
    }
    if (count < 3) {
        throw new NotExecutableException("Workspace does not contain enough content under: " + testRoot + ". At least 3 nodes are required for this test.");
    }
    QueryResult result = execute(stmt);
    checkResult(result, 1);
    assertEquals("Wrong result node.", path, result.getNodes().nextNode().getPath());
}
Also used : NodeIterator(javax.jcr.NodeIterator) QueryResult(javax.jcr.query.QueryResult) NotExecutableException(org.apache.jackrabbit.test.NotExecutableException)

Example 48 with NodeIterator

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

the class XPathDocOrderTest method testDocOrderLastFunction.

/**
     * Tests the <code>last()</code> function.
     * <p>
     * For configuration description see {@link XPathDocOrderTest}.
     */
public void testDocOrderLastFunction() throws Exception {
    String xpath = xpathRoot + "/*[position()=last()]";
    String resultPath = "";
    for (NodeIterator nodes = testRootNode.getNodes(); nodes.hasNext(); ) {
        resultPath = nodes.nextNode().getPath();
    }
    docOrderTest(new Statement(xpath, qsXPATH), resultPath);
}
Also used : NodeIterator(javax.jcr.NodeIterator)

Example 49 with NodeIterator

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

the class XPathPosIndexTest method docOrderTest.

//-----------------------------< internal >---------------------------------
/**
     * Executes a statement, checks if the Result contains exactly one node with
     * <code>path</code>.
     *
     * @param stmt to be executed
     * @param path the path of the node in the query result.
     */
private void docOrderTest(Statement stmt, String path) throws RepositoryException, NotExecutableException {
    if (!isSupported(Repository.QUERY_XPATH_POS_INDEX)) {
        throw new NotExecutableException("Repository does not support document order on result set.");
    }
    int count = 0;
    // check precondition: at least 3 nodes
    for (NodeIterator it = testRootNode.getNodes(); it.hasNext(); it.nextNode()) {
        count++;
    }
    if (count < 3) {
        throw new NotExecutableException("Workspace does not contain enough content under: " + testRoot + ". At least 3 nodes are required for this test.");
    }
    QueryResult result = execute(stmt);
    checkResult(result, 1);
    assertEquals("Wrong result node.", path, result.getNodes().nextNode().getPath());
}
Also used : NodeIterator(javax.jcr.NodeIterator) QueryResult(javax.jcr.query.QueryResult) NotExecutableException(org.apache.jackrabbit.test.NotExecutableException)

Example 50 with NodeIterator

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

the class NodeImpl method getWeakReferences.

/**
     * {@inheritDoc}
     */
public PropertyIterator getWeakReferences(String name) throws RepositoryException {
    if (name == null) {
        return getWeakReferences();
    }
    // check state of this instance
    sanityCheck();
    // shortcut if node isn't referenceable
    if (!isNodeType(NameConstants.MIX_REFERENCEABLE)) {
        return PropertyIteratorAdapter.EMPTY;
    }
    try {
        StringBuilder stmt = new StringBuilder();
        stmt.append("//*[@").append(ISO9075.encode(name));
        stmt.append(" = '").append(data.getId()).append("']");
        Query q = getSession().getWorkspace().getQueryManager().createQuery(stmt.toString(), Query.XPATH);
        QueryResult result = q.execute();
        ArrayList<Property> l = new ArrayList<Property>();
        for (NodeIterator nit = result.getNodes(); nit.hasNext(); ) {
            Node n = nit.nextNode();
            l.add(n.getProperty(name));
        }
        if (l.isEmpty()) {
            return PropertyIteratorAdapter.EMPTY;
        } else {
            return new PropertyIteratorAdapter(l);
        }
    } catch (RepositoryException e) {
        String msg = "Unable to retrieve WEAKREFERENCE properties that refer to " + id;
        log.debug(msg);
        throw new RepositoryException(msg, e);
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) QueryResult(javax.jcr.query.QueryResult) Query(javax.jcr.query.Query) JackrabbitNode(org.apache.jackrabbit.api.JackrabbitNode) Node(javax.jcr.Node) PropertyIteratorAdapter(org.apache.jackrabbit.commons.iterator.PropertyIteratorAdapter) ArrayList(java.util.ArrayList) RepositoryException(javax.jcr.RepositoryException) Property(javax.jcr.Property)

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