KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dom > mem > Test


1 /* $Id: Test.java,v 1.2 2005/01/26 08:28:45 jkjome Exp $ */
2 /*
3  * The Apache Software License, Version 1.1
4  *
5  * Copyright (c) 1999-2000 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Xerces" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache\@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation, and was
52  * originally based on software copyright (c) 1999, International
53  * Business Machines, Inc., http://www.ibm.com . For more information
54  * on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 //
59
// Various DOM tests.
60
// Contents include
61
// 1. Basic functionality for DOMString
62
// 2. Regression tests for bugs fixed.
63
// All individual are wrapped in a memory leak checker.
64
//
65
// This is NOT a complete test of DOM functionality.
66
//
67

68 package dom.mem;
69 import java.lang.reflect.InvocationTargetException JavaDoc;
70 import java.lang.reflect.Method JavaDoc;
71
72 import org.enhydra.apache.xerces.dom.DOMImplementationImpl;
73 import org.enhydra.apache.xerces.dom.DocumentImpl;
74 import org.w3c.dom.Attr JavaDoc;
75 import org.w3c.dom.CDATASection JavaDoc;
76 import org.w3c.dom.Comment JavaDoc;
77 import org.w3c.dom.DOMException JavaDoc;
78 import org.w3c.dom.DOMImplementation JavaDoc;
79 import org.w3c.dom.Document JavaDoc;
80 import org.w3c.dom.DocumentFragment JavaDoc;
81 import org.w3c.dom.DocumentType JavaDoc;
82 import org.w3c.dom.Element JavaDoc;
83 import org.w3c.dom.EntityReference JavaDoc;
84 import org.w3c.dom.NamedNodeMap JavaDoc;
85 import org.w3c.dom.Node JavaDoc;
86 import org.w3c.dom.NodeList JavaDoc;
87 import org.w3c.dom.Notation JavaDoc;
88 import org.w3c.dom.ProcessingInstruction JavaDoc;
89 import org.w3c.dom.Text JavaDoc;
90
91 import dom.util.Assertion;
92
93 public class Test {
94
95     /**
96      * version 3.0 01/25/99
97      *
98      * @return boolean
99      * @param node java.lang.Object
100      * @param mNameIndex int
101      * @param signatureIndex int
102      * @param parameters java.lang.Object[]
103      * @param code short
104      *
105      * @author Philip W. Davis
106      */

