Search in sources :

Example 6 with NodeTypeTemplate

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

the class AddMixinTest method testAddItemsDefinedByMixin2.

public void testAddItemsDefinedByMixin2() throws NotExecutableException, RepositoryException {
    // register mixin
    NodeTypeManager ntm = superuser.getWorkspace().getNodeTypeManager();
    NodeTypeTemplate ntd = ntm.createNodeTypeTemplate();
    ntd.setName("testMixin");
    ntd.setMixin(true);
    NodeDefinitionTemplate nodeDef = ntm.createNodeDefinitionTemplate();
    nodeDef.setName("child");
    nodeDef.setRequiredPrimaryTypeNames(new String[] { "nt:folder" });
    ntd.getNodeDefinitionTemplates().add(nodeDef);
    ntm.registerNodeType(ntd, true);
    // create node and add mixin
    Node node = testRootNode.addNode(nodeName1, "nt:resource");
    node.setProperty("jcr:data", "abc");
    node.addMixin("testMixin");
    superuser.save();
    // node type
    try {
        node.addNode("child");
        fail();
    } catch (ConstraintViolationException e) {
    // success as ChildNode Definition doesn't specify a default primary
    // type -> see comment in ItemDefinitionProvider#getQNodeDefinition
    }
}
Also used : NodeDefinitionTemplate(javax.jcr.nodetype.NodeDefinitionTemplate) NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) Node(javax.jcr.Node) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException)

Example 7 with NodeTypeTemplate

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

the class NodeTypeCreationTest method testInvalidJCRNames.

public void testInvalidJCRNames() throws Exception {
    String invalidName = ":ab[2]";
    // invalid name(s) passed to NT-template methods
    NodeTypeTemplate ntt = ntm.createNodeTypeTemplate();
    try {
        ntt.setName(invalidName);
        fail("ConstraintViolationException expected. Nt-name is invalid");
    } catch (ConstraintViolationException e) {
    // success
    }
    try {
        ntt.setDeclaredSuperTypeNames(new String[] { "{" + NS_MIX_URI + "}" + "littlemixin", invalidName });
        fail("ConstraintViolationException expected. One of the super type names is invalid");
    } catch (ConstraintViolationException e) {
    // success
    }
    try {
        ntt.setPrimaryItemName(invalidName);
        fail("ConstraintViolationException expected. Primary item name is invalid");
    } catch (ConstraintViolationException e) {
    // success
    }
    // invalid name(s) passed to NodeDefinitionTemplate
    NodeDefinitionTemplate ndt = ntm.createNodeDefinitionTemplate();
    try {
        ndt.setName(invalidName);
        fail("ConstraintViolationException expected. Name is invalid");
    } catch (ConstraintViolationException e) {
    // success
    }
    try {
        ndt.setRequiredPrimaryTypeNames(new String[] { "{" + NS_MIX_URI + "}" + "littlemixin", invalidName });
        fail("ConstraintViolationException expected. One of the required primary type names is invalid");
    } catch (ConstraintViolationException e) {
    // success
    }
    try {
        ndt.setDefaultPrimaryTypeName(invalidName);
        fail("ConstraintViolationException expected. Default primary type name is invalid");
    } catch (ConstraintViolationException e) {
    // success
    }
    // invalid name(s) passed to PropertyDefinitionTemplate
    PropertyDefinitionTemplate pdt = ntm.createPropertyDefinitionTemplate();
    try {
        pdt.setName(invalidName);
        fail("ConstraintViolationException expected. Name is invalid");
    } catch (ConstraintViolationException e) {
    // success
    }
}
Also used : NodeDefinitionTemplate(javax.jcr.nodetype.NodeDefinitionTemplate) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) PropertyDefinitionTemplate(javax.jcr.nodetype.PropertyDefinitionTemplate) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException)

Example 8 with NodeTypeTemplate

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

