Search in sources :

Example 91 with ConstraintViolationException

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

the class VersionTest method testSetProperty.

/**
 * Tests if
 * <ul> <li><code>Version.setProperty(String, String[])</code></li>
 * <li><code>Version.setProperty(String, String[], int)</code></li>
 * <li><code>Version.setProperty(String, Value[])</code></li>
 * <li><code>Version.setProperty(String, Value[], int)</code></li>
 * <li><code>Version.setProperty(String, boolean)</code></li>
 * <li><code>Version.setProperty(String, double)</code></li>
 * <li><code>Version.setProperty(String, InputStream)</code></li>
 * <li><code>Version.setProperty(String, String)</code></li>
 * <li><code>Version.setProperty(String, Calendar)</code></li>
 * <li><code>Version.setProperty(String, Node)</code></li>
 * <li><code>Version.setProperty(String, Value)</code></li>
 * <li><code>Version.setProperty(String, long)</code></li>
 * </ul> all throw a
 * {@link javax.jcr.nodetype.ConstraintViolationException}
 */
public void testSetProperty() throws Exception {
    // create Value[] object
    Value[] vArray = new Value[3];
    vArray[0] = superuser.getValueFactory().createValue("abc");
    vArray[1] = superuser.getValueFactory().createValue("xyz");
    vArray[2] = superuser.getValueFactory().createValue("123");
    // create String array
    String[] s = { "abc", "xyz", "123" };
    try {
        version.setProperty(propertyName1, s);
        version.getSession().save();
        fail("Version should be read-only: Version.setProperty(String,String[]) did not throw a ConstraintViolationException");
    } catch (ConstraintViolationException success) {
    }
    try {
        version.setProperty(propertyName1, s, PropertyType.STRING);
        version.getSession().save();
        fail("Version should be read-only: Version.setProperty(String,String[],int) did not throw a ConstraintViolationException");
    } catch (ConstraintViolationException success) {
    }
    try {
        version.setProperty(propertyName1, vArray);
        version.getSession().save();
        fail("Version should be read-only: Version.setProperty(String,Value[]) did not throw a ConstraintViolationException");
    } catch (ConstraintViolationException success) {
    }
    try {
        version.setProperty(propertyName1, vArray, PropertyType.STRING);
        version.getSession().save();
        fail("Version should be read-only: Version.setProperty(String,Value[],int]) did not throw a ConstraintViolationException");
    } catch (ConstraintViolationException success) {
    }
    try {
        version.setProperty(propertyName1, true);
        version.getSession().save();
        fail("Version should be read-only: Version.setProperty(String,boolean) did not throw a ConstraintViolationException");
    } catch (ConstraintViolationException success) {
    }
    try {
        version.setProperty(propertyName1, 123);
        version.getSession().save();
        fail("Version should be read-only: Version.setProperty(String,double) did not throw a ConstraintViolationException");
    } catch (ConstraintViolationException success) {
    }
    try {
        byte[] bytes = { 73, 26, 32, -36, 40, -43, -124 };
        InputStream inpStream = new ByteArrayInputStream(bytes);
        version.setProperty(propertyName1, inpStream);
        version.getSession().save();
        fail("Version should be read-only: Version.setProperty(String,InputStream) did not throw a ConstraintViolationException");
    } catch (ConstraintViolationException success) {
    }
    try {
        version.setProperty(propertyName1, "abc");
        version.getSession().save();
        fail("Version should be read-only: Version.setProperty(String,String) did not throw a ConstraintViolationException");
    } catch (ConstraintViolationException success) {
    }
    try {
        Calendar c = new GregorianCalendar(1945, 1, 6, 16, 20, 0);
        version.setProperty(propertyName1, c);
        version.getSession().save();
        fail("Version should be read-only: Version.setProperty(String,Calendar) did not throw a ConstraintViolationException");
    } catch (ConstraintViolationException success) {
    }
    try {
        version.setProperty(propertyName1, version);
        version.getSession().save();
        fail("Version should be read-only: Version.setProperty(String,Node) did not throw a ConstraintViolationException");
    } catch (ConstraintViolationException success) {
    }
    try {
        Value v = superuser.getValueFactory().createValue("abc");
        version.setProperty(propertyName1, v);
        version.getSession().save();
        fail("Version should be read-only: Version.setProperty(String,Value) did not throw a ConstraintViolationException");
    } catch (ConstraintViolationException success) {
    }
    try {
        version.setProperty(propertyName1, -2147483650L);
        version.getSession().save();
        fail("Version should be read-only: Version.setProperty(String,long) did not throw a ConstraintViolationException");
    } catch (ConstraintViolationException success) {
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) Value(javax.jcr.Value) GregorianCalendar(java.util.GregorianCalendar) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException)

Example 92 with ConstraintViolationException

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

the class AddMixinTest method testAddItemsDefinedByMixin2.

