1 package tests.org.w3c.dom; 2 3 import dalvik.annotation.TestTargets; 4 import dalvik.annotation.TestLevel; 5 import dalvik.annotation.TestTargetNew; 6 import dalvik.annotation.TestTargetClass; 7 8 import org.w3c.dom.Element; 9 import org.w3c.dom.Document; 10 import org.w3c.dom.NodeList; 11 12 import javax.xml.parsers.DocumentBuilder; 13 14 /** 15 * The "hasAttribute()" method for an Element should return true if the element 16 * has an attribute with the given name. Retrieve the first "address" element 17 * and the "hasAttribute()" method should return false since the element does 18 * not have a default value. 19 * 20 * @author NIST 21 * @author Mary Brady 22 * @see <a 23 * 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> 24 */ 25 @TestTargetClass(Element.class) 26 public final class HasAttribute 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 .getConfiguration1()); 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 @TestTargetNew( 56 level = TestLevel.PARTIAL_COMPLETE, 57 notes = "Verifies that hasAttribute method returns false.", 58 method = "hasAttribute", 59 args = {java.lang.String.class} 60 ) 61 public void testHasAttribute1() throws Throwable { 62 Document doc; 63 NodeList elementList; 64 Element testNode; 65 boolean state; 66 doc = (Document) load("staff", builder); 67 elementList = doc.getElementsByTagName("address"); 68 testNode = (Element) elementList.item(4); 69 state = testNode.hasAttribute("domestic"); 70 assertFalse("throw_False", state); 71 } 72 73 // Assumes validation. 74 // public void testHasAttribute2() throws Throwable { 75 // Document doc; 76 // NodeList elementList; 77 // Element testNode; 78 // boolean state; 79 // doc = (Document) load("staff", builder); 80 // elementList = doc.getElementsByTagName("address"); 81 // testNode = (Element) elementList.item(0); 82 // state = testNode.hasAttribute("street"); 83 // assertTrue("throw_True", state); 84 // } 85 @TestTargetNew( 86 level = TestLevel.PARTIAL_COMPLETE, 87 notes = "Verifies that hasAttribute method returns false.", 88 method = "hasAttribute", 89 args = {java.lang.String.class} 90 ) 91 public void testHasAttribute3() throws Throwable { 92 Document doc; 93 NodeList elementList; 94 Element testNode; 95 boolean state; 96 doc = (Document) load("staff", builder); 97 elementList = doc.getElementsByTagName("address"); 98 testNode = (Element) elementList.item(0); 99 state = testNode.hasAttribute("nomatch"); 100 assertFalse("throw_False", state); 101 } 102 @TestTargetNew( 103 level = TestLevel.PARTIAL_COMPLETE, 104 notes = "Verifies that hasAttribute method returns true.", 105 method = "hasAttribute", 106 args = {java.lang.String.class} 107 ) 108 public void testHasAttribute4() throws Throwable { 109 Document doc; 110 NodeList elementList; 111 Element testNode; 112 boolean state; 113 doc = (Document) load("staffNS", builder); 114 elementList = doc.getElementsByTagName("address"); 115 testNode = (Element) elementList.item(0); 116 state = testNode.hasAttribute("dmstc:domestic"); 117 assertTrue("hasDomesticAttr", state); 118 } 119 } 120