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