Search in sources :

Example 71 with NodeTypeManager

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

the class CompatibilityIssuesTest method addNodeTest.

@Test
public void addNodeTest() throws RepositoryException {
    Session session = getAdminSession();
    // node type with default child-node type of to nt:base
    String ntName = "test";
    NodeTypeManager ntm = session.getWorkspace().getNodeTypeManager();
    NodeTypeTemplate ntt = ntm.createNodeTypeTemplate();
    ntt.setName(ntName);
    NodeDefinitionTemplate child = ntm.createNodeDefinitionTemplate();
    child.setName("*");
    child.setDefaultPrimaryTypeName("nt:base");
    child.setRequiredPrimaryTypeNames(new String[] { "nt:base" });
    List<NodeDefinition> children = ntt.getNodeDefinitionTemplates();
    children.add(child);
    ntm.registerNodeType(ntt, true);
    // try to create a node with the default nt:base
    Node node = session.getRootNode().addNode("defaultNtBase", ntName);
    // See OAK-1013
    node.addNode("nothrow");
    try {
        node.addNode("throw", "nt:hierarchyNode");
        fail("Abstract primary type should cause ConstraintViolationException");
    } catch (ConstraintViolationException expected) {
    }
    session.save();
}
Also used : NodeDefinitionTemplate(javax.jcr.nodetype.NodeDefinitionTemplate) NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) Node(javax.jcr.Node) NodeDefinition(javax.jcr.nodetype.NodeDefinition) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) Session(javax.jcr.Session) Test(org.junit.Test)

Example 72 with NodeTypeManager

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

the class RepositoryTest method mixin.

@Test
public void mixin() throws RepositoryException {
    NodeTypeManager ntMgr = getAdminSession().getWorkspace().getNodeTypeManager();
    NodeTypeTemplate mixTest = ntMgr.createNodeTypeTemplate();
    mixTest.setName("mix:test");
    mixTest.setMixin(true);
    ntMgr.registerNodeType(mixTest, false);
    Node testNode = getNode(TEST_PATH);
    NodeType[] mix = testNode.getMixinNodeTypes();
    assertEquals(0, mix.length);
    testNode.addMixin("mix:test");
    testNode.getSession().save();
    Session session2 = createAnonymousSession();
    try {
        mix = session2.getNode(TEST_PATH).getMixinNodeTypes();
        assertEquals(1, mix.length);
        assertEquals("mix:test", mix[0].getName());
    } finally {
        session2.logout();
    }
    testNode.removeMixin("mix:test");
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) JackrabbitNode(org.apache.jackrabbit.api.JackrabbitNode) Node(javax.jcr.Node) NodeType(javax.jcr.nodetype.NodeType) Session(javax.jcr.Session) Test(org.junit.Test)

Example 73 with NodeTypeManager

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

the class NodeTypeTest method trivialUpdates.

@Test
public void trivialUpdates() throws Exception {
    // test various trivial updates that should not trigger repository scans
    // whether or not the repository scan happens can not be checked directly;
    // it requires inspecting the INFO level log 
    String[] types = new String[] { "trivial1", "trivial2" };
    ArrayList<NodeTypeTemplate> ntt = new ArrayList<NodeTypeTemplate>();
    // adding node types
    Session session = getAdminSession();
    NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
    for (String t : types) {
        NodeTypeTemplate nt = manager.createNodeTypeTemplate();
        nt.setName(t);
        ntt.add(nt);
    }
    manager.registerNodeTypes(ntt.toArray(new NodeTypeTemplate[0]), false);
    // adding an optional property
    ntt = new ArrayList<NodeTypeTemplate>();
    for (String t : types) {
        NodeTypeDefinition ntd = manager.getNodeType(t);
        PropertyDefinitionTemplate opt = manager.createPropertyDefinitionTemplate();
        opt.setMandatory(false);
        opt.setName("optional");
        opt.setRequiredType(PropertyType.STRING);
        PropertyDefinitionTemplate opts = manager.createPropertyDefinitionTemplate();
        opts.setMandatory(false);
        opts.setMultiple(true);
        opts.setName("optionals");
        opts.setRequiredType(PropertyType.STRING);
        NodeTypeTemplate nt = manager.createNodeTypeTemplate(ntd);
        List pdt = nt.getPropertyDefinitionTemplates();
        pdt.add(opt);
        pdt.add(opts);
        ntt.add(nt);
    }
    manager.registerNodeTypes(ntt.toArray(new NodeTypeTemplate[0]), true);
    // make one optional property mandatory
    ntt = new ArrayList<NodeTypeTemplate>();
    for (String t : types) {
        NodeTypeDefinition ntd = manager.getNodeType(t);
        PropertyDefinitionTemplate opt = manager.createPropertyDefinitionTemplate();
        opt.setMandatory("trivial2".equals(t));
        opt.setName("optional");
        opt.setRequiredType(PropertyType.STRING);
        PropertyDefinitionTemplate opts = manager.createPropertyDefinitionTemplate();
        opts.setMandatory("trivial2".equals(t));
        opts.setMultiple(true);
        opts.setName("optionals");
        opts.setRequiredType(PropertyType.STRING);
        NodeTypeTemplate nt = manager.createNodeTypeTemplate(ntd);
        List pdt = nt.getPropertyDefinitionTemplates();
        pdt.add(opt);
        pdt.add(opts);
        ntt.add(nt);
    }
    // but update both node types
    manager.registerNodeTypes(ntt.toArray(new NodeTypeTemplate[0]), true);
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) PropertyDefinitionTemplate(javax.jcr.nodetype.PropertyDefinitionTemplate) ArrayList(java.util.ArrayList) NodeTypeDefinition(javax.jcr.nodetype.NodeTypeDefinition) ArrayList(java.util.ArrayList) List(java.util.List) Session(javax.jcr.Session) Test(org.junit.Test) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)

