Search in sources :

Example 21 with ConstraintViolationException

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

the class NodeSetPrimaryTypeTest method testSetMixinAsPrimaryType.

/**
     * Tests if <code>Node.setPrimaryType(String)</code> throws a
     * <code>ConstraintViolationException</code> if the
     * name of a mixin type is passed
     */
public void testSetMixinAsPrimaryType() throws RepositoryException {
    Session session = testRootNode.getSession();
    NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
    NodeTypeIterator nts = manager.getMixinNodeTypes();
    while (nts.hasNext()) {
        try {
            Node node = testRootNode.addNode(nodeName1, testNodeType);
            node.setPrimaryType(nts.nextNodeType().getName());
            fail("Node.setPrimaryType(String) must throw ConstraintViolationException if the specified node type name refers to a mixin.");
        } catch (ConstraintViolationException e) {
        // success
        } finally {
            // reset the changes.
            session.refresh(false);
        }
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) Node(javax.jcr.Node) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) Session(javax.jcr.Session)

Example 22 with ConstraintViolationException

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

the class NodeSetPrimaryTypeTest method testSetAbstractAsPrimaryType.

/**
     * Tests if <code>Node.setPrimaryType(String)</code> throws a
     * <code>ConstraintViolationException</code> if the
     * name of a mixin type is passed
     */
public void testSetAbstractAsPrimaryType() throws RepositoryException {
    Session session = testRootNode.getSession();
    NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
    NodeTypeIterator nts = manager.getPrimaryNodeTypes();
    while (nts.hasNext()) {
        NodeType nt = nts.nextNodeType();
        if (nt.isAbstract()) {
            try {
                Node node = testRootNode.addNode(nodeName1, testNodeType);
                node.setPrimaryType(nt.getName());
                fail("Node.setPrimaryType(String) must throw ConstraintViolationException if the specified node type name refers to an abstract node type.");
            } catch (ConstraintViolationException e) {
            // success
            } finally {
                // reset the changes.
                session.refresh(false);
            }
        }
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NodeType(javax.jcr.nodetype.NodeType) Node(javax.jcr.Node) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) Session(javax.jcr.Session)

Example 23 with ConstraintViolationException

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

the class NodeSetPrimaryTypeTest method testSetPrimaryType.

// TODO: test if node definition is properly reset
// TODO: test if child items are properly reset upon changing definition
// TODO: test if conflicts are properly detected
/**
     * Tests a successful call to <code>Node.setPrimaryType(String)</code>
     */
public void testSetPrimaryType() throws RepositoryException {
    Session session = testRootNode.getSession();
    Session otherSession = null;
    String nonExistingMixinName = NodeMixinUtil.getNonExistingMixinName(session);
    Node node = testRootNode.addNode(nodeName1, testNodeType);
    superuser.save();
    // TODO improve. retrieve settable node type name from config.
    NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
    NodeTypeIterator nts = manager.getPrimaryNodeTypes();
    while (nts.hasNext()) {
        NodeType nt = nts.nextNodeType();
        String ntName = nt.getName();
        if (!nt.isAbstract() && !ntFrozenNode.equals(ntName)) {
            try {
                node.setPrimaryType(ntName);
                // property value must be adjusted immediately
                assertEquals("The value of the jcr:primaryType property must change upon setPrimaryType.", ntName, node.getProperty(jcrPrimaryType).getString());
                // save changes -> reflected upon Node.getPrimaryNodeType and Property.getValue
                superuser.save();
                assertEquals("Node.getPrimaryNodeType must reflect the changes made.", ntName, node.getPrimaryNodeType().getName());
                assertEquals("The value of the jcr:primaryType property must change upon setPrimaryType.", ntName, node.getProperty(jcrPrimaryType).getString());
                otherSession = getHelper().getReadOnlySession();
                assertEquals("Node.getPrimaryNodeType must reflect the changes made.", ntName, otherSession.getNode(node.getPath()).getPrimaryNodeType().getName());
                assertEquals("The value of the jcr:primaryType property must change upon setPrimaryType.", ntName, otherSession.getNode(node.getPath()).getProperty(jcrPrimaryType).getString());
                // was successful
                return;
            } catch (ConstraintViolationException e) {
            // may happen as long as arbitrary primary types are used for testing -> ignore
            } finally {
                if (otherSession != null) {
                    otherSession.logout();
                }
                // revert any unsaved changes.
                session.refresh(false);
            }
        }
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) Node(javax.jcr.Node) NodeType(javax.jcr.nodetype.NodeType) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) Session(javax.jcr.Session)

Example 24 with ConstraintViolationException

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

the class NodeTest method testRemoveMandatoryNode.

/**
     * Creates a node with a mandatory child node using {@link
     * Node#addNode(String, String)}, saves on parent node then tries to delete
     * the mandatory child node.
     * <p>
     * This should throw a {@link ConstraintViolationException}.
     * <p>
     * Prerequisites: <ul>
     * <li><code>javax.jcr.tck.NodeTest.testRemoveMandatoryNode.nodetype2</code>
     * a node type that has a mandatory child node</li> <li><code>javax.jcr.tck.NodeTest.testRemoveMandatoryNode.nodetype3</code>
     * nodetype of the mandatory child node</li> <li><code>javax.jcr.tck.NodeTest.testRemoveMandatoryNode.nodename3</code>
     * name of the mandatory child node</li> </ul>
     */
public void testRemoveMandatoryNode() throws RepositoryException {
    // get default workspace test root node using superuser session
    Node defaultRootNode = (Node) superuser.getItem(testRootNode.getPath());
    // create the node with the mandatory child node definition
    Node defaultTestNode = defaultRootNode.addNode(nodeName2, getProperty("nodetype2"));
    // add the mandatory child node
    Node defaultTestNodeChild = defaultTestNode.addNode(nodeName3, getProperty("nodetype3"));
    // save changes
    defaultRootNode.save();
    try {
        // try to remove the mandatory node
        defaultTestNodeChild.remove();
        defaultTestNode.save();
        fail("Removing a mandatory node should throw a ConstraintViolationException");
    } catch (ConstraintViolationException e) {
    // ok, works as expected
    }
}
Also used : Node(javax.jcr.Node) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException)

