Home | History | Annotate | Download | only in dom
      1 package tests.org.w3c.dom;
      2 
      3 import org.w3c.dom.Document;
      4 import org.w3c.dom.DocumentType;
      5 import org.w3c.dom.DOMImplementation;
      6 import org.w3c.dom.NodeList;
      7 import org.w3c.dom.Element;
      8 
      9 import javax.xml.parsers.DocumentBuilder;
     10 
     11 /**
     12  * The method getElementsByTagNameNS returns a NodeList of all the Elements with
     13  * a given local name and namespace URI in the order in which they are
     14  * encountered in a preorder traversal of the Document tree.
     15  *
     16  * Invoke the getElementsByTagNameNS method on a new Document object with the
     17  * values of namespaceURI=* and localName=*. This should return a nodeList of 1
     18  * item.
     19  *
     20  * @author IBM
     21  * @author Neil Delima
     22  * @see <a
     23  *      href="http://www.w3.org/TR/DOM-Level-2-Core/core">http://www.w3.org/TR/DOM-Level-2-Core/core</a>
     24  * @see <a
     25  *      href="http://www.w3.org/TR/DOM-Level-2-Core/core#ID-getElBTNNS">http://www.w3.org/TR/DOM-Level-2-Core/core#ID-getElBTNNS</a>
     26  * @see <a
     27  *      href="http://www.w3.org/Bugs/Public/show_bug.cgi?id=259">http://www.w3.org/Bugs/Public/show_bug.cgi?id=259</a>
     28  */
     29 public final class DocumentGetElementsByTagnameNS extends DOMTestCase {
     30 
     31     DOMDocumentBuilderFactory factory;
     32 
     33     DocumentBuilder builder;
     34 
     35     protected void setUp() throws Exception {
     36         super.setUp();
     37         try {
     38             factory = new DOMDocumentBuilderFactory(DOMDocumentBuilderFactory
     39                     .getConfiguration2());
     40             builder = factory.getBuilder();
     41         } catch (Exception e) {
     42             fail("Unexpected exception" + e.getMessage());
     43         }
     44     }
     45 
     46     protected void tearDown() throws Exception {
     47         factory = null;
     48         builder = null;
     49         super.tearDown();
     50     }
     51 
     52     /**
     53      * Runs the test case.
     54      *
     55      * @throws Throwable
     56      *             Any uncaught exception causes test to fail
     57      */
     58     public void testGetElementsByTagNameNS1() throws Throwable {
     59         Document doc;
     60         Document newDoc;
     61         DocumentType docType = null;
     62 
     63         DOMImplementation domImpl;
     64         NodeList childList;
     65         String nullNS = null;
     66 
     67         doc = (Document) load("staffNS", builder);
     68         domImpl = doc.getImplementation();
     69         newDoc = domImpl.createDocument(nullNS, "root", docType);
     70         childList = newDoc.getElementsByTagNameNS("*", "*");
     71         assertEquals("documentgetelementsbytagnameNS01", 1, childList
     72                 .getLength());
     73     }
     74     public void testGetElementsByTagNameNS2() throws Throwable {
     75         Document doc;
     76         Element docElem;
     77         Element element;
     78         NodeList childList;
     79 
     80         doc = (Document) load("staffNS", builder);
     81         docElem = doc.getDocumentElement();
     82         element = doc.createElementNS("test", "employeeId");
     83         docElem.appendChild(element);
     84         childList = doc.getElementsByTagNameNS("*", "employeeId");
     85         assertEquals("documentgetelementsbytagnameNS02", 6, childList
     86                 .getLength());
     87     }
     88     public void testGetElementsByTagNameNS3() throws Throwable {
     89         Document doc;
     90         NodeList childList;
     91         doc = (Document) load("staffNS", builder);
     92         childList = doc.getElementsByTagNameNS("**", "*");
     93         assertEquals("documentgetelementsbytagnameNS03", 0, childList
     94                 .getLength());
     95     }
     96     public void testGetElementsByTagNameNS4() throws Throwable {
     97         Document doc;
     98         NodeList childList;
     99         String nullNS = null;
    100 
    101         doc = (Document) load("staffNS", builder);
    102         childList = doc.getElementsByTagNameNS(nullNS, "0");
    103         assertEquals("documentgetelementsbytagnameNS04", 0, childList
    104                 .getLength());
    105     }
    106     public void testGetElementsByTagNameNS5() throws Throwable {
    107         Document doc;
    108         NodeList childList;
    109         doc = (Document) load("staffNS", builder);
    110         childList = doc.getElementsByTagNameNS("null", "elementId");
    111         assertEquals("documentgetelementsbytagnameNS05", 0, childList
    112                 .getLength());
    113     }
    114 }
    115