Example 74 with NodeTypeManager

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

the class NodeTypeTest method removeNodeType.

@Test
public void removeNodeType() throws Exception {
    Session session = getAdminSession();
    Node root = session.getRootNode();
    ValueFactory vf = session.getValueFactory();
    NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
    Node n = root.addNode("q1", "nt:query");
    n.setProperty("jcr:statement", vf.createValue("statement"));
    n.setProperty("jcr:language", vf.createValue("language"));
    session.save();
    try {
        manager.unregisterNodeType("nt:query");
        fail();
    } catch (ConstraintViolationException expected) {
    // this type is referenced in content, so it can't be removed
    }
    n.remove();
    session.save();
    try {
        manager.unregisterNodeType("nt:query");
    // no longer referenced in content, so removal should succeed
    } catch (ConstraintViolationException unexpected) {
        fail();
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) Node(javax.jcr.Node) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) ValueFactory(javax.jcr.ValueFactory) Session(javax.jcr.Session) Test(org.junit.Test) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)

Example 75 with NodeTypeManager

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

the class NodeTypeTest method removeMandatoryPropertyFlag.

@Test
public void removeMandatoryPropertyFlag() throws Exception {
    Session session = getAdminSession();
    Node root = session.getRootNode();
    NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
    String cnd = "<'test'='http://www.apache.org/jackrabbit/test'>\n" + "[test:MyType] > nt:unstructured\n" + " - test:mandatory (string) mandatory";
    CndImporter.registerNodeTypes(new StringReader(cnd), session);
    Node n = root.addNode("test", "test:MyType");
    n.setProperty("test:mandatory", "value");
    session.save();
    try {
        n.getProperty("test:mandatory").remove();
        session.save();
        fail("Must fail with ConstraintViolationException");
    } catch (ConstraintViolationException e) {
        // expected
        session.refresh(false);
    }
    // remove the mandatory property flag
    cnd = "<'test'='http://www.apache.org/jackrabbit/test'>\n" + "[test:MyType] > nt:unstructured\n" + " - test:mandatory (string)";
    CndImporter.registerNodeTypes(new StringReader(cnd), session, true);
    // check node type
    NodeTypeDefinition ntd = manager.getNodeType("test:MyType");
    assertEquals(1, ntd.getDeclaredPropertyDefinitions().length);
    assertFalse(ntd.getDeclaredPropertyDefinitions()[0].isMandatory());
    // now we should be able to remove the property
    n.getProperty("test:mandatory").remove();
    session.save();
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) Node(javax.jcr.Node) StringReader(java.io.StringReader) NodeTypeDefinition(javax.jcr.nodetype.NodeTypeDefinition) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) Session(javax.jcr.Session) Test(org.junit.Test) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)

Aggregations

NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)93 NodeType (javax.jcr.nodetype.NodeType)41 Node (javax.jcr.Node)32 Session (javax.jcr.Session)30 NodeTypeTemplate (javax.jcr.nodetype.NodeTypeTemplate)29 NodeTypeIterator (javax.jcr.nodetype.NodeTypeIterator)27 Test (org.junit.Test)24 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)14 PropertyDefinition (javax.jcr.nodetype.PropertyDefinition)14 RepositoryException (javax.jcr.RepositoryException)13 ByteArrayInputStream (java.io.ByteArrayInputStream)12 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)11 ArrayList (java.util.ArrayList)8 Workspace (javax.jcr.Workspace)8 NoSuchNodeTypeException (javax.jcr.nodetype.NoSuchNodeTypeException)8 NodeDefinition (javax.jcr.nodetype.NodeDefinition)8 PropertyDefinitionTemplate (javax.jcr.nodetype.PropertyDefinitionTemplate)8 NodeDefinitionTemplate (javax.jcr.nodetype.NodeDefinitionTemplate)7 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)7 InputStream (java.io.InputStream)6