public void testAddItemsDefinedByMixin2() throws NotExecutableException, RepositoryException {
    // register mixin
    NodeTypeManager ntm = superuser.getWorkspace().getNodeTypeManager();
    NodeTypeTemplate ntd = ntm.createNodeTypeTemplate();
    ntd.setName("testMixin");
    ntd.setMixin(true);
    NodeDefinitionTemplate nodeDef = ntm.createNodeDefinitionTemplate();
    nodeDef.setName("child");
    nodeDef.setRequiredPrimaryTypeNames(new String[] { "nt:folder" });
    ntd.getNodeDefinitionTemplates().add(nodeDef);
    ntm.registerNodeType(ntd, true);
    // create node and add mixin
    Node node = testRootNode.addNode(nodeName1, "nt:resource");
    node.setProperty("jcr:data", "abc");
    node.addMixin("testMixin");
    superuser.save();
    // node type
    try {
        node.addNode("child");
        fail();
    } catch (ConstraintViolationException e) {
    // success as ChildNode Definition doesn't specify a default primary
    // type -> see comment in ItemDefinitionProvider#getQNodeDefinition
    }
}
Also used : NodeDefinitionTemplate(javax.jcr.nodetype.NodeDefinitionTemplate) NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) Node(javax.jcr.Node) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException)

Example 93 with ConstraintViolationException

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

the class MandatoryItemTest method testRemoval.

public void testRemoval() throws NotExecutableException, RepositoryException {
    Node n;
    Node childN = null;
    Property childP = null;
    try {
        n = testRootNode.addNode(nodeName1, testNodeType);
        if (childNodeDef != null) {
            childN = n.addNode(childNodeDef.getName(), childNodeDef.getDefaultPrimaryType().getName());
        }
        if (childPropDef != null) {
            // TODO: check if definition defines default values
            childP = n.setProperty(childPropDef.getName(), "any value");
        }
        testRootNode.save();
    } catch (RepositoryException e) {
        throw new NotExecutableException();
    }
    // remove the mandatory items ((must succeed))
    if (childN != null) {
        childN.remove();
    }
    if (childP != null) {
        childP.remove();
    }
    // ... however, saving must not be allowed.
    try {
        testRootNode.save();
        fail("removing mandatory child items without re-adding them must fail.");
    } catch (ConstraintViolationException e) {
    // success.
    }
    // re-add the mandatory items
    if (childNodeDef != null) {
        childN = n.addNode(childNodeDef.getName(), childNodeDef.getDefaultPrimaryType().getName());
    }
    if (childPropDef != null) {
        // TODO: check if definition defines default values
        childP = n.setProperty(childPropDef.getName(), "any value");
    }
    // save must succeed now.
    testRootNode.save();
}
Also used : NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) Node(javax.jcr.Node) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) RepositoryException(javax.jcr.RepositoryException) Property(javax.jcr.Property)

Example 94 with ConstraintViolationException

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

the class MandatoryItemTest method testCreation.

public void testCreation() throws NotExecutableException, RepositoryException {
    Node n;
    try {
        n = testRootNode.addNode(nodeName1, testNodeType);
    } catch (RepositoryException e) {
        throw new NotExecutableException();
    }
    try {
        testRootNode.save();
        fail("Saving without having added the mandatory child items must fail.");
    } catch (ConstraintViolationException e) {
    // success
    }
    if (childNodeDef != null) {
        n.addNode(childNodeDef.getName(), childNodeDef.getDefaultPrimaryType().getName());
    }
    if (childPropDef != null) {
        // TODO: check if definition defines default values
        n.setProperty(childPropDef.getName(), "any value");
    }
    // now save must succeed.
    testRootNode.save();
}
Also used : NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) Node(javax.jcr.Node) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) RepositoryException(javax.jcr.RepositoryException)

Example 95 with ConstraintViolationException

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

the class EffectiveNodeType method checkSetProperty.

public void checkSetProperty(PropertyState property) throws RepositoryException {
    PropertyDefinition definition = getDefinition(property);
    if (definition.isProtected()) {
        return;
    }
    NodeType nt = definition.getDeclaringNodeType();
    if (definition.isMultiple()) {
        List<Value> values = ValueFactoryImpl.createValues(property, ntMgr.getNamePathMapper());
        if (!nt.canSetProperty(property.getName(), values.toArray(new Value[values.size()]))) {
            throw new ConstraintViolationException("Cannot set property '" + property.getName() + "' to '" + values + '\'');
        }
    } else {
        Value v = ValueFactoryImpl.createValue(property, ntMgr.getNamePathMapper());
        if (!nt.canSetProperty(property.getName(), v)) {
            throw new ConstraintViolationException("Cannot set property '" + property.getName() + "' to '" + v + '\'');
        }
    }
}
Also used : NodeType(javax.jcr.nodetype.NodeType) Value(javax.jcr.Value) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition)

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