Search in sources :

Example 1 with NodeTypeManager

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

the class QueryTest method testOak1354.

@Test
public void testOak1354() throws Exception {
    Session session = createAdminSession();
    NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
    if (!manager.hasNodeType("mymixinOak1354")) {
        StringBuilder defs = new StringBuilder();
        defs.append("[mymixinOak1354]\n");
        defs.append("  mixin");
        Reader cndReader = new InputStreamReader(new ByteArrayInputStream(defs.toString().getBytes()));
        CndImporter.registerNodeTypes(cndReader, session);
    }
    Node p = session.getRootNode().addNode("one");
    p.addMixin("mymixinOak1354");
    session.save();
    Query q = session.getWorkspace().getQueryManager().createQuery("SELECT * FROM [mymixinOak1354]", Query.JCR_SQL2);
    QueryResult qr = q.execute();
    NodeIterator ni = qr.getNodes();
    assertTrue(ni.hasNext());
    Node n = ni.nextNode();
    assertEquals("/one", n.getPath());
    assertFalse(ni.hasNext());
    session.logout();
}
Also used : NodeIterator(javax.jcr.NodeIterator) NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) QueryResult(javax.jcr.query.QueryResult) InputStreamReader(java.io.InputStreamReader) Query(javax.jcr.query.Query) ByteArrayInputStream(java.io.ByteArrayInputStream) Node(javax.jcr.Node) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) Session(javax.jcr.Session) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession) Test(org.junit.Test) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)

Example 2 with NodeTypeManager

use of javax.jcr.nodetype.NodeTypeManager 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 3 with NodeTypeManager

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

the class UpgradeOldSegmentTest method upgradeFrom10.

@Test
public void upgradeFrom10() throws Exception {
    File testFolder = new File(new File("target"), UpgradeOldSegmentTest.class.getSimpleName());
    FileUtils.deleteDirectory(testFolder);
    File oldRepo = new File(testFolder, "test-repo-1.0");
    oldRepo.mkdirs();
    try (InputStream in = UpgradeOldSegmentTest.class.getResourceAsStream("/test-repo-1.0.zip")) {
        Util.unzip(in, oldRepo);
    }
    SegmentTarNodeStoreContainer newRepoContainer = new SegmentTarNodeStoreContainer();
    OakUpgrade.main("segment-old:" + oldRepo.getPath(), newRepoContainer.getDescription());
    Repository repo = new Jcr(newRepoContainer.open()).createRepository();
    Session s = repo.login(new SimpleCredentials("admin", "admin".toCharArray()));
    Node myType = s.getNode("/jcr:system/jcr:nodeTypes/test:MyType");
    assertEquals(2, Iterators.size(myType.getNodes("jcr:propertyDefinition")));
    NodeTypeManager ntMgr = s.getWorkspace().getNodeTypeManager();
    assertTrue(ntMgr.hasNodeType("test:MyType"));
    NodeType nt = ntMgr.getNodeType("test:MyType");
    PropertyDefinition[] pDefs = nt.getDeclaredPropertyDefinitions();
    assertEquals(2, pDefs.length);
    for (PropertyDefinition pd : pDefs) {
        String name = pd.getName();
        if (name.equals("test:mandatory")) {
            assertTrue(pd.isMandatory());
        } else if (name.equals("test:optional")) {
            assertFalse(pd.isMandatory());
        } else {
            fail("Unexpected property definition: " + name);
        }
    }
    // flip mandatory flag for test:mandatory
    String cnd = "<'test'='http://www.apache.org/jackrabbit/test'>\n" + "[test:MyType] > nt:unstructured\n" + " - test:mandatory (string)\n" + " - test:optional (string)";
    CndImporter.registerNodeTypes(new StringReader(cnd), s, true);
    myType = s.getNode("/jcr:system/jcr:nodeTypes/test:MyType");
    assertEquals(2, Iterators.size(myType.getNodes("jcr:propertyDefinition")));
    nt = ntMgr.getNodeType("test:MyType");
    pDefs = nt.getDeclaredPropertyDefinitions();
    assertEquals(2, pDefs.length);
    for (PropertyDefinition pd : pDefs) {
        String name = pd.getName();
        if (name.equals("test:mandatory")) {
            assertFalse(pd.isMandatory());
        } else if (name.equals("test:optional")) {
            assertFalse(pd.isMandatory());
        } else {
            fail("Unexpected property definition: " + name);
        }
    }
    s.logout();
    if (repo instanceof JackrabbitRepository) {
        ((JackrabbitRepository) repo).shutdown();
    }
    newRepoContainer.close();
    newRepoContainer.clean();
    deleteRecursive(testFolder);
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) InputStream(java.io.InputStream) Node(javax.jcr.Node) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition) SimpleCredentials(javax.jcr.SimpleCredentials) Repository(javax.jcr.Repository) JackrabbitRepository(org.apache.jackrabbit.api.JackrabbitRepository) SegmentTarNodeStoreContainer(org.apache.jackrabbit.oak.upgrade.cli.container.SegmentTarNodeStoreContainer) NodeType(javax.jcr.nodetype.NodeType) Jcr(org.apache.jackrabbit.oak.jcr.Jcr) StringReader(java.io.StringReader) JackrabbitRepository(org.apache.jackrabbit.api.JackrabbitRepository) File(java.io.File) Session(javax.jcr.Session) Test(org.junit.Test)

