use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit by apache.
the class NodeSetPrimaryTypeTest method testSetMixinAsPrimaryType.
/**
* Tests if <code>Node.setPrimaryType(String)</code> throws a
* <code>ConstraintViolationException</code> if the
* name of a mixin type is passed
*/
public void testSetMixinAsPrimaryType() throws RepositoryException {
Session session = testRootNode.getSession();
NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
NodeTypeIterator nts = manager.getMixinNodeTypes();
while (nts.hasNext()) {
try {
Node node = testRootNode.addNode(nodeName1, testNodeType);
node.setPrimaryType(nts.nextNodeType().getName());
fail("Node.setPrimaryType(String) must throw ConstraintViolationException if the specified node type name refers to a mixin.");
} catch (ConstraintViolationException e) {
// success
} finally {
// reset the changes.
session.refresh(false);
}
}
}
use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit by apache.
the class NodeSetPrimaryTypeTest method testSetAbstractAsPrimaryType.
/**
* Tests if <code>Node.setPrimaryType(String)</code> throws a
* <code>ConstraintViolationException</code> if the
* name of a mixin type is passed
*/
public void testSetAbstractAsPrimaryType() throws RepositoryException {
Session session = testRootNode.getSession();
NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
NodeTypeIterator nts = manager.getPrimaryNodeTypes();
while (nts.hasNext()) {
NodeType nt = nts.nextNodeType();
if (nt.isAbstract()) {
try {
Node node = testRootNode.addNode(nodeName1, testNodeType);
node.setPrimaryType(nt.getName());
fail("Node.setPrimaryType(String) must throw ConstraintViolationException if the specified node type name refers to an abstract node type.");
} catch (ConstraintViolationException e) {
// success
} finally {
// reset the changes.
session.refresh(false);
}
}
}
}
use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit by apache.
the class NodeSetPrimaryTypeTest method testSetPrimaryType.
// TODO: test if node definition is properly reset
// TODO: test if child items are properly reset upon changing definition
// TODO: test if conflicts are properly detected
/**
* Tests a successful call to <code>Node.setPrimaryType(String)</code>
*/
public void testSetPrimaryType() throws RepositoryException {
Session session = testRootNode.getSession();
Session otherSession = null;
String nonExistingMixinName = NodeMixinUtil.getNonExistingMixinName(session);
Node node = testRootNode.addNode(nodeName1, testNodeType);
superuser.save();
// TODO improve. retrieve settable node type name from config.
NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
NodeTypeIterator nts = manager.getPrimaryNodeTypes();
while (nts.hasNext()) {
NodeType nt = nts.nextNodeType();
String ntName = nt.getName();
if (!nt.isAbstract() && !ntFrozenNode.equals(ntName)) {
try {
node.setPrimaryType(ntName);
// property value must be adjusted immediately
assertEquals("The value of the jcr:primaryType property must change upon setPrimaryType.", ntName, node.getProperty(jcrPrimaryType).getString());
// save changes -> reflected upon Node.getPrimaryNodeType and Property.getValue
superuser.save();
assertEquals("Node.getPrimaryNodeType must reflect the changes made.", ntName, node.getPrimaryNodeType().getName());
assertEquals("The value of the jcr:primaryType property must change upon setPrimaryType.", ntName, node.getProperty(jcrPrimaryType).getString());
otherSession = getHelper().getReadOnlySession();
assertEquals("Node.getPrimaryNodeType must reflect the changes made.", ntName, otherSession.getNode(node.getPath()).getPrimaryNodeType().getName());
assertEquals("The value of the jcr:primaryType property must change upon setPrimaryType.", ntName, otherSession.getNode(node.getPath()).getProperty(jcrPrimaryType).getString());
// was successful
return;
} catch (ConstraintViolationException e) {
// may happen as long as arbitrary primary types are used for testing -> ignore
} finally {
if (otherSession != null) {
otherSession.logout();
}
// revert any unsaved changes.
session.refresh(false);
}
}
}
}
use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit by apache.
the class NodeTest method testRemoveMandatoryNode.
/**
* Creates a node with a mandatory child node using {@link
* Node#addNode(String, String)}, saves on parent node then tries to delete
* the mandatory child node.
* <p>
* This should throw a {@link ConstraintViolationException}.
* <p>
* Prerequisites: <ul>
* <li><code>javax.jcr.tck.NodeTest.testRemoveMandatoryNode.nodetype2</code>
* a node type that has a mandatory child node</li> <li><code>javax.jcr.tck.NodeTest.testRemoveMandatoryNode.nodetype3</code>
* nodetype of the mandatory child node</li> <li><code>javax.jcr.tck.NodeTest.testRemoveMandatoryNode.nodename3</code>
* name of the mandatory child node</li> </ul>
*/
public void testRemoveMandatoryNode() throws RepositoryException {
// get default workspace test root node using superuser session
Node defaultRootNode = (Node) superuser.getItem(testRootNode.getPath());
// create the node with the mandatory child node definition
Node defaultTestNode = defaultRootNode.addNode(nodeName2, getProperty("nodetype2"));
// add the mandatory child node
Node defaultTestNodeChild = defaultTestNode.addNode(nodeName3, getProperty("nodetype3"));
// save changes
defaultRootNode.save();
try {
// try to remove the mandatory node
defaultTestNodeChild.remove();
defaultTestNode.save();
fail("Removing a mandatory node should throw a ConstraintViolationException");
} catch (ConstraintViolationException e) {
// ok, works as expected
}
}
use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit by apache.
the class NodeImpl method addNode.
/**
* @see Node#addNode(String, String)
*/
public Node addNode(String relPath, String primaryNodeTypeName) throws ItemExistsException, PathNotFoundException, NoSuchNodeTypeException, LockException, VersionException, ConstraintViolationException, RepositoryException {
checkIsWritable();
// build path object and retrieve parent node
Path nodePath = getPath(relPath).getNormalizedPath();
if (nodePath.getIndex() != Path.INDEX_UNDEFINED) {
String msg = "Illegal subscript specified: " + relPath;
log.debug(msg);
throw new RepositoryException(msg);
}
NodeImpl parentNode;
if (nodePath.getLength() == 1) {
parentNode = this;
} else {
Path parentPath = nodePath.getAncestor(1);
ItemManager itemMgr = getItemManager();
if (itemMgr.nodeExists(parentPath)) {
parentNode = (NodeImpl) itemMgr.getNode(parentPath);
} else if (itemMgr.propertyExists(parentPath)) {
String msg = "Cannot add a node to property " + LogUtil.safeGetJCRPath(parentPath, session.getPathResolver());
log.debug(msg);
throw new ConstraintViolationException(msg);
} else {
throw new PathNotFoundException("Cannot add a new node to a non-existing parent at " + LogUtil.safeGetJCRPath(parentPath, session.getPathResolver()));
}
}
// get names objects for node and nt
Name nodeName = nodePath.getName();
Name ntName = (primaryNodeTypeName == null) ? null : getQName(primaryNodeTypeName);
// create new node (including validation checks)
return parentNode.createNode(nodeName, ntName);
}
Aggregations