1 package tests.org.w3c.dom;
2 
3 import org.w3c.dom.NamedNodeMap;
4 import org.w3c.dom.Document;
5 import org.w3c.dom.Attr;
6 import org.w3c.dom.Node;
7 import org.w3c.dom.NodeList;
8 
9 import javax.xml.parsers.DocumentBuilder;
10 
11 /**
12  * The "getNamedItemNS(namespaceURI,localName)" method for a NamedNodeMap should
13  * return a node specified by localName and namespaceURI
14  *
15  * Retrieve a list of elements with tag name "address". Access the second
16  * element from the list and get its attributes. Try to retrieve the attribute
17  * node with local name "domestic" and namespace uri "http://www.usa.com" with
18  * method getNamedItemNS(namespaceURI,localName).
19  *
20  * @author NIST
21  * @author Mary Brady
22  * @see <a
23  *      href="http://www.w3.org/TR/DOM-Level-2-Core/core#ID-F68D095">http://www.w3.org/TR/DOM-Level-2-Core/core#ID-F68D095</a>
24  */
25 public final class GetNamedItemNS extends DOMTestCase {
26 
27     DOMDocumentBuilderFactory factory;
28 
29     DocumentBuilder builder;
30 
setUp()31     protected void setUp() throws Exception {
32         super.setUp();
33         try {
34             factory = new DOMDocumentBuilderFactory(DOMDocumentBuilderFactory
35                     .getConfiguration2());
36             builder = factory.getBuilder();
37         } catch (Exception e) {
38             fail("Unexpected exception" + e.getMessage());
39         }
40     }
41 
tearDown()42     protected void tearDown() throws Exception {
43         factory = null;
44         builder = null;
45         super.tearDown();
46     }
47 
48     /**
49      * Runs the test case.
50      *
51      * @throws Throwable
52      *             Any uncaught exception causes test to fail
53      */
testGetNamedItemNS1()54     public void testGetNamedItemNS1() throws Throwable {
55         Document doc;
56         NodeList elementList;
57         Node testEmployee;
58         NamedNodeMap attributes;
59         Attr domesticAttr;
60         String attrName;
61         doc = (Document) load("staffNS", builder);
62         elementList = doc.getElementsByTagName("address");
63         testEmployee = elementList.item(1);
64         attributes = testEmployee.getAttributes();
65         domesticAttr = (Attr) attributes.getNamedItemNS("http://www.usa.com",
66                 "domestic");
67         attrName = domesticAttr.getNodeName();
68         assertEquals("attrName", "dmstc:domestic", attrName);
69     }
testGetNamedItemNS2()70     public void testGetNamedItemNS2() throws Throwable {
71         String namespaceURI = "http://www.usa.com";
72         String localName = "domest";
73         Document doc;
74         NodeList elementList;
75         Node testEmployee;
76         NamedNodeMap attributes;
77         Attr newAttr;
78         doc = (Document) load("staffNS", builder);
79         elementList = doc.getElementsByTagName("address");
80         testEmployee = elementList.item(1);
81         attributes = testEmployee.getAttributes();
82         newAttr = (Attr) attributes.getNamedItemNS(namespaceURI, localName);
83         assertNull("throw_Null", newAttr);
84     }
85 
86 // Assumes validation.
87 //    public void testGetNamedItemNS3() throws Throwable {
88 //        Document doc;
89 //        DocumentType docType;
90 //        NamedNodeMap entities;
91 //        Entity entity;
92 //        String nullNS = null;
93 //
94 //        doc = (Document) load("staffNS", builder);
95 //        docType = doc.getDoctype();
96 //        entities = docType.getEntities();
97 //        assertNotNull("entitiesNotNull", entities);
98 //        entity = (Entity) entities.getNamedItemNS(nullNS, "ent1");
99 //        assertNotNull("entityNull", entity);
100 //    }
101 
102 // Assumes validation.
103 //    public void testGetNamedItemNS4() throws Throwable {
104 //        Document doc;
105 //        DocumentType docType;
106 //        NamedNodeMap notations;
107 //        Notation notation;
108 //        String nullNS = null;
109 //
110 //        doc = (Document) load("staffNS", builder);
111 //        docType = doc.getDoctype();
112 //        notations = docType.getNotations();
113 //        assertNotNull("notationsNotNull", notations);
114 //        notation = (Notation) notations.getNamedItemNS(nullNS, "notation1");
115 //        assertNotNull("notationNull", notation);
116 //    }
117 }
118