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