Search in sources :

Example 76 with NodeType

use of javax.jcr.nodetype.NodeType in project jackrabbit by apache.

the class SetValueConstraintViolationExceptionTest method testMultipleReferenceProperty.

/**
 * Tests if setValue(Value[] values) where values are of type ReferenceValue
 * throw a ConstraintViolationException if the change would violate a value
 * constraint
 */
public void testMultipleReferenceProperty() throws NotExecutableException, RepositoryException {
    // locate a PropertyDefinition with ValueConstraints
    PropertyDefinition propDef = NodeTypeUtil.locatePropertyDef(superuser, PropertyType.REFERENCE, true, false, true, false);
    if (propDef == null) {
        throw new NotExecutableException("No multiple reference property def with " + "testable value constraints has been found");
    }
    String[] valueConstraints = propDef.getValueConstraints();
    if (valueConstraints == null || valueConstraints.length == 0) {
        throw new NotExecutableException("No reference property def with " + "testable value constraints has been found");
    }
    List<String> constraints = Arrays.asList(valueConstraints);
    String nodeTypeSatisfied = constraints.get(0);
    String nodeTypeNotSatisfied = null;
    NodeTypeManager manager = superuser.getWorkspace().getNodeTypeManager();
    NodeTypeIterator types = manager.getAllNodeTypes();
    // find a NodeType which is not satisfying the constraints
    while (types.hasNext()) {
        NodeType type = types.nextNodeType();
        String name = type.getName();
        if (constraints.contains(name) || ntFrozenNode.equals(name)) {
            continue;
        }
        if (type.getChildNodeDefinitions() != null && type.getChildNodeDefinitions().length > 0) {
            continue;
        }
        nodeTypeNotSatisfied = name;
        break;
    }
    if (nodeTypeNotSatisfied == null) {
        throw new NotExecutableException("No reference property def with " + "testable value constraints has been found");
    }
    // create a sub node of testRootNode of type propDef.getDeclaringNodeType()
    // and add a property with constraints to this node
    Node node;
    Property prop;
    Node nodeSatisfied;
    Node nodeNotSatisfied;
    try {
        String nodeType = propDef.getDeclaringNodeType().getName();
        node = testRootNode.addNode(nodeName2, nodeType);
        // create a referenceable node satisfying the constraint
        nodeSatisfied = testRootNode.addNode(nodeName3, nodeTypeSatisfied);
        ensureMixinType(nodeSatisfied, mixReferenceable);
        // create a referenceable node not satisfying the constraint
        nodeNotSatisfied = testRootNode.addNode(nodeName4, nodeTypeNotSatisfied);
        ensureMixinType(nodeNotSatisfied, mixReferenceable);
        // some implementations may require a save after addMixin()
        testRootNode.getSession().save();
        Value valueSatisfied = superuser.getValueFactory().createValue(nodeSatisfied);
        prop = node.setProperty(propDef.getName(), new Value[] { valueSatisfied });
        testRootNode.getSession().save();
    } catch (ConstraintViolationException e) {
        // implementation specific constraints do not allow to set up test environment
        throw new NotExecutableException("Not able to create required test items.");
    }
    // test of signature setValue(Value value)
    try {
        Value valueNotSatisfied = superuser.getValueFactory().createValue(nodeNotSatisfied);
        prop.setValue(new Value[] { valueNotSatisfied });
        node.save();
        fail("setValue(Value[] values) must throw a ConstraintViolationException " + "if the change would violate a node type constraint " + "either immediately or on save");
    } catch (ConstraintViolationException e) {
    // success
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) NodeType(javax.jcr.nodetype.NodeType) Node(javax.jcr.Node) Value(javax.jcr.Value) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition) Property(javax.jcr.Property)

Example 77 with NodeType

use of javax.jcr.nodetype.NodeType in project jackrabbit by apache.

the class CanAddChildNodeCallWithoutNodeTypeTest method testResidualWithDefault.

/**
 * Tests if <code>NodeType.canAddChildNode(String childNodeName)</code> returns
 * true if <code>NodeType</code> contains a residual <code>NodeDef</code>
 * with a default primary type.
 */
