1package tests.org.w3c.dom; 2 3import dalvik.annotation.TestTargets; 4import dalvik.annotation.TestLevel; 5import dalvik.annotation.TestTargetNew; 6import dalvik.annotation.TestTargetClass; 7 8import java.util.ArrayList; 9import java.util.List; 10 11import org.w3c.dom.Element; 12import org.w3c.dom.Attr; 13import org.w3c.dom.Document; 14import org.w3c.dom.NodeList; 15import org.w3c.dom.DOMException; 16import org.w3c.dom.Node; 17 18import javax.xml.parsers.DocumentBuilder; 19 20/** 21 * The method setAttributeNS adds a new attribute. Create a new element and add 22 * a new attribute node to it using the setAttributeNS method. Check if the 23 * attribute was correctly set by invoking the getAttributeNodeNS method and 24 * checking the nodeName and nodeValue of the returned nodes. 25 * 26 * @author IBM 27 * @author Neil Delima 28 * @see <a 29 * href="http://www.w3.org/TR/DOM-Level-2-Core/core#ID-ElSetAttrNS">http://www.w3.org/TR/DOM-Level-2-Core/core#ID-ElSetAttrNS</a> 30 */ 31@TestTargetClass(Element.class) 32public final class ElementSetAttributeNS extends DOMTestCase { 33 34 DOMDocumentBuilderFactory factory; 35 36 DocumentBuilder builder; 37 38 protected void setUp() throws Exception { 39 super.setUp(); 40 try { 41 factory = new DOMDocumentBuilderFactory(DOMDocumentBuilderFactory 42 .getConfiguration2()); 43 builder = factory.getBuilder(); 44 } catch (Exception e) { 45 fail("Unexpected exception" + e.getMessage()); 46 } 47 } 48 49 protected void tearDown() throws Exception { 50 factory = null; 51 builder = null; 52 super.tearDown(); 53 } 54 55 /** 56 * Runs the test case. 57 * 58 * @throws Throwable 59 * Any uncaught exception causes test to fail 60 */ 61 @TestTargetNew( 62 level = TestLevel.PARTIAL_COMPLETE, 63 notes = "Doesn't verify DOMException.", 64 method = "setAttributeNS", 65 args = {java.lang.String.class, java.lang.String.class, java.lang.String.class} 66 ) 67 public void testSetAttributeNS1() throws Throwable { 68 Document doc; 69 Element element; 70 Attr attribute; 71 String attrName; 72 String attrValue; 73 doc = (Document) load("staff", builder); 74 element = doc.createElementNS("http://www.w3.org/DOM", "dom:elem"); 75 element.setAttributeNS("http://www.w3.org/DOM/Test/setAttributeNS", 76 "attr", "value"); 77 attribute = element.getAttributeNodeNS( 78 "http://www.w3.org/DOM/Test/setAttributeNS", "attr"); 79 attrName = attribute.getNodeName(); 80 attrValue = attribute.getNodeValue(); 81 assertEquals("elementsetattributens01_attrName", "attr", attrName); 82 assertEquals("elementsetattributens01_attrValue", "value", attrValue); 83 } 84 @TestTargetNew( 85 level = TestLevel.PARTIAL_COMPLETE, 86 notes = "Doesn't verify DOMException.", 87 method = "setAttributeNS", 88 args = {java.lang.String.class, java.lang.String.class, java.lang.String.class} 89 ) 90 public void testSetAttributeNS2() throws Throwable { 91 Document doc; 92 Element element; 93 Attr attribute; 94 NodeList elementList; 95 String attrName; 96 String attrValue; 97 doc = (Document) load("staff", builder); 98 elementList = doc.getElementsByTagNameNS("*", "address"); 99 element = (Element) elementList.item(0); 100 element.setAttributeNS("http://www.w3.org/DOM/Test/setAttributeNS", 101 "this:street", "Silver Street"); 102 attribute = element.getAttributeNodeNS( 103 "http://www.w3.org/DOM/Test/setAttributeNS", "street"); 104 attrName = attribute.getNodeName(); 105 attrValue = attribute.getNodeValue(); 106 assertEquals("elementsetattributens02_attrName", "this:street", 107 attrName); 108 assertEquals("elementsetattributens02_attrValue", "Silver Street", 109 attrValue); 110 } 111 @TestTargetNew( 112 level = TestLevel.PARTIAL_COMPLETE, 113 notes = "Doesn't verify DOMException.", 114 method = "setAttributeNS", 115 args = {java.lang.String.class, java.lang.String.class, java.lang.String.class} 116 ) 117 public void testSetAttributeNS3() throws Throwable { 118 Document doc; 119 Element element; 120 Attr attribute; 121 NodeList elementList; 122 String attrName; 123 String attrValue; 124 doc = (Document) load("staffNS", builder); 125 elementList = doc.getElementsByTagName("emp:employee"); 126 element = (Element) elementList.item(0); 127 assertNotNull("empEmployeeNotNull", element); 128 element.setAttributeNS("http://www.w3.org/DOM/Test/1", "defaultAttr", 129 "default1"); 130 element.setAttributeNS("http://www.w3.org/DOM/Test/2", "defaultAttr", 131 "default2"); 132 attribute = element.getAttributeNodeNS("http://www.w3.org/DOM/Test/1", 133 "defaultAttr"); 134 attrName = attribute.getNodeName(); 135 attrValue = attribute.getNodeValue(); 136 assertEquals("elementsetattributens03_attrName", "defaultAttr", 137 attrName); 138 assertEquals("elementsetattributens03_attrValue", "default1", attrValue); 139 } 140 @TestTargetNew( 141 level = TestLevel.PARTIAL_COMPLETE, 142 notes = "Verifies DOMException with INVALID_CHARACTER_ERR.", 143 method = "setAttributeNS", 144 args = {java.lang.String.class, java.lang.String.class, java.lang.String.class} 145 ) 146 public void testSetAttributeNS4() throws Throwable { 147 Document doc; 148 Element element; 149 String qualifiedName; 150 List<String> qualifiedNames = new ArrayList<String>(); 151 qualifiedNames.add("/"); 152 qualifiedNames.add("//"); 153 qualifiedNames.add("\\"); 154 qualifiedNames.add(";"); 155 qualifiedNames.add("&"); 156 qualifiedNames.add("*"); 157 qualifiedNames.add("]]"); 158 qualifiedNames.add(">"); 159 qualifiedNames.add("<"); 160 161 doc = (Document) load("staffNS", builder); 162 element = doc.createElementNS("http://www.w3.org/DOM/Test/L2", 163 "dom:elem"); 164 for (int indexN10058 = 0; indexN10058 < qualifiedNames.size(); indexN10058++) { 165 qualifiedName = (String) qualifiedNames.get(indexN10058); 166 167 { 168 boolean success = false; 169 try { 170 element.setAttributeNS("http://www.w3.org/DOM/Test/L2", 171 qualifiedName, "test"); 172 } catch (DOMException ex) { 173 success = (ex.code == DOMException.INVALID_CHARACTER_ERR); 174 } 175 assertTrue("elementsetattributens04", success); 176 } 177 } 178 } 179 @TestTargetNew( 180 level = TestLevel.PARTIAL_COMPLETE, 181 notes = "Verifies DOMException with NAMESPACE_ERR code.", 182 method = "setAttributeNS", 183 args = {java.lang.String.class, java.lang.String.class, java.lang.String.class} 184 ) 185 public void testSetAttributeNS5() throws Throwable { 186 Document doc; 187 Element element; 188 String nullNS = null; 189 190 doc = (Document) load("staffNS", builder); 191 element = doc.createElementNS("http://www.w3.org/DOM/Test/L2", 192 "dom:elem"); 193 194 { 195 boolean success = false; 196 try { 197 element.setAttributeNS(nullNS, "dom:root", "test"); 198 } catch (DOMException ex) { 199 success = (ex.code == DOMException.NAMESPACE_ERR); 200 } 201 assertTrue("elementsetattributens05", success); 202 } 203 } 204 @TestTargetNew( 205 level = TestLevel.PARTIAL_COMPLETE, 206 notes = "Verifies DOMException with NAMESPACE_ERR code.", 207 method = "setAttributeNS", 208 args = {java.lang.String.class, java.lang.String.class, java.lang.String.class} 209 ) 210 public void testSetAttributeNS8() throws Throwable { 211 Document doc; 212 Element element; 213 doc = (Document) load("staffNS", builder); 214 element = doc.createElementNS("http://www.w3.org/DOMTest/level2", 215 "dom:elem"); 216 217 { 218 boolean success = false; 219 try { 220 element.setAttributeNS("http://www.w3.org/DOMTest/level2", 221 "xmlns", "test"); 222 } catch (DOMException ex) { 223 success = (ex.code == DOMException.NAMESPACE_ERR); 224 } 225 assertTrue("elementsetattributens08_Err1", success); 226 } 227 228 { 229 boolean success = false; 230 try { 231 element.setAttributeNS("http://www.w3.org/DOMTest/level2", 232 "xmlns:root", "test"); 233 } catch (DOMException ex) { 234 success = (ex.code == DOMException.NAMESPACE_ERR); 235 } 236 assertTrue("elementsetattributens08_Err2", success); 237 } 238 } 239 @TestTargetNew( 240 level = TestLevel.PARTIAL_COMPLETE, 241 notes = "Verifies DOMException with NAMESPACE_ERR code.", 242 method = "setAttributeNS", 243 args = {java.lang.String.class, java.lang.String.class, java.lang.String.class} 244 ) 245 public void testSetAttributeNSURINull() throws Throwable { 246 String namespaceURI = null; 247 248 String qualifiedName = "emp:qualifiedName"; 249 Document doc; 250 NodeList elementList; 251 Node testAddr; 252 doc = (Document) load("staff", builder); 253 elementList = doc.getElementsByTagName("employee"); 254 testAddr = elementList.item(0); 255 256 { 257 boolean success = false; 258 try { 259 ((Element) /*Node */testAddr).setAttributeNS(namespaceURI, qualifiedName, "newValue"); 260 } catch (DOMException ex) { 261 success = (ex.code == DOMException.NAMESPACE_ERR); 262 } 263 assertTrue("throw_NAMESPACE_ERR", success); 264 } 265 } 266} 267