use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit by apache.
the class VersionTest method testSetProperty.
/**
* Tests if
* <ul> <li><code>Version.setProperty(String, String[])</code></li>
* <li><code>Version.setProperty(String, String[], int)</code></li>
* <li><code>Version.setProperty(String, Value[])</code></li>
* <li><code>Version.setProperty(String, Value[], int)</code></li>
* <li><code>Version.setProperty(String, boolean)</code></li>
* <li><code>Version.setProperty(String, double)</code></li>
* <li><code>Version.setProperty(String, InputStream)</code></li>
* <li><code>Version.setProperty(String, String)</code></li>
* <li><code>Version.setProperty(String, Calendar)</code></li>
* <li><code>Version.setProperty(String, Node)</code></li>
* <li><code>Version.setProperty(String, Value)</code></li>
* <li><code>Version.setProperty(String, long)</code></li>
* </ul> all throw a
* {@link javax.jcr.nodetype.ConstraintViolationException}
*/
public void testSetProperty() throws Exception {
// create Value[] object
Value[] vArray = new Value[3];
vArray[0] = superuser.getValueFactory().createValue("abc");
vArray[1] = superuser.getValueFactory().createValue("xyz");
vArray[2] = superuser.getValueFactory().createValue("123");
// create String array
String[] s = { "abc", "xyz", "123" };
try {
version.setProperty(propertyName1, s);
version.getSession().save();
fail("Version should be read-only: Version.setProperty(String,String[]) did not throw a ConstraintViolationException");
} catch (ConstraintViolationException success) {
}
try {
version.setProperty(propertyName1, s, PropertyType.STRING);
version.getSession().save();
fail("Version should be read-only: Version.setProperty(String,String[],int) did not throw a ConstraintViolationException");
} catch (ConstraintViolationException success) {
}
try {
version.setProperty(propertyName1, vArray);
version.getSession().save();
fail("Version should be read-only: Version.setProperty(String,Value[]) did not throw a ConstraintViolationException");
} catch (ConstraintViolationException success) {
}
try {
version.setProperty(propertyName1, vArray, PropertyType.STRING);
version.getSession().save();
fail("Version should be read-only: Version.setProperty(String,Value[],int]) did not throw a ConstraintViolationException");
} catch (ConstraintViolationException success) {
}
try {
version.setProperty(propertyName1, true);
version.getSession().save();
fail("Version should be read-only: Version.setProperty(String,boolean) did not throw a ConstraintViolationException");
} catch (ConstraintViolationException success) {
}
try {
version.setProperty(propertyName1, 123);
version.getSession().save();
fail("Version should be read-only: Version.setProperty(String,double) did not throw a ConstraintViolationException");
} catch (ConstraintViolationException success) {
}
try {
byte[] bytes = { 73, 26, 32, -36, 40, -43, -124 };
InputStream inpStream = new ByteArrayInputStream(bytes);
version.setProperty(propertyName1, inpStream);
version.getSession().save();
fail("Version should be read-only: Version.setProperty(String,InputStream) did not throw a ConstraintViolationException");
} catch (ConstraintViolationException success) {
}
try {
version.setProperty(propertyName1, "abc");
version.getSession().save();
fail("Version should be read-only: Version.setProperty(String,String) did not throw a ConstraintViolationException");
} catch (ConstraintViolationException success) {
}
try {
Calendar c = new GregorianCalendar(1945, 1, 6, 16, 20, 0);
version.setProperty(propertyName1, c);
version.getSession().save();
fail("Version should be read-only: Version.setProperty(String,Calendar) did not throw a ConstraintViolationException");
} catch (ConstraintViolationException success) {
}
try {
version.setProperty(propertyName1, version);
version.getSession().save();
fail("Version should be read-only: Version.setProperty(String,Node) did not throw a ConstraintViolationException");
} catch (ConstraintViolationException success) {
}
try {
Value v = superuser.getValueFactory().createValue("abc");
version.setProperty(propertyName1, v);
version.getSession().save();
fail("Version should be read-only: Version.setProperty(String,Value) did not throw a ConstraintViolationException");
} catch (ConstraintViolationException success) {
}
try {
version.setProperty(propertyName1, -2147483650L);
version.getSession().save();
fail("Version should be read-only: Version.setProperty(String,long) did not throw a ConstraintViolationException");
} catch (ConstraintViolationException success) {
}
}
use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit by apache.
the class AddMixinTest method testAddItemsDefinedByMixin2.
public void testAddItemsDefinedByMixin2() throws NotExecutableException, RepositoryException {
// register mixin
NodeTypeManager ntm = superuser.getWorkspace().getNodeTypeManager();
NodeTypeTemplate ntd = ntm.createNodeTypeTemplate();
ntd.setName("testMixin");
ntd.setMixin(true);
NodeDefinitionTemplate nodeDef = ntm.createNodeDefinitionTemplate();
nodeDef.setName("child");
nodeDef.setRequiredPrimaryTypeNames(new String[] { "nt:folder" });
ntd.getNodeDefinitionTemplates().add(nodeDef);
ntm.registerNodeType(ntd, true);
// create node and add mixin
Node node = testRootNode.addNode(nodeName1, "nt:resource");
node.setProperty("jcr:data", "abc");
node.addMixin("testMixin");
superuser.save();
// node type
try {
node.addNode("child");
fail();
} catch (ConstraintViolationException e) {
// success as ChildNode Definition doesn't specify a default primary
// type -> see comment in ItemDefinitionProvider#getQNodeDefinition
}
}
use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit by apache.
the class MandatoryItemTest method testRemoval.
public void testRemoval() throws NotExecutableException, RepositoryException {
Node n;
Node childN = null;
Property childP = null;
try {
n = testRootNode.addNode(nodeName1, testNodeType);
if (childNodeDef != null) {
childN = n.addNode(childNodeDef.getName(), childNodeDef.getDefaultPrimaryType().getName());
}
if (childPropDef != null) {
// TODO: check if definition defines default values
childP = n.setProperty(childPropDef.getName(), "any value");
}
testRootNode.save();
} catch (RepositoryException e) {
throw new NotExecutableException();
}
// remove the mandatory items ((must succeed))
if (childN != null) {
childN.remove();
}
if (childP != null) {
childP.remove();
}
// ... however, saving must not be allowed.
try {
testRootNode.save();
fail("removing mandatory child items without re-adding them must fail.");
} catch (ConstraintViolationException e) {
// success.
}
// re-add the mandatory items
if (childNodeDef != null) {
childN = n.addNode(childNodeDef.getName(), childNodeDef.getDefaultPrimaryType().getName());
}
if (childPropDef != null) {
// TODO: check if definition defines default values
childP = n.setProperty(childPropDef.getName(), "any value");
}
// save must succeed now.
testRootNode.save();
}
use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit by apache.
the class MandatoryItemTest method testCreation.
public void testCreation() throws NotExecutableException, RepositoryException {
Node n;
try {
n = testRootNode.addNode(nodeName1, testNodeType);
} catch (RepositoryException e) {
throw new NotExecutableException();
}
try {
testRootNode.save();
fail("Saving without having added the mandatory child items must fail.");
} catch (ConstraintViolationException e) {
// success
}
if (childNodeDef != null) {
n.addNode(childNodeDef.getName(), childNodeDef.getDefaultPrimaryType().getName());
}
if (childPropDef != null) {
// TODO: check if definition defines default values
n.setProperty(childPropDef.getName(), "any value");
}
// now save must succeed.
testRootNode.save();
}
use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit-oak by apache.
the class EffectiveNodeType method checkSetProperty.
public void checkSetProperty(PropertyState property) throws RepositoryException {
PropertyDefinition definition = getDefinition(property);
if (definition.isProtected()) {
return;
}
NodeType nt = definition.getDeclaringNodeType();
if (definition.isMultiple()) {
List<Value> values = ValueFactoryImpl.createValues(property, ntMgr.getNamePathMapper());
if (!nt.canSetProperty(property.getName(), values.toArray(new Value[values.size()]))) {
throw new ConstraintViolationException("Cannot set property '" + property.getName() + "' to '" + values + '\'');
}
} else {
Value v = ValueFactoryImpl.createValue(property, ntMgr.getNamePathMapper());
if (!nt.canSetProperty(property.getName(), v)) {
throw new ConstraintViolationException("Cannot set property '" + property.getName() + "' to '" + v + '\'');
}
}
}
Aggregations