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.Node;
9import org.w3c.dom.Document;
10import org.w3c.dom.NodeList;
11import org.w3c.dom.Element;
12import org.w3c.dom.Attr;
13
14import javax.xml.parsers.DocumentBuilder;
15
16/**
17 * The "getLocalName()" method for a Node returns the local part of the
18 * qualified name of this node, and for nodes of any type other than
19 * ELEMENT_NODE and ATTRIBUTE_NODE and nodes created with a DOM Level 1 method,
20 * this is null.
21 *
22 * Retrieve the first emp:address node and get the attributes of this node."
23 * Then apply the getLocalName() method to the emp:domestic attribute. The
24 * method should return "domestic".
25 *
26 * @author NIST
27 * @author Mary Brady
28 * @see <a
29 *      href="http://www.w3.org/TR/DOM-Level-2-Core/core#ID-NodeNSLocalN">http://www.w3.org/TR/DOM-Level-2-Core/core#ID-NodeNSLocalN</a>
30 */
31@TestTargetClass(Node.class)
32public final class LocalName extends DOMTestCase {
33
34    DOMDocumentBuilderFactory factory;
35
36    DocumentBuilder builder;
37
38    protected void setUp() throws Exception {
39        super.setUp();
40        try {
41            factory = new DOMDocumentBuilderFactory(DOMDocumentBuilderFactory
42                    .getConfiguration2());
43            builder = factory.getBuilder();
44        } catch (Exception e) {
45            fail("Unexpected exception" + e.getMessage());
46        }
47    }
48
49    protected void tearDown() throws Exception {
50        factory = null;
51        builder = null;
52        super.tearDown();
53    }
54
55    /**
56     * Runs the test case.
57     *
58     * @throws Throwable
59     *             Any uncaught exception causes test to fail
60     */
61    @TestTargetNew(
62        level = TestLevel.PARTIAL_COMPLETE,
63        notes = "Verifies positive functionality.",
64        method = "getLocalName",
65        args = {}
66    )
67    public void testGetLocalName1() throws Throwable {
68        Document doc;
69        NodeList elementList;
70        Element testAddr;
71        Attr addrAttr;
72        String localName;
73        doc = (Document) load("staffNS", builder);
74        elementList = doc.getElementsByTagName("emp:address");
75        testAddr = (Element) elementList.item(0);
76        assertNotNull("empAddrNotNull", testAddr);
77        addrAttr = testAddr.getAttributeNode("emp:domestic");
78        localName = addrAttr.getLocalName();
79        assertEquals("localName", "domestic", localName);
80    }
81    @TestTargetNew(
82        level = TestLevel.PARTIAL_COMPLETE,
83        notes = "Verifies that getLocalName method returns null.",
84        method = "getLocalName",
85        args = {}
86    )
87    public void testGetLocalName2() throws Throwable {
88        Document doc;
89        Node createdNode;
90        String localName;
91        doc = (Document) load("staffNS", builder);
92        createdNode = doc.createElement("test:employee");
93        localName = createdNode.getLocalName();
94        assertNull("localNameNull", localName);
95    }
96    @TestTargetNew(
97        level = TestLevel.PARTIAL_COMPLETE,
98        notes = "Verifies that getLocalName method returns null.",
99        method = "getLocalName",
100        args = {}
101    )
102    public void testGetLocalName3() throws Throwable {
103        Document doc;
104        NodeList elementList;
105        Node testEmployee;
106        Node textNode;
107        String localName;
108        doc = (Document) load("staffNS", builder);
109        elementList = doc.getElementsByTagName("employeeId");
110        testEmployee = elementList.item(0);
111        textNode = testEmployee.getFirstChild();
112        localName = textNode.getLocalName();
113        assertNull("textNodeLocalName", localName);
114    }
115    @TestTargetNew(
116        level = TestLevel.PARTIAL_COMPLETE,
117        notes = "Verifies positive functionality.",
118        method = "getLocalName",
119        args = {}
120    )
121    public void testGetLocalName4() throws Throwable {
122        Document doc;
123        NodeList elementList;
124        Node testEmployee;
125        String employeeLocalName;
126        doc = (Document) load("staffNS", builder);
127        elementList = doc.getElementsByTagName("employee");
128        testEmployee = elementList.item(0);
129        employeeLocalName = testEmployee.getLocalName();
130        assertEquals("lname", "employee", employeeLocalName);
131    }
132}
133