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