107     public static boolean DOMExceptionsTest(Object JavaDoc node,
108                         String JavaDoc methodName,
109                         Class JavaDoc[] methodSignature,
110                         Object JavaDoc[] parameters,
111                         short code)
112     {
113     boolean asExpected = false;
114     Method JavaDoc method;
115
116     try {
117         method = node.getClass().getMethod(methodName,methodSignature);
118         method.invoke(node, parameters);
119     } catch(InvocationTargetException JavaDoc exc) {
120         Throwable JavaDoc realE = exc.getTargetException();
121         if(realE instanceof DOMException JavaDoc) {
122         asExpected = (((DOMException JavaDoc)realE).code== code);
123         if(!asExpected)
124             System.out.println("Wrong DOMException(" +
125                        ((DOMException JavaDoc)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 JavaDoc exc) {
134         System.out.println("test invocation failure (" + exc + ")");
135     }
136     return (asExpected);
137     }
138
139     public static void main(String JavaDoc argv[])
140     {
141     System.out.print("DOM Memory Test.\n");
142
143     //
144
// Test Doc01 Create a new empty document
145
//
146
{
147         Document JavaDoc doc;
148         doc = new DocumentImpl();
149     }
150     
151
152     //
153
// Test Doc02 Create one of each kind of node using the
154
// document createXXX methods.
155
// Watch for memory leaks.
156
//
157
{
158         // Do all operations in a preconditioning step, to force the
159
// creation of implementation objects that are set up on first use.
160
// Don't watch for leaks in this block (no / )
161
Document JavaDoc doc = new DocumentImpl();
162         Element JavaDoc el = doc.createElement("Doc02Element");
163         DocumentFragment JavaDoc frag = doc.createDocumentFragment ();
164         Text JavaDoc text = doc.createTextNode("Doc02TextNode");
165         Comment JavaDoc comment = doc.createComment("Doc02Comment");
166         CDATASection JavaDoc cdataSec = doc.createCDATASection("Doc02CDataSection");
167         DocumentType JavaDoc docType = doc.getImplementation().createDocumentType("Doc02DocumentType", null, null);
168         Notation JavaDoc notation = ((DocumentImpl) doc).createNotation("Doc02Notation");
169         ProcessingInstruction JavaDoc pi = doc.createProcessingInstruction("Doc02PITarget",
170                                     "Doc02PIData");
171         NodeList JavaDoc nodeList = doc.getElementsByTagName("*");
172     }
173
174
175     
176     {
177         Document JavaDoc doc = new DocumentImpl();
178         Element JavaDoc el = doc.createElement("Doc02Element");
179     }
180     
181
182     
183     {
184         Document JavaDoc doc = new DocumentImpl();
185         DocumentFragment JavaDoc frag = doc.createDocumentFragment ();
186     };
187     
188
189
190     
191     {
192         Document JavaDoc doc = new DocumentImpl();
193         Element JavaDoc el = doc.createElement("Doc02Element");
194     }
195     
196
197     
198     {
199         Document JavaDoc doc = new DocumentImpl();
200         Text JavaDoc text = doc.createTextNode("Doc02TextNode");
201     }
202     
203
204     
205     {
206         Document JavaDoc doc = new DocumentImpl();
207         Comment JavaDoc comment = doc.createComment("Doc02Comment");
208     }
209     
210
211     
212     {
213         Document JavaDoc doc = new DocumentImpl();
214         CDATASection JavaDoc cdataSec = doc.createCDATASection("Doc02CDataSection");
215     }
216     
217
218
219     
220     {
221         Document JavaDoc doc = new DocumentImpl();
222         DocumentType JavaDoc docType = doc.getImplementation().createDocumentType("Doc02DocumentType", null, null);
223     }
224     
225
226
227     
228     {
229         Document JavaDoc doc = new DocumentImpl();
230         Notation JavaDoc notation = ((DocumentImpl)doc).createNotation("Doc02Notation");
231     }
232     
233
234
235     
236     {
237         Document JavaDoc doc = new DocumentImpl();
238         ProcessingInstruction JavaDoc pi = doc.createProcessingInstruction("Doc02PITarget",
239                                     "Doc02PIData");
240     }
241     
242
243     
244     {
245         Document JavaDoc doc = new DocumentImpl();
246         Attr JavaDoc attribute = doc.createAttribute("Doc02Attribute");
247     }
248     
249
250
251     
252     {
253         Document JavaDoc doc = new DocumentImpl();
254         EntityReference JavaDoc er = doc.createEntityReference("Doc02EntityReference");
255     }
256     
257
258     
259     {
260         Document JavaDoc doc = new DocumentImpl();
261         NodeList JavaDoc nodeList = doc.getElementsByTagName("*");
262     }
263     
264
265     
266     //
267
// Doc03 - Create a small document tree
268
//
269

270     {
271         Document JavaDoc doc = new DocumentImpl();
272         Element JavaDoc rootEl = doc.createElement("Doc03RootElement");
273         doc.appendChild(rootEl);
274         Text JavaDoc textNode = doc.createTextNode("Doc03 text stuff");
275         rootEl.appendChild(textNode);
276
277         NodeList JavaDoc nodeList = doc.getElementsByTagName("*");
278     };
279     
280
281
282     //
283
// Attr01
284
//
285
{
286         Document JavaDoc doc = new DocumentImpl();
287         Element JavaDoc rootEl = doc.createElement("RootElement");
288         doc.appendChild(rootEl);
289         {
290             Attr JavaDoc attr01 = doc.createAttribute("Attr01");
291             rootEl.setAttributeNode(attr01);
292         }
293         
294         
295         {
296             Attr JavaDoc attr02 = doc.createAttribute("Attr01");
297             rootEl.setAttributeNode(attr02);
298         }
299         
300     };
301
302     //
303
// Attr02
304
//
305

306     {
307         Document JavaDoc doc = new DocumentImpl();
308         Element JavaDoc rootEl = doc.createElement("RootElement");
309         doc.appendChild(rootEl);
310         Attr JavaDoc attr01 = doc.createAttribute("Attr02");
311         rootEl.setAttributeNode(attr01);
312         Attr JavaDoc attr02 = doc.createAttribute("Attr02");
313         rootEl.setAttributeNode(attr02);
314     }
315     
316
317
318     //
319
// Attr03
320
//
321

322     {
323         Document JavaDoc doc = new DocumentImpl();
324         Element JavaDoc rootEl = doc.createElement("RootElement");
325         doc.appendChild(rootEl);
326         Attr JavaDoc attr01 = doc.createAttribute("Attr03");
327         rootEl.setAttributeNode(attr01);
328
329         attr01.setValue("Attr03Value1");
330         attr01.setValue("Attr03Value2");
331     }
332     
333
334
335
336     //
337
// Text01
338
//
339

340     {
341         Document JavaDoc doc = new DocumentImpl();
342         Element JavaDoc rootEl = doc.createElement("RootElement");
343         doc.appendChild(rootEl);
344
345
346         Text JavaDoc txt1 = doc.createTextNode("Hello Goodbye");
347         rootEl.appendChild(txt1);
348
349         txt1.splitText(6);
350         rootEl.normalize();
351
352     }
353     
354
355
356     //
357
// Notation01
358
//
359

360     {
361     /*
362         DOMImplementation impl = DOMImplementationImpl.getDOMImplementation();
363         DocumentType dt =
364       impl.createDocumentType("DocType_for_Notation01", null, null, null);
365         doc.appendChild(dt);
366
367
368         NamedNodeMap notationMap = dt.getNotations();
369         Notation nt1 = ((DocumentImpl) doc).createNotation("Notation01");
370         ((NotationImpl) nt1).setPublicId("Notation01PublicId");
371         notationMap.setNamedItem (nt1);
372         Notation nt2 = (Notation)notationMap.getNamedItem("Notation01");
373         Assertion.assert(nt1==nt2);
374         nt2 = new NotationImpl((DocumentImpl)doc, null);
375         nt1 = null;
376         nt2 = (Notation)notationMap.getNamedItem("Notation01");
377       
378     */

379     }
380     
381
382
383     //
384
// NamedNodeMap01 - comparison operators.
385
//
386

387     {
388         NamedNodeMap JavaDoc nnm = null;
389         Assertion.assertTrue(nnm == null);
390
391         Document JavaDoc doc = new DocumentImpl();
392         nnm = doc.getAttributes(); // Should be null, because node type
393
// is not Element.
394
Assertion.assertTrue(nnm == null);
395         Assertion.assertTrue(!(nnm != null));
396
397         Element JavaDoc el = doc.createElement("NamedNodeMap01");
398         NamedNodeMap JavaDoc nnm2 = el.getAttributes(); // Should be an empty, but non-null map.
399
Assertion.assertTrue(nnm2 != null);
400         Assertion.assertTrue(nnm != nnm2);
401         nnm = nnm2;
402         Assertion.assertTrue(nnm == nnm2);
403     }
404     
405
406
407     //
408
// importNode quick test
409
//
410

411     {
412         Document JavaDoc doc1 = new DocumentImpl();
413         Document JavaDoc doc2 = new DocumentImpl();
414         
415         Element JavaDoc el1 = doc1.createElement("abc");
416         doc1.appendChild(el1);
417         Assertion.assertTrue(el1.getParentNode() != null);
418         el1.setAttribute("foo", "foovalue");
419         Node JavaDoc el2 = doc2.importNode(el1, true);
420         Assertion.assertTrue(el2.getParentNode() == null);
421         String JavaDoc tagName = el2.getNodeName();
422         Assertion.equals(tagName, "abc");
423         Assertion.assertTrue(el2.getOwnerDocument() == doc2);
424         Assertion.equals(((Element JavaDoc) el2).getAttribute("foo"), "foovalue");
425         Assertion.assertTrue(doc1 != doc2);
426     }
427     
428
429     //
430
// getLength() tests. Both Node CharacterData and NodeList implement
431
// getLength(). Early versions of the DOM had a clash
432
// between the two, originating in the implementation class
433
// hirearchy, which has NodeList as a (distant) base class
434
// of CharacterData. This is a regression test to verify
435
// that the problem stays fixed.
436
//
437

438     {
439         Document JavaDoc doc = new DocumentImpl();
440         Text JavaDoc tx = doc.createTextNode("Hello");
441         Element JavaDoc el = doc.createElement("abc");
442         el.appendChild(tx);
443
444         int textLength = tx.getLength();
445         Assertion.assertTrue(textLength == 5);
446
447         NodeList JavaDoc 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     //
458
// NodeList - comparison operators, basic operation.
459
//
460

461     {
462         NodeList JavaDoc nl = null;
463         NodeList JavaDoc nl2 = null;
464         Assertion.assertTrue(nl == null);
465         Assertion.assertTrue(!(nl != null));
466         Assertion.assertTrue(nl == nl2);
467
468         Document JavaDoc doc = new DocumentImpl();
469         nl = doc.getChildNodes(); // Should be non-null, but empty
470

471         Assertion.assertTrue(nl != null);
472         int len = nl.getLength();
473         Assertion.assertTrue(len == 0);
474
475         Element JavaDoc 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     //
488
// Name validity checking.
489
//
490

491     {
492          Document JavaDoc doc = new DocumentImpl();
493          Assertion.assertTrue(DOMExceptionsTest(doc, "createElement",
494                       new Class JavaDoc[]{String JavaDoc.class},
495                       new Object JavaDoc[]{"!@@ bad element name"},
496                       DOMException.INVALID_CHARACTER_ERR));
497     }
498     
499
500
501     //
502
// Assignment ops return value
503
//
504

505     {
506         Document JavaDoc doc = new DocumentImpl();
507         Element JavaDoc el = doc.createElement("NodeList01");
508         doc.appendChild(el);
509         
510         Element JavaDoc 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     //
524
// Cloning of a node with attributes. Regression test for a ref counting
525
// bug in attributes of cloned nodes that occured when the "owned" flag
526
// was not set in the clone.
527
//
528

529     {
530         Document JavaDoc doc = new DocumentImpl();
531         Element JavaDoc root = doc.createElement("CTestRoot");
532         root.setAttribute("CTestAttr", "CTestAttrValue");
533
534         String JavaDoc s = root.getAttribute("CTestAttr");
535         Assertion.equals(s, "CTestAttrValue");
536
537         Element JavaDoc cloned = (Element JavaDoc)root.cloneNode(true);
538         Attr JavaDoc 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     //
554
// DOM Level 2 tests. These should be split out as a separate test.
555
//
556

557
558     //
559
// hasFeature. The set of supported options tested here is for Xerces 1.1
560
//
561

562     {
563         DOMImplementation JavaDoc impl = DOMImplementationImpl.getDOMImplementation();
564         Assertion.assertTrue(impl.hasFeature("XML", "2.0") == true);
565         Assertion.assertTrue(impl.hasFeature("XML", null) == true);
566         // We also support 1.0
567
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     //
588
// CreateDocumentType
589
//
590

591     {
592         DOMImplementation JavaDoc impl = DOMImplementationImpl.getDOMImplementation();
593         
594         String JavaDoc qName = "foo:docName";
595         String JavaDoc pubId = "pubId";
596         String JavaDoc sysId = "http://sysId";
597         
598         DocumentType JavaDoc 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 JavaDoc nnm = dt.getEntities();
612         Assertion.assertTrue(nnm.getLength() == 0);
613         nnm = dt.getNotations();
614         Assertion.assertTrue(nnm.getLength() == 0);
615
616         //
617
// Qualified name without prefix should also work.
618
//
619
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         // Creating a DocumentType with invalid or malformed qName should fail.
634
Assertion.assertTrue(DOMExceptionsTest(impl, "createDocumentType",
635             new Class JavaDoc[]{String JavaDoc.class, String JavaDoc.class, String JavaDoc.class},
636             new Object JavaDoc[]{"<docName", pubId, sysId},
637             DOMException.INVALID_CHARACTER_ERR));
638         Assertion.assertTrue(DOMExceptionsTest(impl, "createDocumentType",
639             new Class JavaDoc[]{String JavaDoc.class, String JavaDoc.class, String JavaDoc.class},
640             new Object JavaDoc[]{":docName", pubId, sysId},
641             DOMException.NAMESPACE_ERR));
642         Assertion.assertTrue(DOMExceptionsTest(impl, "createDocumentType",
643             new Class JavaDoc[]{String JavaDoc.class, String JavaDoc.class, String JavaDoc.class},
644             new Object JavaDoc[]{"docName:", pubId, sysId},
645             DOMException.NAMESPACE_ERR));
646         Assertion.assertTrue(DOMExceptionsTest(impl, "createDocumentType",
647             new Class JavaDoc[]{String JavaDoc.class, String JavaDoc.class, String JavaDoc.class},
648             new Object JavaDoc[]{"<doc::Name", pubId, sysId},
649             DOMException.INVALID_CHARACTER_ERR));
650         Assertion.assertTrue(DOMExceptionsTest(impl, "createDocumentType",
651             new Class JavaDoc[]{String JavaDoc.class, String JavaDoc.class, String JavaDoc.class},
652             new Object JavaDoc[]{"<doc:N:ame", pubId, sysId},
653             DOMException.INVALID_CHARACTER_ERR));
654     }
655
656     //
657
// DOMImplementation.CreateDocument
658
//
659

660     {
661         DOMImplementation JavaDoc impl = DOMImplementationImpl.getDOMImplementation();
662         
663         String JavaDoc qName = "foo:docName";
664         String JavaDoc pubId = "pubId";
665         String JavaDoc sysId = "http://sysId";
666         
667         DocumentType JavaDoc dt = impl.createDocumentType(qName, pubId, sysId);
668         
669         String JavaDoc docNSURI = "http://document.namespace";
670         Document JavaDoc 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 JavaDoc 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         //
692
// Creating a second document with the same docType object should fail.
693
//
694
Assertion.assertTrue(DOMExceptionsTest(impl, "createDocument",
695                        new Class JavaDoc[]{String JavaDoc.class,
696                                String JavaDoc.class,
697                                DocumentType JavaDoc.class},
698                        new Object JavaDoc[]{docNSURI, qName, dt},
699                        DOMException.WRONG_DOCUMENT_ERR));
700
701         // Namespace tests of createDocument are covered by createElementNS below
702
}
703     
704     
705     //
706
// CreateElementNS methods
707
//
708

709     {
710         
711         // Set up an initial (root element only) document.
712
//
713
DOMImplementation JavaDoc impl = DOMImplementationImpl.getDOMImplementation();
714         
715         String JavaDoc qName = "foo:docName";
716         String JavaDoc pubId = "pubId";
717         String JavaDoc sysId = "http://sysId";
718         DocumentType JavaDoc dt = impl.createDocumentType(qName, pubId, sysId);
719         
720         String JavaDoc docNSURI = "http://document.namespace";
721     Document JavaDoc doc = impl.createDocument(docNSURI, qName, dt);
722         Element JavaDoc rootEl = doc.getDocumentElement();
723
724         //
725
// CreateElementNS
726
//
727
Element JavaDoc ela = doc.createElementNS("http://nsa", "a:ela"); // prefix and URI
728
Element JavaDoc elb = doc.createElementNS("http://nsb", "elb"); // URI, no prefix.
729
Element JavaDoc elc = doc.createElementNS(null, "elc"); // No URI, no prefix.
730

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         // Badly formed qualified name
754
Assertion.assertTrue(DOMExceptionsTest(doc, "createElementNS",
755                       new Class JavaDoc[]{String JavaDoc.class, String JavaDoc.class},
756                       new Object JavaDoc[]{"http://nsa", "<a"},
757                       DOMException.INVALID_CHARACTER_ERR));
758     Assertion.assertTrue(DOMExceptionsTest(doc, "createElementNS",
759                       new Class JavaDoc[]{String JavaDoc.class, String JavaDoc.class},
760                       new Object JavaDoc[]{"http://nsa", ":a"},
761                       DOMException.NAMESPACE_ERR));
762     Assertion.assertTrue(DOMExceptionsTest(doc, "createElementNS",
763                       new Class JavaDoc[]{String JavaDoc.class, String JavaDoc.class},
764                       new Object JavaDoc[]{"http://nsa", "a:"},
765                       DOMException.NAMESPACE_ERR));
766     Assertion.assertTrue(DOMExceptionsTest(doc, "createElementNS",
767                       new Class JavaDoc[]{String JavaDoc.class, String JavaDoc.class},
768                       new Object JavaDoc[]{"http://nsa", "a::a"},
769                       DOMException.NAMESPACE_ERR));
770     Assertion.assertTrue(DOMExceptionsTest(doc, "createElementNS",
771                       new Class JavaDoc[]{String JavaDoc.class, String JavaDoc.class},
772                       new Object JavaDoc[]{"http://nsa", "a:a:a"},
773                       DOMException.NAMESPACE_ERR));
774
775         // xml:a must have namespaceURI == "http://www.w3.org/XML/1998/namespace"
776
String JavaDoc 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 JavaDoc[]{String JavaDoc.class, String JavaDoc.class},
780                       new Object JavaDoc[]{"http://nsa", "xml:a"},
781                       DOMException.NAMESPACE_ERR));
782     Assertion.assertTrue(DOMExceptionsTest(doc, "createElementNS",
783                       new Class JavaDoc[]{String JavaDoc.class, String JavaDoc.class},
784                       new Object JavaDoc[]{"", "xml:a"},
785                       DOMException.NAMESPACE_ERR));
786     Assertion.assertTrue(DOMExceptionsTest(doc, "createElementNS",
787                       new Class JavaDoc[]{String JavaDoc.class, String JavaDoc.class},
788                       new Object JavaDoc[]{null, "xml:a"},
789                       DOMException.NAMESPACE_ERR));
790
791         //unlike Attribute, xmlns (no different from foo) can have any namespaceURI for Element
792
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         //unlike Attribute, xmlns:a (no different from foo:a) can have any
798
// namespaceURI for Element except null
799
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 JavaDoc[]{String JavaDoc.class, String JavaDoc.class},
803                       new Object JavaDoc[]{null, "xmlns:a"},
804                       DOMException.NAMESPACE_ERR));
805
806         //In fact, any prefix != null should have a namespaceURI != null
807
Assertion.equals(doc.createElementNS("http://nsa", "foo:a").getNamespaceURI(), "http://nsa");
808     Assertion.assertTrue(DOMExceptionsTest(doc, "createElementNS",
809                       new Class JavaDoc[]{String JavaDoc.class, String JavaDoc.class},
810                       new Object JavaDoc[]{null, "foo:a"},
811                       DOMException.NAMESPACE_ERR));
812
813         //Change prefix
814
Element JavaDoc 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         //The spec does not prevent us from setting prefix to a node without prefix
822
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         //Special case for xml:a where namespaceURI must be xmlURI
831
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 JavaDoc[]{String JavaDoc.class},
836                       new Object JavaDoc[]{"xml"},
837                       DOMException.NAMESPACE_ERR));
838         //However, there is no restriction on prefix xmlns
839
elem.setPrefix("xmlns");
840         //Also an element can not have a prefix with namespaceURI == null
841
elem = doc.createElementNS(null, "a");
842         Assertion.assertTrue(DOMExceptionsTest(elem, "setPrefix",
843                       new Class JavaDoc[]{String JavaDoc.class},
844                       new Object JavaDoc[]{"foo"},
845                       DOMException.NAMESPACE_ERR));
846
847         //Only prefix of Element and Attribute can be changed
848
Assertion.assertTrue(DOMExceptionsTest(doc, "setPrefix",
849                       new Class JavaDoc[]{String JavaDoc.class},
850                       new Object JavaDoc[]{"foo"},
851                       DOMException.NAMESPACE_ERR));
852
853         //Prefix of readonly Element can not be changed.
854
//However, there is no way to create such Element for testing yet.
855
}
856     
857
858
859
860     //
861
// CreateAttributeNS methods
862
//
863

864     {
865         
866         // Set up an initial (root element only) document.
867
//
868
DOMImplementation JavaDoc impl = DOMImplementationImpl.getDOMImplementation();
869         
870         String JavaDoc qName = "foo:docName";
871         String JavaDoc pubId = "pubId";
872         String JavaDoc sysId = "http://sysId";
873         DocumentType JavaDoc dt = impl.createDocumentType(qName, pubId, sysId);
874         
875         String JavaDoc docNSURI = "http://document.namespace";
876         Document JavaDoc doc = impl.createDocument(docNSURI, qName, dt);
877         Element JavaDoc rootEl = doc.getDocumentElement();
878
879         //
880
// CreateAttributeNS
881
//
882
Attr JavaDoc attra = doc.createAttributeNS("http://nsa", "a:attra"); // prefix and URI
883
Attr JavaDoc attrb = doc.createAttributeNS("http://nsb", "attrb"); // URI, no prefix.
884
Attr JavaDoc attrc = doc.createAttributeNS(null, "attrc"); // No URI, no prefix.
885

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         // Badly formed qualified name
909
Assertion.assertTrue(DOMExceptionsTest(doc, "createAttributeNS",
910                       new Class JavaDoc[]{String JavaDoc.class, String JavaDoc.class},
911                       new Object JavaDoc[]{"http://nsa", "<a"},
912                       DOMException.INVALID_CHARACTER_ERR));
913     Assertion.assertTrue(DOMExceptionsTest(doc, "createAttributeNS",
914                       new Class JavaDoc[]{String JavaDoc.class, String JavaDoc.class},
915                       new Object JavaDoc[]{"http://nsa", ":a"},
916                       DOMException.NAMESPACE_ERR));
917         Assertion.assertTrue(DOMExceptionsTest(doc, "createAttributeNS",
918                       new Class JavaDoc[]{String JavaDoc.class, String JavaDoc.class},
919                       new Object JavaDoc[]{"http://nsa", "a:"},
920                       DOMException.NAMESPACE_ERR));
921         Assertion.assertTrue(DOMExceptionsTest(doc, "createAttributeNS",
922                       new Class JavaDoc[]{String JavaDoc.class, String JavaDoc.class},
923                       new Object JavaDoc[]{"http://nsa", "a::a"},
924                       DOMException.NAMESPACE_ERR));
925         Assertion.assertTrue(DOMExceptionsTest(doc, "createAttributeNS",
926                       new Class JavaDoc[]{String JavaDoc.class, String JavaDoc.class},
927                       new Object JavaDoc[]{"http://nsa", "a:a:a"},
928                       DOMException.NAMESPACE_ERR));
929
930         // xml:a must have namespaceURI == "http://www.w3.org/XML/1998/namespace"
931
String JavaDoc 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 JavaDoc[]{String JavaDoc.class, String JavaDoc.class},
935                       new Object JavaDoc[]{"http://nsa", "xml:a"},
936                       DOMException.NAMESPACE_ERR));
937         Assertion.assertTrue(DOMExceptionsTest(doc, "createAttributeNS",
938                       new Class JavaDoc[]{String JavaDoc.class, String JavaDoc.class},
939                       new Object JavaDoc[]{"", "xml:a"},
940                       DOMException.NAMESPACE_ERR));
941         Assertion.assertTrue(DOMExceptionsTest(doc, "createAttributeNS",
942                       new Class JavaDoc[]{String JavaDoc.class, String JavaDoc.class},
943                       new Object JavaDoc[]{null, "xml:a"},
944                       DOMException.NAMESPACE_ERR));
945
946         //unlike Element, xmlns must have namespaceURI == "http://www.w3.org/2000/xmlns/"
947
String JavaDoc 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 JavaDoc[]{String JavaDoc.class, String JavaDoc.class},
951                       new Object JavaDoc[]{"http://nsa", "xmlns"},
952                       DOMException.NAMESPACE_ERR));
953         Assertion.assertTrue(DOMExceptionsTest(doc, "createAttributeNS",
954                       new Class JavaDoc[]{String JavaDoc.class, String JavaDoc.class},
955                       new Object JavaDoc[]{xmlURI, "xmlns"},
956                       DOMException.NAMESPACE_ERR));
957         Assertion.assertTrue(DOMExceptionsTest(doc, "createAttributeNS",
958                       new Class JavaDoc[]{String JavaDoc.class, String JavaDoc.class},
959                       new Object JavaDoc[]{"", "xmlns"},
960                       DOMException.NAMESPACE_ERR));
961         Assertion.assertTrue(DOMExceptionsTest(doc, "createAttributeNS",
962                       new Class JavaDoc[]{String JavaDoc.class, String JavaDoc.class},
963                       new Object JavaDoc[]{null, "xmlns"},
964                       DOMException.NAMESPACE_ERR));
965
966         //unlike Element, xmlns:a must have namespaceURI == "http://www.w3.org/2000/xmlns/"
967
Assertion.equals(doc.createAttributeNS(xmlnsURI, "xmlns:a").getNamespaceURI(), xmlnsURI);
968         Assertion.assertTrue(DOMExceptionsTest(doc, "createAttributeNS",
969                       new Class JavaDoc[]{String JavaDoc.class, String JavaDoc.class},
970                       new Object JavaDoc[]{"http://nsa", "xmlns:a"},
971                       DOMException.NAMESPACE_ERR));
972         Assertion.assertTrue(DOMExceptionsTest(doc, "createAttributeNS",
973                       new Class JavaDoc[]{String JavaDoc.class, String JavaDoc.class},
974                       new Object JavaDoc[]{xmlURI, "xmlns:a"},
975                       DOMException.NAMESPACE_ERR));
976         Assertion.assertTrue(DOMExceptionsTest(doc, "createAttributeNS",
977                       new Class JavaDoc[]{String JavaDoc.class, String JavaDoc.class},
978                       new Object JavaDoc[]{"", "xmlns:a"},
979                       DOMException.NAMESPACE_ERR));
980         Assertion.assertTrue(DOMExceptionsTest(doc, "createAttributeNS",
981                       new Class JavaDoc[]{String JavaDoc.class, String JavaDoc.class},
982                       new Object JavaDoc[]{null, "xmlns:a"},
983                       DOMException.NAMESPACE_ERR));
984
985         //In fact, any prefix != null should have a namespaceURI != null
986
Assertion.equals(doc.createAttributeNS("http://nsa", "foo:a").getNamespaceURI(), "http://nsa");
987         Assertion.assertTrue(DOMExceptionsTest(doc, "createAttributeNS",
988                       new Class JavaDoc[]{String JavaDoc.class, String JavaDoc.class},
989                       new Object JavaDoc[]{null, "foo:a"},
990                       DOMException.NAMESPACE_ERR));
991
992         //Change prefix
993
Attr JavaDoc 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        //The spec does not prevent us from setting prefix to a node without prefix
1001
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        //Special case for xml:a where namespaceURI must be xmlURI
1010
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 JavaDoc[]{String JavaDoc.class},
1015                       new Object JavaDoc[]{"xml"},
1016                       DOMException.NAMESPACE_ERR));
1017        //Special case for xmlns:a where namespaceURI must be xmlURI
1018
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 JavaDoc[]{String JavaDoc.class},
1023                       new Object JavaDoc[]{"xmlns"},
1024                       DOMException.NAMESPACE_ERR));
1025        //Special case for xmlns where no prefix can be set
1026
attr = doc.createAttributeNS(xmlnsURI, "xmlns");
1027        Assertion.assertTrue(DOMExceptionsTest(attr, "setPrefix",
1028                       new Class JavaDoc[]{String JavaDoc.class},
1029                       new Object JavaDoc[]{"xml"},
1030                       DOMException.NAMESPACE_ERR));
1031        //Also an attribute can not have a prefix with namespaceURI == null
1032
attr = doc.createAttributeNS(null, "a");
1033        Assertion.assertTrue(DOMExceptionsTest(attr, "setPrefix",
1034                       new Class JavaDoc[]{String JavaDoc.class},
1035                       new Object JavaDoc[]{"foo"},
1036                       DOMException.NAMESPACE_ERR));
1037        
1038        //Only prefix of Element and Attribute can be changed
1039
Assertion.assertTrue(DOMExceptionsTest(attr, "setPrefix",
1040                       new Class JavaDoc[]{String JavaDoc.class},
1041                       new Object JavaDoc[]{"foo"},
1042                       DOMException.NAMESPACE_ERR));
1043
1044        //Prefix of readonly Attribute can not be changed.
1045
//However, there is no way to create such DOM_Attribute for testing yet.
1046
}
1047    
1048
1049    //
1050
// getElementsByTagName*
1051
//
1052

