// 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();
ArrayList allProperties = new ArrayList();
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 = (Property) 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 = (Property) 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 = (Property) 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