1 2 57 58 68 package dom.mem; 69 import java.lang.reflect.InvocationTargetException ; 70 import java.lang.reflect.Method ; 71 72 import org.enhydra.apache.xerces.dom.DOMImplementationImpl; 73 import org.enhydra.apache.xerces.dom.DocumentImpl; 74 import org.w3c.dom.Attr ; 75 import org.w3c.dom.CDATASection ; 76 import org.w3c.dom.Comment ; 77 import org.w3c.dom.DOMException ; 78 import org.w3c.dom.DOMImplementation ; 79 import org.w3c.dom.Document ; 80 import org.w3c.dom.DocumentFragment ; 81 import org.w3c.dom.DocumentType ; 82 import org.w3c.dom.Element ; 83 import org.w3c.dom.EntityReference ; 84 import org.w3c.dom.NamedNodeMap ; 85 import org.w3c.dom.Node ; 86 import org.w3c.dom.NodeList ; 87 import org.w3c.dom.Notation ; 88 import org.w3c.dom.ProcessingInstruction ; 89 import org.w3c.dom.Text ; 90 91 import dom.util.Assertion; 92 93 public class Test { 94 95 107 public static boolean DOMExceptionsTest(Object node, 108 String methodName, 109 Class [] methodSignature, 110 Object [] parameters, 111 short code) 112 { 113 boolean asExpected = false; 114 Method method; 115 116 try { 117 method = node.getClass().getMethod(methodName,methodSignature); 118 method.invoke(node, parameters); 119 } catch(InvocationTargetException exc) { 120 Throwable realE = exc.getTargetException(); 121 if(realE instanceof DOMException ) { 122 asExpected = (((DOMException )realE).code== code); 123 if(!asExpected) 124 System.out.println("Wrong DOMException(" + 125 ((DOMException )realE).code + ")"); 126 } else { 127 System.out.println("Wrong Exception (" + code + ")"); 128 } 129 if(!asExpected) { 130 System.out.println("Expected DOMException (" + 131 code + ") not thrown"); 132 } 133 } catch(Exception exc) { 134 System.out.println("test invocation failure (" + exc + ")"); 135 } 136 return (asExpected); 137 } 138 139 public static void main(String argv[]) 140 { 141 System.out.print("DOM Memory Test.\n"); 142 143 { 147 Document doc; 148 doc = new DocumentImpl(); 149 } 150 151 152 { 158 Document doc = new DocumentImpl(); 162 Element el = doc.createElement("Doc02Element"); 163 DocumentFragment frag = doc.createDocumentFragment (); 164 Text text = doc.createTextNode("Doc02TextNode"); 165 Comment comment = doc.createComment("Doc02Comment"); 166 CDATASection cdataSec = doc.createCDATASection("Doc02CDataSection"); 167 DocumentType docType = doc.getImplementation().createDocumentType("Doc02DocumentType", null, null); 168 Notation notation = ((DocumentImpl) doc).createNotation("Doc02Notation"); 169 ProcessingInstruction pi = doc.createProcessingInstruction("Doc02PITarget", 170 "Doc02PIData"); 171 NodeList nodeList = doc.getElementsByTagName("*"); 172 } 173 174 175 176 { 177 Document doc = new DocumentImpl(); 178 Element el = doc.createElement("Doc02Element"); 179 } 180 181 182 183 { 184 Document doc = new DocumentImpl(); 185 DocumentFragment frag = doc.createDocumentFragment (); 186 }; 187 188 189 190 191 { 192 Document doc = new DocumentImpl(); 193 Element el = doc.createElement("Doc02Element"); 194 } 195 196 197 198 { 199 Document doc = new DocumentImpl(); 200 Text text = doc.createTextNode("Doc02TextNode"); 201 } 202 203 204 205 { 206 Document doc = new DocumentImpl(); 207 Comment comment = doc.createComment("Doc02Comment"); 208 } 209 210 211 212 { 213 Document doc = new DocumentImpl(); 214 CDATASection cdataSec = doc.createCDATASection("Doc02CDataSection"); 215 } 216 217 218 219 220 { 221 Document doc = new DocumentImpl(); 222 DocumentType docType = doc.getImplementation().createDocumentType("Doc02DocumentType", null, null); 223 } 224 225 226 227 228 { 229 Document doc = new DocumentImpl(); 230 Notation notation = ((DocumentImpl)doc).createNotation("Doc02Notation"); 231 } 232 233 234 235 236 { 237 Document doc = new DocumentImpl(); 238 ProcessingInstruction pi = doc.createProcessingInstruction("Doc02PITarget", 239 "Doc02PIData"); 240 } 241 242 243 244 { 245 Document doc = new DocumentImpl(); 246 Attr attribute = doc.createAttribute("Doc02Attribute"); 247 } 248 249 250 251 252 { 253 Document doc = new DocumentImpl(); 254 EntityReference er = doc.createEntityReference("Doc02EntityReference"); 255 } 256 257 258 259 { 260 Document doc = new DocumentImpl(); 261 NodeList nodeList = doc.getElementsByTagName("*"); 262 } 263 264 265 266 270 { 271 Document doc = new DocumentImpl(); 272 Element rootEl = doc.createElement("Doc03RootElement"); 273 doc.appendChild(rootEl); 274 Text textNode = doc.createTextNode("Doc03 text stuff"); 275 rootEl.appendChild(textNode); 276 277 NodeList nodeList = doc.getElementsByTagName("*"); 278 }; 279 280 281 282 { 286 Document doc = new DocumentImpl(); 287 Element rootEl = doc.createElement("RootElement"); 288 doc.appendChild(rootEl); 289 { 290 Attr attr01 = doc.createAttribute("Attr01"); 291 rootEl.setAttributeNode(attr01); 292 } 293 294 295 { 296 Attr attr02 = doc.createAttribute("Attr01"); 297 rootEl.setAttributeNode(attr02); 298 } 299 300 }; 301 302 306 { 307 Document doc = new DocumentImpl(); 308 Element rootEl = doc.createElement("RootElement"); 309 doc.appendChild(rootEl); 310 Attr attr01 = doc.createAttribute("Attr02"); 311 rootEl.setAttributeNode(attr01); 312 Attr attr02 = doc.createAttribute("Attr02"); 313 rootEl.setAttributeNode(attr02); 314 } 315 316 317 318 322 { 323 Document doc = new DocumentImpl(); 324 Element rootEl = doc.createElement("RootElement"); 325 doc.appendChild(rootEl); 326 Attr attr01 = doc.createAttribute("Attr03"); 327 rootEl.setAttributeNode(attr01); 328 329 attr01.setValue("Attr03Value1"); 330 attr01.setValue("Attr03Value2"); 331 } 332 333 334 335 336 340 { 341 Document doc = new DocumentImpl(); 342 Element rootEl = doc.createElement("RootElement"); 343 doc.appendChild(rootEl); 344 345 346 Text txt1 = doc.createTextNode("Hello Goodbye"); 347 rootEl.appendChild(txt1); 348 349 txt1.splitText(6); 350 rootEl.normalize(); 351 352 } 353 354 355 356 360 { 361 379 } 380 381 382 383 387 { 388 NamedNodeMap nnm = null; 389 Assertion.assertTrue(nnm == null); 390 391 Document doc = new DocumentImpl(); 392 nnm = doc.getAttributes(); Assertion.assertTrue(nnm == null); 395 Assertion.assertTrue(!(nnm != null)); 396 397 Element el = doc.createElement("NamedNodeMap01"); 398 NamedNodeMap nnm2 = el.getAttributes(); Assertion.assertTrue(nnm2 != null); 400 Assertion.assertTrue(nnm != nnm2); 401 nnm = nnm2; 402 Assertion.assertTrue(nnm == nnm2); 403 } 404 405 406 407 411 { 412 Document doc1 = new DocumentImpl(); 413 Document doc2 = new DocumentImpl(); 414 415 Element el1 = doc1.createElement("abc"); 416 doc1.appendChild(el1); 417 Assertion.assertTrue(el1.getParentNode() != null); 418 el1.setAttribute("foo", "foovalue"); 419 Node el2 = doc2.importNode(el1, true); 420 Assertion.assertTrue(el2.getParentNode() == null); 421 String tagName = el2.getNodeName(); 422 Assertion.equals(tagName, "abc"); 423 Assertion.assertTrue(el2.getOwnerDocument() == doc2); 424 Assertion.equals(((Element ) el2).getAttribute("foo"), "foovalue"); 425 Assertion.assertTrue(doc1 != doc2); 426 } 427 428 429 438 { 439 Document doc = new DocumentImpl(); 440 Text tx = doc.createTextNode("Hello"); 441 Element el = doc.createElement("abc"); 442 el.appendChild(tx); 443 444 int textLength = tx.getLength(); 445 Assertion.assertTrue(textLength == 5); 446 447 NodeList nl = tx.getChildNodes(); 448 int nodeListLen = nl.getLength(); 449 Assertion.assertTrue(nodeListLen == 0); 450 451 nl = el.getChildNodes(); 452 nodeListLen = nl.getLength(); 453 Assertion.assertTrue(nodeListLen == 1); 454 } 455 456 457 461 { 462 NodeList nl = null; 463 NodeList nl2 = null; 464 Assertion.assertTrue(nl == null); 465 Assertion.assertTrue(!(nl != null)); 466 Assertion.assertTrue(nl == nl2); 467 468 Document doc = new DocumentImpl(); 469 nl = doc.getChildNodes(); 471 Assertion.assertTrue(nl != null); 472 int len = nl.getLength(); 473 Assertion.assertTrue(len == 0); 474 475 Element el = doc.createElement("NodeList01"); 476 doc.appendChild(el); 477 len = nl.getLength(); 478 Assertion.assertTrue(len == 1); 479 Assertion.assertTrue(nl != nl2); 480 nl2 = nl; 481 Assertion.assertTrue(nl == nl2); 482 } 483 484 485 486 487 491 { 492 Document doc = new DocumentImpl(); 493 Assertion.assertTrue(DOMExceptionsTest(doc, "createElement", 494 new Class []{String .class}, 495 new Object []{"!@@ bad element name"}, 496 DOMException.INVALID_CHARACTER_ERR)); 497 } 498 499 500 501 505 { 506 Document doc = new DocumentImpl(); 507 Element el = doc.createElement("NodeList01"); 508 doc.appendChild(el); 509 510 Element n1, n2, n3; 511 512 n1 = n2 = n3 = el; 513 Assertion.assertTrue(n1 == n2); 514 Assertion.assertTrue(n1 == n3); 515 Assertion.assertTrue(n1 == el); 516 Assertion.assertTrue(n1 != null); 517 n1 = n2 = n3 = null; 518 Assertion.assertTrue(n1 == null); 519 } 520 521 522 523 529 { 530 Document doc = new DocumentImpl(); 531 Element root = doc.createElement("CTestRoot"); 532 root.setAttribute("CTestAttr", "CTestAttrValue"); 533 534 String s = root.getAttribute("CTestAttr"); 535 Assertion.equals(s, "CTestAttrValue"); 536 537 Element cloned = (Element )root.cloneNode(true); 538 Attr a = cloned.getAttributeNode("CTestAttr"); 539 Assertion.assertTrue(a != null); 540 s = a.getValue(); 541 Assertion.equals(s, "CTestAttrValue"); 542 a = null; 543 544 a = cloned.getAttributeNode("CTestAttr"); 545 Assertion.assertTrue(a != null); 546 s = a.getValue(); 547 Assertion.equals(s, "CTestAttrValue"); 548 549 } 550 551 552 553 557 558 562 { 563 DOMImplementation impl = DOMImplementationImpl.getDOMImplementation(); 564 Assertion.assertTrue(impl.hasFeature("XML", "2.0") == true); 565 Assertion.assertTrue(impl.hasFeature("XML", null) == true); 566 Assertion.assertTrue(impl.hasFeature("XML", "1.0") == true); 568 Assertion.assertTrue(impl.hasFeature("XML", "3.0") == false); 569 Assertion.assertTrue(impl.hasFeature("Traversal", null) == true); 570 571 572 Assertion.assertTrue(impl.hasFeature("HTML", null) == false); 573 Assertion.assertTrue(impl.hasFeature("Views", null) == false); 574 Assertion.assertTrue(impl.hasFeature("StyleSheets", null) == false); 575 Assertion.assertTrue(impl.hasFeature("CSS", null) == false); 576 Assertion.assertTrue(impl.hasFeature("CSS2", null) == false); 577 Assertion.assertTrue(impl.hasFeature("Events", null) == true); 578 Assertion.assertTrue(impl.hasFeature("UIEvents", null) == false); 579 Assertion.assertTrue(impl.hasFeature("MouseEvents", null) == false); 580 Assertion.assertTrue(impl.hasFeature("MutationEvents", null) == true); 581 Assertion.assertTrue(impl.hasFeature("HTMLEvents", null) == false); 582 Assertion.assertTrue(impl.hasFeature("Range", null) == false); 583 } 584 585 586 587 591 { 592 DOMImplementation impl = DOMImplementationImpl.getDOMImplementation(); 593 594 String qName = "foo:docName"; 595 String pubId = "pubId"; 596 String sysId = "http://sysId"; 597 598 DocumentType dt = impl.createDocumentType(qName, pubId, sysId); 599 600 Assertion.assertTrue(dt != null); 601 Assertion.assertTrue(dt.getNodeType() == Node.DOCUMENT_TYPE_NODE); 602 Assertion.equals(dt.getNodeName(), qName); 603 Assertion.assertTrue(dt.getNamespaceURI() == null); 604 Assertion.assertTrue(dt.getPrefix() == null); 605 Assertion.assertTrue(dt.getLocalName() == null); 606 Assertion.equals(dt.getPublicId(), pubId); 607 Assertion.equals(dt.getSystemId(), sysId); 608 Assertion.assertTrue(dt.getInternalSubset() == null); 609 Assertion.assertTrue(dt.getOwnerDocument() == null); 610 611 NamedNodeMap nnm = dt.getEntities(); 612 Assertion.assertTrue(nnm.getLength() == 0); 613 nnm = dt.getNotations(); 614 Assertion.assertTrue(nnm.getLength() == 0); 615 616 qName = "docName"; 620 dt = impl.createDocumentType(qName, pubId, sysId); 621 622 Assertion.assertTrue(dt != null); 623 Assertion.assertTrue(dt.getNodeType() == Node.DOCUMENT_TYPE_NODE); 624 Assertion.equals(dt.getNodeName(), qName); 625 Assertion.assertTrue(dt.getNamespaceURI() == null); 626 Assertion.assertTrue(dt.getPrefix() == null); 627 Assertion.assertTrue(dt.getLocalName() == null); 628 Assertion.equals(dt.getPublicId(), pubId); 629 Assertion.equals(dt.getSystemId(), sysId); 630 Assertion.assertTrue(dt.getInternalSubset() == null); 631 Assertion.assertTrue(dt.getOwnerDocument() == null); 632 633 Assertion.assertTrue(DOMExceptionsTest(impl, "createDocumentType", 635 new Class []{String .class, String .class, String .class}, 636 new Object []{"<docName", pubId, sysId}, 637 DOMException.INVALID_CHARACTER_ERR)); 638 Assertion.assertTrue(DOMExceptionsTest(impl, "createDocumentType", 639 new Class []{String .class, String .class, String .class}, 640 new Object []{":docName", pubId, sysId}, 641 DOMException.NAMESPACE_ERR)); 642 Assertion.assertTrue(DOMExceptionsTest(impl, "createDocumentType", 643 new Class []{String .class, String .class, String .class}, 644 new Object []{"docName:", pubId, sysId}, 645 DOMException.NAMESPACE_ERR)); 646 Assertion.assertTrue(DOMExceptionsTest(impl, "createDocumentType", 647 new Class []{String .class, String .class, String .class}, 648 new Object []{"<doc::Name", pubId, sysId}, 649 DOMException.INVALID_CHARACTER_ERR)); 650 Assertion.assertTrue(DOMExceptionsTest(impl, "createDocumentType", 651 new Class []{String .class, String .class, String .class}, 652 new Object []{"<doc:N:ame", pubId, sysId}, 653 DOMException.INVALID_CHARACTER_ERR)); 654 } 655 656 660 { 661 DOMImplementation impl = DOMImplementationImpl.getDOMImplementation(); 662 663 String qName = "foo:docName"; 664 String pubId = "pubId"; 665 String sysId = "http://sysId"; 666 667 DocumentType dt = impl.createDocumentType(qName, pubId, sysId); 668 669 String docNSURI = "http://document.namespace"; 670 Document doc = impl.createDocument(docNSURI, qName, dt); 671 672 Assertion.assertTrue(dt.getOwnerDocument() == doc); 673 Assertion.assertTrue(doc.getOwnerDocument() == null); 674 675 Assertion.assertTrue(doc.getNodeType() == Node.DOCUMENT_NODE); 676 Assertion.assertTrue(doc.getDoctype() == dt); 677 Assertion.equals(doc.getNodeName(), "#document"); 678 Assertion.assertTrue(doc.getNodeValue() == null); 679 680 Element el = doc.getDocumentElement(); 681 682 Assertion.equals(el.getLocalName(), "docName"); 683 Assertion.equals(el.getNamespaceURI(), docNSURI); 684 Assertion.equals(el.getNodeName(), qName); 685 Assertion.assertTrue(el.getOwnerDocument() == doc); 686 Assertion.assertTrue(el.getParentNode() == doc); 687 Assertion.equals(el.getPrefix(), "foo"); 688 Assertion.equals(el.getTagName(), qName); 689 Assertion.assertTrue(el.hasChildNodes() == false); 690 691 Assertion.assertTrue(DOMExceptionsTest(impl, "createDocument", 695 new Class []{String .class, 696 String .class, 697 DocumentType .class}, 698 new Object []{docNSURI, qName, dt}, 699 DOMException.WRONG_DOCUMENT_ERR)); 700 701 } 703 704 705 709 { 710 711 DOMImplementation impl = DOMImplementationImpl.getDOMImplementation(); 714 715 String qName = "foo:docName"; 716 String pubId = "pubId"; 717 String sysId = "http://sysId"; 718 DocumentType dt = impl.createDocumentType(qName, pubId, sysId); 719 720 String docNSURI = "http://document.namespace"; 721 Document doc = impl.createDocument(docNSURI, qName, dt); 722 Element rootEl = doc.getDocumentElement(); 723 724 Element ela = doc.createElementNS("http://nsa", "a:ela"); Element elb = doc.createElementNS("http://nsb", "elb"); Element elc = doc.createElementNS(null, "elc"); 731 rootEl.appendChild(ela); 732 rootEl.appendChild(elb); 733 rootEl.appendChild(elc); 734 735 Assertion.equals(ela.getNodeName(), "a:ela"); 736 Assertion.equals(ela.getNamespaceURI(), "http://nsa"); 737 Assertion.equals(ela.getPrefix(), "a"); 738 Assertion.equals(ela.getLocalName(), "ela"); 739 Assertion.equals(ela.getTagName(), "a:ela"); 740 741 Assertion.equals(elb.getNodeName(), "elb"); 742 Assertion.equals(elb.getNamespaceURI(), "http://nsb"); 743 Assertion.assertTrue(elb.getPrefix() == null); 744 Assertion.equals(elb.getLocalName(), "elb"); 745 Assertion.equals(elb.getTagName(), "elb"); 746 747 Assertion.equals(elc.getNodeName(), "elc"); 748 Assertion.assertTrue(elc.getNamespaceURI() == null); 749 Assertion.assertTrue(elc.getPrefix() == null); 750 Assertion.equals(elc.getLocalName(), "elc"); 751 Assertion.equals(elc.getTagName(), "elc"); 752 753 Assertion.assertTrue(DOMExceptionsTest(doc, "createElementNS", 755 new Class []{String .class, String .class}, 756 new Object []{"http://nsa", "<a"}, 757 DOMException.INVALID_CHARACTER_ERR)); 758 Assertion.assertTrue(DOMExceptionsTest(doc, "createElementNS", 759 new Class []{String .class, String .class}, 760 new Object []{"http://nsa", ":a"}, 761 DOMException.NAMESPACE_ERR)); 762 Assertion.assertTrue(DOMExceptionsTest(doc, "createElementNS", 763 new Class []{String .class, String .class}, 764 new Object []{"http://nsa", "a:"}, 765 DOMException.NAMESPACE_ERR)); 766 Assertion.assertTrue(DOMExceptionsTest(doc, "createElementNS", 767 new Class []{String .class, String .class}, 768 new Object []{"http://nsa", "a::a"}, 769 DOMException.NAMESPACE_ERR)); 770 Assertion.assertTrue(DOMExceptionsTest(doc, "createElementNS", 771 new Class []{String .class, String .class}, 772 new Object []{"http://nsa", "a:a:a"}, 773 DOMException.NAMESPACE_ERR)); 774 775 String xmlURI = "http://www.w3.org/XML/1998/namespace"; 777 Assertion.equals(doc.createElementNS(xmlURI, "xml:a").getNamespaceURI(), xmlURI); 778 Assertion.assertTrue(DOMExceptionsTest(doc, "createElementNS", 779 new Class []{String .class, String .class}, 780 new Object []{"http://nsa", "xml:a"}, 781 DOMException.NAMESPACE_ERR)); 782 Assertion.assertTrue(DOMExceptionsTest(doc, "createElementNS", 783 new Class []{String .class, String .class}, 784 new Object []{"", "xml:a"}, 785 DOMException.NAMESPACE_ERR)); 786 Assertion.assertTrue(DOMExceptionsTest(doc, "createElementNS", 787 new Class []{String .class, String .class}, 788 new Object []{null, "xml:a"}, 789 DOMException.NAMESPACE_ERR)); 790 791 Assertion.equals(doc.createElementNS("http://nsa", "xmlns").getNamespaceURI(), "http://nsa"); 793 Assertion.equals(doc.createElementNS(xmlURI, "xmlns").getNamespaceURI(), xmlURI); 794 Assertion.equals(doc.createElementNS("", "xmlns").getNamespaceURI(), ""); 795 Assertion.assertTrue(doc.createElementNS(null, "xmlns").getNamespaceURI() == null); 796 797 Assertion.equals(doc.createElementNS("http://nsa", "xmlns:a").getNamespaceURI(), "http://nsa"); 800 Assertion.equals(doc.createElementNS(xmlURI, "xmlns:a").getNamespaceURI(), xmlURI); 801 Assertion.assertTrue(DOMExceptionsTest(doc, "createElementNS", 802 new Class []{String .class, String .class}, 803 new Object []{null, "xmlns:a"}, 804 DOMException.NAMESPACE_ERR)); 805 806 Assertion.equals(doc.createElementNS("http://nsa", "foo:a").getNamespaceURI(), "http://nsa"); 808 Assertion.assertTrue(DOMExceptionsTest(doc, "createElementNS", 809 new Class []{String .class, String .class}, 810 new Object []{null, "foo:a"}, 811 DOMException.NAMESPACE_ERR)); 812 813 Element elem = doc.createElementNS("http://nsa", "foo:a"); 815 elem.setPrefix("bar"); 816 Assertion.equals(elem.getNodeName(), "bar:a"); 817 Assertion.equals(elem.getNamespaceURI(), "http://nsa"); 818 Assertion.equals(elem.getPrefix(), "bar"); 819 Assertion.equals(elem.getLocalName(), "a"); 820 Assertion.equals(elem.getTagName(), "bar:a"); 821 elem = doc.createElementNS("http://nsa", "a"); 823 Assertion.equals(elem.getPrefix(), null); 824 elem.setPrefix("bar"); 825 Assertion.equals(elem.getNodeName(), "bar:a"); 826 Assertion.equals(elem.getNamespaceURI(), "http://nsa"); 827 Assertion.equals(elem.getPrefix(), "bar"); 828 Assertion.equals(elem.getLocalName(), "a"); 829 Assertion.equals(elem.getTagName(), "bar:a"); 830 elem = doc.createElementNS(xmlURI, "foo:a"); 832 elem.setPrefix("xml"); 833 elem = doc.createElementNS("http://nsa", "foo:a"); 834 Assertion.assertTrue(DOMExceptionsTest(elem, "setPrefix", 835 new Class []{String .class}, 836 new Object []{"xml"}, 837 DOMException.NAMESPACE_ERR)); 838 elem.setPrefix("xmlns"); 840 elem = doc.createElementNS(null, "a"); 842 Assertion.assertTrue(DOMExceptionsTest(elem, "setPrefix", 843 new Class []{String .class}, 844 new Object []{"foo"}, 845 DOMException.NAMESPACE_ERR)); 846 847 Assertion.assertTrue(DOMExceptionsTest(doc, "setPrefix", 849 new Class []{String .class}, 850 new Object []{"foo"}, 851 DOMException.NAMESPACE_ERR)); 852 853 } 856 857 858 859 860 864 { 865 866 DOMImplementation impl = DOMImplementationImpl.getDOMImplementation(); 869 870 String qName = "foo:docName"; 871 String pubId = "pubId"; 872 String sysId = "http://sysId"; 873 DocumentType dt = impl.createDocumentType(qName, pubId, sysId); 874 875 String docNSURI = "http://document.namespace"; 876 Document doc = impl.createDocument(docNSURI, qName, dt); 877 Element rootEl = doc.getDocumentElement(); 878 879 Attr attra = doc.createAttributeNS("http://nsa", "a:attra"); Attr attrb = doc.createAttributeNS("http://nsb", "attrb"); Attr attrc = doc.createAttributeNS(null, "attrc"); 886 Assertion.equals(attra.getNodeName(), "a:attra"); 887 Assertion.equals(attra.getNamespaceURI(), "http://nsa"); 888 Assertion.equals(attra.getPrefix(), "a"); 889 Assertion.equals(attra.getLocalName(), "attra"); 890 Assertion.equals(attra.getName(), "a:attra"); 891 Assertion.assertTrue(attra.getOwnerElement() == null); 892 893 Assertion.equals(attrb.getNodeName(), "attrb"); 894 Assertion.equals(attrb.getNamespaceURI(), "http://nsb"); 895 Assertion.equals(attrb.getPrefix(), null); 896 Assertion.equals(attrb.getLocalName(), "attrb"); 897 Assertion.equals(attrb.getName(), "attrb"); 898 Assertion.assertTrue(attrb.getOwnerElement() == null); 899 900 Assertion.equals(attrc.getNodeName(), "attrc"); 901 Assertion.assertTrue(attrc.getNamespaceURI() == null); 902 Assertion.assertTrue(attrc.getPrefix() == null); 903 Assertion.equals(attrc.getLocalName(), "attrc"); 904 Assertion.equals(attrc.getName(), "attrc"); 905 Assertion.assertTrue(attrc.getOwnerElement() == null); 906 907 908 Assertion.assertTrue(DOMExceptionsTest(doc, "createAttributeNS", 910 new Class []{String .class, String .class}, 911 new Object []{"http://nsa", "<a"}, 912 DOMException.INVALID_CHARACTER_ERR)); 913 Assertion.assertTrue(DOMExceptionsTest(doc, "createAttributeNS", 914 new Class []{String .class, String .class}, 915 new Object []{"http://nsa", ":a"}, 916 DOMException.NAMESPACE_ERR)); 917 Assertion.assertTrue(DOMExceptionsTest(doc, "createAttributeNS", 918 new Class []{String .class, String .class}, 919 new Object []{"http://nsa", "a:"}, 920 DOMException.NAMESPACE_ERR)); 921 Assertion.assertTrue(DOMExceptionsTest(doc, "createAttributeNS", 922 new Class []{String .class, String .class}, 923 new Object []{"http://nsa", "a::a"}, 924 DOMException.NAMESPACE_ERR)); 925 Assertion.assertTrue(DOMExceptionsTest(doc, "createAttributeNS", 926 new Class []{String .class, String .class}, 927 new Object []{"http://nsa", "a:a:a"}, 928 DOMException.NAMESPACE_ERR)); 929 930 String xmlURI = "http://www.w3.org/XML/1998/namespace"; 932 Assertion.equals(doc.createAttributeNS(xmlURI, "xml:a").getNamespaceURI(), xmlURI); 933 Assertion.assertTrue(DOMExceptionsTest(doc, "createAttributeNS", 934 new Class []{String .class, String .class}, 935 new Object []{"http://nsa", "xml:a"}, 936 DOMException.NAMESPACE_ERR)); 937 Assertion.assertTrue(DOMExceptionsTest(doc, "createAttributeNS", 938 new Class []{String .class, String .class}, 939 new Object []{"", "xml:a"}, 940 DOMException.NAMESPACE_ERR)); 941 Assertion.assertTrue(DOMExceptionsTest(doc, "createAttributeNS", 942 new Class []{String .class, String .class}, 943 new Object []{null, "xml:a"}, 944 DOMException.NAMESPACE_ERR)); 945 946 String xmlnsURI = "http://www.w3.org/2000/xmlns/"; 948 Assertion.equals(doc.createAttributeNS(xmlnsURI, "xmlns").getNamespaceURI(), xmlnsURI); 949 Assertion.assertTrue(DOMExceptionsTest(doc, "createAttributeNS", 950 new Class []{String .class, String .class}, 951 new Object []{"http://nsa", "xmlns"}, 952 DOMException.NAMESPACE_ERR)); 953 Assertion.assertTrue(DOMExceptionsTest(doc, "createAttributeNS", 954 new Class []{String .class, String .class}, 955 new Object []{xmlURI, "xmlns"}, 956 DOMException.NAMESPACE_ERR)); 957 Assertion.assertTrue(DOMExceptionsTest(doc, "createAttributeNS", 958 new Class []{String .class, String .class}, 959 new Object []{"", "xmlns"}, 960 DOMException.NAMESPACE_ERR)); 961 Assertion.assertTrue(DOMExceptionsTest(doc, "createAttributeNS", 962 new Class []{String .class, String .class}, 963 new Object []{null, "xmlns"}, 964 DOMException.NAMESPACE_ERR)); 965 966 Assertion.equals(doc.createAttributeNS(xmlnsURI, "xmlns:a").getNamespaceURI(), xmlnsURI); 968 Assertion.assertTrue(DOMExceptionsTest(doc, "createAttributeNS", 969 new Class []{String .class, String .class}, 970 new Object []{"http://nsa", "xmlns:a"}, 971 DOMException.NAMESPACE_ERR)); 972 Assertion.assertTrue(DOMExceptionsTest(doc, "createAttributeNS", 973 new Class []{String .class, String .class}, 974 new Object []{xmlURI, "xmlns:a"}, 975 DOMException.NAMESPACE_ERR)); 976 Assertion.assertTrue(DOMExceptionsTest(doc, "createAttributeNS", 977 new Class []{String .class, String .class}, 978 new Object []{"", "xmlns:a"}, 979 DOMException.NAMESPACE_ERR)); 980 Assertion.assertTrue(DOMExceptionsTest(doc, "createAttributeNS", 981 new Class []{String .class, String .class}, 982 new Object []{null, "xmlns:a"}, 983 DOMException.NAMESPACE_ERR)); 984 985 Assertion.equals(doc.createAttributeNS("http://nsa", "foo:a").getNamespaceURI(), "http://nsa"); 987 Assertion.assertTrue(DOMExceptionsTest(doc, "createAttributeNS", 988 new Class []{String .class, String .class}, 989 new Object []{null, "foo:a"}, 990 DOMException.NAMESPACE_ERR)); 991 992 Attr attr = doc.createAttributeNS("http://nsa", "foo:a"); 994 attr.setPrefix("bar"); 995 Assertion.equals(attr.getNodeName(), "bar:a"); 996 Assertion.equals(attr.getNamespaceURI(), "http://nsa"); 997 Assertion.equals(attr.getPrefix(), "bar"); 998 Assertion.equals(attr.getLocalName(), "a"); 999 Assertion.equals(attr.getName(), "bar:a"); 1000 attr = doc.createAttributeNS("http://nsa", "a"); 1002 Assertion.assertTrue(attr.getPrefix() == null); 1003 attr.setPrefix("bar"); 1004 Assertion.equals(attr.getNodeName(), "bar:a"); 1005 Assertion.equals(attr.getNamespaceURI(), "http://nsa"); 1006 Assertion.equals(attr.getPrefix(), "bar"); 1007 Assertion.equals(attr.getLocalName(), "a"); 1008 Assertion.equals(attr.getName(), "bar:a"); 1009 attr = doc.createAttributeNS(xmlURI, "foo:a"); 1011 attr.setPrefix("xml"); 1012 attr = doc.createAttributeNS("http://nsa", "foo:a"); 1013 Assertion.assertTrue(DOMExceptionsTest(attr, "setPrefix", 1014 new Class []{String .class}, 1015 new Object []{"xml"}, 1016 DOMException.NAMESPACE_ERR)); 1017 attr = doc.createAttributeNS(xmlnsURI, "foo:a"); 1019 attr.setPrefix("xmlns"); 1020 attr = doc.createAttributeNS("http://nsa", "foo:a"); 1021 Assertion.assertTrue(DOMExceptionsTest(attr, "setPrefix", 1022 new Class []{String .class}, 1023 new Object []{"xmlns"}, 1024 DOMException.NAMESPACE_ERR)); 1025 attr = doc.createAttributeNS(xmlnsURI, "xmlns"); 1027 Assertion.assertTrue(DOMExceptionsTest(attr, "setPrefix", 1028 new Class []{String .class}, 1029 new Object []{"xml"}, 1030 DOMException.NAMESPACE_ERR)); 1031 attr = doc.createAttributeNS(null, "a"); 1033 Assertion.assertTrue(DOMExceptionsTest(attr, "setPrefix", 1034 new Class []{String .class}, 1035 new Object []{"foo"}, 1036 DOMException.NAMESPACE_ERR)); 1037 1038 Assertion.assertTrue(DOMExceptionsTest(attr, "setPrefix", 1040 new Class []{String .class}, 1041 new Object []{"foo"}, 1042 DOMException.NAMESPACE_ERR)); 1043 1044 } 1047 1048 1049 1053 { 1054 1055 DOMImplementation impl = DOMImplementationImpl.getDOMImplementation(); 1058 1059 String qName = "foo:docName"; 1060 String pubId = "pubId"; 1061 String sysId = "http://sysId"; 1062 DocumentType dt = impl.createDocumentType(qName, pubId, sysId); 1063 1064 String docNSURI = "http://document.namespace"; 1065 Document doc = impl.createDocument(docNSURI, qName, dt); 1066 Element rootEl = doc.getDocumentElement(); 1067 1068 Element ela = doc.createElementNS("http://nsa", "a:ela"); 1072 rootEl.appendChild(ela); 1073 Element elb = doc.createElementNS("http://nsb", "elb"); 1074 rootEl.appendChild(elb); 1075 Element elc = doc.createElementNS(null, "elc"); 1076 rootEl.appendChild(elc); 1077 Element eld = doc.createElementNS("http://nsa", "d:ela"); 1078 rootEl.appendChild(eld); 1079 Element ele = doc.createElementNS("http://nse", "elb"); 1080 rootEl.appendChild(ele); 1081 1082 1083 1087 NodeList nl = doc.getElementsByTagName("a:ela"); 1088 Assertion.assertTrue(nl.getLength() == 1); 1089 Assertion.assertTrue(nl.item(0) == ela); 1090 1091 nl = doc.getElementsByTagName("elb"); 1092 Assertion.assertTrue(nl.getLength() == 2); 1093 Assertion.assertTrue(nl.item(0) == elb); 1094 Assertion.assertTrue(nl.item(1) == ele); 1095 1096 nl = doc.getElementsByTagName("d:ela"); 1097 Assertion.assertTrue(nl.getLength() == 1); 1098 Assertion.assertTrue(nl.item(0) == eld); 1099 1100 1104 nl = doc.getElementsByTagNameNS(null, "elc"); 1105 Assertion.assertTrue(nl.getLength() == 1); 1106 Assertion.assertTrue(nl.item(0) == elc); 1107 1108 nl = doc.getElementsByTagNameNS("http://nsa", "ela"); 1109 Assertion.assertTrue(nl.getLength() == 2); 1110 Assertion.assertTrue(nl.item(0) == ela); 1111 Assertion.assertTrue(nl.item(1) == eld); 1112 1113 nl = doc.getElementsByTagNameNS(null, "elb"); 1114 Assertion.assertTrue(nl.getLength() == 0); 1115 1116 nl = doc.getElementsByTagNameNS("http://nsb", "elb"); 1117 Assertion.assertTrue(nl.getLength() == 1); 1118 Assertion.assertTrue(nl.item(0) == elb); 1119 1120 nl = doc.getElementsByTagNameNS("*", "elb"); 1121 Assertion.assertTrue(nl.getLength() == 2); 1122 Assertion.assertTrue(nl.item(0) == elb); 1123 Assertion.assertTrue(nl.item(1) == ele); 1124 1125 nl = doc.getElementsByTagNameNS("http://nsa", "*"); 1126 Assertion.assertTrue(nl.getLength() == 2); 1127 Assertion.assertTrue(nl.item(0) == ela); 1128 Assertion.assertTrue(nl.item(1) == eld); 1129 1130 nl = doc.getElementsByTagNameNS("*", "*"); 1131 Assertion.assertTrue(nl.getLength() == 6); 1133 Assertion.assertTrue(nl.item(6) == null); 1134 1136 nl = rootEl.getElementsByTagNameNS("*", "*"); 1137 Assertion.assertTrue(nl.getLength() == 5); 1138 1139 1140 nl = doc.getElementsByTagNameNS("http://nsa", "d:ela"); 1141 Assertion.assertTrue(nl.getLength() == 0); 1142 1143 1144 1148 nl = doc.getElementsByTagNameNS("*", "*"); 1149 NodeList nla = ela.getElementsByTagNameNS("*", "*"); 1150 1151 Assertion.assertTrue(nl.getLength() == 6); 1152 Assertion.assertTrue(nla.getLength() == 0); 1153 1154 rootEl.removeChild(elc); 1155 Assertion.assertTrue(nl.getLength() == 5); 1156 Assertion.assertTrue(nla.getLength() == 0); 1157 1158 ela.appendChild(elc); 1159 Assertion.assertTrue(nl.getLength() == 6); 1160 Assertion.assertTrue(nla.getLength() == 1); 1161 } 1162 1163 1164 { 1168 1169 DOMImplementation impl = DOMImplementationImpl.getDOMImplementation(); 1172 1173 String qName = "foo:docName"; 1174 String pubId = "pubId"; 1175 String sysId = "http://sysId"; 1176 DocumentType dt = impl.createDocumentType(qName, pubId, sysId); 1177 1178 String docNSURI = "http://document.namespace"; 1179 Document doc = impl.createDocument(docNSURI, qName, dt); 1180 Element rootEl = doc.getDocumentElement(); 1181 1182 Attr attra = doc.createAttributeNS("http://nsa", "a:attra"); 1186 rootEl.setAttributeNodeNS(attra); 1187 Attr attrb = doc.createAttributeNS("http://nsb", "attrb"); 1188 rootEl.setAttributeNodeNS(attrb); 1189 Attr attrc = doc.createAttributeNS(null, "attrc"); 1190 rootEl.setAttributeNodeNS(attrc); 1191 Attr attrd = doc.createAttributeNS("http://nsa", "d:attra"); 1192 rootEl.setAttributeNodeNS(attrd); 1193 Attr attre = doc.createAttributeNS("http://nse", "attrb"); 1194 rootEl.setAttributeNodeNS(attre); 1195 1196 Assertion.equals(attra.getNodeName(), "a:attra"); 1200 Assertion.equals(attra.getNamespaceURI(), "http://nsa"); 1201 Assertion.equals(attra.getLocalName(), "attra"); 1202 Assertion.equals(attra.getName(), "a:attra"); 1203 Assertion.assertTrue(attra.getNodeType() == Node.ATTRIBUTE_NODE); 1204 Assertion.equals(attra.getNodeValue(), ""); 1205 Assertion.equals(attra.getPrefix(), "a"); 1206 Assertion.assertTrue(attra.getSpecified() == true); 1207 Assertion.equals(attra.getValue(), ""); 1208 Assertion.assertTrue(attra.getOwnerElement() == null); 1209 1210 NamedNodeMap nnm = rootEl.getAttributes(); 1212 Assertion.assertTrue(nnm.getLength() == 4); 1213 Assertion.assertTrue(nnm.getNamedItemNS("http://nsa", "attra") == attrd); 1214 Assertion.assertTrue(nnm.getNamedItemNS("http://nsb", "attrb") == attrb); 1215 Assertion.assertTrue(nnm.getNamedItemNS("http://nse", "attrb") == attre); 1216 Assertion.assertTrue(nnm.getNamedItemNS(null, "attrc") == attrc); 1217 Assertion.assertTrue(nnm.getNamedItemNS(null, "attra") == null); 1218 Assertion.assertTrue(nnm.getNamedItemNS("http://nsa", "attrb") == null); 1219 } 1220 }; 1221} 1222 | Popular Tags |