use of javax.jcr.nodetype.NodeTypeManager 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.NodeTypeManager 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) && !ntActivity.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.NodeTypeManager in project jackrabbit by apache.
the class LuceneQueryBuilder method visit.
public Object visit(NodeTypeQueryNode node, Object data) {
List<Term> terms = new ArrayList<Term>();
try {
String mixinTypesField = resolver.getJCRName(NameConstants.JCR_MIXINTYPES);
String primaryTypeField = resolver.getJCRName(NameConstants.JCR_PRIMARYTYPE);
NodeTypeManager ntMgr = session.getWorkspace().getNodeTypeManager();
NodeType base = ntMgr.getNodeType(session.getJCRName(node.getValue()));
if (base.isMixin()) {
// search for nodes where jcr:mixinTypes is set to this mixin
Term t = new Term(FieldNames.PROPERTIES, FieldNames.createNamedValue(mixinTypesField, resolver.getJCRName(node.getValue())));
terms.add(t);
} else {
// search for nodes where jcr:primaryType is set to this type
Term t = new Term(FieldNames.PROPERTIES, FieldNames.createNamedValue(primaryTypeField, resolver.getJCRName(node.getValue())));
terms.add(t);
}
// now search for all node types that are derived from base
NodeTypeIterator allTypes = ntMgr.getAllNodeTypes();
while (allTypes.hasNext()) {
NodeType nt = allTypes.nextNodeType();
NodeType[] superTypes = nt.getSupertypes();
if (Arrays.asList(superTypes).contains(base)) {
Name n = session.getQName(nt.getName());
String ntName = nsMappings.translateName(n);
Term t;
if (nt.isMixin()) {
// search on jcr:mixinTypes
t = new Term(FieldNames.PROPERTIES, FieldNames.createNamedValue(mixinTypesField, ntName));
} else {
// search on jcr:primaryType
t = new Term(FieldNames.PROPERTIES, FieldNames.createNamedValue(primaryTypeField, ntName));
}
terms.add(t);
}
}
} catch (NameException e) {
exceptions.add(e);
} catch (RepositoryException e) {
exceptions.add(e);
}
if (terms.size() == 0) {
// exception occured
return new BooleanQuery();
} else if (terms.size() == 1) {
return new JackrabbitTermQuery(terms.get(0));
} else {
BooleanQuery b = new BooleanQuery();
for (Term term : terms) {
b.add(new JackrabbitTermQuery(term), Occur.SHOULD);
}
return b;
}
}
use of javax.jcr.nodetype.NodeTypeManager in project jackrabbit by apache.
the class RepositoryCopierTest method createSourceRepository.
private RepositoryImpl createSourceRepository() throws Exception {
RepositoryImpl repository = RepositoryImpl.create(RepositoryConfig.install(SOURCE));
Session session = repository.login(CREDENTIALS);
try {
NamespaceRegistry registry = session.getWorkspace().getNamespaceRegistry();
registry.registerNamespace("test", "http://www.example.org/");
NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
NodeTypeTemplate template = manager.createNodeTypeTemplate();
template.setName("test:unstructured");
template.setDeclaredSuperTypeNames(new String[] { "nt:unstructured" });
manager.registerNodeType(template, false);
Node root = session.getRootNode();
Node referenceable = root.addNode("referenceable", "test:unstructured");
referenceable.addMixin(NodeType.MIX_REFERENCEABLE);
session.save();
identifier = referenceable.getIdentifier();
Node properties = root.addNode("properties", "test:unstructured");
properties.setProperty("boolean", true);
Binary binary = session.getValueFactory().createBinary(new ByteArrayInputStream(BINARY));
try {
properties.setProperty("binary", binary);
} finally {
binary.dispose();
}
properties.setProperty("date", DATE);
properties.setProperty("decimal", new BigDecimal(123));
properties.setProperty("double", Math.PI);
properties.setProperty("long", 9876543210L);
properties.setProperty("reference", referenceable);
properties.setProperty("string", "test");
properties.setProperty("multiple", "a,b,c".split(","));
session.save();
binary = properties.getProperty("binary").getBinary();
try {
InputStream stream = binary.getStream();
try {
for (int i = 0; i < BINARY.length; i++) {
assertEquals(BINARY[i], (byte) stream.read());
}
assertEquals(-1, stream.read());
} finally {
stream.close();
}
} finally {
binary.dispose();
}
} finally {
session.logout();
}
return repository;
}
use of javax.jcr.nodetype.NodeTypeManager in project jackrabbit by apache.
the class AbstractRepositoryOperationTest method testRegisterNodeType.
public void testRegisterNodeType() throws Exception {
assertDefaultPrivileges(NameConstants.JCR_NODE_TYPE_DEFINITION_MANAGEMENT);
assertPermission(Permission.NODE_TYPE_DEF_MNGMT, false);
Workspace testWsp = getTestWorkspace();
NodeTypeManager ntm = testWsp.getNodeTypeManager();
NodeTypeTemplate ntd = ntm.createNodeTypeTemplate();
ntd.setName("testNodeType");
ntd.setMixin(true);
try {
ntm.registerNodeType(ntd, true);
fail("Node type registration should be denied.");
} catch (AccessDeniedException e) {
// success
}
try {
ntm.registerNodeType(ntd, false);
fail("Node type registration should be denied.");
} catch (AccessDeniedException e) {
// success
}
NodeTypeTemplate[] ntds = new NodeTypeTemplate[2];
ntds[0] = ntd;
ntds[1] = ntm.createNodeTypeTemplate();
ntds[1].setName("anotherNodeType");
ntds[1].setDeclaredSuperTypeNames(new String[] { "nt:file" });
try {
ntm.registerNodeTypes(ntds, true);
fail("Node type registration should be denied.");
} catch (AccessDeniedException e) {
// success
}
try {
ntm.registerNodeTypes(ntds, false);
fail("Node type registration should be denied.");
} catch (AccessDeniedException e) {
// success
}
}
Aggregations