Search in sources :

Example 91 with NodeType

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

the class PredefinedNodeTypeTest method testPredefinedNodeType.

/**
 * Tests that the named node type matches the JSR 170 specification.
 * The test is performed by genererating a node type definition spec
 * string in the format used by the JSR 170 specification, and comparing
 * the result with a static spec file extracted from the specification
 * itself.
 * <p>
 * Note that the extracted spec files are not exact copies of the node
 * type specification in the JSR 170 document. Some formatting and
 * ordering changes have been made to simplify the test code, but the
 * semantics remain the same.
 *
 * @param name node type name
 * @param propsVariant whether the properties of this node type may
 *   have implementation variant autocreated and OPV flags.
 * @throws NotExecutableException if the node type is not supported by
 *   this repository implementation.
 */
private void testPredefinedNodeType(String name, boolean propsVariant) throws NotExecutableException {
    try {
        StringBuffer spec = new StringBuffer();
        String resource = "org/apache/jackrabbit/test/api/nodetype/spec/" + name.replace(':', '-') + ".txt";
        Reader reader = new InputStreamReader(getClass().getClassLoader().getResourceAsStream(resource));
        for (int ch = reader.read(); ch != -1; ch = reader.read()) {
            spec.append((char) ch);
        }
        NodeType type = manager.getNodeType(name);
        String current = getNodeTypeSpec(type, propsVariant).trim();
        if (!System.getProperty("line.separator").equals("\n")) {
            current = normalizeLineSeparators(current);
        }
        String expected = normalizeLineSeparators(spec.toString()).trim();
        assertEquals("Predefined node type " + name, expected, current);
        // check minimum declared supertypes
        Set<String> declaredSupertypes = new HashSet<String>();
        for (Iterator<NodeType> it = Arrays.asList(type.getDeclaredSupertypes()).iterator(); it.hasNext(); ) {
            NodeType nt = it.next();
            declaredSupertypes.add(nt.getName());
        }
        for (Iterator<String> it = Arrays.asList(SUPERTYPES.get(name)).iterator(); it.hasNext(); ) {
            String supertype = it.next();
            assertTrue("Predefined node type " + name + " does not " + "declare supertype " + supertype, declaredSupertypes.contains(supertype));
        }
    } catch (IOException e) {
        fail(e.getMessage());
    } catch (NoSuchNodeTypeException e) {
        // only nt:base is strictly required
        if ("nt:base".equals(name)) {
            fail(e.getMessage());
        } else {
            throw new NotExecutableException("NodeType " + name + " not supported by this repository implementation.");
        }
    } catch (RepositoryException e) {
        fail(e.getMessage());
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) NoSuchNodeTypeException(javax.jcr.nodetype.NoSuchNodeTypeException) NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) NodeType(javax.jcr.nodetype.NodeType) HashSet(java.util.HashSet)

Example 92 with NodeType

use of javax.jcr.nodetype.NodeType 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 93 with NodeType

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

the class NodeTypeTest method testIsNodeTypeQName.

/**
 * Like {@link #testIsNodeType()}, but using qualified names
 */
public void testIsNodeTypeQName() throws RepositoryException {
    // find a primary node type but not "nt:base"
    NodeTypeIterator types = manager.getPrimaryNodeTypes();
    while (types.hasNext()) {
        NodeType type = types.nextNodeType();
        String typename = type.getName();
        String ns = session.getNamespaceURI(AbstractJCRTest.getPrefix(typename));
        if (ns.length() != 0 && !ns.contains(":")) {
            log.warn("Node type '" + typename + "' has invalid namespace '" + ns + "', thus skipping testIsNodeTypeQName() for this type");
        } else {
            String qn = AbstractJCRTest.getQualifiedName(session, typename);
            assertTrue("isNodeType(String nodeTypeName) must return true if " + "NodeType is nodeTypeName", type.isNodeType(qn));
        }
        if (type.isMixin()) {
            assertFalse("isNodeType(String nodeTypeName) must return " + "false if NodeType is not a subtype of " + "nodeTypeName", type.isNodeType(NodeType.NT_BASE));
        } else {
            assertTrue("isNodeType(String nodeTypeName) must return true if " + "NodeType is a subtype of nodeTypeName", type.isNodeType(NodeType.NT_BASE));
        }
    }
}
Also used : NodeType(javax.jcr.nodetype.NodeType) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator)

