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