Search in sources :

Example 46 with PropertyDefinition

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

the class PropertyTest method testRequiredTypeLongChangeBoolean.

public void testRequiredTypeLongChangeBoolean() throws Exception {
    Property p = node.setProperty(LONG_PROP_NAME, 12345);
    assertEquals(PropertyType.LONG, p.getType());
    assertEquals(PropertyType.LONG, p.getValue().getType());
    PropertyDefinition def = p.getDefinition();
    assertEquals(PropertyType.LONG, def.getRequiredType());
    assertEquals(NT_NAME, def.getDeclaringNodeType().getName());
    superuser.save();
    try {
        p.setValue(true);
        fail("Conversion from BOOLEAN to LONG must throw ValueFormatException");
    } catch (ValueFormatException e) {
    // success
    }
    try {
        node.setProperty(LONG_PROP_NAME, true);
        fail("Conversion from BOOLEAN to LONG must throw ValueFormatException");
    } catch (ValueFormatException e) {
    // success
    }
}
Also used : ValueFormatException(javax.jcr.ValueFormatException) Property(javax.jcr.Property) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition)

Example 47 with PropertyDefinition

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

the class PropertyTest method testMixTitleOnUnstructured2.

public void testMixTitleOnUnstructured2() throws Exception {
    Node n = testRootNode.addNode("unstructured", JcrConstants.NT_UNSTRUCTURED);
    n.addMixin("mix:title");
    superuser.save();
    // create jcr:title as STRING => declaring NT is mix:title
    Property title = n.setProperty("jcr:title", "str");
    assertEquals(PropertyType.STRING, title.getType());
    PropertyDefinition def = title.getDefinition();
    assertEquals("mix:title", def.getDeclaringNodeType().getName());
    assertEquals(PropertyType.STRING, def.getRequiredType());
    // changing value to BOOLEAN => value is converted
    title.setValue(true);
    assertEquals(PropertyType.STRING, title.getType());
    def = title.getDefinition();
    assertEquals("mix:title", def.getDeclaringNodeType().getName());
    assertEquals(PropertyType.STRING, def.getRequiredType());
    // re-setting property to type BOOLEAN => declaring NT is nt:unstructured
    title = n.setProperty("jcr:title", true);
    def = title.getDefinition();
    assertEquals(JcrConstants.NT_UNSTRUCTURED, def.getDeclaringNodeType().getName());
    assertEquals(PropertyType.UNDEFINED, def.getRequiredType());
    // same if property is set to type DOUBLE
    title = n.setProperty("jcr:title", superuser.getValueFactory().createValue(2.3));
    assertEquals(PropertyType.DOUBLE, title.getType());
    def = title.getDefinition();
    assertEquals(JcrConstants.NT_UNSTRUCTURED, def.getDeclaringNodeType().getName());
    assertEquals(PropertyType.UNDEFINED, def.getRequiredType());
    // setting property to STRING => declaring NT is back to mix:title
    title = n.setProperty("jcr:title", "str");
    assertEquals(PropertyType.STRING, title.getType());
    def = title.getDefinition();
    assertEquals("mix:title", def.getDeclaringNodeType().getName());
    assertEquals(PropertyType.STRING, def.getRequiredType());
}
Also used : Node(javax.jcr.Node) Property(javax.jcr.Property) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition)

Example 48 with PropertyDefinition

use of javax.jcr.nodetype.PropertyDefinition 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 49 with PropertyDefinition

use of javax.jcr.nodetype.PropertyDefinition 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 50 with PropertyDefinition

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

the class NodeImpl method createChildNode.

protected synchronized NodeImpl createChildNode(Name name, NodeTypeImpl nodeType, NodeId id) throws RepositoryException {
    // create a new node state
    NodeState nodeState = stateMgr.createTransientNodeState(id, nodeType.getQName(), getNodeId(), ItemState.STATUS_NEW);
    // create Node instance wrapping new node state
    NodeImpl node;
    try {
        // NOTE: since the node is not yet connected to its parent, avoid
        // calling ItemManager#getItem(ItemId) which may include a permission
        // check (with subsequent usage of the hierarachy-mgr -> error).
        // just let the mgr create the new node that is known to exist and
        // which has not been accessed before.
        node = (NodeImpl) itemMgr.createItemInstance(nodeState);
    } catch (RepositoryException re) {
        // something went wrong
        stateMgr.disposeTransientItemState(nodeState);
        // re-throw
        throw re;
    }
    // modify the state of 'this', i.e. the parent node
    NodeState thisState = (NodeState) getOrCreateTransientItemState();
    // add new child node entry
    thisState.addChildNodeEntry(name, nodeState.getNodeId());
    // add 'auto-create' properties defined in node type
    for (PropertyDefinition aPda : nodeType.getAutoCreatedPropertyDefinitions()) {
        PropertyDefinitionImpl pd = (PropertyDefinitionImpl) aPda;
        node.createChildProperty(pd.unwrap().getName(), pd.getRequiredType(), pd);
    }
    // recursively add 'auto-create' child nodes defined in node type
    for (NodeDefinition aNda : nodeType.getAutoCreatedNodeDefinitions()) {
        NodeDefinitionImpl nd = (NodeDefinitionImpl) aNda;
        node.createChildNode(nd.unwrap().getName(), (NodeTypeImpl) nd.getDefaultPrimaryType(), null);
    }
    return node;
}
Also used : NodeDefinitionImpl(org.apache.jackrabbit.spi.commons.nodetype.NodeDefinitionImpl) NodeState(org.apache.jackrabbit.core.state.NodeState) QNodeDefinition(org.apache.jackrabbit.spi.QNodeDefinition) NodeDefinition(javax.jcr.nodetype.NodeDefinition) PropertyDefinitionImpl(org.apache.jackrabbit.spi.commons.nodetype.PropertyDefinitionImpl) RepositoryException(javax.jcr.RepositoryException) QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition)

Aggregations

PropertyDefinition (javax.jcr.nodetype.PropertyDefinition)141 NodeType (javax.jcr.nodetype.NodeType)79 Value (javax.jcr.Value)70 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)61 Node (javax.jcr.Node)27 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)22 Property (javax.jcr.Property)20 NodeTypeIterator (javax.jcr.nodetype.NodeTypeIterator)15 NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)14 RepositoryException (javax.jcr.RepositoryException)13 NodeDefinition (javax.jcr.nodetype.NodeDefinition)13 QPropertyDefinition (org.apache.jackrabbit.spi.QPropertyDefinition)11 Test (org.junit.Test)11 InputStream (java.io.InputStream)5 Session (javax.jcr.Session)5 ValueFormatException (javax.jcr.ValueFormatException)5 PropertyDefinitionImpl (org.apache.jackrabbit.spi.commons.nodetype.PropertyDefinitionImpl)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 IOException (java.io.IOException)4 Name (org.apache.jackrabbit.spi.Name)4