use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit-oak by apache.
the class IntermediatePathTest method testInvalidAbsolutePaths.
@Test
public void testInvalidAbsolutePaths() throws Exception {
new NodeUtil(root.getTree("/")).addChild("testNode", NodeTypeConstants.NT_OAK_UNSTRUCTURED);
List<String> invalidPaths = ImmutableList.of("/", PathUtils.getAncestorPath(UserConstants.DEFAULT_GROUP_PATH, 1), PathUtils.getAncestorPath(UserConstants.DEFAULT_GROUP_PATH, 2), "/testNode", "/nonExisting");
for (String absPath : invalidPaths) {
try {
createAuthorizable(false, absPath);
fail("Invalid path " + absPath + " outside of configured scope.");
} catch (ConstraintViolationException e) {
// success
} finally {
root.refresh();
}
}
}
use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit-oak by apache.
the class NodeImpl method addNode.
@Override
@Nonnull
public Node addNode(final String relPath, String primaryNodeTypeName) throws RepositoryException {
final String oakPath = getOakPathOrThrowNotFound(relPath);
final String oakTypeName;
if (primaryNodeTypeName != null) {
oakTypeName = getOakName(primaryNodeTypeName);
} else {
oakTypeName = null;
}
checkIndexOnName(relPath);
return perform(new ItemWriteOperation<Node>("addNode") {
@Nonnull
@Override
public Node perform() throws RepositoryException {
String oakName = PathUtils.getName(oakPath);
String parentPath = PathUtils.getParentPath(oakPath);
NodeDelegate parent = dlg.getChild(parentPath);
if (parent == null) {
// is it a property?
String grandParentPath = PathUtils.getParentPath(parentPath);
NodeDelegate grandParent = dlg.getChild(grandParentPath);
if (grandParent != null) {
String propName = PathUtils.getName(parentPath);
if (grandParent.getPropertyOrNull(propName) != null) {
throw new ConstraintViolationException("Can't add new node to property.");
}
}
throw new PathNotFoundException(relPath);
}
if (parent.getChild(oakName) != null) {
throw new ItemExistsException(relPath);
}
// modification of that property in the PermissionValidator
if (oakTypeName != null) {
PropertyState prop = PropertyStates.createProperty(JCR_PRIMARYTYPE, oakTypeName, NAME);
sessionContext.getAccessManager().checkPermissions(parent.getTree(), prop, Permissions.NODE_TYPE_MANAGEMENT);
}
NodeDelegate added = parent.addChild(oakName, oakTypeName);
if (added == null) {
throw new ItemExistsException(format("Node [%s/%s] exists", getNodePath(), oakName));
}
return createNode(added, sessionContext);
}
@Override
public String toString() {
return format("Adding node [%s/%s]", dlg.getPath(), relPath);
}
});
}
use of javax.jcr.nodetype.ConstraintViolationException 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();
}
use of javax.jcr.nodetype.ConstraintViolationException 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();
}
}
use of javax.jcr.nodetype.ConstraintViolationException 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();
}
Aggregations