Example 4 with NodeTypeManager

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

the class RepositoryUpgradeTest method verifyCustomNodeTypes.

@Test
public void verifyCustomNodeTypes() throws Exception {
    Session session = createAdminSession();
    try {
        NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
        assertTrue(manager.hasNodeType("test:unstructured"));
        NodeType type = manager.getNodeType("test:unstructured");
        assertFalse(type.isMixin());
        assertTrue(type.isNodeType("nt:unstructured"));
        boolean foundDefaultString = false;
        boolean foundDefaultPath = false;
        for (PropertyDefinition pDef : type.getPropertyDefinitions()) {
            if ("defaultString".equals(pDef.getName())) {
                assertEquals(PropertyType.STRING, pDef.getRequiredType());
                assertNotNull(pDef.getDefaultValues());
                assertEquals(1, pDef.getDefaultValues().length);
                assertEquals("stringValue", pDef.getDefaultValues()[0].getString());
                foundDefaultString = true;
            } else if ("defaultPath".equals(pDef.getName())) {
                assertEquals(PropertyType.PATH, pDef.getRequiredType());
                assertNotNull(pDef.getDefaultValues());
                assertEquals(1, pDef.getDefaultValues().length);
                assertEquals("/jcr:path/nt:value", pDef.getDefaultValues()[0].getString());
                foundDefaultPath = true;
            }
        }
        assertTrue("Expected property definition with name \"defaultString\"", foundDefaultString);
        assertTrue("Expected property definition with name \"defaultPath\"", foundDefaultPath);
    } finally {
        session.logout();
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NodeType(javax.jcr.nodetype.NodeType) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition) Session(javax.jcr.Session) Test(org.junit.Test)

Example 5 with NodeTypeManager

use of javax.jcr.nodetype.NodeTypeManager 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)

Aggregations

NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)103 NodeType (javax.jcr.nodetype.NodeType)47 Node (javax.jcr.Node)38 Session (javax.jcr.Session)35 NodeTypeTemplate (javax.jcr.nodetype.NodeTypeTemplate)31 NodeTypeIterator (javax.jcr.nodetype.NodeTypeIterator)29 Test (org.junit.Test)25 RepositoryException (javax.jcr.RepositoryException)18 ByteArrayInputStream (java.io.ByteArrayInputStream)15 PropertyDefinition (javax.jcr.nodetype.PropertyDefinition)15 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)14 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)11 ArrayList (java.util.ArrayList)10 Workspace (javax.jcr.Workspace)10 InputStream (java.io.InputStream)9 NodeDefinition (javax.jcr.nodetype.NodeDefinition)9 PropertyDefinitionTemplate (javax.jcr.nodetype.PropertyDefinitionTemplate)9 InputStreamReader (java.io.InputStreamReader)8 NamespaceRegistry (javax.jcr.NamespaceRegistry)8 Value (javax.jcr.Value)8