Search in sources :

Example 41 with NodeTypeManager

use of javax.jcr.nodetype.NodeTypeManager 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 42 with NodeTypeManager

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

the class NodeTypeUtil method locatePropertyDef.

/**
 * Locate a property def parsing all node types
 *
 * @param session      the session to access the node types
 * @param propertyType the type of the returned property. -1 indicates to
 *                     return a property of any type but not UNDEFIEND
 * @param multiple     if true, the returned <code>PropertyDef</code> is
 *                     multiple, else not
 * @param isProtected  if true, the returned <code>PropertyDef</code> is
 *                     protected, else not
 * @param residual     if true, the returned <code>PropertyDef</code> is of
 *                     the residual name "*", else not
 * @return the first <code>PropertyDef</code> found fitting the
 *         requirements
 */
public static PropertyDefinition locatePropertyDef(Session session, int propertyType, boolean multiple, boolean isProtected, boolean constraints, boolean residual) throws RepositoryException {
    NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
    NodeTypeIterator types = manager.getAllNodeTypes();
    while (types.hasNext()) {
        NodeType type = types.nextNodeType();
        PropertyDefinition[] propDefs = type.getDeclaredPropertyDefinitions();
        for (int i = 0; i < propDefs.length; i++) {
            PropertyDefinition propDef = propDefs[i];
            if (propertyType != ANY_PROPERTY_TYPE && propDef.getRequiredType() != propertyType) {
                continue;
            }
            if (propertyType == ANY_PROPERTY_TYPE && propDef.getRequiredType() == PropertyType.UNDEFINED) {
                continue;
            }
            if (multiple && !propDef.isMultiple()) {
                continue;
            }
            if (!multiple && propDef.isMultiple()) {
                continue;
            }
            if (isProtected && !propDef.isProtected()) {
                continue;
            }
            if (!isProtected && propDef.isProtected()) {
                continue;
            }
            String[] vc = propDef.getValueConstraints();
            if (!constraints && vc != null && vc.length > 0) {
                continue;
            }
            if (constraints) {
                // property def with constraints requested
                if (vc == null || vc.length == 0) {
                    // property def has no constraints
                    continue;
                }
            }
            if (!residual && propDef.getName().equals("*")) {
                continue;
            }
            if (residual && !propDef.getName().equals("*")) {
                continue;
            }
            // is another residual definition
            if (residual) {
                // check if there is another residual property def
                if (getNumResidualPropDefs(type) > 1) {
                    continue;
                }
            }
            if (!residual) {
                // type
                if (getNumResidualPropDefs(type) > 0) {
                    continue;
                }
            }
            return propDef;
        }
    }
    return null;
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NodeType(javax.jcr.nodetype.NodeType) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition)

Example 43 with NodeTypeManager

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

the class ChildNodeTest method testChildNodesDoNotMatchSelector.

public void testChildNodesDoNotMatchSelector() throws RepositoryException, NotExecutableException {
    testRootNode.addNode(nodeName1, testNodeType);
    superuser.save();
    NodeTypeManager ntMgr = superuser.getWorkspace().getNodeTypeManager();
    NodeTypeIterator it = ntMgr.getPrimaryNodeTypes();
    NodeType testNt = ntMgr.getNodeType(testNodeType);
    while (it.hasNext()) {
        NodeType nt = it.nextNodeType();
        if (!testNt.isNodeType(nt.getName())) {
            // perform test
            QueryObjectModel qom = qf.createQuery(qf.selector(nt.getName(), "s"), qf.childNode("s", testRoot), null, null);
            checkQOM(qom, new Node[] {});
            return;
        }
    }
    throw new NotExecutableException("No suitable node type found to " + "perform test against '" + testNodeType + "' nodes");
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) NodeType(javax.jcr.nodetype.NodeType) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator) QueryObjectModel(javax.jcr.query.qom.QueryObjectModel)

Example 44 with NodeTypeManager

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

the class DescendantNodeTest method testDescendantNodesDoNotMatchSelector.

public void testDescendantNodesDoNotMatchSelector() throws RepositoryException, NotExecutableException {
    testRootNode.addNode(nodeName1, testNodeType);
    superuser.save();
    NodeTypeManager ntMgr = superuser.getWorkspace().getNodeTypeManager();
    NodeTypeIterator it = ntMgr.getPrimaryNodeTypes();
    NodeType testNt = ntMgr.getNodeType(testNodeType);
    while (it.hasNext()) {
        NodeType nt = it.nextNodeType();
        if (!testNt.isNodeType(nt.getName())) {
            // perform test
            QueryObjectModel qom = qf.createQuery(qf.selector(nt.getName(), "s"), qf.descendantNode("s", testRoot), null, null);
            checkQOM(qom, new Node[] {});
            return;
        }
    }
    throw new NotExecutableException("No suitable node type found to " + "perform test against '" + testNodeType + "' nodes");
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) NodeType(javax.jcr.nodetype.NodeType) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator) QueryObjectModel(javax.jcr.query.qom.QueryObjectModel)

Example 45 with NodeTypeManager

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

the class NodeTypeUtil method locatePropertyDef.

/**
 * Locate a property def parsing all node types
 *
 * @param session     the session to access the node types
 * @param isProtected if true, the returned <code>PropertyDef</code> is
 *                    protected, else not
 * @param mandatory   if true, the returned <code>PropertyDef</code> is
 *                    mandatory, else not
 * @return the first <code>PropertyDef</code> found fitting the
 *         requirements
 */
public static PropertyDefinition locatePropertyDef(Session session, boolean isProtected, boolean mandatory) throws RepositoryException {
    NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
    NodeTypeIterator types = manager.getAllNodeTypes();
    while (types.hasNext()) {
        NodeType type = types.nextNodeType();
        PropertyDefinition[] propDefs = type.getDeclaredPropertyDefinitions();
        for (int i = 0; i < propDefs.length; i++) {
            PropertyDefinition propDef = propDefs[i];
            if (propDef.getName().equals("*")) {
                continue;
            }
            if (isProtected && !propDef.isProtected()) {
                continue;
            }
            if (!isProtected && propDef.isProtected()) {
                continue;
            }
            if (mandatory && !propDef.isMandatory()) {
                continue;
            }
            if (!mandatory && propDef.isMandatory()) {
                continue;
            }
            return propDef;
        }
    }
    return null;
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NodeType(javax.jcr.nodetype.NodeType) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition)

Aggregations

NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)103 NodeType (javax.jcr.nodetype.NodeType)47 Node (javax.jcr.Node)38 Session (javax.jcr.Session)35 NodeTypeTemplate (javax.jcr.nodetype.NodeTypeTemplate)31 NodeTypeIterator (javax.jcr.nodetype.NodeTypeIterator)29 Test (org.junit.Test)25 RepositoryException (javax.jcr.RepositoryException)18 ByteArrayInputStream (java.io.ByteArrayInputStream)15 PropertyDefinition (javax.jcr.nodetype.PropertyDefinition)15 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)14 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)11 ArrayList (java.util.ArrayList)10 Workspace (javax.jcr.Workspace)10 InputStream (java.io.InputStream)9 NodeDefinition (javax.jcr.nodetype.NodeDefinition)9 PropertyDefinitionTemplate (javax.jcr.nodetype.PropertyDefinitionTemplate)9 InputStreamReader (java.io.InputStreamReader)8 NamespaceRegistry (javax.jcr.NamespaceRegistry)8 Value (javax.jcr.Value)8