Search in sources :

Example 56 with NodeDefinition

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

the class ObservationTest method deepNodeTypeMixinHierarchy.

@Test
public void deepNodeTypeMixinHierarchy() throws Exception {
    NodeTypeManager ntm = getAdminSession().getWorkspace().getNodeTypeManager();
    NodeTypeTemplate parentMixin = ntm.createNodeTypeTemplate();
    parentMixin.setName("parentmixin");
    parentMixin.setMixin(true);
    ntm.registerNodeType(parentMixin, false);
    NodeTypeTemplate childMixin = ntm.createNodeTypeTemplate();
    childMixin.setName("childmixin");
    childMixin.setMixin(true);
    childMixin.setDeclaredSuperTypeNames(new String[] { "parentmixin" });
    ntm.registerNodeType(childMixin, false);
    NodeTypeTemplate mytype = ntm.createNodeTypeTemplate();
    mytype.setName("mytype");
    mytype.setMixin(false);
    mytype.setDeclaredSuperTypeNames(new String[] { "childmixin" });
    NodeDefinitionTemplate child = ntm.createNodeDefinitionTemplate();
    child.setName("*");
    child.setDefaultPrimaryTypeName("nt:base");
    child.setRequiredPrimaryTypeNames(new String[] { "nt:base" });
    List<NodeDefinition> children = mytype.getNodeDefinitionTemplates();
    children.add(child);
    ntm.registerNodeType(mytype, false);
    getAdminSession().save();
    // create a fresh session here to catch the above new node type definitions
    observingSession = createAdminSession();
    observationManager = observingSession.getWorkspace().getObservationManager();
    JackrabbitObservationManager oManager = (JackrabbitObservationManager) observationManager;
    ExpectationListener listener = new ExpectationListener();
    JackrabbitEventFilter filter = new JackrabbitEventFilter().setAbsPath("/").setIsDeep(true).setNodeTypes(new String[] { "parentmixin" }).setEventTypes(ALL_EVENTS);
    oManager.addEventListener(listener, filter);
    Node n = getNode(TEST_PATH).addNode("n", "mytype");
    listener.expect(n.getPath() + "/jcr:primaryType", PROPERTY_ADDED);
    Node m = n.addNode("m", "nt:unstructured");
    listener.expect(m.getPath(), NODE_ADDED);
    getAdminSession().save();
    Thread.sleep(1000);
    List<Expectation> missing = listener.getMissing(TIME_OUT, TimeUnit.SECONDS);
    assertTrue("Missing events: " + missing, missing.isEmpty());
    List<Event> unexpected = listener.getUnexpected();
    assertTrue("Unexpected events: " + unexpected, unexpected.isEmpty());
}
Also used : NodeDefinitionTemplate(javax.jcr.nodetype.NodeDefinitionTemplate) JackrabbitObservationManager(org.apache.jackrabbit.api.observation.JackrabbitObservationManager) NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) JackrabbitNode(org.apache.jackrabbit.api.JackrabbitNode) Node(javax.jcr.Node) NodeDefinition(javax.jcr.nodetype.NodeDefinition) JackrabbitEventFilter(org.apache.jackrabbit.api.observation.JackrabbitEventFilter) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) Event(javax.jcr.observation.Event) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest) Test(org.junit.Test)

Example 57 with NodeDefinition

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

the class PredefinedNodeTypeTest method getNodeTypeSpec.

/**
     * Creates and returns a spec string for the given node type definition.
     * The returned spec string follows the node type definition format
     * used in the JSR 170 specification.
     *
     * @param type node type definition
     * @param propsVariant whether the properties of this node type may
     *   have implementation variant autocreated and OPV flags.
     * @return spec string
     * @throws RepositoryException on repository errors
     */
private static String getNodeTypeSpec(NodeType type, boolean propsVariant) throws RepositoryException {
    String typeName = type.getName();
    StringWriter buffer = new StringWriter();
    PrintWriter writer = new PrintWriter(buffer);
    writer.println("NodeTypeName");
    writer.println("  " + typeName);
    writer.println("IsMixin");
    writer.println("  " + type.isMixin());
    writer.println("HasOrderableChildNodes");
    writer.println("  " + type.hasOrderableChildNodes());
    writer.println("PrimaryItemName");
    writer.println("  " + type.getPrimaryItemName());
    NodeDefinition[] nodes = type.getDeclaredChildNodeDefinitions();
    Arrays.sort(nodes, NODE_DEF_COMPARATOR);
    for (int i = 0; i < nodes.length; i++) {
        writer.print(getChildNodeDefSpec(nodes[i]));
    }
    PropertyDefinition[] properties = type.getDeclaredPropertyDefinitions();
    Arrays.sort(properties, PROPERTY_DEF_COMPARATOR);
    for (int i = 0; i < properties.length; i++) {
        writer.print(getPropertyDefSpec(properties[i], propsVariant));
    }
    return buffer.toString();
}
Also used : StringWriter(java.io.StringWriter) NodeDefinition(javax.jcr.nodetype.NodeDefinition) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition) PrintWriter(java.io.PrintWriter)

Example 58 with NodeDefinition

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

the class NodeTypeCreationTest method testNonEmptyNodeTypeTemplate.

