Home | History | Annotate | Download | only in dom
      1 package tests.org.w3c.dom;
      2 
      3 import org.w3c.dom.Element;
      4 import org.w3c.dom.Document;
      5 import org.w3c.dom.NodeList;
      6 
      7 import javax.xml.parsers.DocumentBuilder;
      8 
      9 /**
     10  * The "hasAttribute()" method for an Element should return true if the element
     11  * has an attribute with the given name. Retrieve the first "address" element
     12  * and the "hasAttribute()" method should return false since the element does
     13  * not have a default value.
     14  *
     15  * @author NIST
     16  * @author Mary Brady
     17  * @see <a
     18  *      href="http://www.w3.org/TR/DOM-Level-2-Core/core#ID-ElHasAttr">http://www.w3.org/TR/DOM-Level-2-Core/core#ID-ElHasAttr</a>
     19  */
     20 public final class HasAttribute extends DOMTestCase {
     21 
     22     DOMDocumentBuilderFactory factory;
     23 
     24     DocumentBuilder builder;
     25 
     26     protected void setUp() throws Exception {
     27         super.setUp();
     28         try {
     29             factory = new DOMDocumentBuilderFactory(DOMDocumentBuilderFactory
     30                     .getConfiguration1());
     31             builder = factory.getBuilder();
     32         } catch (Exception e) {
     33             fail("Unexpected exception" + e.getMessage());
     34         }
     35     }
     36 
     37     protected void tearDown() throws Exception {
     38         factory = null;
     39         builder = null;
     40         super.tearDown();
     41     }
     42 
     43     /**
     44      * Runs the test case.
     45      *
     46      * @throws Throwable
     47      *             Any uncaught exception causes test to fail
     48      */
     49     public void testHasAttribute1() throws Throwable {
     50         Document doc;
     51         NodeList elementList;
     52         Element testNode;
     53         boolean state;
     54         doc = (Document) load("staff", builder);
     55         elementList = doc.getElementsByTagName("address");
     56         testNode = (Element) elementList.item(4);
     57         state = testNode.hasAttribute("domestic");
     58         assertFalse("throw_False", state);
     59     }
     60 
     61 // Assumes validation.
     62 //    public void testHasAttribute2() throws Throwable {
     63 //        Document doc;
     64 //        NodeList elementList;
     65 //        Element testNode;
     66 //        boolean state;
     67 //        doc = (Document) load("staff", builder);
     68 //        elementList = doc.getElementsByTagName("address");
     69 //        testNode = (Element) elementList.item(0);
     70 //        state = testNode.hasAttribute("street");
     71 //        assertTrue("throw_True", state);
     72 //    }
     73     public void testHasAttribute3() throws Throwable {
     74         Document doc;
     75         NodeList elementList;
     76         Element testNode;
     77         boolean state;
     78         doc = (Document) load("staff", builder);
     79         elementList = doc.getElementsByTagName("address");
     80         testNode = (Element) elementList.item(0);
     81         state = testNode.hasAttribute("nomatch");
     82         assertFalse("throw_False", state);
     83     }
     84     public void testHasAttribute4() throws Throwable {
     85         Document doc;
     86         NodeList elementList;
     87         Element testNode;
     88         boolean state;
     89         doc = (Document) load("staffNS", builder);
     90         elementList = doc.getElementsByTagName("address");
     91         testNode = (Element) elementList.item(0);
     92         state = testNode.hasAttribute("dmstc:domestic");
     93         assertTrue("hasDomesticAttr", state);
     94     }
     95 }
     96