Search in sources :

Example 86 with NodeIterator

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

the class AbstractWriteTest method testACItemsAreProtected.

public void testACItemsAreProtected() throws NotExecutableException, RepositoryException {
    // search for a rep:policy node
    Node policyNode = findPolicyNode(superuser.getRootNode());
    if (policyNode == null) {
        throw new NotExecutableException("no policy node found.");
    }
    assertTrue("The rep:Policy node must be protected", policyNode.getDefinition().isProtected());
    try {
        policyNode.remove();
        fail("rep:Policy node must be protected.");
    } catch (ConstraintViolationException e) {
    // success
    }
    for (NodeIterator it = policyNode.getNodes(); it.hasNext(); ) {
        Node n = it.nextNode();
        if (n.isNodeType("rep:ACE")) {
            try {
                n.remove();
                fail("ACE node must be protected.");
            } catch (ConstraintViolationException e) {
            // success
            }
            break;
        }
    }
    try {
        policyNode.setProperty("test", "anyvalue");
        fail("rep:policy node must be protected.");
    } catch (ConstraintViolationException e) {
    // success
    }
    try {
        policyNode.addNode("test", "rep:ACE");
        fail("rep:policy node must be protected.");
    } catch (ConstraintViolationException e) {
    // success
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) JackrabbitNode(org.apache.jackrabbit.api.JackrabbitNode) Node(javax.jcr.Node) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException)

Example 87 with NodeIterator

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

the class AbstractJCRTest method cleanUpTestRoot.

/**
     * Reverts any pending changes made by <code>s</code> and deletes any nodes
     * under {@link #testRoot}. If there is no node at {@link #testRoot} then
     * the necessary nodes are created.
     *
     * @param s the session to clean up.
     * @return the {@link javax.jcr.Node} that represents the test root.
     * @throws RepositoryException if an error occurs.
     */
protected Node cleanUpTestRoot(Session s) throws RepositoryException {
    // do a 'rollback'
    s.refresh(false);
    Node root = s.getRootNode();
    Node testRootNode;
    if (root.hasNode(testPath)) {
        RetentionManager rm;
        try {
            rm = s.getRetentionManager();
        } catch (UnsupportedRepositoryOperationException e) {
            rm = null;
        }
        // clean test root
        testRootNode = root.getNode(testPath);
        NodeIterator children = testRootNode.getNodes();
        while (children.hasNext()) {
            Node child = children.nextNode();
            // Remove retention policy if needed
            String childPath = child.getPath();
            if (rm != null && rm.getRetentionPolicy(childPath) != null) {
                rm.removeRetentionPolicy(childPath);
                s.save();
            }
            NodeDefinition nodeDef = child.getDefinition();
            if (!nodeDef.isMandatory() && !nodeDef.isProtected()) {
                // try to remove child
                try {
                    child.remove();
                } catch (ConstraintViolationException e) {
                    log.println("unable to remove node: " + child.getPath());
                }
            }
        }
    } else {
        // create nodes to testPath
        StringTokenizer names = new StringTokenizer(testPath, "/");
        Node currentNode = root;
        while (names.hasMoreTokens()) {
            String name = names.nextToken();
            if (currentNode.hasNode(name)) {
                currentNode = currentNode.getNode(name);
            } else {
                currentNode = currentNode.addNode(name, testNodeTypeTestRoot);
            }
        }
        testRootNode = currentNode;
    }
    s.save();
    return testRootNode;
}
Also used : RetentionManager(javax.jcr.retention.RetentionManager) NodeIterator(javax.jcr.NodeIterator) UnsupportedRepositoryOperationException(javax.jcr.UnsupportedRepositoryOperationException) StringTokenizer(java.util.StringTokenizer) Node(javax.jcr.Node) NodeDefinition(javax.jcr.nodetype.NodeDefinition) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException)

Example 88 with NodeIterator

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

the class JsonWriterTest method testDoubleOutput.