1053    {
1054        
1055        // Set up an initial (root element only) document.
1056
//
1057
DOMImplementation JavaDoc impl = DOMImplementationImpl.getDOMImplementation();
1058        
1059        String JavaDoc qName = "foo:docName";
1060        String JavaDoc pubId = "pubId";
1061        String JavaDoc sysId = "http://sysId";
1062        DocumentType JavaDoc dt = impl.createDocumentType(qName, pubId, sysId);
1063        
1064        String JavaDoc docNSURI = "http://document.namespace";
1065    Document JavaDoc doc = impl.createDocument(docNSURI, qName, dt);
1066        Element JavaDoc rootEl = doc.getDocumentElement();
1067
1068        //
1069
// Populate the document
1070
//
1071
Element JavaDoc ela = doc.createElementNS("http://nsa", "a:ela");
1072        rootEl.appendChild(ela);
1073        Element JavaDoc elb = doc.createElementNS("http://nsb", "elb");
1074        rootEl.appendChild(elb);
1075        Element JavaDoc elc = doc.createElementNS(null, "elc");
1076        rootEl.appendChild(elc);
1077        Element JavaDoc eld = doc.createElementNS("http://nsa", "d:ela");
1078        rootEl.appendChild(eld);
1079        Element JavaDoc ele = doc.createElementNS("http://nse", "elb");
1080        rootEl.appendChild(ele);
1081
1082
1083        //
1084
// Access with DOM Level 1 getElementsByTagName
1085
//
1086

1087        NodeList JavaDoc 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        //
1101
// Access with DOM Level 2 getElementsByTagNameNS
1102
//
1103

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); // Gets the document root element, plus 5 more
1132

