use of javax.jcr.nodetype.NodeTypeManager in project jackrabbit-oak by apache.
the class ObservationTest method deepNodeTypeMixinHierarchy.
@Test
public void deepNodeTypeMixinHierarchy() throws Exception {
NodeTypeManager ntm = getAdminSession().getWorkspace().getNodeTypeManager();
NodeTypeTemplate parentMixin = ntm.createNodeTypeTemplate();
parentMixin.setName("parentmixin");
parentMixin.setMixin(true);
ntm.registerNodeType(parentMixin, false);
NodeTypeTemplate childMixin = ntm.createNodeTypeTemplate();
childMixin.setName("childmixin");
childMixin.setMixin(true);
childMixin.setDeclaredSuperTypeNames(new String[] { "parentmixin" });
ntm.registerNodeType(childMixin, false);
NodeTypeTemplate mytype = ntm.createNodeTypeTemplate();
mytype.setName("mytype");
mytype.setMixin(false);
mytype.setDeclaredSuperTypeNames(new String[] { "childmixin" });
NodeDefinitionTemplate child = ntm.createNodeDefinitionTemplate();
child.setName("*");
child.setDefaultPrimaryTypeName("nt:base");
child.setRequiredPrimaryTypeNames(new String[] { "nt:base" });
List<NodeDefinition> children = mytype.getNodeDefinitionTemplates();
children.add(child);
ntm.registerNodeType(mytype, false);
getAdminSession().save();
// create a fresh session here to catch the above new node type definitions
observingSession = createAdminSession();
observationManager = observingSession.getWorkspace().getObservationManager();
JackrabbitObservationManager oManager = (JackrabbitObservationManager) observationManager;
ExpectationListener listener = new ExpectationListener();
JackrabbitEventFilter filter = new JackrabbitEventFilter().setAbsPath("/").setIsDeep(true).setNodeTypes(new String[] { "parentmixin" }).setEventTypes(ALL_EVENTS);
oManager.addEventListener(listener, filter);
Node n = getNode(TEST_PATH).addNode("n", "mytype");
listener.expect(n.getPath() + "/jcr:primaryType", PROPERTY_ADDED);
Node m = n.addNode("m", "nt:unstructured");
listener.expect(m.getPath(), NODE_ADDED);
getAdminSession().save();
Thread.sleep(1000);
List<Expectation> missing = listener.getMissing(TIME_OUT, TimeUnit.SECONDS);
assertTrue("Missing events: " + missing, missing.isEmpty());
List<Event> unexpected = listener.getUnexpected();
assertTrue("Unexpected events: " + unexpected, unexpected.isEmpty());
}
use of javax.jcr.nodetype.NodeTypeManager in project jackrabbit-oak by apache.
the class OpvIgnoreTest method createNodeDefinitionWithIgnoreOPVNode.
private NodeDefinitionTemplate createNodeDefinitionWithIgnoreOPVNode(String nodeTypeName) throws RepositoryException {
NodeTypeManager manager = superuser.getWorkspace().getNodeTypeManager();
NodeDefinitionTemplate def = manager.createNodeDefinitionTemplate();
def.setOnParentVersion(OnParentVersionAction.IGNORE);
def.setName("child");
def.setRequiredPrimaryTypeNames(new String[] { JcrConstants.NT_BASE });
NodeTypeTemplate tmpl = manager.createNodeTypeTemplate();
tmpl.setName(nodeTypeName);
tmpl.setMixin(true);
tmpl.getNodeDefinitionTemplates().add(def);
manager.registerNodeType(tmpl, true);
return def;
}
use of javax.jcr.nodetype.NodeTypeManager in project jackrabbit by apache.
the class IndexingConfigurationImplTest method testAddNodeTypeToRegistry.
public void testAddNodeTypeToRegistry() throws Exception {
IndexingConfiguration config = createConfig("config4");
// add node type
NodeTypeManager ntMgr = session.getWorkspace().getNodeTypeManager();
String baseName = "indexingTextNodeType";
int i = 0;
String nt;
do {
nt = baseName + "_" + i++;
} while (ntMgr.hasNodeType(nt));
// register node type
NodeTypeTemplate ntTemplate = ntMgr.createNodeTypeTemplate();
ntTemplate.setName(nt);
ntTemplate.setDeclaredSuperTypeNames(new String[] { ntUnstructured });
ntMgr.registerNodeType(ntTemplate, false);
// create node
Node n = testRootNode.addNode(nodeName2, nt);
session.save();
// get state
NodeState state = (NodeState) getSearchIndex().getContext().getItemStateManager().getItemState(new NodeId(n.getIdentifier()));
assertTrue(config.isIndexed(state, FOO));
assertFalse(config.isIncludedInNodeScopeIndex(state, FOO));
}
use of javax.jcr.nodetype.NodeTypeManager in project jackrabbit by apache.
the class SetValueConstraintViolationExceptionTest method testReferenceProperty.
/**
* Tests if setValue(Node value) and setValue(Value value) where value is a
* ReferenceValue throw a ConstraintViolationException if the change would
* violate a value constraint
*/
public void testReferenceProperty() throws NotExecutableException, RepositoryException {
// locate a PropertyDefinition with ValueConstraints
PropertyDefinition propDef = NodeTypeUtil.locatePropertyDef(superuser, PropertyType.REFERENCE, false, false, true, false);
if (propDef == null) {
throw new NotExecutableException("No reference property def with " + "testable value constraints has been found");
}
String[] valueConstraints = propDef.getValueConstraints();
if (valueConstraints == null || valueConstraints.length == 0) {
throw new NotExecutableException("No reference property def with " + "testable value constraints has been found");
}
List<String> constraints = Arrays.asList(valueConstraints);
String nodeTypeSatisfied = constraints.get(0);
String nodeTypeNotSatisfied = null;
NodeTypeManager manager = superuser.getWorkspace().getNodeTypeManager();
NodeTypeIterator types = manager.getAllNodeTypes();
// find a NodeType which is not satisfying the constraints
while (types.hasNext()) {
NodeType type = types.nextNodeType();
String name = type.getName();
if (constraints.contains(name) || ntFrozenNode.equals(name)) {
continue;
}
if (type.getChildNodeDefinitions() != null && type.getChildNodeDefinitions().length > 0) {
continue;
}
nodeTypeNotSatisfied = name;
break;
}
if (nodeTypeNotSatisfied == null) {
throw new NotExecutableException("No reference property def with " + "testable value constraints has been found");
}
// create a sub node of testRootNode of type propDef.getDeclaringNodeType()
// and add a property with constraints to this node
Node node;
Property prop;
Node nodeSatisfied;
Node nodeNotSatisfied;
try {
String nodeType = propDef.getDeclaringNodeType().getName();
node = testRootNode.addNode(nodeName2, nodeType);
// create a referenceable node satisfying the constraint
nodeSatisfied = testRootNode.addNode(nodeName3, nodeTypeSatisfied);
ensureMixinType(nodeSatisfied, mixReferenceable);
// create a referenceable node not satisfying the constraint
nodeNotSatisfied = testRootNode.addNode(nodeName4, nodeTypeNotSatisfied);
ensureMixinType(nodeNotSatisfied, mixReferenceable);
// some implementations may require a save after addMixin()
testRootNode.getSession().save();
prop = node.setProperty(propDef.getName(), nodeSatisfied);
testRootNode.getSession().save();
} catch (ConstraintViolationException e) {
// implementation specific constraints do not allow to set up test environment
throw new NotExecutableException("Not able to create required test items.");
}
// test of signature setValue(Node value)
try {
prop.setValue(nodeNotSatisfied);
node.save();
fail("setValue(Node value) must throw a ConstraintViolationException " + "if the change would violate a node type constraint " + "either immediately or on save");
} catch (ConstraintViolationException e) {
// success
}
// test of signature setValue(Value value)
try {
prop.setValue(superuser.getValueFactory().createValue(nodeNotSatisfied));
node.save();
fail("setValue(Value value) must throw a ConstraintViolationException " + "if the change would violate a node type constraint " + "either immediately or on save");
} catch (ConstraintViolationException e) {
// success
}
}
use of javax.jcr.nodetype.NodeTypeManager in project jackrabbit by apache.
the class SetPropertyConstraintViolationExceptionTest method testReferenceProperty.
/**
* Tests if setProperty(String name, Node value) and setProperty(String
* name, Value value) where value is a ReferenceValue throw a
* ConstraintViolationException either immediately (by setProperty()), or on
* save, if the change would violate a node type constraint
*/
public void testReferenceProperty() throws NotExecutableException, RepositoryException {
// locate a PropertyDefinition with ValueConstraints
PropertyDefinition propDef = NodeTypeUtil.locatePropertyDef(superuser, PropertyType.REFERENCE, false, false, true, false);
if (propDef == null) {
throw new NotExecutableException("No reference property def with " + "testable value constraints has been found");
}
String[] valueConstraints = propDef.getValueConstraints();
if (valueConstraints == null || valueConstraints.length == 0) {
throw new NotExecutableException("No reference property def with " + "testable value constraints has been found");
}
List<String> constraints = Arrays.asList(valueConstraints);
String nodeTypeNotSatisfied = null;
NodeTypeManager manager = superuser.getWorkspace().getNodeTypeManager();
NodeTypeIterator types = manager.getAllNodeTypes();
// find a NodeType which is not satisfying the constraints
while (types.hasNext()) {
NodeType type = types.nextNodeType();
String name = type.getName();
if (constraints.contains(name) || ntFrozenNode.equals(name)) {
continue;
}
if (type.getChildNodeDefinitions() != null && type.getChildNodeDefinitions().length > 0) {
continue;
}
nodeTypeNotSatisfied = name;
break;
}
if (nodeTypeNotSatisfied == null) {
throw new NotExecutableException("No reference property def with " + "testable value constraints has been found");
}
// create a sub node of testRootNode of type propDef.getDeclaringNodeType()
Node node;
Node nodeNotSatisfied;
try {
String nodeType = propDef.getDeclaringNodeType().getName();
node = testRootNode.addNode(nodeName2, nodeType);
// create a referenceable node not satisfying the constraint
nodeNotSatisfied = testRootNode.addNode(nodeName4, nodeTypeNotSatisfied);
ensureMixinType(nodeNotSatisfied, mixReferenceable);
testRootNode.getSession().save();
} catch (ConstraintViolationException e) {
// implementation specific constraints do not allow to set up test environment
throw new NotExecutableException("Not able to create required test items.");
}
// test of signature setProperty(String name, Node value)
try {
node.setProperty(propDef.getName(), nodeNotSatisfied);
node.save();
fail("setProperty(String name, Node value) must throw a " + "ConstraintViolationException if the change would violate a " + "node type constraint either immediately or on save");
} catch (ConstraintViolationException e) {
// success
}
// test of signature setProperty(String name, Value value)
try {
node.setProperty(propDef.getName(), superuser.getValueFactory().createValue(nodeNotSatisfied));
node.save();
fail("setProperty(String name, Value value) must throw a " + "ConstraintViolationException if the change would violate a " + "node type constraint either immediately or on save");
} catch (ConstraintViolationException e) {
// success
}
}
Aggregations