use of javax.jcr.PropertyIterator in project jackrabbit by apache.
the class NodeReadMethodsTest method testGetPropertiesNamePattern.
/**
* Test getProperties(String namePattern) with all possible patterns. Tested
* node: root - a NotExecutableException is thrown when root node has no
* properties.
*/
public void testGetPropertiesNamePattern() throws NotExecutableException, RepositoryException {
// get root node and build an ArrayList of its sub nodes
Node node = testRootNode;
if (!node.hasProperties()) {
fail("Root node must always have at least one property: jcr:primaryType");
}
PropertyIterator allPropertiesIt = node.getProperties();
List<Property> allProperties = new ArrayList<Property>();
StringBuffer notExistingPropertyName = new StringBuffer();
while (allPropertiesIt.hasNext()) {
Property p = allPropertiesIt.nextProperty();
allProperties.add(p);
notExistingPropertyName.append(p.getName() + "X");
}
// test that an empty NodeIterator is returned
// when the pattern is not matching any child node
String pattern0 = notExistingPropertyName.toString().replaceAll(":", "");
NodeIterator properties0 = node.getNodes(pattern0);
try {
properties0.nextNode();
fail("An empty NodeIterator must be returned if pattern does" + "not match any child node.");
} catch (NoSuchElementException e) {
// success
}
// all tests are running using root's first property
Property firstProperty = allProperties.get(0);
// test: getProperties("*")
String pattern1 = "*";
String assertString1 = "node.getProperties(\"" + pattern1 + "\"): ";
PropertyIterator properties1 = node.getProperties(pattern1);
assertEquals(assertString1 + "number of properties found: ", allProperties.size(), getSize(properties1));
// test: getProperties("propertyName")
String pattern2 = firstProperty.getName();
String assertString2 = "node.getProperties(\"" + pattern2 + "\"): ";
// test if the names of the found properties are matching the pattern
PropertyIterator properties2 = node.getProperties(pattern2);
while (properties2.hasNext()) {
Property p = properties2.nextProperty();
assertEquals(assertString2 + "name comparison failed: ", firstProperty.getName(), p.getName());
}
// test if the number of found properties is correct
int numExpected2 = 0;
for (int i = 0; i < allProperties.size(); i++) {
Property p = allProperties.get(i);
if (p.getName().equals(firstProperty.getName())) {
numExpected2++;
}
}
properties2 = node.getProperties(pattern2);
assertEquals(assertString2 + "number of properties found: ", numExpected2, getSize(properties2));
// test: getProperties("propertyName|propertyName")
String pattern3 = firstProperty.getName() + "|" + firstProperty.getName();
String assertString3 = "node.getProperties(\"" + pattern3 + "\"): ";
// test if the names of the found properties are matching the pattern
PropertyIterator properties3 = node.getProperties(pattern3);
while (properties3.hasNext()) {
Property p = properties3.nextProperty();
assertEquals(assertString2 + "name comparison failed: ", firstProperty.getName(), p.getName());
}
// test if the number of found properties is correct
int numExpected3 = 0;
for (int i = 0; i < allProperties.size(); i++) {
Property p = allProperties.get(i);
if (p.getName().equals(firstProperty.getName())) {
numExpected3++;
}
}
properties3 = node.getProperties(pattern3);
assertEquals(assertString3 + "number of properties found: ", numExpected3, getSize(properties3));
// test: getProperties("*opertyNam*")
if (firstProperty.getName().length() > 2) {
String name = firstProperty.getName();
String shortenName = name.substring(1, name.length() - 1);
String pattern4 = "*" + shortenName + "*";
String assertString4 = "node.getProperties(\"" + pattern4 + "\"): ";
// test if the names of the found properties are matching the pattern
PropertyIterator properties4 = node.getProperties(pattern4);
while (properties4.hasNext()) {
Property p = properties4.nextProperty();
assertTrue(assertString4 + "name comparison failed: *" + shortenName + "* not found in " + p.getName(), p.getName().indexOf(shortenName) != -1);
}
// test if the number of found properties is correct
int numExpected4 = 0;
for (int i = 0; i < allProperties.size(); i++) {
Property p = allProperties.get(i);
if (p.getName().indexOf(shortenName) != -1) {
numExpected4++;
}
}
properties4 = node.getProperties(pattern4);
assertEquals(assertString4 + "number of properties found: ", numExpected4, getSize(properties4));
}
}
use of javax.jcr.PropertyIterator in project jackrabbit by apache.
the class NodeReadMethodsTest method testGetPropertiesNamePatternArray.
/**
* Test getProperties(String[] namePattern) with all possible patterns.
* @throws NotExecutableException is thrown when root node has no properties.
*/
public void testGetPropertiesNamePatternArray() throws NotExecutableException, RepositoryException {
// get root node and build an ArrayList of its sub nodes
Node node = testRootNode;
if (!node.hasProperties()) {
fail("Root node must always have at least one property: jcr:primaryType");
}
PropertyIterator allPropertiesIt = node.getProperties();
List<Property> allProperties = new ArrayList<Property>();
StringBuffer notExistingPropertyName = new StringBuffer();
while (allPropertiesIt.hasNext()) {
Property p = allPropertiesIt.nextProperty();
allProperties.add(p);
notExistingPropertyName.append(p.getName() + "X");
}
// all tests are running using root's first property
Property firstProperty = allProperties.get(0);
// test: getProperties("*")
String pattern1 = "*";
String assertString1 = "node.getProperties(\"" + pattern1 + "\"): ";
PropertyIterator properties1 = node.getProperties(new String[] { pattern1 });
assertEquals(assertString1 + "number of properties found: ", allProperties.size(), getSize(properties1));
// test: getProperties("propertyName")
String pattern2 = firstProperty.getName();
String assertString2 = "node.getProperties(\"" + pattern2 + "\"): ";
// test if the names of the found properties are matching the pattern
PropertyIterator properties2 = node.getProperties(new String[] { pattern2 });
while (properties2.hasNext()) {
Property p = properties2.nextProperty();
assertEquals(assertString2 + "name comparison failed: ", firstProperty.getName(), p.getName());
}
// test if the number of found properties is correct
int numExpected2 = 0;
for (int i = 0; i < allProperties.size(); i++) {
Property p = allProperties.get(i);
if (p.getName().equals(firstProperty.getName())) {
numExpected2++;
}
}
assertEquals(assertString2 + "number of properties found: ", numExpected2, getSize(properties2));
// test: getProperties("propertyName|propertyName")
// test: getProperties("propertyName", "propertyName")
String pattern4 = firstProperty.getName() + "," + firstProperty.getName();
String assertString4 = "node.getProperties(\"" + pattern4 + "\"): ";
// test if the names of the found properties are matching the pattern
PropertyIterator properties4 = node.getProperties(new String[] { firstProperty.getName(), firstProperty.getName() });
while (properties4.hasNext()) {
Property p = properties4.nextProperty();
assertEquals(assertString2 + "name comparison failed: ", firstProperty.getName(), p.getName());
}
// test if the number of found properties is correct
int numExpected4 = 0;
for (int i = 0; i < allProperties.size(); i++) {
Property p = allProperties.get(i);
if (p.getName().equals(firstProperty.getName())) {
numExpected4++;
}
}
assertEquals(assertString4 + "number of properties found: ", numExpected4, getSize(properties4));
// test: getProperties("*opertyNam*")
if (firstProperty.getName().length() > 2) {
String name = firstProperty.getName();
String shortenName = name.substring(1, name.length() - 1);
String pattern5 = "*" + shortenName + "*";
String assertString5 = "node.getProperties(\"" + pattern5 + "\"): ";
// test if the names of the found properties are matching the pattern
PropertyIterator properties5 = node.getProperties(new String[] { pattern5 });
while (properties5.hasNext()) {
Property p = properties5.nextProperty();
assertTrue(assertString5 + "name comparison failed: *" + shortenName + "* not found in " + p.getName(), p.getName().indexOf(shortenName) != -1);
}
// test if the number of found properties is correct
int numExpected5 = 0;
for (int i = 0; i < allProperties.size(); i++) {
Property p = allProperties.get(i);
if (p.getName().indexOf(shortenName) != -1) {
numExpected5++;
}
}
properties5 = node.getProperties(pattern5);
assertEquals(assertString5 + "number of properties found: ", numExpected5, getSize(properties5));
}
}
use of javax.jcr.PropertyIterator in project jackrabbit by apache.
the class NodeReadMethodsTest method testHasProperties.
/**
* Test if hasProperty() returns true if any property exists or false if
* not. Tested node: root
*
* @throws RepositoryException
*/
public void testHasProperties() throws RepositoryException {
Node node = testRootNode;
PropertyIterator properties = node.getProperties();
int i = 0;
while (properties.hasNext()) {
Property p = properties.nextProperty();
log.println(p.getName());
i++;
}
if (i == 0) {
assertFalse("Must return false when no properties exist", node.hasProperties());
} else {
assertTrue("Must return true when one or more properties exist", node.hasProperties());
}
}
use of javax.jcr.PropertyIterator in project jackrabbit by apache.
the class GetWeakReferencesTest method testMultiValues.
public void testMultiValues() throws RepositoryException {
Value weakRef = vf.createValue(target, true);
Value[] refs = new Value[] { weakRef, weakRef };
referring.setProperty(propertyName1, refs);
superuser.save();
PropertyIterator it = target.getWeakReferences();
assertTrue("no weak references returned", it.hasNext());
Property p = it.nextProperty();
assertEquals("wrong weak reference property", referring.getProperty(propertyName1).getPath(), p.getPath());
assertFalse("no more weak references expected", it.hasNext());
}
use of javax.jcr.PropertyIterator in project jackrabbit by apache.
the class MembershipCache method isMemberOfNodeBaseMembershipGroup.
/**
* traverses the group structure of a node-based group to check if the given authorizable is member of this group.
*
* @param authorizableNodeIdentifier Identifier of the authorizable node
* @param groupId if of the group
* @param nIds output set to update of group node ids that were found via the node memberships
* @param node the node to traverse
* @throws RepositoryException if an error occurs
*/
private void isMemberOfNodeBaseMembershipGroup(String authorizableNodeIdentifier, String groupId, Set<String> nIds, NodeImpl node) throws RepositoryException {
PropertyIterator pIter = node.getProperties();
while (pIter.hasNext()) {
PropertyImpl p = (PropertyImpl) pIter.nextProperty();
if (p.getType() == PropertyType.WEAKREFERENCE) {
Value[] values = p.isMultiple() ? p.getValues() : new Value[] { p.getValue() };
for (Value v : values) {
if (v.getString().equals(authorizableNodeIdentifier)) {
nIds.add(groupId);
return;
}
}
}
}
NodeIterator iter = node.getNodes();
while (iter.hasNext()) {
NodeImpl child = (NodeImpl) iter.nextNode();
if (child.isNodeType(NT_REP_MEMBERS)) {
isMemberOfNodeBaseMembershipGroup(authorizableNodeIdentifier, groupId, nIds, child);
}
}
}
Aggregations