1
2
3
4
5
6
7
8
9
10 package org.dom4j;
11
12 import java.util.Collections;
13 import java.util.Comparator;
14 import java.util.List;
15
16 import junit.framework.Test;
17 import junit.framework.TestSuite;
18 import junit.textui.TestRunner;
19 import org.dom4j.Node;
20
21 import org.dom4j.io.XMLWriter;
22
23 /*** A test harness to test the backed list feature of DOM4J
24 *
25 * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a>
26 * @version $Revision: 1.15 $
27 */
28 public class TestBackedList extends AbstractTestCase {
29
30 public static void main( String[] args ) {
31 TestRunner.run( suite() );
32 }
33
34 public static Test suite() {
35 return new TestSuite( TestBackedList.class );
36 }
37
38 public TestBackedList(String name) {
39 super(name);
40 }
41
42
43
44 public void testXPaths() throws Exception {
45 Element element = (Element) document.selectSingleNode( "/root" );
46 mutate(element);
47 element = (Element) document.selectSingleNode( "//author" );
48 mutate(element);
49 }
50
51 public void testAddRemove() throws Exception {
52 Element parentElement = (Element) document.selectSingleNode( "/root" );
53 List children = parentElement.elements();
54 int lastPos = children.size() - 1;
55 Element child = (Element) children.get(lastPos);
56 try {
57
58 children.add(0, child);
59 fail();
60 } catch (IllegalAddException e) {}
61 }
62
63 public void testAddWithIndex() throws Exception {
64 DocumentFactory factory = DocumentFactory.getInstance();
65
66 Element root = (Element) document.selectSingleNode( "/root" );
67 List children = root.elements();
68
69 assertEquals(2, children.size());
70
71 children.add(1, factory.createElement("dummy1"));
72 children = root.elements();
73
74 assertEquals(3, children.size());
75
76 children = root.elements("author");
77
78 assertEquals(2, children.size());
79
80 children.add(1, factory.createElement("dummy2"));
81
82 children = root.elements();
83
84 assertEquals(4, children.size());
85 assertEquals("dummy1", ((Node) children.get(1)).getName());
86 assertEquals("dummy2", ((Node) children.get(2)).getName());
87
88
89
90
91
92 children.add(children.size(), factory.createElement("dummy3"));
93 children = root.elements("author");
94 children.add(children.size(), factory.createElement("dummy4"));
95 }
96
97
98
99 protected void mutate(Element element) throws Exception {
100 DocumentFactory factory = DocumentFactory.getInstance();
101
102 List list = element.elements();
103 list.add(factory.createElement("last" ));
104 list.add(0, factory.createElement("first" ));
105
106 List list2 = element.elements();
107
108 assertTrue( "Both lists should contain same number of elements", list.size() == list2.size() );
109
110 XMLWriter writer = new XMLWriter( System.out );
111
112 log( "Element content is now: " + element.content() );
113 writer.write( element );
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164