use of javax.jcr.nodetype.NodeTypeManager 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.NodeTypeManager in project jackrabbit-oak by apache.
the class RepositoryTest method mixin.
@Test
public void mixin() throws RepositoryException {
NodeTypeManager ntMgr = getAdminSession().getWorkspace().getNodeTypeManager();
NodeTypeTemplate mixTest = ntMgr.createNodeTypeTemplate();
mixTest.setName("mix:test");
mixTest.setMixin(true);
ntMgr.registerNodeType(mixTest, false);
Node testNode = getNode(TEST_PATH);
NodeType[] mix = testNode.getMixinNodeTypes();
assertEquals(0, mix.length);
testNode.addMixin("mix:test");
testNode.getSession().save();
Session session2 = createAnonymousSession();
try {
mix = session2.getNode(TEST_PATH).getMixinNodeTypes();
assertEquals(1, mix.length);
assertEquals("mix:test", mix[0].getName());
} finally {
session2.logout();
}
testNode.removeMixin("mix:test");
}
use of javax.jcr.nodetype.NodeTypeManager in project jackrabbit-oak by apache.
the class NodeTypeTest method trivialUpdates.
@Test
public void trivialUpdates() throws Exception {
// test various trivial updates that should not trigger repository scans
// whether or not the repository scan happens can not be checked directly;
// it requires inspecting the INFO level log
String[] types = new String[] { "trivial1", "trivial2" };
ArrayList<NodeTypeTemplate> ntt = new ArrayList<NodeTypeTemplate>();
// adding node types
Session session = getAdminSession();
NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
for (String t : types) {
NodeTypeTemplate nt = manager.createNodeTypeTemplate();
nt.setName(t);
ntt.add(nt);
}
manager.registerNodeTypes(ntt.toArray(new NodeTypeTemplate[0]), false);
// adding an optional property
ntt = new ArrayList<NodeTypeTemplate>();
for (String t : types) {
NodeTypeDefinition ntd = manager.getNodeType(t);
PropertyDefinitionTemplate opt = manager.createPropertyDefinitionTemplate();
opt.setMandatory(false);
opt.setName("optional");
opt.setRequiredType(PropertyType.STRING);
PropertyDefinitionTemplate opts = manager.createPropertyDefinitionTemplate();
opts.setMandatory(false);
opts.setMultiple(true);
opts.setName("optionals");
opts.setRequiredType(PropertyType.STRING);
NodeTypeTemplate nt = manager.createNodeTypeTemplate(ntd);
List pdt = nt.getPropertyDefinitionTemplates();
pdt.add(opt);
pdt.add(opts);
ntt.add(nt);
}
manager.registerNodeTypes(ntt.toArray(new NodeTypeTemplate[0]), true);
// make one optional property mandatory
ntt = new ArrayList<NodeTypeTemplate>();
for (String t : types) {
NodeTypeDefinition ntd = manager.getNodeType(t);
PropertyDefinitionTemplate opt = manager.createPropertyDefinitionTemplate();
opt.setMandatory("trivial2".equals(t));
opt.setName("optional");
opt.setRequiredType(PropertyType.STRING);
PropertyDefinitionTemplate opts = manager.createPropertyDefinitionTemplate();
opts.setMandatory("trivial2".equals(t));
opts.setMultiple(true);
opts.setName("optionals");
opts.setRequiredType(PropertyType.STRING);
NodeTypeTemplate nt = manager.createNodeTypeTemplate(ntd);
List pdt = nt.getPropertyDefinitionTemplates();
pdt.add(opt);
pdt.add(opts);
ntt.add(nt);
}
// but update both node types
manager.registerNodeTypes(ntt.toArray(new NodeTypeTemplate[0]), true);
}
use of javax.jcr.nodetype.NodeTypeManager 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.NodeTypeManager 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