1133        Assertion.assertTrue(nl.item(6) == null);
1134        // Assertion.assert(nl.item(-1) == 0);
1135

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        //
1145
// Node lists are Live
1146
//
1147

1148        nl = doc.getElementsByTagNameNS("*", "*");
1149        NodeList JavaDoc 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   //
1165
// Attributes and NamedNodeMaps.
1166
//
1167
{
1168
1169        // Set up an initial (root element only) document.
1170
//
1171
DOMImplementation JavaDoc impl = DOMImplementationImpl.getDOMImplementation();
1172        
1173        String JavaDoc qName = "foo:docName";
1174        String JavaDoc pubId = "pubId";
1175        String JavaDoc sysId = "http://sysId";
1176        DocumentType JavaDoc dt = impl.createDocumentType(qName, pubId, sysId);
1177        
1178        String JavaDoc docNSURI = "http://document.namespace";
1179        Document JavaDoc doc = impl.createDocument(docNSURI, qName, dt);
1180        Element JavaDoc rootEl = doc.getDocumentElement();
1181
1182        //
1183
// Create a set of attributes and hang them on the root element.
1184
//
1185
Attr JavaDoc attra = doc.createAttributeNS("http://nsa", "a:attra");
1186        rootEl.setAttributeNodeNS(attra);
1187        Attr JavaDoc attrb = doc.createAttributeNS("http://nsb", "attrb");
1188        rootEl.setAttributeNodeNS(attrb);
1189        Attr JavaDoc attrc = doc.createAttributeNS(null, "attrc");
1190        rootEl.setAttributeNodeNS(attrc);
1191        Attr JavaDoc attrd = doc.createAttributeNS("http://nsa", "d:attra");
1192        rootEl.setAttributeNodeNS(attrd);
1193        Attr JavaDoc attre = doc.createAttributeNS("http://nse", "attrb");
1194        rootEl.setAttributeNodeNS(attre);
1195
1196        //
1197
// Check that the attribute nodes were created with the correct properties.
1198
//
1199
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        // Test methods of NamedNodeMap
1211
NamedNodeMap JavaDoc 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