Search in sources :

Example 6 with NodeTypeIterator

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

the class CanAddChildNodeCallWithNodeTypeTest method testCanAddMixinType.

/**
     * Tests if <code>NodeType.canAddChildNode(String childNodeName, String nodeTypeName)</code>
     * returns false if <code>nodeTypeName</code> represents a mixin.
     */
public void testCanAddMixinType() throws NotExecutableException, RepositoryException {
    NodeDefinition nodeDef = NodeTypeUtil.locateChildNodeDef(session, false, false, false);
    if (nodeDef == null) {
        throw new NotExecutableException("No testable node type found.");
    }
    NodeType nodeType = nodeDef.getDeclaringNodeType();
    String childNodeName = nodeDef.getName();
    String mixinName;
    NodeTypeIterator it = manager.getMixinNodeTypes();
    if (it.hasNext()) {
        mixinName = it.nextNodeType().getName();
    } else {
        throw new NotExecutableException("No mixin type found.");
    }
    assertFalse("NodeType.canAddChildNode(String childNodeName, String nodeTypeName) " + "must return false if nodeTypeName represents a mixin type.", nodeType.canAddChildNode(childNodeName, mixinName));
}
Also used : NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) NodeType(javax.jcr.nodetype.NodeType) NodeDefinition(javax.jcr.nodetype.NodeDefinition) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator)

Example 7 with NodeTypeIterator

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

the class NodeTypeManagerImpl method toString.

//-------------------------------------------------------------< Object >---
/**
     * Returns the the state of this instance in a human readable format.
     */
public String toString() {
    StringBuilder builder = new StringBuilder();
    builder.append("NodeTypeManager (" + super.toString() + ")\n");
    builder.append("All NodeTypes:\n");
    try {
        NodeTypeIterator iter = this.getAllNodeTypes();
        while (iter.hasNext()) {
            NodeType nt = iter.nextNodeType();
            builder.append(nt.getName());
            builder.append("\n\tSupertypes");
            for (NodeType supertype : nt.getSupertypes()) {
                builder.append("\n\t\t" + supertype.getName());
            }
            builder.append("\n\tMixin\t" + nt.isMixin());
            builder.append("\n\tOrderableChildNodes\t" + nt.hasOrderableChildNodes());
            builder.append("\n\tPrimaryItemName\t" + (nt.getPrimaryItemName() == null ? "<null>" : nt.getPrimaryItemName()));
            for (PropertyDefinition aPd : nt.getPropertyDefinitions()) {
                builder.append("\n\tPropertyDefinition");
                builder.append(" (declared in " + aPd.getDeclaringNodeType().getName() + ") ");
                builder.append("\n\t\tName\t\t" + (aPd.getName()));
                String type = aPd.getRequiredType() == 0 ? "null" : PropertyType.nameFromValue(aPd.getRequiredType());
                builder.append("\n\t\tRequiredType\t" + type);
                String[] vca = aPd.getValueConstraints();
                StringBuffer constraints = new StringBuffer();
                if (vca == null) {
                    constraints.append("<null>");
                } else {
                    for (String aVca : vca) {
                        if (constraints.length() > 0) {
                            constraints.append(", ");
                        }
                        constraints.append(aVca);
                    }
                }
                builder.append("\n\t\tValueConstraints\t" + constraints.toString());
                Value[] defVals = aPd.getDefaultValues();
                StringBuffer defaultValues = new StringBuffer();
                if (defVals == null) {
                    defaultValues.append("<null>");
                } else {
                    for (Value defVal : defVals) {
                        if (defaultValues.length() > 0) {
                            defaultValues.append(", ");
                        }
                        defaultValues.append(defVal.getString());
                    }
                }
                builder.append("\n\t\tDefaultValue\t" + defaultValues.toString());
                builder.append("\n\t\tAutoCreated\t" + aPd.isAutoCreated());
                builder.append("\n\t\tMandatory\t" + aPd.isMandatory());
                builder.append("\n\t\tOnVersion\t" + OnParentVersionAction.nameFromValue(aPd.getOnParentVersion()));
                builder.append("\n\t\tProtected\t" + aPd.isProtected());
                builder.append("\n\t\tMultiple\t" + aPd.isMultiple());
            }
            for (NodeDefinition aNd : nt.getChildNodeDefinitions()) {
                builder.append("\n\tNodeDefinition");
                builder.append(" (declared in " + aNd.getDeclaringNodeType() + ") ");
                builder.append("\n\t\tName\t\t" + aNd.getName());
                NodeType[] reqPrimaryTypes = aNd.getRequiredPrimaryTypes();
                if (reqPrimaryTypes != null && reqPrimaryTypes.length > 0) {
                    for (NodeType reqPrimaryType : reqPrimaryTypes) {
                        builder.append("\n\t\tRequiredPrimaryType\t" + reqPrimaryType.getName());
                    }
                }
                NodeType defPrimaryType = aNd.getDefaultPrimaryType();
                if (defPrimaryType != null) {
                    builder.append("\n\t\tDefaultPrimaryType\t" + defPrimaryType.getName());
                }
                builder.append("\n\t\tAutoCreated\t" + aNd.isAutoCreated());
                builder.append("\n\t\tMandatory\t" + aNd.isMandatory());
                builder.append("\n\t\tOnVersion\t" + OnParentVersionAction.nameFromValue(aNd.getOnParentVersion()));
                builder.append("\n\t\tProtected\t" + aNd.isProtected());
                builder.append("\n\t\tAllowsSameNameSiblings\t" + aNd.allowsSameNameSiblings());
            }
        }
    } catch (RepositoryException e) {
        builder.append(e.getMessage());
    }
    return builder.toString();
}
Also used : NodeType(javax.jcr.nodetype.NodeType) Value(javax.jcr.Value) QNodeDefinition(org.apache.jackrabbit.spi.QNodeDefinition) NodeDefinition(javax.jcr.nodetype.NodeDefinition) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator) RepositoryException(javax.jcr.RepositoryException) QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition)

Example 8 with NodeTypeIterator

use of javax.jcr.nodetype.NodeTypeIterator 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 9 with NodeTypeIterator

use of javax.jcr.nodetype.NodeTypeIterator 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 10 with NodeTypeIterator

use of javax.jcr.nodetype.NodeTypeIterator 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)

Aggregations

NodeTypeIterator (javax.jcr.nodetype.NodeTypeIterator)65 NodeType (javax.jcr.nodetype.NodeType)53 NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)28 PropertyDefinition (javax.jcr.nodetype.PropertyDefinition)15 NodeDefinition (javax.jcr.nodetype.NodeDefinition)13 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)11 Node (javax.jcr.Node)9 RepositoryException (javax.jcr.RepositoryException)9 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)8 ArrayList (java.util.ArrayList)6 Session (javax.jcr.Session)6 HashSet (java.util.HashSet)4 Value (javax.jcr.Value)3 QueryObjectModel (javax.jcr.query.qom.QueryObjectModel)3 NodeTypeIteratorAdapter (org.apache.jackrabbit.commons.iterator.NodeTypeIteratorAdapter)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 Property (javax.jcr.Property)2