Search in sources :

Example 46 with NodeTypeIterator

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

the class NodeTypeIndexingUtils method createPrimaryTypeSynonymsFile.

public static File createPrimaryTypeSynonymsFile(String path, Session session) throws Exception {
    File file = new File(path);
    StringWriter stringWriter = new StringWriter();
    NodeTypeIterator allNodeTypes = session.getWorkspace().getNodeTypeManager().getAllNodeTypes();
    while (allNodeTypes.hasNext()) {
        NodeType nodeType = allNodeTypes.nextNodeType();
        NodeType[] superTypes = nodeType.getSupertypes();
        if (superTypes != null && superTypes.length > 0) {
            stringWriter.append(nodeType.getName()).append(" => ");
            for (int i = 0; i < superTypes.length; i++) {
                stringWriter.append(superTypes[i].getName());
                if (i < superTypes.length - 1) {
                    stringWriter.append(',');
                }
                stringWriter.append(' ');
            }
            stringWriter.append('\n');
        }
    }
    FileOutputStream fileOutputStream = new FileOutputStream(file);
    fileOutputStream.write(stringWriter.toString().getBytes("UTF-8"));
    fileOutputStream.flush();
    fileOutputStream.close();
    if (file.exists() || file.createNewFile()) {
        return file;
    } else {
        throw new IOException("primary types synonyms file could not be created");
    }
}
Also used : StringWriter(java.io.StringWriter) NodeType(javax.jcr.nodetype.NodeType) FileOutputStream(java.io.FileOutputStream) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator) IOException(java.io.IOException) File(java.io.File)

Example 47 with NodeTypeIterator

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

the class PropertyDefTest method testGetValueConstraints.

/**
     * Tests if value constraints match the pattern specified by the required
     * property type.
     * <p>
     * The test runs for all value constraints of all properties of all
     * available node types.
     */
public void testGetValueConstraints() throws RepositoryException {
    NodeTypeIterator types = manager.getAllNodeTypes();
    // loop all node types
    while (types.hasNext()) {
        NodeType type = types.nextNodeType();
        PropertyDefinition[] defs = type.getPropertyDefinitions();
        for (int i = 0; i < defs.length; i++) {
            PropertyDefinition def = defs[i];
            String[] constraints = def.getValueConstraints();
            if (constraints != null) {
                for (int j = 0; j < constraints.length; j++) {
                    Matcher matcher;
                    switch(defs[i].getRequiredType()) {
                        case PropertyType.STRING:
                        case PropertyType.UNDEFINED:
                            // any value matches
                            break;
                        case PropertyType.BINARY:
                            matcher = CONSTRAINTSPATTERN_BINARY.matcher(constraints[j]);
                            assertTrue("Value constraint does not match " + "the pattern of PropertyType.BINARY ", matcher.matches());
                            break;
                        case PropertyType.DATE:
                            matcher = CONSTRAINTSPATTERN_DATE.matcher(constraints[j]);
                            assertTrue("Value constraint does not match " + "the pattern of PropertyType.DATE ", matcher.matches());
                            break;
                        case PropertyType.LONG:
                            matcher = CONSTRAINTSPATTERN_LONG.matcher(constraints[j]);
                            assertTrue("Value constraint does not match " + "the pattern of PropertyType.LONG", matcher.matches());
                            break;
                        case PropertyType.DOUBLE:
                            matcher = CONSTRAINTSPATTERN_DOUBLE.matcher(constraints[j]);
                            assertTrue("Value constraint does not match " + "the pattern of PropertyType.DOUBLE", matcher.matches());
                            break;
                        case PropertyType.NAME:
                            matcher = PropertyUtil.PATTERN_NAME.matcher(constraints[j]);
                            assertTrue("Value constraint does not match " + "the pattern of PropertyType.NAME", matcher.matches());
                            checkPrefix(constraints[j]);
                            break;
                        case PropertyType.PATH:
                            matcher = CONSTRAINTSPATTERN_PATH.matcher(constraints[j]);
                            assertTrue("Value constraint does not match " + "the pattern of PropertyType.PATH", matcher.matches());
                            String[] elems = constraints[j].split("/");
                            for (int k = 0; k < elems.length; k++) {
                                checkPrefix(elems[k]);
                            }
                            break;
                        case PropertyType.REFERENCE:
                            matcher = PropertyUtil.PATTERN_NAME.matcher(constraints[j]);
                            assertTrue("Value constraint does not match " + "the pattern of PropertyType.REFERENCE", matcher.matches());
                            checkPrefix(constraints[j]);
                            break;
                        case PropertyType.BOOLEAN:
                            assertTrue("Value constraint does not match " + "the pattern of PropertyType.BOOLEAN", constraints[j].equals("true") || constraints[j].equals("false"));
                            break;
                    }
                }
            }
        }
    }
}
Also used : Matcher(java.util.regex.Matcher) NodeType(javax.jcr.nodetype.NodeType) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition)

