use of javax.jcr.ItemExistsException in project jackrabbit by apache.
the class AddNodeOperation method perform.
public Node perform(SessionContext context) throws RepositoryException {
ItemManager itemMgr = context.getItemManager();
// Get the canonical path of the new node
Path path;
try {
path = PathFactoryImpl.getInstance().create(node.getPrimaryPath(), context.getQPath(relPath), true);
} catch (NameException e) {
throw new RepositoryException("Failed to resolve path " + relPath + " relative to " + node, e);
}
// Check that the last path element is a simple name
if (!path.denotesName() || path.getIndex() != Path.INDEX_UNDEFINED) {
throw new RepositoryException("Invalid last path element for adding node " + relPath + " relative to " + node);
}
// Get the parent node instance
NodeImpl parentNode;
Path parentPath = path.getAncestor(1);
try {
parentNode = itemMgr.getNode(parentPath);
} catch (PathNotFoundException e) {
if (itemMgr.propertyExists(parentPath)) {
throw new ConstraintViolationException("Unable to add a child node to property " + context.getJCRPath(parentPath));
}
throw e;
} catch (AccessDeniedException ade) {
throw new PathNotFoundException("Failed to resolve path " + relPath + " relative to " + node);
}
// Resolve node type name (if any)
Name typeName = null;
if (nodeTypeName != null) {
typeName = context.getQName(nodeTypeName);
}
// Check that the given UUID (if any) does not already exist
NodeId id = null;
if (uuid != null) {
id = new NodeId(uuid);
if (itemMgr.itemExists(id)) {
throw new ItemExistsException("A node with this UUID already exists: " + uuid);
}
}
return parentNode.addNode(path.getName(), typeName, id);
}
use of javax.jcr.ItemExistsException in project jackrabbit by apache.
the class AddNodeTest method testSameNameSiblings.
/**
* Tests if same name siblings have equal names or if same name
* siblings are not supported a ItemExistsException is thrown.
*/
public void testSameNameSiblings() throws RepositoryException {
if (testRootNode.getDefinition().allowsSameNameSiblings()) {
Node n1 = testRootNode.addNode(nodeName1, testNodeType);
Node n2 = testRootNode.addNode(nodeName1, testNodeType);
testRootNode.getSession().save();
assertEquals("Names of same name siblings are not equal.", n1.getName(), n2.getName());
} else {
testRootNode.addNode(nodeName1, testNodeType);
try {
testRootNode.addNode(nodeName1, testNodeType);
fail("Expected ItemExistsException.");
} catch (ItemExistsException e) {
// correct
}
}
}
use of javax.jcr.ItemExistsException in project jackrabbit by apache.
the class SaveTest method testItemExistsException.
/**
* Tests if an {@link javax.jcr.ItemExistsException} is thrown when a query
* is stored on an existing node and same name siblings are not allowed.
* @throws NotExecutableException if nt:query is not supported.
*/
public void testItemExistsException() throws RepositoryException, NotExecutableException {
checkNtQuery();
Query query = superuser.getWorkspace().getQueryManager().createQuery(statement, Query.XPATH);
Node qNode = query.storeAsNode(testRoot + "/" + nodeName1);
// create another one
query = superuser.getWorkspace().getQueryManager().createQuery(statement, Query.XPATH);
try {
query.storeAsNode(testRoot + "/" + nodeName1);
if (!qNode.getDefinition().allowsSameNameSiblings()) {
// must throw if same name siblings are not allowed
fail("Query.storeAsNode() did not throw ItemExistsException");
}
} catch (ItemExistsException e) {
if (qNode.getDefinition().allowsSameNameSiblings()) {
fail("Query.storeAsNode() must not throw ItemExistsException " + "when same name siblings are allowed");
} else {
// expected behaviour
}
}
}
use of javax.jcr.ItemExistsException in project jackrabbit by apache.
the class RestoreTest method testRestoreWithUUIDConflict.
/**
* Tests if restoring the <code>Version</code> of an existing node throws an
* <code>ItemExistsException</code> if removeExisting is set to FALSE.
*/
@SuppressWarnings("deprecation")
public void testRestoreWithUUIDConflict() throws RepositoryException, NotExecutableException {
try {
Node naa = createVersionableNode(versionableNode, nodeName4, versionableNodeType);
// Verify that nodes used for the test have proper opv behaviour
NodeDefinition nd = naa.getDefinition();
if (nd.getOnParentVersion() != OnParentVersionAction.COPY && nd.getOnParentVersion() != OnParentVersionAction.VERSION) {
throw new NotExecutableException("Child nodes must have OPV COPY or VERSION in order to be able to test Node.restore with uuid conflict.");
}
Version v = versionableNode.checkin();
versionableNode.checkout();
superuser.move(naa.getPath(), versionableNode2.getPath() + "/" + naa.getName());
superuser.save();
versionableNode.restore(v, false);
fail("Node.restore( Version, boolean ): An ItemExistsException must be thrown if the node to be restored already exsits and removeExisting was set to false.");
} catch (ItemExistsException e) {
// success
}
}
use of javax.jcr.ItemExistsException in project jackrabbit by apache.
the class RestoreTest method testRestoreWithUUIDConflictJcr2.
/**
* Tests if restoring the <code>Version</code> of an existing node throws an
* <code>ItemExistsException</code> if removeExisting is set to FALSE.
*/
public void testRestoreWithUUIDConflictJcr2() throws RepositoryException, NotExecutableException {
try {
Node naa = createVersionableNode(versionableNode, nodeName4, versionableNodeType);
// Verify that nodes used for the test have proper opv behaviour
NodeDefinition nd = naa.getDefinition();
if (nd.getOnParentVersion() != OnParentVersionAction.COPY && nd.getOnParentVersion() != OnParentVersionAction.VERSION) {
throw new NotExecutableException("Child nodes must have OPV COPY or VERSION in order to be able to test Node.restore with uuid conflict.");
}
Version v = versionManager.checkin(versionableNode.getPath());
versionManager.checkout(versionableNode.getPath());
superuser.move(naa.getPath(), versionableNode2.getPath() + "/" + naa.getName());
superuser.save();
versionManager.restore(v, false);
fail("Node.restore( Version, boolean ): An ItemExistsException must be thrown if the node to be restored already exsits and removeExisting was set to false.");
} catch (ItemExistsException e) {
// success
}
}
Aggregations