Home | History | Annotate | Download | only in dom
      1 package tests.org.w3c.dom;
      2 
      3 import org.w3c.dom.Node;
      4 import org.w3c.dom.Document;
      5 import org.w3c.dom.NodeList;
      6 import org.w3c.dom.Element;
      7 import org.w3c.dom.Attr;
      8 
      9 import javax.xml.parsers.DocumentBuilder;
     10 
     11 /**
     12  * The "getLocalName()" method for a Node returns the local part of the
     13  * qualified name of this node, and for nodes of any type other than
     14  * ELEMENT_NODE and ATTRIBUTE_NODE and nodes created with a DOM Level 1 method,
     15  * this is null.
     16  *
     17  * Retrieve the first emp:address node and get the attributes of this node."
     18  * Then apply the getLocalName() method to the emp:domestic attribute. The
     19  * method should return "domestic".
     20  *
     21  * @author NIST
     22  * @author Mary Brady
     23  * @see <a
     24  *      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>
     25  */
     26 public final class LocalName extends DOMTestCase {
     27 
     28     DOMDocumentBuilderFactory factory;
     29 
     30     DocumentBuilder builder;
     31 
     32     protected void setUp() throws Exception {
     33         super.setUp();
     34         try {
     35             factory = new DOMDocumentBuilderFactory(DOMDocumentBuilderFactory
     36                     .getConfiguration2());
     37             builder = factory.getBuilder();
     38         } catch (Exception e) {
     39             fail("Unexpected exception" + e.getMessage());
     40         }
     41     }
     42 
     43     protected void tearDown() throws Exception {
     44         factory = null;
     45         builder = null;
     46         super.tearDown();
     47     }
     48 
     49     /**
     50      * Runs the test case.
     51      *
     52      * @throws Throwable
     53      *             Any uncaught exception causes test to fail
     54      */
     55     public void testGetLocalName1() throws Throwable {
     56         Document doc;
     57         NodeList elementList;
     58         Element testAddr;
     59         Attr addrAttr;
     60         String localName;
     61         doc = (Document) load("staffNS", builder);
     62         elementList = doc.getElementsByTagName("emp:address");
     63         testAddr = (Element) elementList.item(0);
     64         assertNotNull("empAddrNotNull", testAddr);
     65         addrAttr = testAddr.getAttributeNode("emp:domestic");
     66         localName = addrAttr.getLocalName();
     67         assertEquals("localName", "domestic", localName);
     68     }
     69     public void testGetLocalName2() throws Throwable {
     70         Document doc;
     71         Node createdNode;
     72         String localName;
     73         doc = (Document) load("staffNS", builder);
     74         createdNode = doc.createElement("test:employee");
     75         localName = createdNode.getLocalName();
     76         assertNull("localNameNull", localName);
     77     }
     78     public void testGetLocalName3() throws Throwable {
     79         Document doc;
     80         NodeList elementList;
     81         Node testEmployee;
     82         Node textNode;
     83         String localName;
     84         doc = (Document) load("staffNS", builder);
     85         elementList = doc.getElementsByTagName("employeeId");
     86         testEmployee = elementList.item(0);
     87         textNode = testEmployee.getFirstChild();
     88         localName = textNode.getLocalName();
     89         assertNull("textNodeLocalName", localName);
     90     }
     91     public void testGetLocalName4() throws Throwable {
     92         Document doc;
     93         NodeList elementList;
     94         Node testEmployee;
     95         String employeeLocalName;
     96         doc = (Document) load("staffNS", builder);
     97         elementList = doc.getElementsByTagName("employee");
     98         testEmployee = elementList.item(0);
     99         employeeLocalName = testEmployee.getLocalName();
    100         assertEquals("lname", "employee", employeeLocalName);
    101     }
    102 }
    103