1
2
3
4
5
6
7
8 package org.dom4j;
9
10 import junit.textui.TestRunner;
11
12 import java.util.Iterator;
13 import java.util.List;
14
15 /***
16 * A test harness to test the content API in DOM4J
17 *
18 * @author <a href="mailto:james.strachan@metastuff.com">James Strachan </a>
19 * @version $Revision: 1.3 $
20 */
21 public class ContentTest extends AbstractTestCase {
22 protected DocumentFactory factory = new DocumentFactory();
23
24 public static void main(String[] args) {
25 TestRunner.run(ContentTest.class);
26 }
27
28
29
30 public void testRoot() throws Exception {
31 Element root = document.getRootElement();
32 assertTrue("Has root element", root != null);
33
34 List authors = root.elements("author");
35 assertTrue("Root has children", (authors != null)
36 && (authors.size() == 2));
37
38 Element author1 = (Element) authors.get(0);
39 Element author2 = (Element) authors.get(1);
40
41 assertTrue("Author1 is James", author1.attributeValue("name").equals(
42 "James"));
43 assertTrue("Author2 is Bob", author2.attributeValue("name").equals(
44 "Bob"));
45
46 testGetAttributes(author1);
47 testGetAttributes(author2);
48 }
49
50 public void testContent() throws Exception {
51 Element root = document.getRootElement();
52 assertTrue("Has root element", root != null);
53
54 List content = root.content();
55 assertTrue("Root has content", (content != null)
56 && (content.size() >= 2));
57
58 boolean iterated = false;
59
60 for (Iterator iter = content.iterator(); iter.hasNext();) {
61 Object object = iter.next();
62 assertTrue("Content object is a node", object instanceof Node);
63 iterated = true;
64 }
65
66 assertTrue("Iteration completed", iterated);
67 }
68
69 public void testGetNode() throws Exception {
70 Element root = document.getRootElement();
71 assertTrue("Has root element", root != null);
72
73 int count = root.nodeCount();
74 assertTrue("Root has correct node count", count == 2);
75
76 boolean iterated = false;
77
78 for (int i = 0; i < count; i++) {
79 Node node = root.node(i);
80 assertTrue("Valid node returned from node()", node != null);
81 iterated = true;
82 }
83
84 assertTrue("Iteration completed", iterated);
85 }
86
87 public void testGetXPathNode() throws Exception {
88 Element root = document.getRootElement();
89 assertTrue("Has root element", root != null);
90
91 int count = root.nodeCount();
92 assertTrue("Root has correct node count", count == 2);
93
94 boolean iterated = false;
95
96 for (int i = 0; i < count; i++) {
97 Node node = root.getXPathResult(i);
98 assertTrue("Valid node returned from node()", node != null);
99 assertTrue("Node supports the parent relationship", node
100 .supportsParent());
101 iterated = true;
102 }
103
104 assertTrue("Iteration completed", iterated);
105 }
106
107 public void testOrderOfPI() throws Exception {
108 Document document = factory.createDocument();
109 document.addProcessingInstruction("xml-stylesheet",
110 "type=\"text/xsl\" href=\"...\"");
111 document.addElement("root");
112
113 List list = document.content();
114
115 assertNotNull(list);
116 assertEquals(2, list.size());
117
118 Object pi = list.get(0);
119 Object root = list.get(1);
120
121 assertTrue("First element is not a PI",
122 pi instanceof ProcessingInstruction);
123 assertTrue("Second element is an element", root instanceof Element);
124
125 String xml = "<?xml version=\"1.0\" ?>\n"
126 + "<?xml-stylesheet type=\"text/xsl\" href=\"foo\" ?>\n"
127 + "<root/>";
128 document = DocumentHelper.parseText(xml);
129
130 list = document.content();
131
132 assertNotNull(list);
133 assertEquals(2, list.size());
134 pi = list.get(0);
135 root = list.get(1);
136
137 assertTrue("First element is not a PI",
138 pi instanceof ProcessingInstruction);
139 assertTrue("Second element is an element", root instanceof Element);
140 }
141
142 public void testAddingInTheMiddle() throws Exception {
143 Document doc = factory.createDocument();
144 Element root = doc.addElement("html");
145 Element header = root.addElement("header");
146 Element footer = root.addElement("footer");
147
148
149 List list = root.content();
150 Element foo = factory.createElement("foo");
151 list.add(1, foo);
152
153
154 assertTrue(list.size() == 3);
155 assertTrue(list.get(0) == header);
156 assertTrue(list.get(1) == foo);
157 assertTrue(list.get(2) == footer);
158 }
159
160 public void testAddAtIndex() throws Exception {
161 Document doc = factory.createDocument();
162 Element root = doc.addElement("html");
163 Element header = root.addElement("header");
164 Element body = root.addElement("body");
165
166 Element foo = factory.createElement("foo");
167 Element bar = factory.createElement("bar");
168
169 List content = header.content();
170 content.add(0, foo);
171 content.add(0, bar);
172
173 assertEquals("foo", header.node(1).getName());
174 assertEquals("bar", header.node(0).getName());
175
176 foo = factory.createElement("foo");
177 bar = factory.createElement("bar");
178
179 content = body.content();
180 content.add(0, foo);
181 content.add(1, bar);
182
183 assertEquals("foo", body.node(0).getName());
184 assertEquals("bar", body.node(1).getName());
185 }
186
187 public void testAddAtIndex2() throws Exception {
188 Document doc = factory.createDocument();
189 Element parent = doc.addElement("parent");
190 Element child = parent.addElement("child");
191 Element anotherChild = factory.createElement("child2");
192
193 List elements = parent.elements();
194 int index = elements.indexOf(child);
195
196 assertEquals(0, index);
197
198 elements.add(1, anotherChild);
199 elements = parent.elements();
200 assertEquals(child, elements.get(0));
201 assertEquals(anotherChild, elements.get(1));
202 }
203
204
205
206 protected void testGetAttributes(Element author) throws Exception {
207 String definedName = "name";
208 String undefinedName = "undefined-attribute-name";
209 String defaultValue = "** Default Value **";
210
211 String value = author.attributeValue(definedName, defaultValue);
212 assertTrue("Defined value doesn't return specified default value",
213 value != defaultValue);
214
215 value = author.attributeValue(undefinedName, defaultValue);
216 assertTrue("Undefined value returns specified default value",
217 value == defaultValue);
218 }
219 }
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256