the class PropertyTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    NodeTypeManager ntMgr = superuser.getWorkspace().getNodeTypeManager();
    NodeTypeTemplate template = ntMgr.createNodeTypeTemplate();
    template.setName(NT_NAME);
    template.setDeclaredSuperTypeNames(new String[] { JcrConstants.NT_BASE });
    template.setMixin(false);
    PropertyDefinitionTemplate pdt = ntMgr.createPropertyDefinitionTemplate();
    pdt.setName(BOOLEAN_PROP_NAME);
    pdt.setRequiredType(PropertyType.BOOLEAN);
    template.getPropertyDefinitionTemplates().add(pdt);
    pdt = ntMgr.createPropertyDefinitionTemplate();
    pdt.setName(LONG_PROP_NAME);
    pdt.setRequiredType(PropertyType.LONG);
    template.getPropertyDefinitionTemplates().add(pdt);
    ntMgr.registerNodeType(template, true);
    node = testRootNode.addNode(nodeName2, NT_NAME);
    assertEquals(NT_NAME, node.getPrimaryNodeType().getName());
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) PropertyDefinitionTemplate(javax.jcr.nodetype.PropertyDefinitionTemplate)

Example 9 with NodeTypeTemplate

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

the class RepositoryUpgradeTest method createSourceContent.

