Search in sources :

Example 61 with PropertyDefinition

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

the class AuthorizablePropertiesImpl method checkProtectedProperty.

private void checkProtectedProperty(@Nonnull Tree parent, @Nonnull PropertyState property) throws RepositoryException {
    ReadOnlyNodeTypeManager nodeTypeManager = authorizable.getUserManager().getNodeTypeManager();
    PropertyDefinition def = nodeTypeManager.getDefinition(parent, property, false);
    if (def.isProtected()) {
        throw new ConstraintViolationException("Attempt to set an protected property " + property.getName());
    }
}
Also used : ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) ReadOnlyNodeTypeManager(org.apache.jackrabbit.oak.plugins.nodetype.ReadOnlyNodeTypeManager) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition)

Example 62 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 63 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 64 with PropertyDefinition

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

the class PropertyTest method testMixTitleOnUnstructured.

public void testMixTitleOnUnstructured() throws Exception {
    Node n = testRootNode.addNode("unstructured", JcrConstants.NT_UNSTRUCTURED);
    n.addMixin("mix:title");
    superuser.save();
    // create jcr:title property with type LONG => declaring NT is nt:unstructured
    Property title = n.setProperty("jcr:title", 12345);
    assertEquals(PropertyType.LONG, title.getType());
    PropertyDefinition def = title.getDefinition();
    assertEquals(JcrConstants.NT_UNSTRUCTURED, def.getDeclaringNodeType().getName());
    assertEquals(PropertyType.UNDEFINED, def.getRequiredType());
    // changing value to STRING => ValueFormatException expected
    try {
        title.setValue("str");
        fail();
    } catch (ValueFormatException e) {
    // success
    }
    // re-setting property to STRING -> change definition => declaring NT is mix:title
    n.setProperty("jcr:title", "str");
    assertEquals(PropertyType.STRING, title.getType());
    assertEquals(PropertyType.STRING, title.getValue().getType());
    def = title.getDefinition();
    assertEquals("mix:title", def.getDeclaringNodeType().getName());
    assertEquals(PropertyType.STRING, def.getRequiredType());
}
Also used : Node(javax.jcr.Node) ValueFormatException(javax.jcr.ValueFormatException) Property(javax.jcr.Property) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition)

Example 65 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)

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