1package tests.org.w3c.dom;
2
3import org.w3c.dom.Element;
4import org.w3c.dom.Document;
5import org.w3c.dom.Attr;
6
7import javax.xml.parsers.DocumentBuilder;
8
9/**
10 * The method hasAttribute returns true when an attribute with a given name is
11 * specified on this element or has a default value, false otherwise Invoke the
12 * hasAttribute method to check if the documentElement has attributres.
13 *
14 * @author IBM
15 * @author Neil Delima
16 * @see <a
17 *      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>
18 */
19public final class ElementHasAttribute extends DOMTestCase {
20
21    DOMDocumentBuilderFactory factory;
22
23    DocumentBuilder builder;
24
25    protected void setUp() throws Exception {
26        super.setUp();
27        try {
28            factory = new DOMDocumentBuilderFactory(DOMDocumentBuilderFactory
29                    .getConfiguration1());
30            builder = factory.getBuilder();
31        } catch (Exception e) {
32            fail("Unexpected exception" + e.getMessage());
33        }
34    }
35
36    protected void tearDown() throws Exception {
37        factory = null;
38        builder = null;
39        super.tearDown();
40    }
41
42    /**
43     * Runs the test case.
44     *
45     * @throws Throwable
46     *             Any uncaught exception causes test to fail
47     */
48    public void testHasAttribute1() throws Throwable {
49        Document doc;
50        Element element;
51        boolean state;
52        doc = (Document) load("staff", builder);
53        element = doc.getDocumentElement();
54        state = element.hasAttribute("");
55        assertFalse("elementhasattribute01", state);
56    }
57
58// Assumes validation.
59//    public void testHasAttribute2() throws Throwable {
60//        Document doc;
61//        Element element;
62//        boolean state;
63//        NodeList elementList;
64//        doc = (Document) load("staffNS", builder);
65//        elementList = doc.getElementsByTagName("emp:employee");
66//        element = (Element) elementList.item(0);
67//        assertNotNull("empEmployeeNotNull", element);
68//        state = element.hasAttribute("defaultAttr");
69//        assertTrue("elementhasattribute02", state);
70//    }
71    public void testHasAttribute3() throws Throwable {
72        Document doc;
73        Element element;
74        boolean state;
75        Attr attribute;
76
77        doc = (Document) load("staff", builder);
78        element = doc.createElement("address");
79        attribute = doc.createAttribute("domestic");
80        state = element.hasAttribute("domestic");
81        assertFalse("elementhasattribute03_False", state);
82        element.setAttributeNode(attribute);
83        state = element.hasAttribute("domestic");
84        assertTrue("elementhasattribute03_True", state);
85    }
86    public void testHasAttribute4() throws Throwable {
87        Document doc;
88        Element element;
89        boolean state;
90        Attr attribute;
91
92        doc = (Document) load("staff", builder);
93        element = doc.createElement("address");
94        attribute = doc.createAttribute("domestic");
95        element.setAttributeNode(attribute);
96        state = element.hasAttribute("domestic");
97        assertTrue("elementhasattribute04", state);
98    }
99}
100