public void testNonEmptyNodeTypeTemplate() throws Exception {
    NodeTypeDefinition ntd = ntm.getNodeType("nt:address");
    NodeTypeTemplate ntt = ntm.createNodeTypeTemplate(ntm.getNodeType("nt:address"));
    assertEquals(ntt.getName(), ntd.getName());
    assertEquals(ntt.isMixin(), ntd.isMixin());
    assertEquals(ntt.isAbstract(), ntd.isAbstract());
    assertEquals(ntt.hasOrderableChildNodes(), ntd.hasOrderableChildNodes());
    assertEquals(ntt.isQueryable(), ntd.isQueryable());
    assertEquals(ntt.getPrimaryItemName(), ntd.getPrimaryItemName());
    assertTrue(Arrays.equals(ntt.getDeclaredSupertypeNames(), ntd.getDeclaredSupertypeNames()));
    NodeDefinition[] nda = ntt.getDeclaredChildNodeDefinitions();
    NodeDefinition[] nda1 = ntd.getDeclaredChildNodeDefinitions();
    assertEquals(nda.length, nda1.length);
    for (int i = 0; i < nda.length; i++) {
        assertEquals(nda[i].getName(), nda1[i].getName());
        assertEquals(nda[i].allowsSameNameSiblings(), nda1[i].allowsSameNameSiblings());
        assertTrue(Arrays.equals(nda[i].getRequiredPrimaryTypeNames(), nda1[i].getRequiredPrimaryTypeNames()));
        assertEquals(nda[i].getDefaultPrimaryTypeName(), nda1[i].getDefaultPrimaryTypeName());
        assertEquals(nda[i].getRequiredPrimaryTypeNames(), nda1[i].getRequiredPrimaryTypeNames());
    }
    PropertyDefinition[] pda = ntt.getDeclaredPropertyDefinitions();
    PropertyDefinition[] pda1 = ntd.getDeclaredPropertyDefinitions();
    assertEquals(pda.length, pda1.length);
    for (int i = 0; i < pda.length; i++) {
        assertEquals(pda[i].getName(), pda1[i].getName());
        assertEquals(pda[i].getRequiredType(), pda1[i].getRequiredType());
        assertTrue(Arrays.equals(pda[i].getAvailableQueryOperators(), pda1[i].getAvailableQueryOperators()));
        assertTrue(Arrays.equals(pda[i].getValueConstraints(), pda1[i].getValueConstraints()));
        assertEquals(pda[i].isFullTextSearchable(), pda1[i].isFullTextSearchable());
        assertEquals(pda[i].isMultiple(), pda1[i].isMultiple());
        assertEquals(pda[i].isQueryOrderable(), pda1[i].isQueryOrderable());
    }
}
Also used : NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) NodeTypeDefinition(javax.jcr.nodetype.NodeTypeDefinition) NodeDefinition(javax.jcr.nodetype.NodeDefinition) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition)

Example 59 with NodeDefinition

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

the class NodeDefTest method testGetDeclaringNodeType.

/**
     * Test getDeclaringNodeType() returns the node type which is defining the
     * requested child node def. Test runs for all existing node types.
     */
public void testGetDeclaringNodeType() throws RepositoryException {
    NodeTypeIterator types = manager.getAllNodeTypes();
    // loop all node types
    while (types.hasNext()) {
        NodeType currentType = types.nextNodeType();
        NodeDefinition[] defsOfCurrentType = currentType.getChildNodeDefinitions();
        // loop all child node defs of each node type
        for (int i = 0; i < defsOfCurrentType.length; i++) {
            NodeDefinition def = defsOfCurrentType[i];
            NodeType type = def.getDeclaringNodeType();
            // check if def is part of the child node defs of the
            // declaring node type
            NodeDefinition[] defs = type.getChildNodeDefinitions();
            boolean hasType = false;
            for (int j = 0; j < defs.length; j++) {
                if (defs[j].getName().equals(def.getName())) {
                    hasType = true;
                    break;
                }
            }
            assertTrue("getDeclaringNodeType() must return the node " + "which defines the corresponding child node def.", hasType);
        }
    }
}
Also used : NodeType(javax.jcr.nodetype.NodeType) NodeDefinition(javax.jcr.nodetype.NodeDefinition) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator)

Example 60 with NodeDefinition

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

the class NodeDefTest method testGetRequiredPrimaryTypes.

/**
     * Tests if getRequiredPrimaryTypes() does not return an empty array. Test
     * runs for all existing node types.
     */
public void testGetRequiredPrimaryTypes() throws RepositoryException {
    // loop all node types
    for (NodeTypeIterator types = manager.getAllNodeTypes(); types.hasNext(); ) {
        NodeType type = types.nextNodeType();
        NodeDefinition[] defs = type.getChildNodeDefinitions();
        for (int i = 0; i < defs.length; i++) {
            assertTrue("getRequiredPrimaryTypes() must never return an " + "empty array.", defs[i].getRequiredPrimaryTypes().length > 0);
        }
    }
}
Also used : NodeType(javax.jcr.nodetype.NodeType) NodeDefinition(javax.jcr.nodetype.NodeDefinition) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator)

Aggregations

NodeDefinition (javax.jcr.nodetype.NodeDefinition)77 NodeType (javax.jcr.nodetype.NodeType)44 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)29 Node (javax.jcr.Node)17 ItemExistsException (javax.jcr.ItemExistsException)14 NodeTypeIterator (javax.jcr.nodetype.NodeTypeIterator)13 PropertyDefinition (javax.jcr.nodetype.PropertyDefinition)13 Version (javax.jcr.version.Version)12 Test (org.junit.Test)11 NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)8 RepositoryException (javax.jcr.RepositoryException)7 HashSet (java.util.HashSet)5 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)5 Name (org.apache.jackrabbit.spi.Name)5 QNodeDefinition (org.apache.jackrabbit.spi.QNodeDefinition)5 NodeDefinitionImpl (org.apache.jackrabbit.spi.commons.nodetype.NodeDefinitionImpl)5 ArrayList (java.util.ArrayList)4 NodeState (org.apache.jackrabbit.core.state.NodeState)4 QPropertyDefinition (org.apache.jackrabbit.spi.QPropertyDefinition)4 Session (javax.jcr.Session)3