@Override
@SuppressWarnings("unchecked")
protected void createSourceContent(Session session) throws Exception {
    JackrabbitWorkspace workspace = (JackrabbitWorkspace) session.getWorkspace();
    NamespaceRegistry registry = workspace.getNamespaceRegistry();
    registry.registerNamespace("test", "http://www.example.org/");
    NodeTypeManager nodeTypeManager = workspace.getNodeTypeManager();
    NodeTypeTemplate template = nodeTypeManager.createNodeTypeTemplate();
    template.setName("test:unstructured");
    template.setDeclaredSuperTypeNames(new String[] { "nt:unstructured" });
    PropertyDefinitionTemplate pDef1 = nodeTypeManager.createPropertyDefinitionTemplate();
    pDef1.setName("defaultString");
    pDef1.setRequiredType(PropertyType.STRING);
    Value stringValue = session.getValueFactory().createValue("stringValue");
    pDef1.setDefaultValues(new Value[] { stringValue });
    template.getPropertyDefinitionTemplates().add(pDef1);
    PropertyDefinitionTemplate pDef2 = nodeTypeManager.createPropertyDefinitionTemplate();
    pDef2.setName("defaultPath");
    pDef2.setRequiredType(PropertyType.PATH);
    Value pathValue = session.getValueFactory().createValue("/jcr:path/nt:value", PropertyType.PATH);
    pDef2.setDefaultValues(new Value[] { pathValue });
    template.getPropertyDefinitionTemplates().add(pDef2);
    nodeTypeManager.registerNodeType(template, false);
    template = nodeTypeManager.createNodeTypeTemplate();
    template.setName("test:referenceable");
    template.setDeclaredSuperTypeNames(new String[] { "nt:unstructured", "mix:referenceable" });
    nodeTypeManager.registerNodeType(template, false);
    Node root = session.getRootNode();
    Node referenceable = root.addNode("referenceable", "test:unstructured");
    referenceable.addMixin(NodeType.MIX_REFERENCEABLE);
    Node versionable = root.addNode("versionable", NT_UNSTRUCTURED);
    versionable.addMixin(MIX_VERSIONABLE);
    Node child = versionable.addNode("child", "test:referenceable");
    child.addNode("child2", NT_UNSTRUCTURED);
    session.save();
    Node sns = root.addNode("sns");
    sns.addNode("sibling");
    sns.addNode("sibling");
    sns.addNode("sibling");
    session.getWorkspace().getVersionManager().checkin("/versionable");
    Node properties = root.addNode("properties", "test:unstructured");
    properties.setProperty("boolean", true);
    Binary binary = session.getValueFactory().createBinary(new ByteArrayInputStream(BINARY));
    try {
        properties.setProperty("binary", binary);
    } finally {
        binary.dispose();
    }
    properties.setProperty("date", DATE);
    properties.setProperty("decimal", new BigDecimal(123));
    properties.setProperty("double", Math.PI);
    properties.setProperty("long", 9876543210L);
    properties.setProperty("reference", referenceable);
    properties.setProperty("weak_reference", session.getValueFactory().createValue(referenceable, true));
    properties.setProperty("mv_reference", new Value[] { session.getValueFactory().createValue(versionable, false) });
    properties.setProperty("mv_weak_reference", new Value[] { session.getValueFactory().createValue(versionable, true) });
    properties.setProperty("string", "test");
    properties.setProperty("multiple", "a,b,c".split(","));
    session.save();
    binary = properties.getProperty("binary").getBinary();
    try {
        InputStream stream = binary.getStream();
        try {
            for (byte aBINARY : BINARY) {
                assertEquals(aBINARY, (byte) stream.read());
            }
            assertEquals(-1, stream.read());
        } finally {
            stream.close();
        }
    } finally {
        binary.dispose();
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NamespaceRegistry(javax.jcr.NamespaceRegistry) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) PropertyDefinitionTemplate(javax.jcr.nodetype.PropertyDefinitionTemplate) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Node(javax.jcr.Node) Value(javax.jcr.Value) JackrabbitWorkspace(org.apache.jackrabbit.api.JackrabbitWorkspace) Binary(javax.jcr.Binary) BigDecimal(java.math.BigDecimal)

Example 10 with NodeTypeTemplate

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

the class NodeTypeDefinitionManagementTest method testRegisterNodeType.

@Test
public void testRegisterNodeType() throws Exception {
    Workspace testWsp = testSession.getWorkspace();
    NodeTypeManager ntm = testWsp.getNodeTypeManager();
    NodeTypeTemplate ntd = ntm.createNodeTypeTemplate();
    ntd.setName("testRegisterNodeType");
    ntd.setMixin(true);
    try {
        ntm.registerNodeType(ntd, true);
        fail("Node type registration should be denied.");
    } catch (AccessDeniedException e) {
    // success
    }
    try {
        ntm.registerNodeType(ntd, false);
        fail("Node type registration should be denied.");
    } catch (AccessDeniedException e) {
    // success
    }
    NodeTypeTemplate[] ntds = new NodeTypeTemplate[2];
    ntds[0] = ntd;
    ntds[1] = ntm.createNodeTypeTemplate();
    ntds[1].setName("anotherRegisterNodeType");
    ntds[1].setDeclaredSuperTypeNames(new String[] { "nt:file" });
    try {
        ntm.registerNodeTypes(ntds, true);
        fail("Node type registration should be denied.");
    } catch (AccessDeniedException e) {
    // success
    }
    try {
        ntm.registerNodeTypes(ntds, false);
        fail("Node type registration should be denied.");
    } catch (AccessDeniedException e) {
    // success
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) AccessDeniedException(javax.jcr.AccessDeniedException) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) Workspace(javax.jcr.Workspace) Test(org.junit.Test)

Aggregations

NodeTypeTemplate (javax.jcr.nodetype.NodeTypeTemplate)46 NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)29 Node (javax.jcr.Node)17 PropertyDefinitionTemplate (javax.jcr.nodetype.PropertyDefinitionTemplate)13 Test (org.junit.Test)12 Session (javax.jcr.Session)11 NodeDefinitionTemplate (javax.jcr.nodetype.NodeDefinitionTemplate)11 List (java.util.List)7 Workspace (javax.jcr.Workspace)7 NamespaceRegistry (javax.jcr.NamespaceRegistry)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)5 NodeTypeDefinition (javax.jcr.nodetype.NodeTypeDefinition)5 JackrabbitWorkspace (org.apache.jackrabbit.api.JackrabbitWorkspace)5 InputStream (java.io.InputStream)4 ArrayList (java.util.ArrayList)4 AccessDeniedException (javax.jcr.AccessDeniedException)4 RepositoryException (javax.jcr.RepositoryException)4 JackrabbitNode (org.apache.jackrabbit.api.JackrabbitNode)4 BigDecimal (java.math.BigDecimal)3