1
2
3
4
5
6
7
8
9
10 package org.dom4j;
11
12 import java.util.Iterator;
13 import java.util.List;
14
15 import junit.framework.Test;
16 import junit.framework.TestSuite;
17 import junit.textui.TestRunner;
18
19 /*** A test harness to test the parent relationship and use of the
20 * {@link Node#asXPathResult} method.
21 *
22 * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a>
23 * @version $Revision: 1.12 $
24 */
25 public class TestParent extends AbstractTestCase {
26
27 public static void main( String[] args ) {
28 TestRunner.run( suite() );
29 }
30
31 public static Test suite() {
32 return new TestSuite( TestParent.class );
33 }
34
35 public TestParent(String name) {
36 super(name);
37 }
38
39
40
41 public void testDocument() throws Exception {
42 testParentRelationship( document.getRootElement() );
43 }
44
45 public void testFragment() throws Exception {
46
47 DocumentFactory factory = new DocumentFactory();
48 Element root = factory.createElement( "root" );
49 Element first = root.addElement( "child" );
50 Element second = root.addElement( "child" );
51
52 testXPathNode( root, first );
53 testXPathNode( root, second );
54 }
55
56
57
58 protected void testParentRelationship( Element parent, List content ) {
59 for ( Iterator iter = content.iterator(); iter.hasNext(); ) {
60 Object object = iter.next();
61 if ( object instanceof Element ) {
62 testParentRelationship( (Element) object );
63 }
64 testXPathNode( parent, (Node) object );
65 }
66 }
67
68 protected void testParentRelationship( Element element ) {
69 testParentRelationship( element, element.attributes() );
70 testParentRelationship( element, element.content() );
71 }
72
73
74 protected void testXPathNode( Element parent, Node node ) {
75 if ( node.supportsParent() ) {
76 log( "Node: " + node );
77 log( "Parent: " + parent );
78 log( "getParent(): " + node.getParent() );
79
80 assertTrue( "getParent() returns parent for: " + node, node.getParent() == parent );
81 }
82 else {
83
84 Node xpathNode = node.asXPathResult( parent );
85 assertTrue( "XPath Node supports parent for: " + xpathNode, xpathNode.supportsParent() );
86 assertTrue( "getParent() returns parent for: " + xpathNode, xpathNode.getParent() == parent );
87 }
88 }
89 }
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137