@Test
public void testDoubleOutput() throws Exception {
    StringWriter writer = new StringWriter();
    JsonWriter jsonWriter = new JsonWriter(writer);
    Node parent = createMock(Node.class);
    Property doubleProperty = createMock(Property.class);
    Value doublePropertyValue = createMock(Value.class);
    expect(doubleProperty.getType()).andReturn(PropertyType.DOUBLE).anyTimes();
    expect(doubleProperty.getName()).andReturn("singleValued").anyTimes();
    expect(doubleProperty.isMultiple()).andReturn(false).anyTimes();
    expect(doubleProperty.getValue()).andReturn(doublePropertyValue).anyTimes();
    expect(doublePropertyValue.getType()).andReturn(PropertyType.DOUBLE).anyTimes();
    expect(doublePropertyValue.getDouble()).andReturn(5d).anyTimes();
    expect(doublePropertyValue.getString()).andReturn("5").anyTimes();
    Property mvDoubleProperty = createMock(Property.class);
    Value mvDoublePropertyValue1 = createMock(Value.class);
    Value mvDoublePropertyValue2 = createMock(Value.class);
    expect(mvDoubleProperty.getType()).andReturn(PropertyType.DOUBLE).anyTimes();
    expect(mvDoubleProperty.getName()).andReturn("multiValued").anyTimes();
    expect(mvDoubleProperty.isMultiple()).andReturn(true).anyTimes();
    expect(mvDoubleProperty.getValues()).andReturn(new Value[] { mvDoublePropertyValue1, mvDoublePropertyValue2 }).anyTimes();
    expect(mvDoublePropertyValue1.getType()).andReturn(PropertyType.DOUBLE).anyTimes();
    expect(mvDoublePropertyValue1.getDouble()).andReturn(42d).anyTimes();
    expect(mvDoublePropertyValue1.getString()).andReturn("42").anyTimes();
    expect(mvDoublePropertyValue2.getType()).andReturn(PropertyType.DOUBLE).anyTimes();
    expect(mvDoublePropertyValue2.getDouble()).andReturn(98.6).anyTimes();
    expect(mvDoublePropertyValue2.getString()).andReturn("98.6").anyTimes();
    final List<Property> properties = new ArrayList<Property>();
    properties.add(doubleProperty);
    properties.add(mvDoubleProperty);
    expect(parent.getProperties()).andAnswer(new IAnswer<PropertyIterator>() {

        @Override
        public PropertyIterator answer() throws Throwable {
            return new PropertyIteratorAdapter(properties.iterator());
        }
    });
    expect(parent.getNodes()).andAnswer(new IAnswer<NodeIterator>() {

        @Override
        public NodeIterator answer() throws Throwable {
            return new NodeIteratorAdapter(Collections.<Node>emptyIterator());
        }
    });
    replayAll();
    jsonWriter.write(parent, 1);
    assertEquals("{\":singleValued\":\"Double\",\"singleValued\":5,\":multiValued\":\"Double\",\"multiValued\":[42,98.6],\"::NodeIteratorSize\":0}", writer.toString());
    verifyAll();
}
Also used : NodeIterator(javax.jcr.NodeIterator) Node(javax.jcr.Node) PropertyIteratorAdapter(org.apache.jackrabbit.commons.iterator.PropertyIteratorAdapter) ArrayList(java.util.ArrayList) PropertyIterator(javax.jcr.PropertyIterator) NodeIteratorAdapter(org.apache.jackrabbit.commons.iterator.NodeIteratorAdapter) StringWriter(java.io.StringWriter) Value(javax.jcr.Value) Property(javax.jcr.Property) Test(org.junit.Test)

Example 89 with NodeIterator

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

the class AbstractQueryTest method checkResult.

/**
     * Checks if the result set contains exactly the <code>nodes</code>.
     * @param result the query result.
     * @param requiredNodes the nodes that need to be in the result set
     * 		(null if no node is required).
     * @param optionalNodes the nodes that may be in the result set
     * 		(null if no node is optional).
     */
protected void checkResult(QueryResult result, Node[] requiredNodes, Node[] optionalNodes) throws RepositoryException {
    // collect paths
    Set<String> requiredPaths = getPathSet(requiredNodes);
    Set<String> optionalPaths = getPathSet(optionalNodes);
    Set<String> resultPaths = new HashSet<String>();
    for (NodeIterator it = result.getNodes(); it.hasNext(); ) {
        resultPaths.add(it.nextNode().getPath());
    }
    // check if all required nodes are in result
    for (Iterator<String> it = requiredPaths.iterator(); it.hasNext(); ) {
        String path = it.next();
        assertTrue(path + " is not part of the result set", resultPaths.contains(path));
    }
    // check result does not contain more than expected
    for (Iterator<String> it = resultPaths.iterator(); it.hasNext(); ) {
        String path = it.next();
        if (!optionalPaths.contains(path)) {
            assertTrue(path + " is not expected to be part of the result set", requiredPaths.contains(path));
        }
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) HashSet(java.util.HashSet)

Example 90 with NodeIterator

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

the class XPathDocOrderTest method testDocOrderPositionFunction.

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

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