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 import org.dom4j.io.XMLWriter;
15
16 /***
17 * A test harness to test the backed list feature of DOM4J
18 *
19 * @author <a href="mailto:james.strachan@metastuff.com">James Strachan </a>
20 * @version $Revision: 1.3 $
21 */
22 public class BackedListTest extends AbstractTestCase {
23 public static void main(String[] args) {
24 TestRunner.run(BackedListTest.class);
25 }
26
27
28
29 public void testXPaths() throws Exception {
30 Element element = (Element) document.selectSingleNode("/root");
31 mutate(element);
32 element = (Element) document.selectSingleNode("//author");
33 mutate(element);
34 }
35
36 public void testAddRemove() throws Exception {
37 Element parentElement = (Element) document.selectSingleNode("/root");
38 List children = parentElement.elements();
39 int lastPos = children.size() - 1;
40 Element child = (Element) children.get(lastPos);
41
42 try {
43
44 children.add(0, child);
45 fail();
46 } catch (IllegalAddException e) {
47 }
48 }
49
50 public void testAddWithIndex() throws Exception {
51 DocumentFactory factory = DocumentFactory.getInstance();
52
53 Element root = (Element) document.selectSingleNode("/root");
54 List children = root.elements();
55
56
57 assertEquals(2, children.size());
58
59 children.add(1, factory.createElement("dummy1"));
60 children = root.elements();
61
62 assertEquals(3, children.size());
63
64 children = root.elements("author");
65
66 assertEquals(2, children.size());
67
68 children.add(1, factory.createElement("dummy2"));
69
70 children = root.elements();
71
72 assertEquals(4, children.size());
73 assertEquals("dummy1", ((Node) children.get(1)).getName());
74 assertEquals("dummy2", ((Node) children.get(2)).getName());
75
76
77
78
79 children.add(children.size(), factory.createElement("dummy3"));
80 children = root.elements("author");
81 children.add(children.size(), factory.createElement("dummy4"));
82 }
83
84
85
86 protected void mutate(Element element) throws Exception {
87 DocumentFactory factory = DocumentFactory.getInstance();
88
89 List list = element.elements();
90 list.add(factory.createElement("last"));
91 list.add(0, factory.createElement("first"));
92
93 List list2 = element.elements();
94
95 assertTrue("Both lists should contain same number of elements", list
96 .size() == list2.size());
97
98 XMLWriter writer = new XMLWriter(System.out);
99
100 log("Element content is now: " + element.content());
101 writer.write(element);
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
138
139
140