1 9 10 package test.dom4j; 11 12 import junit.framework.Test; 13 import junit.framework.TestSuite; 14 import junit.textui.TestRunner; 15 import org.dom4j.Element; 16 import org.dom4j.Node; 17 18 import java.util.Iterator ; 19 import java.util.List ; 20 21 27 public class TestParent extends AbstractTestCase { 28 29 public static void main( String [] args ) { 30 TestRunner.run( suite() ); 31 } 32 33 public static Test suite() { 34 return new TestSuite( TestParent.class ); 35 } 36 37 public TestParent(String name) { 38 super(name); 39 } 40 41 public void testDocument() throws Exception { 44 testParentRelationship( document.getRootElement() ); 45 } 46 47 public void testFragment() throws Exception { 48 49 Element root = nodeFactory.createElement( "root" ); 50 Element first = root.addElement( "child" ); 51 Element second = root.addElement( "child" ); 52 53 testXPathNode( root, first ); 54 testXPathNode( root, second ); 55 } 56 57 protected void testParentRelationship( Element parent, List content ) { 60 for ( Iterator iter = content.iterator(); iter.hasNext(); ) { 61 Object object = iter.next(); 62 if ( object instanceof Element ) { 63 testParentRelationship( (Element) object ); 64 } 65 testXPathNode( parent, (Node) object ); 66 } 67 } 68 69 protected void testParentRelationship( Element element ) { 70 testParentRelationship( element, element.attributes() ); 71 testParentRelationship( element, element.content() ); 72 } 73 74 75 protected void testXPathNode( Element parent, Node node ) { 76 if ( node.supportsParent() ) { 77 log( "Node: " + node ); 78 log( "Parent: " + parent ); 79 log( "getParent(): " + node.getParent() ); 80 81 assertTrue( "getParent() returns parent for: " + node, node.getParent() == parent ); 82 } 83 else { 84 Node xpathNode = node.asXPathResult( parent ); 86 assertTrue( "XPath Node supports parent for: " + xpathNode, xpathNode.supportsParent() ); 87 assertTrue( "getParent() returns parent for: " + xpathNode, xpathNode.getParent() == parent ); 88 } 89 } 90 } 91 92 93 94 95 139 | Popular Tags |