Search in sources :

Example 76 with Property

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

the class SetValueConstraintViolationExceptionTest method testDateProperty.

/**
     * Tests if setValue(Calendar value) and setValue(Value value) where value
     * is a DateValue throw a ConstraintViolationException if the change would
     * violate a value constraint
     */
public void testDateProperty() throws NotExecutableException, RepositoryException {
    // locate a PropertyDefinition with ValueConstraints
    PropertyDefinition propDef = NodeTypeUtil.locatePropertyDef(superuser, PropertyType.DATE, false, false, true, false);
    if (propDef == null) {
        throw new NotExecutableException("No date property def with " + "testable value constraints has been found");
    }
    // find a Value that does not satisfy the ValueConstraints of propDef
    Value valueNotSatisfied = NodeTypeUtil.getValueAccordingToValueConstraints(superuser, propDef, false);
    if (valueNotSatisfied == null) {
        throw new NotExecutableException("No date property def with " + "testable value constraints has been found");
    }
    // find a Value that does satisfy the ValueConstraints of propDef
    Value valueSatisfied = NodeTypeUtil.getValueAccordingToValueConstraints(superuser, propDef, true);
    if (valueSatisfied == null) {
        throw new NotExecutableException("The value constraints do not allow any value.");
    }
    // create a sub node of testRootNode of type propDef.getDeclaringNodeType()
    // and add a property with constraints to this node
    Node node;
    Property prop;
    try {
        String nodeType = propDef.getDeclaringNodeType().getName();
        node = testRootNode.addNode(nodeName2, nodeType);
        prop = node.setProperty(propDef.getName(), 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(Calendar value)
    try {
        prop.setValue(valueNotSatisfied.getDate());
        node.save();
        fail("setValue(Date value) must throw a ConstraintViolationException " + "if the change would violate a node type constraint " + "either immediately or on save");
    } catch (ConstraintViolationException e) {
    // success
    }
    // test of signature setValue(Value value)
    try {
        prop.setValue(valueNotSatisfied);
        node.save();
        fail("setValue(Value value) must throw a ConstraintViolationException " + "if the change would violate a node type constraint " + "either immediately or on save");
    } catch (ConstraintViolationException e) {
    // success
    }
}
Also used : NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) Node(javax.jcr.Node) Value(javax.jcr.Value) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition) Property(javax.jcr.Property)

Example 77 with Property

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

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

the class SetValueConstraintViolationExceptionTest method testMultipleBooleanProperty.

/**
     * Tests if setValue(Value[] values) where values are of type BooleanValue
     * throw a ConstraintViolationException if the change would violate a value
     * constraint
     */
public void testMultipleBooleanProperty() throws NotExecutableException, RepositoryException {
    // locate a PropertyDefinition with ValueConstraints
    PropertyDefinition propDef = NodeTypeUtil.locatePropertyDef(superuser, PropertyType.BOOLEAN, true, false, true, false);
    if (propDef == null) {
        throw new NotExecutableException("No multiple boolean property def with " + "testable value constraints has been found");
    }
    // find a Value that does not satisfy the ValueConstraints of propDef
    Value valueNotSatisfied = NodeTypeUtil.getValueAccordingToValueConstraints(superuser, propDef, false);
    if (valueNotSatisfied == null) {
        throw new NotExecutableException("No multiple boolean property def with " + "testable value constraints has been found");
    }
    // find a Value that does satisfy the ValueConstraints of propDef
    Value valueSatisfied = NodeTypeUtil.getValueAccordingToValueConstraints(superuser, propDef, true);
    if (valueSatisfied == null) {
        throw new NotExecutableException("The value constraints do not allow any value.");
    }
    // create a sub node of testRootNode of type propDef.getDeclaringNodeType()
    // and add a property with constraints to this node
    Node node;
    Property prop;
    try {
        String nodeType = propDef.getDeclaringNodeType().getName();
        node = testRootNode.addNode(nodeName2, nodeType);
        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.");
    }
    try {
        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 : NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) Node(javax.jcr.Node) Value(javax.jcr.Value) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition) Property(javax.jcr.Property)

