use of javax.jcr.NodeIterator in project jackrabbit by apache.
the class NodeReadMethodsTest method locateNodeWithReference.
/**
* Returns the first descendant of <code>node</code> which has a property of
* type {@link javax.jcr.PropertyType#REFERENCE} set and is <b>not</b>
* multivalued.
*
* @param node <code>Node</code> to start traversal.
* @return first node with a property of PropertType.REFERENCE
*/
private Node locateNodeWithReference(Node node) throws RepositoryException {
PropertyIterator properties = node.getProperties();
while (properties.hasNext()) {
Property p = properties.nextProperty();
if (p.getType() == PropertyType.REFERENCE && !p.getDefinition().isMultiple()) {
return node;
}
}
NodeIterator nodes = node.getNodes();
while (nodes.hasNext()) {
Node returnedNode = locateNodeWithReference(nodes.nextNode());
if (returnedNode != null) {
return returnedNode;
}
}
return null;
}
use of javax.jcr.NodeIterator in project jackrabbit by apache.
the class NodeReadMethodsTest method testGetNodesNamePattern.
/**
* Test getNodes(String namePattern) with all possible patterns. Tested
* node: root - NotExecutableException is thrown when root node has no sub
* nodes.
*/
public void testGetNodesNamePattern() throws NotExecutableException, RepositoryException {
// get root node and build an ArrayList of its sub nodes
Node node = testRootNode;
if (!node.hasNodes()) {
throw new NotExecutableException("Workspace does not have sufficient content for this test. " + "Root node must have at least one child node.");
}
NodeIterator allNodesIt = node.getNodes();
List<Node> allNodes = new ArrayList<Node>();
while (allNodesIt.hasNext()) {
Node n = allNodesIt.nextNode();
allNodes.add(n);
}
// test if an empty NodeIterator is returned
// when the pattern is not matching any child node
String pattern0 = "";
NodeIterator nodes0 = node.getNodes(pattern0);
try {
nodes0.nextNode();
fail("An empty NodeIterator must be returned if pattern does" + "not match any child node.");
} catch (NoSuchElementException e) {
// success
}
// all further tests are using root's first sub node
Node firstNode = allNodes.get(0);
// test pattern "*"
String pattern1 = "*";
String assertString1 = "node.getNodes(\"" + pattern1 + "\"): ";
NodeIterator nodes1 = node.getNodes(pattern1);
// test if the number of found nodes is correct
assertEquals(assertString1 + "number of nodes found: ", allNodes.size(), getSize(nodes1));
// test pattern "nodeName"
String pattern2 = firstNode.getName();
String assertString2 = "node.getNodes(\"" + pattern2 + "\"): ";
// test if the names of the found nodes are matching the pattern
NodeIterator nodes2 = node.getNodes(pattern2);
while (nodes2.hasNext()) {
Node n = nodes2.nextNode();
assertEquals(assertString2 + "name comparison failed: ", firstNode.getName(), n.getName());
}
// test if the number of found nodes is correct
int numExpected2 = 0;
for (int i = 0; i < allNodes.size(); i++) {
Node n = allNodes.get(i);
if (n.getName().equals(firstNode.getName())) {
numExpected2++;
}
}
nodes2 = node.getNodes(pattern2);
assertEquals(assertString2 + "number of nodes found: ", numExpected2, getSize(nodes2));
// test pattern "nodeName|nodeName"
String pattern3 = firstNode.getName() + "|" + firstNode.getName();
String assertString3 = "node.getNodes(\"" + pattern3 + "\"): ";
// test if the names of the found nodes are matching the pattern
NodeIterator nodes3 = node.getNodes(pattern3);
while (nodes3.hasNext()) {
Node n = nodes3.nextNode();
assertEquals(assertString2 + "name comparison failed: ", firstNode.getName(), n.getName());
}
// test if the number of found nodes is correct
int numExpected3 = 0;
for (int i = 0; i < allNodes.size(); i++) {
Node n = allNodes.get(i);
if (n.getName().equals(firstNode.getName())) {
numExpected3++;
}
}
nodes3 = node.getNodes(pattern3);
assertEquals(assertString3 + "number of nodes found: ", numExpected3, getSize(nodes3));
// test pattern "*odeNam*"
if (firstNode.getName().length() > 2) {
String name = firstNode.getName();
String shortenName = name.substring(1, name.length() - 1);
String pattern4 = "*" + shortenName + "*";
String assertString4 = "node.getNodes(\"" + pattern4 + "\"): ";
// test if the names of the found nodes are matching the pattern
NodeIterator nodes4 = node.getNodes(pattern4);
while (nodes4.hasNext()) {
Node n = nodes4.nextNode();
assertTrue(assertString4 + "name comparison failed: *" + shortenName + "* not found in " + n.getName(), n.getName().indexOf(shortenName) != -1);
}
// test if the number of found nodes is correct
int numExpected4 = 0;
for (int i = 0; i < allNodes.size(); i++) {
Node n = allNodes.get(i);
if (n.getName().indexOf(shortenName) != -1) {
numExpected4++;
}
}
nodes4 = node.getNodes(pattern4);
assertEquals(assertString4 + "number of nodes found: ", numExpected4, getSize(nodes4));
}
}
use of javax.jcr.NodeIterator in project jackrabbit by apache.
the class NodeReadMethodsTest method setUp.
/**
* Sets up the fixture for this test.
*/
protected void setUp() throws Exception {
isReadOnly = true;
super.setUp();
session = getHelper().getReadOnlySession();
testRootNode = session.getRootNode().getNode(testPath);
NodeIterator nodes = testRootNode.getNodes();
try {
childNode = nodes.nextNode();
} catch (NoSuchElementException e) {
}
}
use of javax.jcr.NodeIterator in project jackrabbit by apache.
the class NodeReadMethodsTest method testHasNodes.
/**
* Test if hasNodes() returns true if any sub node exists or false if not.
* Tested node: root
*/
public void testHasNodes() throws RepositoryException {
Node node = testRootNode;
NodeIterator nodes = node.getNodes();
int i = 0;
while (nodes.hasNext()) {
nodes.nextNode();
i++;
}
if (i == 0) {
assertFalse("node.hasNodes() returns true although " + "no sub nodes existing", node.hasNodes());
} else {
assertTrue("node.hasNodes() returns false althuogh " + "sub nodes are existing", node.hasNodes());
}
}
use of javax.jcr.NodeIterator in project jackrabbit by apache.
the class NodeReadMethodsTest method testHasNode.
/**
* Test if hasNode(String relPath) returns true if the required node exists
* and false if it doesn't. Tested node: root
*/
public void testHasNode() throws NotExecutableException, RepositoryException {
Node node = testRootNode;
NodeIterator nodes = node.getNodes();
StringBuffer notExistingNodeName = new StringBuffer();
while (nodes.hasNext()) {
Node n = nodes.nextNode();
assertTrue("hasNode(String relPath) returns false although " + "node at relPath is existing", node.hasNode(n.getName()));
notExistingNodeName.append(n.getName() + "X");
}
if (notExistingNodeName.toString().equals("")) {
throw new NotExecutableException("Workspace does not have sufficient content for this test. " + "Root node must have at least one child node.");
}
assertFalse("hasNode(String relPath) returns true although " + "node at relPath is not existing", node.hasNode(notExistingNodeName.toString().replaceAll(":", "")));
}
Aggregations