1 7 8 package org.dom4j; 9 10 import junit.textui.TestRunner; 11 12 import java.util.Iterator ; 13 import java.util.List ; 14 15 22 public class ParentTest extends AbstractTestCase { 23 public static void main(String [] args) { 24 TestRunner.run(ParentTest.class); 25 } 26 27 public void testDocument() throws Exception { 30 testParentRelationship(document.getRootElement()); 31 } 32 33 public void testFragment() throws Exception { 34 DocumentFactory factory = new DocumentFactory(); 35 Element root = factory.createElement("root"); 36 Element first = root.addElement("child"); 37 Element second = root.addElement("child"); 38 39 testXPathNode(root, first); 40 testXPathNode(root, second); 41 } 42 43 protected void testParentRelationship(Element parent, List content) { 46 for (Iterator iter = content.iterator(); iter.hasNext();) { 47 Object object = iter.next(); 48 49 if (object instanceof Element) { 50 testParentRelationship((Element) object); 51 } 52 53 testXPathNode(parent, (Node) object); 54 } 55 } 56 57 protected void testParentRelationship(Element element) { 58 testParentRelationship(element, element.attributes()); 59 testParentRelationship(element, element.content()); 60 } 61 62 protected void testXPathNode(Element parent, Node node) { 63 if (node.supportsParent()) { 64 log("Node: " + node); 65 log("Parent: " + parent); 66 log("getParent(): " + node.getParent()); 67 68 assertTrue("getParent() returns parent for: " + node, node 69 .getParent() == parent); 70 } else { 71 Node xpathNode = node.asXPathResult(parent); 73 assertTrue("XPath Node supports parent for: " + xpathNode, 74 xpathNode.supportsParent()); 75 assertTrue("getParent() returns parent for: " + xpathNode, 76 xpathNode.getParent() == parent); 77 } 78 } 79 } 80 81 117 | Popular Tags |