1 21 22 package nu.xom.tests; 23 24 import java.io.File ; 25 import java.io.IOException ; 26 27 import nu.xom.Attribute; 28 import nu.xom.Builder; 29 import nu.xom.Comment; 30 import nu.xom.DocType; 31 import nu.xom.Document; 32 import nu.xom.Element; 33 import nu.xom.Elements; 34 import nu.xom.IllegalAddException; 35 import nu.xom.IllegalNameException; 36 import nu.xom.MalformedURIException; 37 import nu.xom.MultipleParentException; 38 import nu.xom.NamespaceConflictException; 39 import nu.xom.NoSuchAttributeException; 40 import nu.xom.Node; 41 import nu.xom.Nodes; 42 import nu.xom.ParsingException; 43 import nu.xom.ProcessingInstruction; 44 import nu.xom.Text; 45 46 47 56 public class ElementTest extends XOMTestCase { 57 58 private Element element; 59 private Element child1; 60 private Text child2; 61 private Comment child3; 62 private Element child4; 63 private Element child5; 64 private String [] legal = { 65 "http://www.is.edu/sakdsk#sjadh", 66 "http://www.is.edu/sakdsk?name=value&name=head", 67 "uri:isbn:0832473864", 68 "http://www.examples.com:80", 69 "http://www.examples.com:80/", 70 "http://www.is.edu/%20sakdsk#sjadh" 71 }; 72 73 private String [] illegal = { 74 "http://www.is.edu/%sakdsk#sjadh", 75 "http://www.is.edu/k\u0245kakdsk#sjadh", 76 "!@#$%^&*()", 77 "fred", 78 "#fred", 79 "/fred" 80 }; 81 82 83 protected void setUp() { 84 85 child1 = new Element("test"); 86 child2 = new Text("test2"); 87 child3 = new Comment("test3"); 88 child4 = new Element("pre:test", "http://www.example.com"); 89 child5 = new Element("test", "http://www.example.com"); 90 element = new Element("name"); 91 92 element.appendChild(child1); 93 element.appendChild(child2); 94 element.appendChild(child3); 95 element.appendChild(child4); 96 element.appendChild(child5); 97 element.appendChild(" \r\n"); 98 99 } 100 101 102 public ElementTest(String name) { 103 super(name); 104 } 105 106 107 public void testGetChildElementsNull() { 108 109 Elements elements = element.getChildElements( 110 "", "http://www.example.com"); 111 assertEquals(2, elements.size()); 112 elements = element.getChildElements("", ""); 113 assertEquals(1, elements.size()); 114 elements = element.getChildElements(null, 115 "http://www.example.com"); 116 assertEquals(2, elements.size()); 117 elements = element.getChildElements("", null); 118 assertEquals(1, elements.size()); 119 120 } 121 122 123 public void testGetFirstChildElement() { 124 125 Element first = element.getFirstChildElement("test"); 126 assertEquals(child1, first); 127 128 first = element.getFirstChildElement( 129 "test", "http://www.example.com"); 130 assertEquals(child4, first); 131 132 assertNull(element.getFirstChildElement("nonesuch")); 133 assertNull(element.getFirstChildElement("pre:test")); 134 assertNull( 135 element.getFirstChildElement( 136 "nonesuch", "http://www.example.com") 137 ); 138 139 } 140 141 142 public void testConstructor1() { 143 144 String name = "sakjdhjhd"; 145 Element e = new Element(name); 146 147 assertEquals(name, e.getLocalName()); 148 assertEquals(name, e.getQualifiedName()); 149 assertEquals("", e.getNamespacePrefix()); 150 assertEquals("", e.getNamespaceURI()); 151 } 152 153 154 public void testConstructor2() { 155 String name = "sakjdhjhd"; 156 String uri = "http://www.something.com/"; 157 Element e = new Element(name, uri); 158 159 assertEquals(name, e.getLocalName()); 160 assertEquals(name, e.getQualifiedName()); 161 assertEquals("", e.getNamespacePrefix()); 162 assertEquals(uri, e.getNamespaceURI()); 163 164 } 165 166 167 public void testConstructor3() { 168 169 String name = "red:sakjdhjhd"; 170 String uri = "http://www.something.com/"; 171 Element e = new Element(name, uri); 172 173 assertEquals("sakjdhjhd", e.getLocalName()); 174 assertEquals(name, e.getQualifiedName()); 175 assertEquals("red", e.getNamespacePrefix()); 176 assertEquals(uri, e.getNamespaceURI()); 177 178 } 179 180 181 public void testCopyConstructorWithAdditionalNamespaces() { 182 183 Element original = new Element("red"); 184 original.addNamespaceDeclaration("pre", "http://www.example.org"); 185 Element copy = new Element(original); 186 assertEquals("http://www.example.org", copy.getNamespaceURI("pre")); 187 188 } 189 190 191 public void testAllowEmptyNamespace() { 192 193 String name = "sakjdhjhd"; 194 String uri = "http://www.something.com/"; 195 Element e = new Element(name, uri); 196 197 e.setNamespaceURI(""); 198 199 assertEquals("", e.getNamespaceURI()); 200 201 } 202 203 204 public void testToString() { 205 206 String name = "sakjdhjhd"; 207 String uri = "http://www.something.com/"; 208 Element e = new Element(name, uri); 209 210 String s = e.toString(); 211 assertTrue(s.startsWith("[nu.xom.Element: ")); 212 assertTrue(s.endsWith("]")); 213 assertTrue(s.indexOf(name) != -1); 214 215 } 216 217 218 public void testToXML() { 219 220 String name = "sakjdhjhd"; 221 String uri = "http://www.something.com/"; 222 Element e = new Element(name, uri); 223 224 String s = e.toXML(); 225 assertTrue(s.endsWith("/>")); 226 assertTrue(s.startsWith("<" + name)); 227 assertTrue(s.indexOf(uri) != -1); 228 assertTrue(s.indexOf("xmlns=") != -1); 229 230 } 231 232 233 public void testToXML2() throws ParsingException, IOException { 234 235 Builder builder = new Builder(); 236 File f = new File ("data"); 237 f = new File (f, "soapresponse.xml"); 238 Document doc = builder.build(f); 239 Element root = doc.getRootElement(); 240 String form = root.toXML(); 241 Document doc2 242 = builder.build(form, f.toURL().toExternalForm()); 243 Element root2 = doc2.getRootElement(); 244 assertEquals(root, root2); 245 246 } 247 248 249 public void testToXMLWithXMLLangAttribute() { 250 Element e = new Element("e"); 251 e.addAttribute(new Attribute("xml:lang", 252 "http://www.w3.org/XML/1998/namespace", "en")); 253 assertEquals("<e xml:lang=\"en\" />", e.toXML()); 254 } 255 256 257 public void testAllowNullNamespace() { 258 String name = "sakjdhjhd"; 259 String uri = "http://www.something.com/"; 260 Element e = new Element(name, uri); 261 262 e.setNamespaceURI(null); 263 264 assertEquals("", e.getNamespaceURI()); 265 266 } 267 268 269 public void testCantInsertDoctype() { 270 String name = "sakjdhjhd"; 271 String uri = "http://www.something.com/"; 272 Element e = new Element(name, uri); 273 274 try { 275 e.appendChild(new DocType(name)); 276 fail("Appended a document type declaration to an element"); 277 } 278 catch (IllegalAddException success) { 279 assertNotNull(success.getMessage()); 280 } 281 282 } 283 284 285 public void testXMLNamespace() { 286 287 String name = "red:sakjdhjhd"; 288 String uri = "http://www.red.com/"; 289 Element e = new Element(name, uri); 290 291 String xmlNamespace = "http://www.w3.org/XML/1998/namespace"; 292 293 assertEquals(xmlNamespace, e.getNamespaceURI("xml")); 294 295 try { 296 e.addNamespaceDeclaration("xml", "http://www.yahoo.com/"); 297 fail("remapped xml prefix!"); 298 } 299 catch (NamespaceConflictException success) { 300 assertNotNull(success.getMessage()); 301 } 302 assertEquals(e.getNamespaceURI("xml"), xmlNamespace); 303 304 e.addNamespaceDeclaration("xml", xmlNamespace); 306 assertEquals(xmlNamespace, e.getNamespaceURI("xml")); 307 308 309 } 310 311 312 public void testUndeclareDefaultNamespace() { 313 314 String name = "red:sakjdhjhd"; 315 String uri = "http://www.red.com/"; 316 Element child = new Element(name, uri); 317 Element parent = new Element( 318 "parent", "http://www.example.com"); 319 320 assertEquals("http://www.example.com", 321 parent.getNamespaceURI("")); 322 323 parent.appendChild(child); 324 child.addNamespaceDeclaration("", ""); 325 326 assertEquals("", child.getNamespaceURI("")); 327 assertEquals( 328 "http://www.example.com", 329 parent.getNamespaceURI("")); 330 331 Element child2 = new Element("name", "http://www.default.com"); 332 parent.appendChild(child2); 333 334 try { 336 child2.addNamespaceDeclaration("", ""); 337 fail("Illegally undeclared default namespace"); 338 } 339 catch (NamespaceConflictException success) { 340 assertNotNull(success.getMessage()); 341 } 342 343 } 344 345 346 public void testUnsetDefaultNamespaceWithAttribute() { 347 348 String name = "sakjdhjhd"; 349 String uri = "http://www.red.com/"; 350 Element element = new Element(name, uri); 351 element.addAttribute(new Attribute("test", "test")); 352 element.setNamespaceURI(""); 353 assertEquals("", element.getNamespaceURI("")); 354 355 } 356 357 358 public void testSetNamespaceWithAttribute() { 359 360 String name = "red:sakjdhjhd"; 361 String uri = "http://www.red.com/"; 362 Element element = new Element(name, uri); 363 element.addAttribute( 364 new Attribute("red:attribute", uri, "test")); 365 element.setNamespaceURI(uri); 366 assertEquals(uri, element.getNamespaceURI()); 367 assertEquals(uri, element.getNamespaceURI("red")); 368 369 } 370 371 372 public void testAddNamespaceToElementWithAttribute() { 373 374 String name = "a"; 375 String uri = "http://www.w3.org/1999/xhtml"; 376 Element element = new Element(name); 377 element.addAttribute( 378 new Attribute("href", "http://www.elharo.com")); 379 element.setNamespaceURI(uri); 380 assertEquals(uri, element.getNamespaceURI()); 381 382 } 383 384 385 public void testSameNamespaceForElementAndAttribute() { 386 387 String name = "a"; 388 String uri = "http://www.w3.org/1999/xhtml"; 389 Element element = new Element(name); 390 element.addAttribute( 391 new Attribute("html:href", uri, "http://www.elharo.com")); 392 element.setNamespaceURI("http://www.example.com"); 393 element.setNamespacePrefix("pre"); 394 element.setNamespaceURI(uri); 395 element.setNamespacePrefix("html"); 396 assertEquals(uri, element.getNamespaceURI()); 397 assertEquals("html", element.getNamespacePrefix()); 398 399 } 400 401 402 public void testToXMLWithXMLAttributes() { 403 Element e = new Element("test"); 404 e.addAttribute( 405 new Attribute("xml:space", 406 "http://www.w3.org/XML/1998/namespace", 407 "preserve")); 408 e.addAttribute( 409 new Attribute("zzz:zzz", "http://www.example.org", "preserve")); 410 String result = e.toXML(); 411 assertEquals("<test xmlns:zzz=\"http://www.example.org\" xml:space=\"preserve\" zzz:zzz=\"preserve\" />", result); 412 } 413 414 415 public void testGetNamespacePrefixInt() { 416 Element e = new Element("test"); 417 e.addAttribute( 418 new Attribute("xml:space", 419 "http://www.w3.org/XML/1998/namespace", 420 "preserve")); 421 e.addAttribute( 422 new Attribute("zzz:zzz", "http://www.example.org", "preserve")); 423 assertEquals(2, e.getNamespaceDeclarationCount()); 424 try { 425 e.getNamespacePrefix(2); 426 fail("Got prefix beyond bounds"); 427 } 428 catch (IndexOutOfBoundsException success) { 429 assertNotNull(success.getMessage()); 430 } 431 } 432 433 434 public void testXMLPrefixOnElement() { 436 Element e = new Element("xml:test", "http://www.w3.org/XML/1998/namespace"); 437 assertEquals(0, e.getNamespaceDeclarationCount()); 438 assertEquals("<xml:test />", e.toXML()); 439 try { 440 e.getNamespacePrefix(0); 441 fail("Got prefix beyond bounds"); 442 } 443 catch (IndexOutOfBoundsException success) { 444 assertNotNull(success.getMessage()); 445 } 446 } 447 448 449 public void testNamespaceMappings() { 450 451 String name = "red:sakjdhjhd"; 452 String uri = "http://www.red.com/"; 453 Element e = new Element(name, uri); 454 e.addNamespaceDeclaration("blue", "http://www.blue.com/"); 455 e.addNamespaceDeclaration("green", "http://www.green.com/"); 456 Attribute a1 = new Attribute("test", "test"); 457 Attribute a2 = new Attribute( 458 "pre1:green", "http://www.green.com/", "data"); 459 Attribute a3 = new Attribute( 460 "yellow:sfsdadf", "http://www.yellow.com/", "data"); 461 e.addAttribute(a1); 462 e.addAttribute(a2); 463 e.addAttribute(a3); 464 465 assertEquals("http://www.red.com/", e.getNamespaceURI("red")); 466 assertEquals( 467 "http://www.green.com/", 468 e.getNamespaceURI("green")); 469 assertEquals( 470 "http://www.blue.com/", 471 e.getNamespaceURI("blue")); 472 assertEquals( 473 "http://www.green.com/", 474 e.getNamespaceURI("pre1")); 475 assertEquals( 476 "http://www.yellow.com/", 477 e.getNamespaceURI("yellow")); 478 479 480 Element e2 = new Element( 481 "mauve:child", "http://www.mauve.com"); 482 e.appendChild(e2); 483 assertEquals( 484 "http://www.red.com/", 485 e2.getNamespaceURI("red")); 486 assertEquals( 487 "http://www.blue.com/", 488 e2.getNamespaceURI("blue")); 489 assertEquals( 490 "http://www.green.com/", 491 e2.getNamespaceURI("green")); 492 assertEquals( 493 "http://www.green.com/", 494 e2.getNamespaceURI("pre1")); 495 assertEquals( 496 "http://www.yellow.com/", 497 e2.getNamespaceURI("yellow")); 498 assertNull(e2.getNamespaceURI("head")); 499 500 501 try { 502 e.addNamespaceDeclaration("pre1", "http://www.blue2.com"); 503 fail("Added conflicting namespace"); 504 } 505 catch (NamespaceConflictException success) { 506 assertNotNull(success.getMessage()); 507 } 508 509 try { 510 Attribute a4 = new Attribute( 511 "pre1:mauve", "http://www.sadas.com/", "data"); 512 e.addAttribute(a4); 513 fail("Added conflicting namespace"); 514 } 515 catch (NamespaceConflictException success) { 516 assertNotNull(success.getMessage()); 517 } 518 519 try { 522 Attribute a4 = new Attribute( 523 "pre1:green", "http://www.example.com/", "data"); 524 e.addAttribute(a4); 525 } 526 catch (NamespaceConflictException success) { 527 assertNotNull(success.getMessage()); 528 } 529 530 e.removeNamespaceDeclaration("green"); 531 assertNull(e.getNamespaceURI("green")); 532 e.addNamespaceDeclaration("green", "http://www.green2.com/"); 533 assertEquals( 534 "http://www.green2.com/", 535 e.getNamespaceURI("green")); 536 537 } 538 539 540 public void testAttributes() { 541 542 String name = "red:sakjdhjhd"; 543 String uri = "http://www.red.com/"; 544 Element e = new Element(name, uri); 545 546 Attribute a1 = new Attribute("name", "simple"); 547 Attribute a2 = new Attribute( 548 "pre1:green", "http://www.green.com/", "data"); 549 550 e.addAttribute(a1); 551 e.addAttribute(a2); 552 553 assertEquals( 554 a2, 555 e.getAttribute("green", "http://www.green.com/")); 556 assertEquals(a1, e.getAttribute("name")); 557 assertEquals(a1, e.getAttribute("name", "")); 558 assertEquals(e, a1.getParent()); 559 assertEquals("simple", e.getAttribute("name").getValue()); 560 561 a1.detach(); 562 assertNull(a1.getParent()); 563 assertNull(e.getAttribute("name")); 564 565 Attribute removed = e.removeAttribute(a2); 566 assertNull(a2.getParent()); 567 assertEquals(a2, removed); 568 assertNull( e.getAttribute("green", "http://www.green.com/")); 569 570 } 571 572 573 public void testRemoveNullAttribute() { 574 575 String name = "red:sakjdhjhd"; 576 String uri = "http://www.red.com/"; 577 Element e = new Element(name, uri); 578 579 Attribute a1 = new Attribute("name", "simple"); 580 Attribute a2 = new Attribute( 581 "pre1:green", "http://www.green.com/", "data"); 582 583 e.addAttribute(a1); 584 e.addAttribute(a2); 585 586 try { 587 e.removeAttribute(null); 588 fail("Removed Null Attribute"); 589 } 590 catch (NullPointerException success) { 591 assertNotNull(success.getMessage()); 592 } 593 594 } 595 596 public void testRemoveAttributeFromElementWithNoAttributes() { 597 598 String name = "red:sakjdhjhd"; 599 String uri = "http://www.red.com/"; 600 Element e = new Element(name, uri); 601 602 Attribute a1 = new Attribute("name", "simple"); 603 604 try { 605 e.removeAttribute(a1); 606 fail("Removed Attribute that didn't belong"); 607 } 608 catch (NoSuchAttributeException success) { 609 assertTrue(success.getMessage().indexOf(a1.getQualifiedName()) > 0); 610 } 611 612 } 613 614 615 public void testRemoveAttributeFromElementWithDifferentAttributes() { 616 617 String name = "red:sakjdhjhd"; 618 String uri = "http://www.red.com/"; 619 Element e = new Element(name, uri); 620 e.addAttribute(new Attribute("name", "value")); 621 Attribute a1 = new Attribute("name", "simple"); 622 623 try { 624 e.removeAttribute(a1); 625 fail("Removed Attribute that didn't belong"); 626 } 627 catch (NoSuchAttributeException success) { 628 assertTrue(success.getMessage().indexOf(a1.getQualifiedName()) > 0); 629 } 630 631 } 632 633 634 public void testGetValue() { 635 636 String name = "red:sakjdhjhd"; 637 String uri = "http://www.red.com/"; 638 Element e = new Element(name, uri); 639 640 assertEquals(e.getValue(), ""); 641 642 e.appendChild(new Text("data")); 643 assertEquals(e.getValue(), "data"); 644 e.appendChild(new Text(" moredata")); 645 assertEquals(e.getValue(), "data moredata"); 646 e.appendChild(new Comment(" moredata")); 647 assertEquals(e.getValue(), "data moredata"); 648 e.appendChild( 649 new ProcessingInstruction("target", "moredata")); 650 assertEquals(e.getValue(), "data moredata"); 651 652 Element e2 = new Element("child"); 653 e.appendChild(e2); 654 assertEquals("data moredata", e.getValue()); 655 e2.appendChild(new Text("something")); 656 assertEquals("data moredatasomething", e.getValue()); 657 658 } 659 660 661 public void testSetLocalName() { 662 663 String name = "red:sakjdhjhd"; 664 String uri = "http://www.red.com/"; 665 Element e = new Element(name, uri); 666 667 assertEquals("sakjdhjhd", e.getLocalName()); 668 669 e.setLocalName("dude"); 670 assertEquals("dude", e.getLocalName()); 671 e.setLocalName("digits__"); 672 assertEquals("digits__", e.getLocalName()); 673 e.setLocalName("digits1234"); 674 assertEquals("digits1234", e.getLocalName()); 675 e.setLocalName("digits-z"); 676 assertEquals("digits-z", e.getLocalName()); 677 678 try { 679 e.setLocalName("spaces "); 680 fail("Local name allowed to contain spaces"); 681 } 682 catch (IllegalNameException success) { 683 assertNotNull(success.getMessage()); 684 assertEquals("spaces ", success.getData()); 685 } 686 687 try { 688 e.setLocalName("digits:test"); 689 fail("Local name allowed to contain colon"); 690 } 691 catch (IllegalNameException success) { 692 assertNotNull(success.getMessage()); 693 } 694 695 try { 696 e.setLocalName("digits!test"); 697 fail("Local name allowed to contain bang"); 698 } 699 catch (IllegalNameException success) { 700 assertNotNull(success.getMessage()); 701 assertEquals("digits!test", success.getData()); 702 } 703 704 try { 705 e.setLocalName("digits\u0000test"); 706 fail("Local name allowed to contain null"); 707 } 708 catch (IllegalNameException success) { 709 assertNotNull(success.getMessage()); 710 assertEquals("digits\u0000test", success.getData()); 711 } 712 713 } 714 715 716 public void testSetNamespacePrefix() { 717 718 String name = "red:sakjdhjhd"; 719 String uri = "http://www.red.com/"; 720 String prefix = "red"; 721 Element e = new Element(name, uri); 722 723 assertEquals(prefix, e.getNamespacePrefix()); 724 725 e.setNamespacePrefix("dude"); 726 assertEquals("dude", e.getNamespacePrefix()); 727 e.setNamespacePrefix("digits__"); 728 assertEquals("digits__", e.getNamespacePrefix()); 729 e.setNamespacePrefix("digits1234"); 730 assertEquals("digits1234", e.getNamespacePrefix()); 731 e.setNamespacePrefix("digits-z"); 732 assertEquals("digits-z", e.getNamespacePrefix()); 733 e.setNamespacePrefix(""); 734 assertEquals("", e.getNamespacePrefix()); 735 e.setNamespacePrefix(null); 736 assertEquals("", e.getNamespacePrefix()); 737 738 try { 739 e.setNamespacePrefix("spaces "); 740 fail("namespace prefix allowed to contain spaces"); 741 } 742 catch (IllegalNameException success) { 743 assertNotNull(success.getMessage()); 744 assertEquals("spaces ", success.getData()); 745 } 746 747 try { 748 e.setNamespacePrefix("digits:test"); 749 fail("namespace prefix allowed to contain colon"); 750 } 751 catch (IllegalNameException success) { 752 assertNotNull(success.getMessage()); 753 } 754 755 try { 756 e.setNamespacePrefix("digits!test"); 757 fail("namespace prefix allowed to contain bang"); 758 } 759 catch (IllegalNameException success) { 760 assertNotNull(success.getMessage()); 761 assertEquals("digits!test", success.getData()); 762 } 763 764 try { 765 e.setNamespacePrefix("digits\u0000test"); 766 fail("namespace prefix allowed to contain null"); 767 } 768 catch (IllegalNameException success) { 769 assertNotNull(success.getMessage()); 770 assertEquals("digits\u0000test", success.getData()); 771 } 772 773 } 774 775 776 public void testSetNamespaceURI() { 777 778 String name = "red:sakjdhjhd"; 779 String uri = "http://www.red.com/"; 780 Element e = new Element(name, uri); 781 782 assertEquals(e.getNamespaceURI(), uri); 783 784 for (int i = 0; i < legal.length; i++) { 785 e.setNamespaceURI(legal[i]); 786 assertEquals(legal[i], e.getNamespaceURI()); 787 } 788 789 for (int i = 0; i < illegal.length; i++) { 790 try { 791 e.setNamespaceURI(illegal[i]); 792 fail("illegal namespace URI allowed"); 793 } 794 catch (MalformedURIException success) { 795 assertNotNull(success.getMessage()); 796 } 797 } 798 799 } 800 801 802 public void 803 testSetNamespaceURIConflictsWithAdditionalNamespaceDeclaration() 804 { 805 806 String name = "red:sakjdhjhd"; 807 String uri = "http://www.red.com/"; 808 Element e = new Element(name, uri); 809 e.addNamespaceDeclaration("red", "http://www.red.com/"); 810 811 try { 812 e.setNamespaceURI("http://www.example.com"); 813 fail("illegal namespace conflict"); 814 } 815 catch (NamespaceConflictException success) { 816 assertNotNull(success.getMessage()); 817 } 818 819 } 820 821 822 public void testSetNamespaceURIConflictsWithAttributeNamespace() { 823 824 String name = "red:sakjdhjhd"; 825 String uri = "http://www.red.com/"; 826 Element e = new Element(name, uri); 827 e.addAttribute( 828 new Attribute("red:test", "http://www.red.com/", "value")); 829 830 try { 831 e.setNamespaceURI("http://www.example.com"); 832 fail("illegal attribute namespace conflict"); 833 } 834 catch (NamespaceConflictException success) { 835 assertNotNull(success.getMessage()); 836 } 837 838 } 839 840 841 public void testChangePrefix() { 842 843 String name = "red:sakjdhjhd"; 844 String uri = "http://www.red.com/"; 845 Element element = new Element(name, uri); 846 element.addNamespaceDeclaration("blue", "http://www.foo.com/"); 847 element.addAttribute(new Attribute("green:money", 848 "http://example.com/", "value")); 849 element.addNamespaceDeclaration("purple", uri); 850 element.addAttribute( 851 new Attribute("mauve:money", uri, "value")); 852 853 try { 854 element.setNamespacePrefix("blue"); 855 fail("Conflicting prefix allowed against additional " + 856 "namespace declaration"); 857 } 858 catch (NamespaceConflictException success) { 859 assertNotNull(success.getMessage()); 860 } 861 862 try { 863 element.setNamespacePrefix("green"); 864 fail("Conflicting prefix allowed against attribute"); 865 } 866 catch (NamespaceConflictException success) { 867 assertNotNull(success.getMessage()); 868 } 869 870 element.setNamespacePrefix("purple"); 871 assertEquals("purple", element.getNamespacePrefix()); 872 element.setNamespacePrefix("mauve"); 873 assertEquals("mauve", element.getNamespacePrefix()); 874 875 } 876 877 878 public void testDeclareNamespacePrefix() { 879 880 String name = "red:sakjdhjhd"; 881 String uri = "http://www.red.com/"; 882 Element e = new Element(name, uri); 883 884 for (int i = 0; i < legal.length; i++) { 885 e.removeNamespaceDeclaration("prefix"); 886 e.addNamespaceDeclaration("prefix", legal[i]); 887 assertEquals(legal[i], e.getNamespaceURI("prefix")); 888 } 889 890 for (int i = 0; i < illegal.length; i++) { 891 try { 892 e.addNamespaceDeclaration("prefix", illegal[i]); 893 fail("illegal namespace URI allowed " + illegal[i]); 894 } 895 catch (MalformedURIException ex) { 896 assertNotNull(ex.getMessage()); 898 } 899 } 900 901 } 902 903 904 public void testInsertChildUsingString() { 905 906 String name = "red:sakjdhjhd"; 907 String uri = "http://www.red.com/"; 908 Element e = new Element(name, uri); 909 910 Element e2 = new Element( 911 "mauve:child", "http://www.mauve.com"); 912 e.insertChild(e2, 0); 913 Element e3 = new Element( 914 "mauve:child", "http://www.mauve.com"); 915 e.insertChild(e3, 0); 916 917 e.insertChild("Hello", 0); 918 assertEquals("Hello", e.getChild(0).getValue()); 919 920 } 921 922 923 public void testInsertNull() { 924 925 String name = "red:sakjdhjhd"; 926 String uri = "http://www.red.com/"; 927 Element e = new Element(name, uri); 928 String data = null; 929 try { 930 e.insertChild(data, 0); 931 fail("Inserted null"); 932 } 933 catch (NullPointerException success) { 934 } 936 } 937 938 939 public void appendNullChild() { 940 941 String name = "red:sakjdhjhd"; 942 String uri = "http://www.red.com/"; 943 Element e = new Element(name, uri); 944 String data = null; 945 try { 946 e.appendChild(data); 947 fail("Appended null"); 948 } 949 catch (NullPointerException success) { 950 } 952 } 953 954 955 public void testInsertChild() { 956 957 String name = "red:sakjdhjhd"; 958 String uri = "http://www.red.com/"; 959 Element e = new Element(name, uri); 960 961 Element e2 = new Element("mv:child", "http://www.mauve.com"); 962 e.insertChild(e2, 0); 963 Element e3 = new Element("mv:child", "http://www.mauve.com"); 964 e.insertChild(e3, 0); 965 Element e4 = new Element("mv:child", "http://www.mauve.com"); 966 e3.insertChild(e4, 0); 967 968 assertEquals(e3, e.getChild(0)); 969 970 try { 971 Element root = new Element("root"); 972 Document doc = new Document(root); 973 e.insertChild(doc, 0); 974 fail("Added document as child"); 975 } 976 catch (IllegalAddException ex) { 977 assertNotNull(ex.getMessage()); 979 } 980 try { 981 e.insertChild(e2, 0); 982 fail("Added child twice"); 983 } 984 catch (MultipleParentException ex) { 985 assertNotNull(ex.getMessage()); 987 } 988 try { 989 e.insertChild(e4, 0); 990 fail("Added child twice"); 991 } 992 catch (MultipleParentException ex) { 993 assertNotNull(ex.getMessage()); 995 } 996 try { 997 e.insertChild((Node) null, 0); 998 fail("Added null child"); 999 } 1000 catch (NullPointerException success) { 1001 } 1003 try { 1004 e.insertChild(new Comment("test"), 20); 1005 fail("Added comment after end"); 1006 } 1007 catch (IndexOutOfBoundsException success) { 1008 assertNotNull(success.getMessage()); 1010 } 1011 try { 1012 e.insertChild(new Comment("test"), -20); 1013 fail("Added comment before start"); 1014 } 1015 catch (IndexOutOfBoundsException success) { 1016 assertNotNull(success.getMessage()); 1018 } 1019 1020 } 1021 1022 1023 public void testInsertString() { 1024 1025 Element element = new Element("test"); 1026 element.insertChild("test", 0); 1027 1028 String s = null; 1029 try { 1030 element.insertChild(s, 0); 1031 fail("Inserted string as null"); 1032 } 1033 catch (NullPointerException success) { 1034 } 1036 1037 element.insertChild("" , 0); 1038 assertEquals(2, element.getChildCount()); 1040 1041 } 1042 1043 1044 public void testUnsetNamespaceWhenPrefixed() { 1045 1046 Element element 1047 = new Element("prefix:name", "http://www.foo.com/"); 1048 1049 try { 1050 element.setNamespaceURI(""); 1051 fail("Unset namespace"); 1052 } 1053 catch (NamespaceConflictException success) { 1054 assertNotNull(success.getMessage()); 1055 } 1056 try { 1057 element.setNamespaceURI(null); 1058 fail("Unset namespace"); 1059 } 1060 catch (NamespaceConflictException success) { 1061 assertNotNull(success.getMessage()); 1062 } 1063 1064 } 1065 1066 1067 public void testGetChildElements() { 1068 1069 Elements children = element.getChildElements(); 1070 assertEquals(3, children.size()); 1071 assertEquals(child1, children.get(0)); 1072 assertEquals(child4, children.get(1)); 1073 assertEquals(child5, children.get(2)); 1074 1075 children = element.getChildElements("nonesuch"); 1076 assertEquals(0, children.size()); 1077 1078 children = element.getChildElements("test"); 1079 assertEquals(1, children.size()); 1080 assertEquals(child1, children.get(0)); 1081 1082 children = element.getChildElements( 1083 "test", "http://www.example.com"); 1084 assertEquals(2, children.size()); 1085 assertEquals(child4, children.get(0)); 1086 assertEquals(child5, children.get(1)); 1087 1088 } 1089 1090 1091 public void testAddAttribute() { 1092 1093 Element element = new Element("name"); 1094 Attribute a1 = new Attribute("name", "value"); 1095 Attribute a2 = new Attribute("xlink:type", 1096 "http://www.w3.org/TR/1999/xlink", "simple"); 1097 1098 element.addAttribute(a1); 1099 element.addAttribute(a2); 1100 1101 assertEquals(2, element.getAttributeCount()); 1102 1103 1104 Element element2 = new Element("name"); 1105 try { 1106 element2.addAttribute(a1); 1107 fail("added attribute with existing parent"); 1108 } 1109 catch (MultipleParentException success) { 1110 assertNotNull(success.getMessage()); 1111 } 1112 1113 a1.detach(); 1114 element2.addAttribute(a1); 1115 1116 a2.detach(); 1117 Element funky 1118 = new Element("xlink:funky", "http://www.funky.org"); 1119 try { 1120 funky.addAttribute(a2); 1121 fail("added conflicting namespace"); 1122 } 1123 catch (NamespaceConflictException success) { 1124 assertNotNull(success.getMessage()); 1125 } 1126 1127 a2.detach(); 1128 Element notasfunky = new Element( 1129 "prefix:funky", "http://www.w3.org/TR/1999/xlink"); 1130 notasfunky.addAttribute(a2); 1131 1132 1133 Attribute a3 = new Attribute( 1134 "xlink:type", "http://www.w3.org/TR/1999/xlink", "simple"); 1135 Attribute a4 = new Attribute( 1136 "xlink:href", "http://www.w3.org/1998/xlink", "simple"); 1137 Element test = new Element("test"); 1138 try { 1139 test.addAttribute(a3); 1140 test.addAttribute(a4); 1141 fail("added conflicting attributes"); 1142 } 1143 catch (NamespaceConflictException success) { 1144 assertNotNull(success.getMessage()); 1145 } 1146 1147 Attribute a5 = new Attribute( 1148 "xlink:type", "http://www.w3.org/TR/1999/xlink", "simple"); 1149 Attribute a6 = new Attribute( 1150 "xlink:type", "http://www.w3.org/1998/xlink", "simple"); 1151 Element test2 = new Element("test"); 1152 try { 1153 test2.addAttribute(a5); 1154 test2.addAttribute(a6); 1155 fail("added conflicting attributes"); 1156 } 1157 catch (NamespaceConflictException success) { 1158 assertNotNull(success.getMessage()); 1159 } 1160 1161 } 1162 1163 1164 public void testAddAttributesWithAdditionalNamespaces() { 1165 1166 Element element = new Element("name"); 1167 element.addNamespaceDeclaration( 1168 "xlink", "http://www.w3.org/TR/1999/xlink"); 1169 element.addNamespaceDeclaration( 1170 "pre", "http://www.example.com"); 1171 1172 1173 Attribute a1 = new Attribute("name", "value"); 1174 Attribute a2 = new Attribute("xlink:type", 1175 "http://www.w3.org/TR/1999/xlink", "simple"); 1176 1177 element.addAttribute(a1); 1178 element.addAttribute(a2); 1179 1180 assertEquals(2, element.getAttributeCount()); 1181 1182 try { 1183 element.addAttribute( 1184 new Attribute("pre:att", "ftp://example.com/", "value") 1185 ); 1186 fail("added attribute that conflicts with " + 1187 "additional namespace declaration"); 1188 } 1189 catch (NamespaceConflictException success) { 1190 assertNotNull(success.getMessage()); 1191 } 1192 1193 element.addAttribute( 1194 new Attribute("ok:att", "ftp://example.com/", "value") 1195 ); 1196 assertEquals(3, element.getAttributeCount()); 1197 1198 try { 1199 element.addNamespaceDeclaration( 1200 "ok", "http://www.example.net"); 1201 fail("added namespace declaration that " + 1202 "conflicts with attribute"); 1203 } 1204 catch (NamespaceConflictException ex) { 1205 assertNotNull(ex.getMessage()); 1206 } 1207 1208 assertEquals( 1209 "ftp://example.com/", 1210 element.getNamespaceURI("ok")); 1211 assertEquals( 1212 "http://www.w3.org/TR/1999/xlink", 1213 element.getNamespaceURI("xlink")); 1214 assertEquals( 1215 "http://www.example.com", 1216 element.getNamespaceURI("pre")); 1217 1218 } 1219 1220 1221 public void testTriple() 1222 throws IOException , ParsingException { 1223 String data = "<b><c1 /><c2 /></b>"; 1224 Builder builder = new Builder(); 1225 Document doc = builder.build(data, "http://www.example.org/"); 1226 Node root = doc.getRootElement(); 1227 Node rootcopy = root.copy(); 1228 assertEquals(data, rootcopy.toXML()); 1229 } 1230 1231 1232 public void testCopyChildElementWithNoChildren() { 1233 1234 Element parent = new Element("parent"); 1235 Element child = new Element("child"); 1236 parent.appendChild(child); 1237 Element copy = new Element(child); 1238 assertEquals(child, copy); 1239 1240 } 1241 1242 1243 public void testSimpleCopy() { 1244 1245 Element parent = new Element("parent"); 1246 Element child = new Element("child"); 1247 parent.appendChild(child); 1248 Element copy = new Element(parent); 1249 assertEquals(parent, copy); 1250 1251 } 1252 1253 1254 public void testCopyEmptyElement() { 1255 1256 Element parent = new Element("parent"); 1257 Element copy = new Element(parent); 1258 assertEquals(parent, copy); 1259 1260 } 1261 1262 1263 public void testEmptyElementAsRootElementCopy() { 1264 1265 Element root = new Element("root"); 1266 Document doc = new Document(root); 1267 Node copy = doc.copy(); 1268 assertEquals(doc, copy); 1269 } 1270 1271 1272 public void testCopy() { 1273 1274 String name = "red:sakjdhjhd"; 1275 String uri = "http://www.red.com/"; 1276 String baseURI = "http://www.example.com/"; 1277 Element e = new Element(name, uri); 1278 1279 e.addNamespaceDeclaration("blue", "http://www.blue.com"); 1280 e.addNamespaceDeclaration("green", "http://www.green.com"); 1281 Attribute a1 = new Attribute("test", "test"); 1282 Attribute a2 = new Attribute("pre1:green", 1283 "http://www.green.com/", "data"); 1284 Attribute a3 = new Attribute("yellow:sfsdadf", 1285 "http://www.yellow.com/", "data"); 1286 e.addAttribute(a1); 1287 e.addAttribute(a2); 1288 e.addAttribute(a3); 1289 1290 1291 Element e2 = new Element("mv:child", "http://www.mauve.com"); 1292 e.appendChild(e2); 1293 1294 Element e3 = new Element("mv:child", "http://www.mauve.com"); 1295 e.insertChild(e3, 0); 1296 Element e4 = new Element("mv:child", "http://www.mauve.com"); 1297 e3.insertChild(e4, 0); 1298 e.setBaseURI(baseURI); 1299 1300 Element copy = (Element) e.copy(); 1301 1302 assertEquals( 1303 e.getNamespaceURI("red"), 1304 copy.getNamespaceURI("red")); 1305 assertEquals( 1306 e.getNamespaceURI("blue"), 1307 copy.getNamespaceURI("blue")); 1308 assertEquals(e.getValue(), copy.getValue()); 1309 assertEquals(e.getAttribute("test").getValue(), 1310 copy.getAttribute("test").getValue()); 1311 assertEquals(e.getBaseURI(), copy.getBaseURI()); 1312 1313 } 1314 1315 1316 public void testDeepCopy() { 1317 1318 Element top = new Element("e"); 1319 Element parent = top; 1320 for (int i = 0; i < 100; i++) { 1321 Element child = new Element("e" + i); 1322 parent.appendChild(child); 1323 parent = child; 1324 } 1325 Element copy = (Element) top.copy(); 1326 assertEquals(top, copy); 1327 1328 } 1329 1330 1331 public void testRemoveChildren() { 1332 1333 String name = "red:sakjdhjhd"; 1334 String uri = "http://www.red.com/"; 1335 Element parent = new Element(name, uri); 1336 1337 Attribute a1 = new Attribute("test", "test"); 1338 parent.addAttribute(a1); 1339 1340 Element child1 = new Element("mv:child", "http://www.mauve.com"); 1341 parent.appendChild(child1); 1342 Element child2 = new Element("mv:child", "http://www.mauve.com"); 1343 parent.appendChild(child2); 1344 Element grandchild = new Element("mv:child", "http://www.mauve.com"); 1345 child2.insertChild(grandchild, 0); 1346 1347 assertEquals(child2, grandchild.getParent()); 1348 assertEquals(parent, child1.getParent()); 1349 assertEquals(parent, child2.getParent()); 1350 1351 Nodes result = parent.removeChildren(); 1352 1353 assertEquals(0, parent.getChildCount()); 1354 assertNull(child1.getParent()); 1355 assertNull(child2.getParent()); 1356 assertEquals(child2, grandchild.getParent()); 1357 assertEquals(parent, a1.getParent()); 1358 1359 assertEquals(2, result.size()); 1360 assertEquals(child1, result.get(0)); 1361 assertEquals(child2, result.get(1)); 1362 assertEquals(grandchild, child2.getChild(0)); 1363 assertEquals(child2, grandchild.getParent()); 1364 1365 } 1366 1367 1368 public void testRemovedChildrenInheritBaseURI() { 1369 1370 String base = "http://www.example.com/"; 1371 Element parent = new Element("parent"); 1372 Element child = new Element("child"); 1373 parent.setBaseURI(base); 1374 parent.appendChild(child); 1375 parent.removeChildren(); 1376 assertEquals(base, child.getBaseURI()); 1377 1378 } 1379 1380 1381 public void testRemoveNonElementChildren() { 1382 1383 String name = "red:sakjdhjhd"; 1384 String uri = "http://www.red.com/"; 1385 Element parent = new Element(name, uri); 1386 1387 Attribute a1 = new Attribute("test", "test"); 1388 parent.addAttribute(a1); 1389 1390 Node child1 = new Text("http://www.mauve.com"); 1391 parent.appendChild(child1); 1392 Node child2 = new ProcessingInstruction( 1393 "child", "http://www.mauve.com"); 1394 parent.appendChild(child2); 1395 Node child3 = new Comment("http://www.mauve.com"); 1396 parent.appendChild(child3); 1397 1398 assertEquals(parent, child3.getParent()); 1399 assertEquals(parent, child1.getParent()); 1400 assertEquals(parent, child2.getParent()); 1401 1402 Nodes result = parent.removeChildren(); 1403 1404 assertEquals(0, parent.getChildCount()); 1405 assertNull(child1.getParent()); 1406 assertNull(child2.getParent()); 1407 assertNull(child3.getParent()); 1408 assertEquals(parent, a1.getParent()); 1409 1410 assertEquals(3, result.size()); 1411 assertEquals(child1, result.get(0)); 1412 assertEquals(child2, result.get(1)); 1413 assertEquals(child3, result.get(2)); 1414 1415 } 1416 1417 1418 public void testGetAttributeValue() { 1419 1420 String name = "sakjdhjhd"; 1421 Element e = new Element(name); 1422 1423 assertNull(e.getAttributeValue("test")); 1424 assertNull(e.getAttributeValue("base", 1425 "http://www.w3.org/XML/1998/namespace")); 1426 1427 e.addAttribute(new Attribute("test", "value")); 1428 e.addAttribute(new Attribute("xml:base", 1429 "http://www.w3.org/XML/1998/namespace", 1430 "http://www.example.com/")); 1431 1432 assertEquals("value", e.getAttributeValue("test")); 1433 assertEquals( 1434 "http://www.example.com/", 1435 e.getAttributeValue("base", 1436 "http://www.w3.org/XML/1998/namespace")); 1437 assertNull(e.getAttributeValue("xml:base")); 1438 assertNull(e.getAttributeValue("base")); 1439 assertNull(e.getAttributeValue("test", 1440 "http://www.w3.org/XML/1998/namespace")); 1441 1442 } 1443 1444 1445 public void testGetAttribute() { 1446 1447 String name = "sakjdhjhd"; 1448 Element e = new Element(name); 1449 1450 assertNull(e.getAttribute("test")); 1451 assertNull(e.getAttribute("base", 1452 "http://www.w3.org/XML/1998/namespace")); 1453 1454 try { 1455 e.getAttribute(0); 1456 } 1457 catch (IndexOutOfBoundsException success) { 1458 } 1460 1461 Attribute a1 = new Attribute("test", "value"); 1462 Attribute a2 = new Attribute("xml:base", 1463 "http://www.w3.org/XML/1998/namespace", 1464 "http://www.example.com/"); 1465 1466 e.addAttribute(a1); 1467 e.addAttribute(a2); 1468 1469 assertEquals(a1, e.getAttribute("test")); 1470 assertEquals(a2, e.getAttribute("base", 1471 "http://www.w3.org/XML/1998/namespace")); 1472 1473 try { 1474 e.getAttribute(2); 1475 fail("Got attribute beyond bounds"); 1476 } 1477 catch (IndexOutOfBoundsException success) { 1478 assertNotNull(success.getMessage()); 1479 } 1480 1481 try { 1482 e.getAttribute(-1); 1483 fail("Got attribute with negative index"); 1484 } 1485 catch (IndexOutOfBoundsException success) { 1486 assertNotNull(success.getMessage()); 1487 } 1488 1489 } 1490 1491 1492 public void testGetNamespacePrefix() { 1493 Element html = new Element("html"); 1494 assertEquals("", html.getNamespacePrefix()); 1495 } 1496 1497 1498 public void testXMLPrefixAllowed() { 1499 Element test = new Element("xml:base", 1500 "http://www.w3.org/XML/1998/namespace"); 1501 assertEquals("xml", test.getNamespacePrefix()); 1502 assertEquals("http://www.w3.org/XML/1998/namespace", test.getNamespaceURI()); 1503 assertEquals("xml:base", test.getQualifiedName()); 1504 } 1505 1506 1507 public void testXMLPrefixNotAllowedWithWrongURI() { 1508 try { 1509 new Element("xml:base", "http://www.example.org/"); 1510 fail("Allowed wrong namespace for xml prefix"); 1511 } 1512 catch (NamespaceConflictException success) { 1513 assertNotNull(success.getMessage()); 1514 } 1515 1516 } 1517 1518 1519 public void testWrongPrefixNotAllowedWithXMLURI() { 1520 1521 try { 1522 new Element("test:base", "http://www.w3.org/XML/1998/namespace"); 1523 fail("Allowed XML namespace to be associated with non-xml prefix"); 1524 } 1525 catch (NamespaceConflictException success) { 1526 assertNotNull(success.getMessage()); 1527 } 1528 1529 1530 } 1531 1532 1533 public void testEmptyName() { 1534 1535 try { 1536 new Element(""); 1537 fail("Allowed empty string for element name"); 1538 } 1539 catch (IllegalNameException success) { 1540 assertNotNull(success.getMessage()); 1541 assertEquals("", success.getData()); 1542 } 1543 1544 } 1545 1546 1547 public void testBadNameStartCharacter() { 1548 1549 try { 1550 new Element("1Kelvin"); 1551 fail("Allowed element name to begin with digit"); 1552 } 1553 catch (IllegalNameException success) { 1554 assertNotNull(success.getMessage()); 1555 assertEquals("1Kelvin", success.getData()); 1556 } 1557 1558 } 1559 1560 1561 public void testNullName() { 1562 1563 try { 1564 new Element((String ) null); 1565 fail("Allowed null for element name"); 1566 } 1567 catch (NullPointerException success) { 1568 } 1570 1571 } 1572 1573 1574} 1575 | Popular Tags |