1 21 22 package nu.xom.tests; 23 24 import java.io.File ; 25 import java.io.IOException ; 26 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; 37 import nu.xom.Text; 38 import nu.xom.WellformednessException; 39 40 41 50 public class DocumentTest extends XOMTestCase { 51 52 public DocumentTest(String name) { 53 super(name); 54 } 55 56 private Element root; 57 private Document doc; 58 59 protected void setUp() { 60 root = new Element("root"); 61 doc = new Document(root); 62 } 63 64 65 public void testToString() { 66 assertEquals("[nu.xom.Document: root]", doc.toString()); 67 } 68 69 70 public void testDocTypeInsertion() { 71 72 DocType type1 = new DocType("root"); 73 74 try { 75 doc.insertChild(type1, 1); 76 fail("inserted doctype after root element"); 77 } 78 catch (IllegalAddException success) { 79 assertNotNull(success.getMessage()); 80 } 81 82 doc.insertChild(type1, 0); 83 assertEquals(type1, doc.getDocType()); 84 85 DocType type2 = new DocType("test"); 86 try { 87 doc.insertChild(type2, 1); 88 fail("Inserted 2nd DocType"); 89 } 90 catch (IllegalAddException success) { 91 assertNotNull(success.getMessage()); 92 } 93 assertEquals(type1, doc.getDocType()); 94 assertNull(type2.getParent()); 95 assertEquals(type1, doc.getChild(0)); 96 97 doc.setDocType(type2); 98 assertEquals(doc.getDocType(), type2); 99 assertNull(type1.getParent()); 100 assertEquals(type2, doc.getChild(0)); 101 102 } 103 104 105 public void testSetDocType() { 106 107 DocType type1 = new DocType("root"); 108 doc.setDocType(type1); 109 assertEquals(type1, doc.getDocType()); 110 111 doc.insertChild(new Comment("test"), 0); 112 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()); 119 120 doc.setDocType(type2); 122 assertEquals(type2, doc.getDocType()); 123 assertEquals(1, doc.indexOf(type2)); 124 assertEquals(doc, type2.getParent()); 125 126 try { 127 doc.setDocType(null); 128 fail("Allowed null doctype"); 129 } 130 catch (NullPointerException success) { 131 assertNotNull(success.getMessage()); 132 assertEquals(type2, doc.getDocType()); 133 } 134 135 try { 136 Document doc2 = new Document(new Element("root")); 137 doc2.setDocType(type2); 138 fail("Allowed multiple parents for doctype"); 139 } 140 catch (MultipleParentException success) { 141 assertNotNull(success.getMessage()); 142 } 143 144 } 145 146 147 public void testBaseURI() { 148 149 assertEquals("", doc.getBaseURI()); 150 doc.setBaseURI("http://www.example.com/index.xml"); 151 assertEquals( 152 "http://www.example.com/index.xml", 153 doc.getBaseURI() 154 ); 155 doc.setBaseURI("file:///home/elharo/XOM/data/test.xml"); 156 assertEquals( 157 "file:///home/elharo/XOM/data/test.xml", 158 doc.getBaseURI() 159 ); 160 doc.setBaseURI("file:///home/elharo/XO%4D/data/test.xml"); 161 assertEquals( 162 "file:///home/elharo/XO%4D/data/test.xml", 163 doc.getBaseURI() 164 ); 165 166 } 167 168 169 public void testSecondRoot() { 170 171 try { 172 doc.insertChild(new Element("test"), 0); 173 fail("Added second root element"); 174 } 175 catch (IllegalAddException success) { 176 assertNotNull(success.getMessage()); 177 } 178 179 } 180 181 182 public void testSetRoot() { 183 184 Element newRoot = new Element("newroot"); 185 doc.setRootElement(newRoot); 186 187 assertEquals(newRoot, doc.getRootElement()); 188 assertNull(root.getParent()); 189 190 try { 191 doc.setRootElement(null); 192 } 193 catch (NullPointerException success) { 194 assertNotNull(success.getMessage()); 195 } 196 197 Element top = new Element("top"); 198 Element child = new Element("child"); 199 top.appendChild(child); 200 try { 201 doc.setRootElement(child); 202 fail("Allowed element with two parents"); 203 } 204 catch (MultipleParentException success) { 205 assertNotNull(success.getMessage()); 206 } 207 208 } 209 210 211 public void testReplaceRootWithItself() { 212 Element root = doc.getRootElement(); 213 doc.setRootElement(root); 214 assertEquals(root, doc.getRootElement()); 215 } 216 217 218 public void testReplaceRootElementWithDifferentElementUsingReplaceChild() { 219 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()); 226 227 } 228 229 230 public void testInsertionAllowed() { 231 232 Document doc = new Document(new Element("root")); 233 Comment original = new Comment("original"); 234 doc.insertChild(original, 0); 235 236 Element temp = new Element("temp"); 237 Comment c2 = new Comment("new comment"); 238 temp.appendChild(c2); 239 240 try { 241 doc.replaceChild(original, c2); 242 fail("Missed multiple parent exception"); 243 } 244 catch (MultipleParentException success) { 245 assertEquals(2, doc.getChildCount()); 246 } 247 248 } 249 250 251 public void testReplaceDocTypeWithDifferentDocTypeUsingReplaceChild() { 252 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()); 260 261 } 262 263 264 public void testReplaceDocTypeWithParentedDocTypeUsingReplaceChild() { 265 266 DocType newDocType = new DocType("new"); 267 DocType oldDocType = new DocType("old"); 268 Document temp = new Document(new Element("root")); 269 temp.setDocType(newDocType); 270 271 doc.setDocType(oldDocType); 272 try { 273 doc.replaceChild(oldDocType, newDocType); 274 fail("Missed MultipleParentException"); 275 } 276 catch (MultipleParentException success) { 277 assertEquals(2, doc.getChildCount()); 278 assertEquals(2, temp.getChildCount()); 279 } 280 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()); 287 288 } 289 290 291 public void testReplaceRootElementWithParentedElementUsingReplaceChild() { 292 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); 298 299 try { 300 doc.replaceChild(oldRoot, newRoot); 301 fail("Missed MultipleParentException"); 302 } 303 catch (MultipleParentException success) { 304 assertEquals(1, doc.getChildCount()); 305 assertEquals(1, temp.getChildCount()); 306 } 307 308 assertEquals(oldRoot, doc.getRootElement()); 309 assertEquals(newRoot, temp.getChild(0)); 310 assertNotNull(oldRoot.getParent()); 311 assertEquals(temp, newRoot.getParent()); 312 313 } 314 315 316 public void testReplaceRootElementWithComment() { 317 318 Element oldRoot = new Element("oldRoot"); 319 Document doc = new Document(oldRoot); 320 321 try { 322 doc.replaceChild(oldRoot, new Comment("c")); 323 fail("Replaced root with comment"); 324 } 325 catch (WellformednessException success) { 326 assertNotNull(success.getMessage()); 327 } 328 329 assertEquals(oldRoot, doc.getRootElement()); 330 assertEquals(doc, oldRoot.getParent()); 331 332 } 333 334 335 public void testReplaceNonDocTypeWithDocTypeUsingReplaceChild() { 336 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()); 344 345 } 346 347 348 public void testDetach() { 349 Comment comment 350 = new Comment("This will be attached then detached"); 351 doc.appendChild(comment); 352 assertEquals(doc, comment.getParent()); 353 comment.detach(); 354 assertNull(comment.getParent()); 355 } 356 357 358 public void testGetDocument() { 359 assertEquals(doc, doc.getDocument()); 360 } 361 362 363 public void testConstructor() { 364 365 assertEquals(root, doc.getRootElement()); 366 assertEquals(1, doc.getChildCount()); 367 368 Element newRoot = new Element("newRoot"); 369 doc.setRootElement(newRoot); 370 assertEquals(newRoot, doc.getRootElement()); 371 assertEquals(1, doc.getChildCount()); 372 373 doc.appendChild(new Comment("test")); 374 assertEquals(2, doc.getChildCount()); 375 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); 381 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); 387 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); 394 395 396 try { 397 Element nullRoot = null; 398 new Document(nullRoot); 399 fail("allowed null root!"); 400 } 401 catch (NullPointerException success) { 402 } 403 404 try { 405 Document nullDoc = null; 406 new Document(nullDoc); 407 fail("allowed null doc!"); 408 } 409 catch (NullPointerException success) { 410 } 412 413 } 414 415 416 public void testCopyConstructor() { 417 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")); 424 425 Document doc2 = new Document(doc); 426 assertEquals(doc, doc2); 427 428 } 429 430 431 public void testCopyConstructorBaseURI() { 432 433 doc.setBaseURI("http://www.example.com/"); 434 435 Document doc2 = new Document(doc); 436 assertEquals(doc.getBaseURI(), doc2.getBaseURI()); 437 assertEquals("http://www.example.com/", doc2.getBaseURI()); 438 assertEquals( 439 doc.getRootElement().getBaseURI(), 440 doc2.getRootElement().getBaseURI() 441 ); 442 assertEquals( 443 "http://www.example.com/", 444 doc2.getRootElement().getBaseURI() 445 ); 446 447 } 448 449 450 public void testCopy() { 451 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")); 458 459 Document doc2 = (Document) doc.copy(); 460 assertEquals(doc, doc2); 461 462 } 463 464 465 public void testAppend() { 466 467 Element root = new Element("root"); 468 Document doc = new Document(root); 469 470 try { 471 doc.appendChild(new Text("test")); 472 fail("appended string"); 473 } 474 catch (IllegalAddException success) { 475 assertNotNull(success.getMessage()); 476 } 477 478 try { 479 doc.appendChild(new Text(" ")); 480 fail("appended white space"); 481 } 482 catch (IllegalAddException success) { 483 assertNotNull(success.getMessage()); 484 } 485 486 try { 487 doc.appendChild(new Text("test")); 488 fail("appended Text"); 489 } 490 catch (IllegalAddException success) { 491 assertNotNull(success.getMessage()); 492 } 493 494 try { 495 doc.appendChild(new Comment("test")); 496 doc.appendChild(new Element("test")); 497 fail("appended element"); 498 } 499 catch (IllegalAddException success) { 500 assertNotNull(success.getMessage()); 501 } 502 503 try { 504 doc.insertChild(new Element("test"), 0); 505 fail("inserted element"); 506 } 507 catch (IllegalAddException success) { 508 assertNotNull(success.getMessage()); 509 } 510 511 } 512 513 514 public void testRemoval() { 515 516 Element root = new Element("root"); 517 Document doc = new Document(root); 518 519 try { 520 root.detach(); 521 fail("detached root element"); 522 } 523 catch (WellformednessException success) { 524 assertNotNull(success.getMessage()); 525 } 526 527 try { 528 doc.removeChild(root); 529 fail("removed root element"); 530 } 531 catch (WellformednessException success) { 532 assertNotNull(success.getMessage()); 533 } 534 535 try { 536 doc.removeChild(0); 537 fail("removed root element"); 538 } 539 catch (WellformednessException success) { 540 assertNotNull(success.getMessage()); 541 } 542 543 doc.appendChild(new Comment("test")); 544 doc.removeChild(1); 545 assertEquals(1, doc.getChildCount()); 546 547 Comment test = new Comment("test"); 548 doc.appendChild(test); 549 doc.removeChild(test); 550 assertEquals(1, doc.getChildCount()); 551 552 try { 553 Comment something = new Comment("sd"); 554 doc.removeChild(something); 555 fail("Removed nonchild"); 556 } 557 catch (NoSuchChildException success) { 558 assertNotNull(success.getMessage()); 559 } 560 561 try { 562 doc.removeChild(20); 563 fail("removed overly sized element"); 564 } 565 catch (IndexOutOfBoundsException success) { 566 assertNotNull(success.getMessage()); 567 } 568 569 } 570 571 572 public void testToXML() throws ParsingException, IOException { 573 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); 581 582 } 583 584 585 } 586 | Popular Tags |