1
2
3
4
5
6
7
8
9
10 package org.dom4j;
11
12 import java.util.List;
13
14 import junit.framework.Test;
15 import junit.framework.TestSuite;
16 import junit.textui.TestRunner;
17
18 /*** A test harness to test the copy() methods on Element
19 *
20 * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a>
21 * @version $Revision: 1.6 $
22 */
23 public class TestCopy extends AbstractTestCase {
24
25 public static void main( String[] args ) {
26 TestRunner.run( suite() );
27 }
28
29 public static Test suite() {
30 return new TestSuite( TestCopy.class );
31 }
32
33 public TestCopy(String name) {
34 super(name);
35 }
36
37
38
39 public void testRoot() throws Exception {
40 document.setName( "doc1" );
41
42 Element root = document.getRootElement();
43 List authors = root.elements( "author" );
44
45 assertTrue( "Should be at least 2 authors", authors.size() == 2 );
46
47 Element author1 = (Element) authors.get(0);
48 Element author2 = (Element) authors.get(1);
49
50 testCopy( root );
51 testCopy( author1 );
52 testCopy( author2 );
53 }
54
55
56 protected void testCopy(Element element) throws Exception {
57 assertTrue( "Not null", element != null );
58
59 int attributeCount = element.attributeCount();
60 int nodeCount = element.nodeCount();
61
62 Element copy = element.createCopy();
63
64 assertTrue( "Same node size after copy", element.nodeCount() == nodeCount );
65 assertTrue( "Same attribute size after copy", element.attributeCount() == attributeCount );
66
67 assertTrue( "Copy has same node size", copy.nodeCount() == nodeCount );
68 assertTrue( "Copy has same attribute size", copy.attributeCount() == attributeCount );
69
70 for (int i = 0; i < attributeCount; i++ ) {
71 Attribute attr1 = element.attribute(i);
72 Attribute attr2 = copy.attribute(i);
73
74 assertTrue( "Attribute: " + i + " name is equal", attr1.getName().equals( attr2.getName() ) );
75 assertTrue( "Attribute: " + i + " value is equal", attr1.getValue().equals( attr2.getValue() ) );
76 }
77
78 for (int i = 0; i < nodeCount; i++ ) {
79 Node node1 = element.node(i);
80 Node node2 = copy.node(i);
81
82 assertTrue( "Node: " + i + " type is equal", node1.getNodeType() == node2.getNodeType() );
83 assertTrue( "Node: " + i + " value is equal", node1.getText().equals( node2.getText() ) );
84 }
85 }
86 }
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