Search in sources :

Example 51 with Property

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

the class PathPropertyTest method testGetProperty.

/**
     * Since JCR 2.0 a path property can be dereferenced if it points to a
     * Property.
     * TODO: create several tests out of this one
     */
public void testGetProperty() throws RepositoryException {
    if (!multiple) {
        String propPath = prop.getPath();
        String propName = prop.getName();
        // absolute property path
        prop.getParent().setProperty(propName, propPath, PropertyType.PATH);
        String path = prop.getString();
        Property p = prop.getProperty();
        assertEquals("The path of the dereferenced property must be equal to the value", path, p.getPath());
        assertTrue("The property value must be resolved to the correct property.", prop.isSame(p));
        // relative property path
        prop.getParent().setProperty(propName, propName, PropertyType.PATH);
        path = prop.getString();
        p = prop.getProperty();
        assertEquals("The path of the dereferenced property must be equal to the value", path, p.getName());
        assertTrue("The property value must be resolved to the correct property.", prop.getParent().getProperty(path).isSame(p));
        // non-existing property path
        while (session.propertyExists(propPath)) {
            propPath += "x";
        }
        prop.getParent().setProperty(propName, propPath, PropertyType.PATH);
        try {
            prop.getProperty();
            fail("Calling Property.getProperty() for a PATH value that doesn't have a corresponding Property, ItemNotFoundException is expected");
        } catch (ItemNotFoundException e) {
        //ok
        }
    } else {
        try {
            prop.getProperty();
            fail("Property.getNode() called on a multivalue property " + "should throw a ValueFormatException.");
        } catch (ValueFormatException vfe) {
        // ok
        }
    }
}
Also used : ValueFormatException(javax.jcr.ValueFormatException) Property(javax.jcr.Property) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 52 with Property

use of javax.jcr.Property 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 53 with Property

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

the class NodeReadMethodsTest method testGetReferences.

public void testGetReferences() throws NotExecutableException, RepositoryException {
    Node node = locateNodeWithReference(testRootNode);
    if (node == null) {
        throw new NotExecutableException("Workspace does not contain a node with a reference property set");
    }
    PropertyIterator properties = node.getProperties();
    while (properties.hasNext()) {
        Property p = properties.nextProperty();
        if (p.getType() == PropertyType.REFERENCE && !p.getDefinition().isMultiple()) {
            Node referencedNode = p.getNode();
            PropertyIterator refs = referencedNode.getReferences();
            boolean referenceFound = false;
            while (refs.hasNext()) {
                Property ref = refs.nextProperty();
                if (ref.isSame(p)) {
                    referenceFound = true;
                }
            }
            assertTrue("Correct reference not found", referenceFound);
        }
    }
}
Also used : NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) Node(javax.jcr.Node) PropertyIterator(javax.jcr.PropertyIterator) Property(javax.jcr.Property)

Example 54 with Property

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

the class NodeReadMethodsTest method testHasProperty.

/**
     * Test if hasProperty(String relPath) returns true if a required property
     * exists and false if it doesn't. Tested node: root
     */
public void testHasProperty() throws NotExecutableException, RepositoryException {
    Node node = testRootNode;
    PropertyIterator properties = node.getProperties();
    StringBuffer notExistingPropertyName = new StringBuffer();
    while (properties.hasNext()) {
        Property p = properties.nextProperty();
        assertTrue("node.hasProperty(\"relPath\") returns false " + "although property at relPath is existing", node.hasProperty(p.getName()));
        notExistingPropertyName.append(p.getName() + "X");
    }
    if (notExistingPropertyName.toString().equals("")) {
        fail("Root node must at least have one property: jcr:primaryType");
    }
    assertFalse("node.hasProperty(\"relPath\") returns true " + "although property at relPath is not existing", node.hasProperty(notExistingPropertyName.toString().replaceAll(":", "")));
}
Also used : Node(javax.jcr.Node) PropertyIterator(javax.jcr.PropertyIterator) Property(javax.jcr.Property)

Example 55 with Property

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

the class NodeReadMethodsTest method testGetProperty.

/**
     * Test if getProperty(String relPath) returns the correct node and if a
     * PathNotFoundException is thrown when property at relPath does not exist
     */
public void testGetProperty() throws NotExecutableException, RepositoryException {
    StringBuffer notExistingPath = new StringBuffer("X");
    PropertyIterator properties = testRootNode.getProperties();
    while (properties.hasNext()) {
        // build a path that for sure is not existing
        // (":" of namespace prefix will be replaced later on)
        notExistingPath.append(properties.nextProperty().getName());
    }
    try {
        testRootNode.getProperty(notExistingPath.toString().replaceAll(":", ""));
        fail("getProperty(String relPath) must throw a " + "PathNotFoundException if no node exists at relPath");
    } catch (PathNotFoundException e) {
    // success
    }
    try {
        PropertyIterator properties2 = testRootNode.getProperties();
        Property property = properties2.nextProperty();
        assertTrue("Property returned by getProperties() is not the same as returned by getProperty(String).", testRootNode.getProperty(property.getName()).isSame(property));
    } catch (NoSuchElementException e) {
        fail("Root node must always have at least one property: jcr:primaryType");
    }
}
Also used : PropertyIterator(javax.jcr.PropertyIterator) PathNotFoundException(javax.jcr.PathNotFoundException) Property(javax.jcr.Property) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

Property (javax.jcr.Property)445 Node (javax.jcr.Node)252 Value (javax.jcr.Value)99 Test (org.junit.Test)87 Session (javax.jcr.Session)78 PropertyIterator (javax.jcr.PropertyIterator)68 RepositoryException (javax.jcr.RepositoryException)64 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)47 JackrabbitNode (org.apache.jackrabbit.api.JackrabbitNode)37 ValueFormatException (javax.jcr.ValueFormatException)34 QValue (org.apache.jackrabbit.spi.QValue)29 ArrayList (java.util.ArrayList)24 NodeIterator (javax.jcr.NodeIterator)24 QValueValue (org.apache.jackrabbit.spi.commons.value.QValueValue)23 PropertyDefinition (javax.jcr.nodetype.PropertyDefinition)20 Item (javax.jcr.Item)19 PathNotFoundException (javax.jcr.PathNotFoundException)17 InvalidItemStateException (javax.jcr.InvalidItemStateException)16 Version (javax.jcr.version.Version)16 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)15