public void testResidualWithDefault() throws NotExecutableException, RepositoryException {
    NodeDefinition nodeDef = NodeTypeUtil.locateChildNodeDef(session, true, true, true);
    if (nodeDef == null) {
        throw new NotExecutableException("No residual child node def " + "without a defaultPrimaryType found.");
    }
    NodeType nodeType = nodeDef.getDeclaringNodeType();
    String undefinedName = NodeTypeUtil.getUndefinedChildNodeName(nodeType);
    assertTrue("NodeType.canAddChildNode(String childNodeName) must return " + "true for a not defined childNodeName if NodeType has a residual child node " + "definition with a defaultPrimaryType", nodeType.canAddChildNode(undefinedName));
}
Also used : NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) NodeType(javax.jcr.nodetype.NodeType) NodeDefinition(javax.jcr.nodetype.NodeDefinition)

Example 78 with NodeType

use of javax.jcr.nodetype.NodeType in project jackrabbit by apache.

the class CanAddChildNodeCallWithoutNodeTypeTest method testResidualWithoutDefault.

/**
 * Tests if <code>NodeType.canAddChildNode(String childNodeName)</code> returns
 * true if <code>NodeType</code> contains a residual <code>NodeDef</code>
 * without a default primary type.
 */
public void testResidualWithoutDefault() throws NotExecutableException, RepositoryException {
    NodeDefinition nodeDef = NodeTypeUtil.locateChildNodeDef(session, true, false, true);
    if (nodeDef == null) {
        throw new NotExecutableException("No residual child node def " + "with a defaultPrimaryType found.");
    }
    NodeType nodeType = nodeDef.getDeclaringNodeType();
    String undefinedName = NodeTypeUtil.getUndefinedChildNodeName(nodeType);
    assertFalse("NodeType.canAddChildNode(String childNodeName) must return " + "false for a not defiend childNodeName if NodeType has a " + "residual child node definition without a defaultPrimaryType", nodeType.canAddChildNode(undefinedName));
}
Also used : NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) NodeType(javax.jcr.nodetype.NodeType) NodeDefinition(javax.jcr.nodetype.NodeDefinition)

Example 79 with NodeType

use of javax.jcr.nodetype.NodeType in project jackrabbit by apache.

the class CanAddChildNodeCallWithoutNodeTypeTest method testDefinedWithoutDefault.

/**
 * Tests if <code>NodeType.canAddChildNode(String childNodeName)</code> returns
 * true if <code>NodeType</code> contains a <code>NodeDef</code>  named
 * <code>childNodeName</code> without a default primary type.
 */
public void testDefinedWithoutDefault() throws NotExecutableException, RepositoryException {
    NodeDefinition nodeDef = NodeTypeUtil.locateChildNodeDef(session, true, false, false);
    if (nodeDef == null) {
        throw new NotExecutableException("No child node def without " + "defaultPrimaryType found");
    }
    NodeType nodeType = nodeDef.getDeclaringNodeType();
    assertFalse("NodeType.canAddChildNode(String childNodeName) must return false " + "if child node def 'childNodeName' does not define a defaultPrimaryType.", nodeType.canAddChildNode(nodeDef.getName()));
}
Also used : NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) NodeType(javax.jcr.nodetype.NodeType) NodeDefinition(javax.jcr.nodetype.NodeDefinition)

Example 80 with NodeType

use of javax.jcr.nodetype.NodeType in project jackrabbit by apache.

the class CanSetPropertyBinaryTest method testConversionsMultiple.

/**
 * Tests if NodeType.canSetProperty(String propertyName, Value[] values)
 * returns true if all values and its types are convertible to BinaryValue.
 */