Example 94 with NodeType

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

the class NodeTypeTest method testGetDeclaredPropertyDefs.

/**
 * Test if all property defs returned by getDeclatedPropertyDefs() are also
 * returned by getPropertyDefs(). All existing node types are tested.
 */
public void testGetDeclaredPropertyDefs() throws RepositoryException {
    NodeTypeIterator types = manager.getAllNodeTypes();
    while (types.hasNext()) {
        NodeType type = types.nextNodeType();
        PropertyDefinition[] declaredDefs = type.getDeclaredPropertyDefinitions();
        PropertyDefinition[] defs = type.getPropertyDefinitions();
        try {
            for (int i = 0; i < declaredDefs.length; i++) {
                boolean exists = false;
                for (int j = 0; j < defs.length; j++) {
                    if (defs[j].getName().equals(declaredDefs[i].getName())) {
                        exists = true;
                        break;
                    }
                }
                assertTrue("All property defs returned by " + "getDeclaredPropertyDefs() must also be " + "returned by getPropertyDefs()", exists);
            }
        } catch (ArrayIndexOutOfBoundsException e) {
            fail("The array returned by " + "getDeclaredPropertyDefs() must not exceed " + "the one returned by getPropertyDefs()");
        }
    }
}
Also used : NodeType(javax.jcr.nodetype.NodeType) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition)

Example 95 with NodeType

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

the class NodeTypeTest method testGetPropertyDefs.

/**
 * Test if getPropertyDefs() of a primary node returns also "jcr:primaryType"
 * which is inherited from "nt:base".
 */
public void testGetPropertyDefs() throws NotExecutableException, RepositoryException {
    // find a primary node type but not "nt:base"
    NodeTypeIterator types = manager.getPrimaryNodeTypes();
    while (types.hasNext()) {
        NodeType type = types.nextNodeType();
        PropertyDefinition[] defs = type.getPropertyDefinitions();
        boolean hasJCRPrimaryType = false;
        for (int i = 0; i < defs.length; i++) {
            if (defs[i].getName().equals(jcrPrimaryType)) {
                hasJCRPrimaryType = true;
                break;
            }
        }
        assertTrue("getPropertyDefs() of a primary node type " + "must return also \"jcr:primaryType\".", hasJCRPrimaryType);
    }
}
Also used : NodeType(javax.jcr.nodetype.NodeType) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition)

Aggregations

NodeType (javax.jcr.nodetype.NodeType)272 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)84 PropertyDefinition (javax.jcr.nodetype.PropertyDefinition)81 Node (javax.jcr.Node)63 Value (javax.jcr.Value)60 NodeTypeIterator (javax.jcr.nodetype.NodeTypeIterator)55 NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)46 NodeDefinition (javax.jcr.nodetype.NodeDefinition)45 RepositoryException (javax.jcr.RepositoryException)41 ArrayList (java.util.ArrayList)30 Test (org.junit.Test)30 Session (javax.jcr.Session)29 NodeTypeIteratorAdapter (org.apache.jackrabbit.commons.iterator.NodeTypeIteratorAdapter)16 NoSuchNodeTypeException (javax.jcr.nodetype.NoSuchNodeTypeException)15 Name (org.apache.jackrabbit.spi.Name)15 HashSet (java.util.HashSet)14 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)12 NodeIterator (javax.jcr.NodeIterator)7 Property (javax.jcr.Property)7 QNodeTypeDefinition (org.apache.jackrabbit.spi.QNodeTypeDefinition)7