Home | History | Annotate | Download | only in dom
      1 /*
      2  This Java source file was generated by test-to-java.xsl
      3  and is a derived work from the source document.
      4  The source document contained the following notice:
      5 
      6 
      7 
      8  Copyright (c) 2001 World Wide Web Consortium,
      9  (Massachusetts Institute of Technology, Institut National de
     10  Recherche en Informatique et en Automatique, Keio University).  All
     11  Rights Reserved.  This program is distributed under the W3C's Software
     12  Intellectual Property License.  This program is distributed in the
     13  hope that it will be useful, but WITHOUT ANY WARRANTY; without even
     14  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     15  PURPOSE.
     16 
     17  See W3C License http://www.w3.org/Consortium/Legal/ for more details.
     18 
     19 
     20  */
     21 
     22 package tests.org.w3c.dom;
     23 
     24 import org.w3c.dom.Node;
     25 import org.w3c.dom.Document;
     26 import org.w3c.dom.Element;
     27 import org.w3c.dom.NodeList;
     28 import org.w3c.dom.DocumentType;
     29 import org.w3c.dom.Attr;
     30 import org.w3c.dom.DOMImplementation;
     31 
     32 import javax.xml.parsers.DocumentBuilder;
     33 
     34 /**
     35  * The method hasAttributes returns whether this node (if it is an element) has
     36  * any attributes. Retreive an element node without attributes. Verify if
     37  * hasAttributes returns false. Retreive another element node with attributes.
     38  * Verify if hasAttributes returns true.
     39  *
     40  * @author IBM
     41  * @author Neil Delima
     42  * @see <a
     43  *      href="http://www.w3.org/TR/DOM-Level-2-Core/core#ID-NodeHasAttrs">http://www.w3.org/TR/DOM-Level-2-Core/core#ID-NodeHasAttrs</a>
     44  */
     45 public final class NodeHasAttributes extends DOMTestCase {
     46 
     47     DOMDocumentBuilderFactory factory;
     48 
     49     DocumentBuilder builder;
     50 
     51     protected void setUp() throws Exception {
     52         super.setUp();
     53         try {
     54             factory = new DOMDocumentBuilderFactory(DOMDocumentBuilderFactory
     55                     .getConfiguration1());
     56             builder = factory.getBuilder();
     57         } catch (Exception e) {
     58             fail("Unexpected exception" + e.getMessage());
     59         }
     60     }
     61 
     62     protected void tearDown() throws Exception {
     63         factory = null;
     64         builder = null;
     65         super.tearDown();
     66     }
     67 
     68     /**
     69      * Runs the test case.
     70      *
     71      * @throws Throwable
     72      *             Any uncaught exception causes test to fail
     73      */
     74     public void testHasAttributes1() throws Throwable {
     75         Document doc;
     76         Element element;
     77         NodeList elementList;
     78         boolean hasAttributes;
     79         doc = (Document) load("staff", builder);
     80         elementList = doc.getElementsByTagName("employee");
     81         element = (Element) elementList.item(0);
     82         hasAttributes = element.hasAttributes();
     83         assertFalse("nodehasattributes01_1", hasAttributes);
     84         elementList = doc.getElementsByTagName("address");
     85         element = (Element) elementList.item(0);
     86         hasAttributes = element.hasAttributes();
     87         assertTrue("nodehasattributes01_2", hasAttributes);
     88     }
     89     public void testHasAttributes2() throws Throwable {
     90         Document doc;
     91         DocumentType docType;
     92         boolean hasAttributes;
     93         doc = (Document) load("staffNS", builder);
     94         docType = doc.getDoctype();
     95         hasAttributes = docType.hasAttributes();
     96         assertFalse("nodehasattributes02", hasAttributes);
     97     }
     98     public void testHasAttributes3() throws Throwable {
     99         Document doc;
    100         Element element;
    101         NodeList elementList;
    102         boolean hasAttributes;
    103         doc = (Document) load("staffNS", builder);
    104         elementList = doc.getElementsByTagName("emp:employee");
    105         element = (Element) elementList.item(0);
    106         assertNotNull("empEmployeeNotNull", element);
    107         hasAttributes = element.hasAttributes();
    108         assertTrue("hasAttributes", hasAttributes);
    109     }
    110     public void testHasAttributes4() throws Throwable {
    111         Document doc;
    112         Document newDoc;
    113         DocumentType docType = null;
    114 
    115         DOMImplementation domImpl;
    116         Element element;
    117         Element elementTest;
    118         Element elementDoc;
    119         Attr attribute;
    120 
    121 
    122         NodeList elementList;
    123         boolean hasAttributes;
    124         doc = (Document) load("staffNS", builder);
    125         domImpl = doc.getImplementation();
    126         newDoc = domImpl.createDocument("http://www.w3.org/DOM/Test", "test",
    127                 docType);
    128         element = newDoc.createElementNS("http://www.w3.org/DOM/Test",
    129                 "dom:elem");
    130         attribute = newDoc.createAttribute("attr");
    131         element.setAttributeNode(attribute);
    132         elementDoc = newDoc.getDocumentElement();
    133         elementDoc.appendChild(element);
    134         elementList = newDoc.getElementsByTagNameNS(
    135                 "http://www.w3.org/DOM/Test", "elem");
    136         elementTest = (Element) elementList.item(0);
    137         hasAttributes = elementTest.hasAttributes();
    138         assertTrue("nodehasattributes04", hasAttributes);
    139     }
    140 
    141 }
    142