1
/* Copyright 2002-2004 Elliotte Rusty Harold
3
This library is free software; you can redistribute it and/or modify
4
it under the terms of version 2.1 of the GNU Lesser General Public
5
License as published by the Free Software Foundation.
7
This library is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU Lesser General Public License for more details.
12
You should have received a copy of the GNU Lesser General Public
13
License along with this library; if not, write to the
14
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
15
Boston, MA 02111-1307 USA
17
You can contact Elliotte Rusty Harold by sending e-mail to
18
elharo@metalab.unc.edu. Please include the word "XOM" in the
19
subject line. The XOM home page is located at http://www.xom.nu/
25
import java.io.IOException;
27
import nu.xom.Builder;
28
import nu.xom.Comment;
29
import nu.xom.DocType;
30
import nu.xom.Document;
31
import nu.xom.Element;
32
import nu.xom.IllegalAddException;
33
import nu.xom.MultipleParentException;
34
import nu.xom.NoSuchChildException;
35
import nu.xom.ParsingException;
36
import nu.xom.ProcessingInstruction;
38
import nu.xom.WellformednessException;
43
* Various basic tests for the <code>Document</code> class.
46
* @author Elliotte Rusty Harold
50
public class DocumentTest extends XOMTestCase {
52
public DocumentTest(String name) {
59
protected void setUp() {
60
root = new Element("root");
61
doc = new Document(root);
65
public void testToString() {
66
assertEquals("[nu.xom.Document: root]", doc.toString());
70
public void testDocTypeInsertion() {
72
DocType type1 = new DocType("root");
75
doc.insertChild(type1, 1);
76
fail("inserted doctype after root element");
78
catch (IllegalAddException success) {
79
assertNotNull(success.getMessage());
82
doc.insertChild(type1, 0);
83
assertEquals(type1, doc.getDocType());
85
DocType type2 = new DocType("test");
87
doc.insertChild(type2, 1);
88
fail("Inserted 2nd DocType");
90
catch (IllegalAddException success) {
91
assertNotNull(success.getMessage());
93
assertEquals(type1, doc.getDocType());
94
assertNull(type2.getParent());
95
assertEquals(type1, doc.getChild(0));
97
doc.setDocType(type2);
98
assertEquals(doc.getDocType(), type2);
99
assertNull(type1.getParent());
100
assertEquals(type2, doc.getChild(0));
105
public void testSetDocType() {
107
DocType type1 = new DocType("root");
108
doc.setDocType(type1);
109
assertEquals(type1, doc.getDocType());
111
doc.insertChild(new Comment("test"), 0);
113
DocType type2 = new DocType("root", "http://www.example.com/");
114
doc.setDocType(type2);
115
assertEquals(type2, doc.getDocType());
116
assertEquals(1, doc.indexOf(type2));
117
assertNull(type1.getParent());
118
assertEquals(doc, type2.getParent());
121
doc.setDocType(type2);
122
assertEquals(type2, doc.getDocType());
123
assertEquals(1, doc.indexOf(type2));
124
assertEquals(doc, type2.getParent());
127
doc.setDocType(null);
128
fail("Allowed null doctype");
130
catch (NullPointerException success) {
131
assertNotNull(success.getMessage());
132
assertEquals(type2, doc.getDocType());
136
Document doc2 = new Document(new Element("root"));
137
doc2.setDocType(type2);
138
fail("Allowed multiple parents for doctype");
140
catch (MultipleParentException success) {
141
assertNotNull(success.getMessage());
147
public void testBaseURI() {
149
assertEquals("", doc.getBaseURI());
150
doc.setBaseURI("http://www.example.com/index.xml");
152
"http://www.example.com/index.xml",
155
doc.setBaseURI("file:///home/elharo/XOM/data/test.xml");
157
"file:///home/elharo/XOM/data/test.xml",
160
doc.setBaseURI("file:///home/elharo/XO%4D/data/test.xml");
162
"file:///home/elharo/XO%4D/data/test.xml",
169
public void testSecondRoot() {
172
doc.insertChild(new Element("test"), 0);
173
fail("Added second root element");
175
catch (IllegalAddException success) {
176
assertNotNull(success.getMessage());
182
public void testSetRoot() {
184
Element newRoot = new Element("newroot");
185
doc.setRootElement(newRoot);
187
assertEquals(newRoot, doc.getRootElement());
188
assertNull(root.getParent());
191
doc.setRootElement(null);
193
catch (NullPointerException success) {
194
assertNotNull(success.getMessage());
197
Element top = new Element("top");
198
Element child = new Element("child");
199
top.appendChild(child);
201
doc.setRootElement(child);
202
fail("Allowed element with two parents");
204
catch (MultipleParentException success) {
205
assertNotNull(success.getMessage());
211
public void testReplaceRootWithItself() {
212
Element root = doc.getRootElement();
213
doc.setRootElement(root);
214
assertEquals(root, doc.getRootElement());
218
public void testReplaceRootElementWithDifferentElementUsingReplaceChild() {
220
Element newRoot = new Element("newRoot");
221
Element oldRoot = doc.getRootElement();
222
doc.replaceChild(oldRoot, newRoot);
223
assertEquals(newRoot, doc.getRootElement());
224
assertNull(oldRoot.getParent());
225
assertEquals(doc, newRoot.getParent());
230
public void testInsertionAllowed() {
232
Document doc = new Document(new Element("root"));
233
Comment original = new Comment("original");
234
doc.insertChild(original, 0);
236
Element temp = new Element("temp");
237
Comment c2 = new Comment("new comment");
238
temp.appendChild(c2);
241
doc.replaceChild(original, c2);
242
fail("Missed multiple parent exception");
244
catch (MultipleParentException success) {
245
assertEquals(2, doc.getChildCount());
251
public void testReplaceDocTypeWithDifferentDocTypeUsingReplaceChild() {
253
DocType newDocType = new DocType("new");
254
DocType oldDocType = new DocType("old");
255
doc.setDocType(oldDocType);
256
doc.replaceChild(oldDocType, newDocType);
257
assertEquals(newDocType, doc.getDocType());
258
assertNull(oldDocType.getParent());
259
assertEquals(doc, newDocType.getParent());
264
public void testReplaceDocTypeWithParentedDocTypeUsingReplaceChild() {
266
DocType newDocType = new DocType("new");
267
DocType oldDocType = new DocType("old");
268
Document temp = new Document(new Element("root"));
269
temp.setDocType(newDocType);
271
doc.setDocType(oldDocType);
273
doc.replaceChild(oldDocType, newDocType);
274
fail("Missed MultipleParentException");
276
catch (MultipleParentException success) {
277
assertEquals(2, doc.getChildCount());
278
assertEquals(2, temp.getChildCount());
281
assertEquals(oldDocType, doc.getDocType());
282
assertNotNull(oldDocType.getParent());
283
assertEquals(doc, oldDocType.getParent());
284
assertEquals(newDocType, temp.getDocType());
285
assertNotNull(oldDocType.getParent());
286
assertEquals(temp, newDocType.getParent());
291
public void testReplaceRootElementWithParentedElementUsingReplaceChild() {
293
Element oldRoot = new Element("oldRoot");
294
Element newRoot = new Element("newRoot");
295
Document doc = new Document(oldRoot);
296
Element temp = new Element("temp");
297
temp.appendChild(newRoot);
300
doc.replaceChild(oldRoot, newRoot);
301
fail("Missed MultipleParentException");
303
catch (MultipleParentException success) {
304
assertEquals(1, doc.getChildCount());
305
assertEquals(1, temp.getChildCount());
308
assertEquals(oldRoot, doc.getRootElement());
309
assertEquals(newRoot, temp.getChild(0));
310
assertNotNull(oldRoot.getParent());
311
assertEquals(temp, newRoot.getParent());
316
public void testReplaceRootElementWithComment() {
318
Element oldRoot = new Element("oldRoot");
319
Document doc = new Document(oldRoot);
322
doc.replaceChild(oldRoot, new Comment("c"));
323
fail("Replaced root with comment");
325
catch (WellformednessException success) {
326
assertNotNull(success.getMessage());
329
assertEquals(oldRoot, doc.getRootElement());
330
assertEquals(doc, oldRoot.getParent());
335
public void testReplaceNonDocTypeWithDocTypeUsingReplaceChild() {
337
Comment c = new Comment("Not a doctype");
338
DocType newDocType = new DocType("new");
339
doc.insertChild(c, 0);
340
doc.replaceChild(c, newDocType);
341
assertEquals(newDocType, doc.getDocType());
342
assertNull(c.getParent());
343
assertEquals(doc, newDocType.getParent());
348
public void testDetach() {
350
= new Comment("This will be attached then detached");
351
doc.appendChild(comment);
352
assertEquals(doc, comment.getParent());
354
assertNull(comment.getParent());
358
public void testGetDocument() {
359
assertEquals(doc, doc.getDocument());
363
public void testConstructor() {
365
assertEquals(root, doc.getRootElement());
366
assertEquals(1, doc.getChildCount());
368
Element newRoot = new Element("newRoot");
369
doc.setRootElement(newRoot);
370
assertEquals(newRoot, doc.getRootElement());
371
assertEquals(1, doc.getChildCount());
373
doc.appendChild(new Comment("test"));
374
assertEquals(2, doc.getChildCount());
376
doc.insertChild(new Comment("prolog comment"), 0);
377
assertEquals(3, doc.getChildCount());
378
assertTrue(doc.getChild(0) instanceof Comment);
379
assertTrue(doc.getChild(1) instanceof Element);
380
assertTrue(doc.getChild(2) instanceof Comment);
382
doc.insertChild(new ProcessingInstruction("target", "data"),1);
383
assertTrue(doc.getChild(0) instanceof Comment);
384
assertTrue(doc.getChild(1) instanceof ProcessingInstruction);
385
assertTrue(doc.getChild(2) instanceof Element);
386
assertTrue(doc.getChild(3) instanceof Comment);
388
doc.insertChild(new ProcessingInstruction("epilog", "data"),3);
389
assertTrue(doc.getChild(0) instanceof Comment);
390
assertTrue(doc.getChild(1) instanceof ProcessingInstruction);
391
assertTrue(doc.getChild(2) instanceof Element);
392
assertTrue(doc.getChild(3) instanceof ProcessingInstruction);
393
assertTrue(doc.getChild(4) instanceof Comment);
397
Element nullRoot = null;
398
new Document(nullRoot);
399
fail("allowed null root!");
401
catch (NullPointerException success) {
405
Document nullDoc = null;
406
new Document(nullDoc);
407
fail("allowed null doc!");
409
catch (NullPointerException success) {
416
public void testCopyConstructor() {
418
doc.insertChild(new Comment("text"), 0);
419
doc.insertChild(new ProcessingInstruction("text", "data"), 1);
420
doc.insertChild(new DocType("text"), 2);
421
root.appendChild("some data");
422
doc.appendChild(new Comment("after"));
423
doc.appendChild(new ProcessingInstruction("text", "after"));
425
Document doc2 = new Document(doc);
426
assertEquals(doc, doc2);
431
public void testCopyConstructorBaseURI() {
433
doc.setBaseURI("http://www.example.com/");
435
Document doc2 = new Document(doc);
436
assertEquals(doc.getBaseURI(), doc2.getBaseURI());
437
assertEquals("http://www.example.com/", doc2.getBaseURI());
439
doc.getRootElement().getBaseURI(),
440
doc2.getRootElement().getBaseURI()
443
"http://www.example.com/",
444
doc2.getRootElement().getBaseURI()
450
public void testCopy() {
452
doc.insertChild(new Comment("text"), 0);
453
doc.insertChild(new ProcessingInstruction("text", "data"), 1);
454
doc.insertChild(new DocType("text"), 2);
455
root.appendChild("some data");
456
doc.appendChild(new Comment("after"));
457
doc.appendChild(new ProcessingInstruction("text", "after"));
459
Document doc2 = (Document) doc.copy();
460
assertEquals(doc, doc2);
465
public void testAppend() {
467
Element root = new Element("root");
468
Document doc = new Document(root);
471
doc.appendChild(new Text("test"));
472
fail("appended string");
474
catch (IllegalAddException success) {
475
assertNotNull(success.getMessage());
479
doc.appendChild(new Text(" "));
480
fail("appended white space");
482
catch (IllegalAddException success) {
483
assertNotNull(success.getMessage());
487
doc.appendChild(new Text("test"));
488
fail("appended Text");
490
catch (IllegalAddException success) {
491
assertNotNull(success.getMessage());
495
doc.appendChild(new Comment("test"));
496
doc.appendChild(new Element("test"));
497
fail("appended element");
499
catch (IllegalAddException success) {
500
assertNotNull(success.getMessage());
504
doc.insertChild(new Element("test"), 0);
505
fail("inserted element");
507
catch (IllegalAddException success) {
508
assertNotNull(success.getMessage());
514
public void testRemoval() {
516
Element root = new Element("root");
517
Document doc = new Document(root);
521
fail("detached root element");
523
catch (WellformednessException success) {
524
assertNotNull(success.getMessage());
528
doc.removeChild(root);
529
fail("removed root element");
531
catch (WellformednessException success) {
532
assertNotNull(success.getMessage());
537
fail("removed root element");
539
catch (WellformednessException success) {
540
assertNotNull(success.getMessage());
543
doc.appendChild(new Comment("test"));
545
assertEquals(1, doc.getChildCount());
547
Comment test = new Comment("test");
548
doc.appendChild(test);
549
doc.removeChild(test);
550
assertEquals(1, doc.getChildCount());
553
Comment something = new Comment("sd");
554
doc.removeChild(something);
555
fail("Removed nonchild");
557
catch (NoSuchChildException success) {
558
assertNotNull(success.getMessage());
563
fail("removed overly sized element");
565
catch (IndexOutOfBoundsException success) {
566
assertNotNull(success.getMessage());
572
public void testToXML() throws ParsingException, IOException {
574
Builder builder = new Builder();
575
File f = new File("data");
576
f = new File(f, "test.xml");
577
Document input = builder.build(f);
578
String s = input.toXML();
579
Document output = builder.build(s, f.toURL().toExternalForm());
580
assertEquals(input, output);