Example 48 with NodeTypeIterator

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

the class IndexDefinition method getAllNodeTypes.

private static List<String> getAllNodeTypes(ReadOnlyNodeTypeManager ntReg) {
    try {
        List<String> typeNames = newArrayList();
        NodeTypeIterator ntItr = ntReg.getAllNodeTypes();
        while (ntItr.hasNext()) {
            typeNames.add(ntItr.nextNodeType().getName());
        }
        return typeNames;
    } catch (RepositoryException e) {
        throw new RuntimeException(e);
    }
}
Also used : NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator) RepositoryException(javax.jcr.RepositoryException)

Example 49 with NodeTypeIterator

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

the class SetValueConstraintViolationExceptionTest method testReferenceProperty.

/**
     * Tests if setValue(Node value) and setValue(Value value) where value is a
     * ReferenceValue throw a ConstraintViolationException if the change would
     * violate a value constraint
     */
public void testReferenceProperty() throws NotExecutableException, RepositoryException {
    // locate a PropertyDefinition with ValueConstraints
    PropertyDefinition propDef = NodeTypeUtil.locatePropertyDef(superuser, PropertyType.REFERENCE, false, false, true, false);
    if (propDef == null) {
        throw new NotExecutableException("No 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();
        prop = node.setProperty(propDef.getName(), nodeSatisfied);
        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(Node value)
    try {
        prop.setValue(nodeNotSatisfied);
        node.save();
        fail("setValue(Node value) must throw a ConstraintViolationException " + "if the change would violate a node type constraint " + "either immediately or on save");
    } catch (ConstraintViolationException e) {
    // success
    }
    // test of signature setValue(Value value)
    try {
        prop.setValue(superuser.getValueFactory().createValue(nodeNotSatisfied));
        node.save();
        fail("setValue(Value value) 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) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition) Property(javax.jcr.Property)

Example 50 with NodeTypeIterator

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

the class SetPropertyConstraintViolationExceptionTest method testReferenceProperty.

/**
     * Tests if setProperty(String name, Node value) and setProperty(String
     * name, Value value) where value is a ReferenceValue throw a
     * ConstraintViolationException either immediately (by setProperty()), or on
     * save, if the change would violate a node type constraint
     */
public void testReferenceProperty() throws NotExecutableException, RepositoryException {
    // locate a PropertyDefinition with ValueConstraints
    PropertyDefinition propDef = NodeTypeUtil.locatePropertyDef(superuser, PropertyType.REFERENCE, false, false, true, false);
    if (propDef == null) {
        throw new NotExecutableException("No 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 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()
    Node node;
    Node nodeNotSatisfied;
    try {
        String nodeType = propDef.getDeclaringNodeType().getName();
        node = testRootNode.addNode(nodeName2, nodeType);
        // create a referenceable node not satisfying the constraint
        nodeNotSatisfied = testRootNode.addNode(nodeName4, nodeTypeNotSatisfied);
        ensureMixinType(nodeNotSatisfied, mixReferenceable);
        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 setProperty(String name, Node value)
    try {
        node.setProperty(propDef.getName(), nodeNotSatisfied);
        node.save();
        fail("setProperty(String name, Node value) must throw a " + "ConstraintViolationException if the change would violate a " + "node type constraint either immediately or on save");
    } catch (ConstraintViolationException e) {
    // success
    }
    // test of signature setProperty(String name, Value value)
    try {
        node.setProperty(propDef.getName(), superuser.getValueFactory().createValue(nodeNotSatisfied));
        node.save();
        fail("setProperty(String name, Value value) 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) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition)

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