1 package tests.org.w3c.dom;
2 
3 import org.w3c.dom.Element;
4 import org.w3c.dom.Document;
5 import org.w3c.dom.Attr;
6 
7 import javax.xml.parsers.DocumentBuilder;
8 
9 /**
10  * The method getAttributeNodeNS retrieves an Attr node by local name and
11  * namespace URI. Create a new element node and add 2 new attribute nodes to it
12  * that have the same local name but different namespaceURIs and prefixes.
13  * Retrieve an attribute using namespace and localname and check its value, name
14  * and namespaceURI.
15  *
16  * @author IBM
17  * @author Neil Delima
18  * @see <a
19  *      href="http://www.w3.org/TR/DOM-Level-2-Core/core#ID-ElGetAtNodeNS">http://www.w3.org/TR/DOM-Level-2-Core/core#ID-ElGetAtNodeNS</a>
20  */
21 public final class ElementGetAttributeNodeNS extends DOMTestCase {
22 
23     DOMDocumentBuilderFactory factory;
24 
25     DocumentBuilder builder;
26 
setUp()27     protected void setUp() throws Exception {
28         super.setUp();
29         try {
30             factory = new DOMDocumentBuilderFactory(DOMDocumentBuilderFactory
31                     .getConfiguration2());
32             builder = factory.getBuilder();
33         } catch (Exception e) {
34             fail("Unexpected exception" + e.getMessage());
35         }
36     }
37 
tearDown()38     protected void tearDown() throws Exception {
39         factory = null;
40         builder = null;
41         super.tearDown();
42     }
43 
44     /**
45      * Runs the test case.
46      *
47      * @throws Throwable
48      *             Any uncaught exception causes test to fail
49      */
testGetAttributeNodeNS1()50     public void testGetAttributeNodeNS1() throws Throwable {
51         Document doc;
52         Element element;
53         Attr attribute1;
54         Attr attribute2;
55 
56 
57         Attr attribute;
58         String attrValue;
59         String attrName;
60         String attNodeName;
61         String attrLocalName;
62         String attrNS;
63         doc = (Document) load("staffNS", builder);
64         element = doc.createElementNS("namespaceURI", "root");
65         attribute1 = doc.createAttributeNS("http://www.w3.org/DOM/Level2",
66                 "l2:att");
67         element.setAttributeNodeNS(attribute1);
68         attribute2 = doc.createAttributeNS("http://www.w3.org/DOM/Level1",
69                 "att");
70         element.setAttributeNodeNS(attribute2);
71         attribute = element.getAttributeNodeNS("http://www.w3.org/DOM/Level2",
72                 "att");
73         attrValue = attribute.getNodeValue();
74         attrName = attribute.getName();
75         attNodeName = attribute.getNodeName();
76         attrLocalName = attribute.getLocalName();
77         attrNS = attribute.getNamespaceURI();
78         assertEquals("elementgetattributenodens01_attrValue", "", attrValue);
79         assertEquals("elementgetattributenodens01_attrName", "l2:att", attrName);
80         assertEquals("elementgetattributenodens01_attrNodeName", "l2:att",
81                 attNodeName);
82         assertEquals("elementgetattributenodens01_attrLocalName", "att",
83                 attrLocalName);
84         assertEquals("elementgetattributenodens01_attrNs",
85                 "http://www.w3.org/DOM/Level2", attrNS);
86     }
testGetAttributeNodeNS2()87     public void testGetAttributeNodeNS2() throws Throwable {
88         Document doc;
89         Element element;
90         Attr attribute;
91 
92         String attrValue;
93         doc = (Document) load("staffNS", builder);
94         element = doc.createElementNS("namespaceURI", "root");
95         attribute = doc.createAttributeNS("http://www.w3.org/DOM/Level2",
96                 "l2:att");
97         element.setAttributeNodeNS(attribute);
98         attribute = element.getAttributeNodeNS("http://www.w3.org/DOM/Level2",
99                 "att");
100         attrValue = attribute.getNodeValue();
101         assertEquals("elementgetattributenodens02", "", attrValue);
102     }
103 
104 // Assumes validation.
105 //    public void testGetAttributeNodeNS3() throws Throwable {
106 //        Document doc;
107 //        Element element;
108 //        Attr attribute;
109 //        String attrValue;
110 //        NodeList childList;
111 //        String nullNS = null;
112 //
113 //        doc = (Document) load("staffNS", builder);
114 //        childList = doc.getElementsByTagNameNS("http://www.nist.gov",
115 //                "employee");
116 //        element = (Element) childList.item(1);
117 //        attribute = element.getAttributeNodeNS(nullNS, "defaultAttr");
118 //        attrValue = attribute.getNodeValue();
119 //        assertEquals("elementgetattributenodens03", "defaultVal", attrValue);
120 //    }
121 }
122