Search in sources :

Example 66 with ConstraintViolationException

use of javax.jcr.nodetype.ConstraintViolationException 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 67 with ConstraintViolationException

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

the class SetPropertyAssumeTypeTest method testValueConstraintViolationExceptionBecauseOfInvalidTypeParameter.

/**
 * Tests if <code>Node.setProperty(String, Value, int)</code> throws a
 * ConstraintViolationException if the type parameter and the type of the
 * property do not match. The exception has to be thrown either immediately
 * (by this method) or on save.
 */
public void testValueConstraintViolationExceptionBecauseOfInvalidTypeParameter() throws NotExecutableException, RepositoryException {
    try {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date(0));
        Value v = superuser.getValueFactory().createValue(ISO8601.format(cal));
        testNode.setProperty(propertyName1, v, PropertyType.DATE);
        testRootNode.getSession().save();
        fail("Node.setProperty(String, Value, int) must throw a " + "ConstraintViolationExcpetion if the type parameter and the " + "type of the property do not match.");
    } catch (ConstraintViolationException e) {
    // success
    }
}
Also used : Calendar(java.util.Calendar) Value(javax.jcr.Value) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) Date(java.util.Date)

Example 68 with ConstraintViolationException

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

the class SetPropertyAssumeTypeTest method testStringConstraintViolationExceptionBecauseOfInvalidTypeParameter.

/**
 * Tests if <code>Node.setProperty(String, String, int)</code> throws a
 * ConstraintViolationException if the type parameter and the type of the
 * property do not match. The exception has to be thrown either immediately
 * (by this method) or on save.
 */
public void testStringConstraintViolationExceptionBecauseOfInvalidTypeParameter() throws NotExecutableException, RepositoryException {
    try {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date(0));
        testNode.setProperty(propertyName1, ISO8601.format(cal), PropertyType.DATE);
        testRootNode.getSession().save();
        fail("Node.setProperty(String, Value, int) must throw a " + "ConstraintViolationExcpetion if the type parameter and the " + "type of the property do not match.");
    } catch (ConstraintViolationException e) {
    // success
    }
}
Also used : Calendar(java.util.Calendar) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) Date(java.util.Date)

Example 69 with ConstraintViolationException

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

the class SessionTest method testMoveConstraintViolationExceptionSrc.

/**
 * Moves a node using {@link javax.jcr.Session#move(String src, String dest)},
 * afterwards it tries to only save the old parent node.
 * <p>
 * This should throw {@link javax.jcr.nodetype.ConstraintViolationException}.
 * <p>
 * Prerequisites: <ul> <li><code>javax.jcr.tck.nodetype</code>
 * must accept children of same nodetype</li> </ul>
 */
public void testMoveConstraintViolationExceptionSrc() throws RepositoryException {
    // get default workspace test root node using superuser session
    Node defaultRootNode = (Node) superuser.getItem(testRootNode.getPath());
    // create parent node
    Node srcParentNode = defaultRootNode.addNode(nodeName1, testNodeType);
    // create node to be moved
    Node moveNode = srcParentNode.addNode(nodeName2, testNodeType);
    // create a node that will serve as new parent
    Node destParentNode = defaultRootNode.addNode(nodeName3, testNodeType);
    // save the new nodes
    superuser.save();
    // move the node
    superuser.move(moveNode.getPath(), destParentNode.getPath() + "/" + nodeName2);
    // save only old parent node
    try {
        srcParentNode.save();
        fail("Saving only the source parent node after a Session.move() operation must throw ConstraintViolationException");
    } catch (ConstraintViolationException e) {
    // ok both work as expected
    }
}
Also used : Node(javax.jcr.Node) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException)

Example 70 with ConstraintViolationException

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

the class SessionTest method testMoveConstraintViolationExceptionDest.

/**
 * Moves a node using {@link javax.jcr.Session#move(String src, String dest)},
 * afterwards it tries to only save the destination parent
 * node.
 * <p>
 * This should throw a {@link javax.jcr.nodetype.ConstraintViolationException}.
 * <p>
 * Prerequisites: <ul> <li><code>javax.jcr.tck.nodetype</code>
 * must accept children of same nodetype</li> </ul>
 */
public void testMoveConstraintViolationExceptionDest() throws RepositoryException {
    // get default workspace test root node using superuser session
    Node defaultRootNode = (Node) superuser.getItem(testRootNode.getPath());
    // create parent node
    Node srcParentNode = defaultRootNode.addNode(nodeName1, testNodeType);
    // create node to be moved
    Node moveNode = srcParentNode.addNode(nodeName2, testNodeType);
    // create a node that will serve as new parent
    Node destParentNode = defaultRootNode.addNode(nodeName3, testNodeType);
    // save the new nodes
    superuser.save();
    // move the node
    superuser.move(moveNode.getPath(), destParentNode.getPath() + "/" + nodeName2);
    // save only moved node
    try {
        destParentNode.save();
        fail("Saving only moved node after a Session.move() operation should throw ConstraintViolationException");
    } catch (ConstraintViolationException e) {
    // ok try to save the source
    }
}
Also used : Node(javax.jcr.Node) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException)

Aggregations

ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)180 Node (javax.jcr.Node)74 RepositoryException (javax.jcr.RepositoryException)40 Name (org.apache.jackrabbit.spi.Name)33 Value (javax.jcr.Value)31 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)28 Test (org.junit.Test)27 PropertyDefinition (javax.jcr.nodetype.PropertyDefinition)23 QPropertyDefinition (org.apache.jackrabbit.spi.QPropertyDefinition)23 Session (javax.jcr.Session)18 ItemExistsException (javax.jcr.ItemExistsException)17 NodeState (org.apache.jackrabbit.core.state.NodeState)16 QNodeDefinition (org.apache.jackrabbit.spi.QNodeDefinition)16 Property (javax.jcr.Property)14 NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)14 ArrayList (java.util.ArrayList)13 NodeId (org.apache.jackrabbit.core.id.NodeId)13 EffectiveNodeType (org.apache.jackrabbit.core.nodetype.EffectiveNodeType)13 NodeType (javax.jcr.nodetype.NodeType)12 ChildNodeEntry (org.apache.jackrabbit.core.state.ChildNodeEntry)12