Example 79 with Property

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

the class SetValueConstraintViolationExceptionTest method testMultipleLongProperty.

/**
     * Tests if setValue(Value[] values) where values are of type LongValue
     * throw a ConstraintViolationException if the change would violate a value
     * constraint
     */
public void testMultipleLongProperty() throws NotExecutableException, RepositoryException {
    // locate a PropertyDefinition with ValueConstraints
    PropertyDefinition propDef = NodeTypeUtil.locatePropertyDef(superuser, PropertyType.LONG, true, false, true, false);
    if (propDef == null) {
        throw new NotExecutableException("No multiple long property def with " + "testable value constraints has been found");
    }
    // find a Value that does not satisfy the ValueConstraints of propDef
    Value valueNotSatisfied = NodeTypeUtil.getValueAccordingToValueConstraints(superuser, propDef, false);
    if (valueNotSatisfied == null) {
        throw new NotExecutableException("No multiple long property def with " + "testable value constraints has been found");
    }
    // find a Value that does satisfy the ValueConstraints of propDef
    Value valueSatisfied = NodeTypeUtil.getValueAccordingToValueConstraints(superuser, propDef, true);
    if (valueSatisfied == null) {
        throw new NotExecutableException("The value constraints do not allow any value.");
    }
    // create a sub node of testRootNode of type propDef.getDeclaringNodeType()
    // and add a property with constraints to this node
    Node node;
    Property prop;
    try {
        String nodeType = propDef.getDeclaringNodeType().getName();
        node = testRootNode.addNode(nodeName2, nodeType);
        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 {
        prop.setValue(new Value[] { valueNotSatisfied });
        node.save();
        fail("setValue(Value value) must throw a ConstraintViolationException " + "if the change would violate a node type constraint " + "either immediately or on save");
    } catch (ConstraintViolationException e) {
    // success
    }
}
Also used : NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) Node(javax.jcr.Node) Value(javax.jcr.Value) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition) Property(javax.jcr.Property)

Example 80 with Property

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

the class SetValueConstraintViolationExceptionTest method testMultipleBinaryProperty.

/**
     * Tests if setValue(Value[] values) where values are of type BinaryValue
     * throw a ConstraintViolationException if the change would violate a value
     * constraint
     */
public void testMultipleBinaryProperty() throws NotExecutableException, RepositoryException {
    // locate a PropertyDefinition with ValueConstraints
    PropertyDefinition propDef = NodeTypeUtil.locatePropertyDef(superuser, PropertyType.BINARY, true, false, true, false);
    if (propDef == null) {
        throw new NotExecutableException("No multiple binary property def with " + "testable value constraints has been found");
    }
    // find a Value that does not satisfy the ValueConstraints of propDef
    Value valueNotSatisfied = NodeTypeUtil.getValueAccordingToValueConstraints(superuser, propDef, false);
    if (valueNotSatisfied == null) {
        throw new NotExecutableException("No multiple binary property def with " + "testable value constraints has been found");
    }
    // find a Value that does satisfy the ValueConstraints of propDef
    Value valueSatisfied = NodeTypeUtil.getValueAccordingToValueConstraints(superuser, propDef, true);
    if (valueSatisfied == null) {
        throw new NotExecutableException("The value constraints do not allow any value.");
    }
    // create a sub node of testRootNode of type propDef.getDeclaringNodeType()
    // and add a property with constraints to this node
    Node node;
    Property prop;
    try {
        String nodeType = propDef.getDeclaringNodeType().getName();
        node = testRootNode.addNode(nodeName2, nodeType);
        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.");
    }
    try {
        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 : NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) Node(javax.jcr.Node) Value(javax.jcr.Value) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition) Property(javax.jcr.Property)

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