public void testConversionsMultiple() throws NotExecutableException, RepositoryException {
    PropertyDefinition propDef = NodeTypeUtil.locatePropertyDef(session, PropertyType.BINARY, true, false, false, false);
    if (propDef == null) {
        throw new NotExecutableException("No multiple string property def that meets the " + "requirements of the test has been found");
    }
    NodeType nodeType = propDef.getDeclaringNodeType();
    Value binaryValue = NodeTypeUtil.getValueOfType(session, PropertyType.BINARY);
    Value stringValue = NodeTypeUtil.getValueOfType(session, PropertyType.STRING);
    Value[] stringValues = new Value[] { stringValue };
    assertTrue("canSetProperty(String propertyName, Value[] values) must return " + "true if the property is of type Binary and values are of type StringValue", nodeType.canSetProperty(propDef.getName(), stringValues));
    Value[] binaryValues = new Value[] { binaryValue };
    assertTrue("canSetProperty(String propertyName, Value[] values) must return " + "true if the property is of type Binary and values are of type BinaryValue", nodeType.canSetProperty(propDef.getName(), binaryValues));
    Value dateValue = NodeTypeUtil.getValueOfType(session, PropertyType.DATE);
    Value[] dateValues = new Value[] { dateValue };
    assertTrue("canSetProperty(String propertyName, Value[] values) must return " + "true if the property is of type Binary and values are of type DateValue", nodeType.canSetProperty(propDef.getName(), dateValues));
    Value doubleValue = NodeTypeUtil.getValueOfType(session, PropertyType.DOUBLE);
    Value[] doubleValues = new Value[] { doubleValue };
    assertTrue("canSetProperty(String propertyName, Value[] values) must return " + "true if the property is of type Binary and values are of type DoubleValue", nodeType.canSetProperty(propDef.getName(), doubleValues));
    Value longValue = NodeTypeUtil.getValueOfType(session, PropertyType.LONG);
    Value[] longValues = new Value[] { longValue };
    assertTrue("canSetProperty(String propertyName, Value[] values) must return " + "true if the property is of type Binary and values are of type LongValue", nodeType.canSetProperty(propDef.getName(), longValues));
    Value booleanValue = NodeTypeUtil.getValueOfType(session, PropertyType.BOOLEAN);
    Value[] booleanValues = new Value[] { booleanValue };
    assertTrue("canSetProperty(String propertyName, Value[] values) must return " + "true if the property is of type Binary and values are of type BooleanValue", nodeType.canSetProperty(propDef.getName(), booleanValues));
    Value nameValue = NodeTypeUtil.getValueOfType(session, PropertyType.NAME);
    Value[] nameValues = new Value[] { nameValue };
    assertTrue("canSetProperty(String propertyName, Value[] values) must return " + "true if the property is of type Binary and values are of type NameValue", nodeType.canSetProperty(propDef.getName(), nameValues));
    Value pathValue = NodeTypeUtil.getValueOfType(session, PropertyType.PATH);
    Value[] pathValues = new Value[] { pathValue };
    assertTrue("canSetProperty(String propertyName, Value[] values) must return " + "true if the property is of type Binary and values are of type PathValue", nodeType.canSetProperty(propDef.getName(), pathValues));
}
Also used : NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) NodeType(javax.jcr.nodetype.NodeType) Value(javax.jcr.Value) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition)

Aggregations

NodeType (javax.jcr.nodetype.NodeType)272 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)84 PropertyDefinition (javax.jcr.nodetype.PropertyDefinition)81 Node (javax.jcr.Node)63 Value (javax.jcr.Value)60 NodeTypeIterator (javax.jcr.nodetype.NodeTypeIterator)55 NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)46 NodeDefinition (javax.jcr.nodetype.NodeDefinition)45 RepositoryException (javax.jcr.RepositoryException)41 ArrayList (java.util.ArrayList)30 Test (org.junit.Test)30 Session (javax.jcr.Session)29 NodeTypeIteratorAdapter (org.apache.jackrabbit.commons.iterator.NodeTypeIteratorAdapter)16 NoSuchNodeTypeException (javax.jcr.nodetype.NoSuchNodeTypeException)15 Name (org.apache.jackrabbit.spi.Name)15 HashSet (java.util.HashSet)14 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)12 NodeIterator (javax.jcr.NodeIterator)7 Property (javax.jcr.Property)7 QNodeTypeDefinition (org.apache.jackrabbit.spi.QNodeTypeDefinition)7