Example 25 with ConstraintViolationException

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

the class NodeImpl method addNode.

/**
     * @see Node#addNode(String, String)
     */
public Node addNode(String relPath, String primaryNodeTypeName) throws ItemExistsException, PathNotFoundException, NoSuchNodeTypeException, LockException, VersionException, ConstraintViolationException, RepositoryException {
    checkIsWritable();
    // build path object and retrieve parent node
    Path nodePath = getPath(relPath).getNormalizedPath();
    if (nodePath.getIndex() != Path.INDEX_UNDEFINED) {
        String msg = "Illegal subscript specified: " + relPath;
        log.debug(msg);
        throw new RepositoryException(msg);
    }
    NodeImpl parentNode;
    if (nodePath.getLength() == 1) {
        parentNode = this;
    } else {
        Path parentPath = nodePath.getAncestor(1);
        ItemManager itemMgr = getItemManager();
        if (itemMgr.nodeExists(parentPath)) {
            parentNode = (NodeImpl) itemMgr.getNode(parentPath);
        } else if (itemMgr.propertyExists(parentPath)) {
            String msg = "Cannot add a node to property " + LogUtil.safeGetJCRPath(parentPath, session.getPathResolver());
            log.debug(msg);
            throw new ConstraintViolationException(msg);
        } else {
            throw new PathNotFoundException("Cannot add a new node to a non-existing parent at " + LogUtil.safeGetJCRPath(parentPath, session.getPathResolver()));
        }
    }
    // get names objects for node and nt
    Name nodeName = nodePath.getName();
    Name ntName = (primaryNodeTypeName == null) ? null : getQName(primaryNodeTypeName);
    // create new node (including validation checks)
    return parentNode.createNode(nodeName, ntName);
}
Also used : Path(org.apache.jackrabbit.spi.Path) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) RepositoryException(javax.jcr.RepositoryException) PathNotFoundException(javax.jcr.PathNotFoundException) Name(org.apache.jackrabbit.spi.Name)

Aggregations

ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)177 Node (javax.jcr.Node)73 RepositoryException (javax.jcr.RepositoryException)39 Name (org.apache.jackrabbit.spi.Name)32 Value (javax.jcr.Value)30 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)28 Test (org.junit.Test)26 PropertyDefinition (javax.jcr.nodetype.PropertyDefinition)22 QPropertyDefinition (org.apache.jackrabbit.spi.QPropertyDefinition)22 Session (javax.jcr.Session)17 ItemExistsException (javax.jcr.ItemExistsException)16 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 EffectiveNodeType (org.apache.jackrabbit.core.nodetype.EffectiveNodeType)13 Authorizable (org.apache.jackrabbit.api.security.user.Authorizable)12 NodeId (org.apache.jackrabbit.core.id.NodeId)12 Path (org.apache.jackrabbit.spi.Path)12