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 dalvik.annotation.TestTargets; 25 import dalvik.annotation.TestLevel; 26 import dalvik.annotation.TestTargetNew; 27 import dalvik.annotation.TestTargetClass; 28 29 import org.w3c.dom.NamedNodeMap; 30 import org.w3c.dom.Document; 31 import org.w3c.dom.Attr; 32 import org.w3c.dom.Node; 33 import org.w3c.dom.NodeList; 34 import org.w3c.dom.Element; 35 import org.w3c.dom.DOMException; 36 import org.w3c.dom.DOMImplementation; 37 import org.w3c.dom.DocumentType; 38 39 import javax.xml.parsers.DocumentBuilder; 40 41 /** 42 * The method setNamedItemNS adds a node using its namespaceURI and localName. 43 * If a node with that namespace URI and that local name is already present in 44 * this map, it is replaced by the new one. 45 * 46 * Retreive the first element whose localName is address and namespaceURI 47 * http://www.nist.gov", and put its attributes into a named node map. Create a 48 * new attribute node and add it to this map. Verify if the attr node was 49 * successfully added by checking the nodeName of the retreived atttribute. 50 * 51 * @author IBM 52 * @author Neil Delima 53 * @see <a 54 * href="http://www.w3.org/TR/DOM-Level-2-Core/core#ID-getNamedItemNS">http://www.w3.org/TR/DOM-Level-2-Core/core#ID-getNamedItemNS</a> 55 */ 56 @TestTargetClass(NamedNodeMap.class) 57 public final class NamedNodeMapSetNamedItemNS extends DOMTestCase { 58 59 DOMDocumentBuilderFactory factory; 60 61 DocumentBuilder builder; 62 63 protected void setUp() throws Exception { 64 super.setUp(); 65 try { 66 // Changed to configuration #2. This test case just doesn't make 67 // sense without a namespace-aware parser. It actually fails even 68 // on the JDK in that case. 69 factory = new DOMDocumentBuilderFactory(DOMDocumentBuilderFactory 70 .getConfiguration2()); 71 builder = factory.getBuilder(); 72 } catch (Exception e) { 73 fail("Unexpected exception" + e.getMessage()); 74 } 75 } 76 77 protected void tearDown() throws Exception { 78 factory = null; 79 builder = null; 80 super.tearDown(); 81 } 82 83 /** 84 * Runs the test case. 85 * 86 * @throws Throwable 87 * Any uncaught exception causes test to fail 88 */ 89 @TestTargetNew( 90 level = TestLevel.PARTIAL, 91 notes = "Doesn't verify DOMException.", 92 method = "setNamedItemNS", 93 args = {org.w3c.dom.Node.class} 94 ) 95 public void testSetNamedItemNS1() throws Throwable { 96 Document doc; 97 NamedNodeMap attributes; 98 Node element; 99 Attr attribute; 100 101 Attr newAttr1; 102 NodeList elementList; 103 String attrName; 104 doc = (Document) load("staffNS", builder); 105 elementList = doc.getElementsByTagNameNS("http://www.nist.gov", 106 "address"); 107 element = elementList.item(0); 108 attributes = element.getAttributes(); 109 newAttr1 = doc.createAttributeNS("http://www.w3.org/DOM/L1", "streets"); 110 ((Element) /* Node */element).setAttributeNodeNS(newAttr1); 111 attribute = (Attr) attributes.getNamedItemNS( 112 "http://www.w3.org/DOM/L1", "streets"); 113 attrName = attribute.getNodeName(); 114 assertEquals("namednodemapsetnameditemns01", "streets", attrName); 115 } 116 @TestTargetNew( 117 level = TestLevel.PARTIAL, 118 notes = "Doesn't verify DOMException.", 119 method = "setNamedItemNS", 120 args = {org.w3c.dom.Node.class} 121 ) 122 public void testSetNamedItemNS2() throws Throwable { 123 Document doc; 124 NamedNodeMap attributes; 125 Element element; 126 Attr attribute; 127 Attr attribute1; 128 129 String attrName; 130 doc = (Document) load("staffNS", builder); 131 element = doc.createElementNS("http://www.w3.org/DOM/Test", "root"); 132 attribute1 = doc 133 .createAttributeNS("http://www.w3.org/DOM/L1", "L1:att"); 134 attributes = element.getAttributes(); 135 attributes.setNamedItemNS(attribute1); 136 attribute = (Attr) attributes.getNamedItemNS( 137 "http://www.w3.org/DOM/L1", "att"); 138 attrName = attribute.getNodeName(); 139 assertEquals("namednodemapsetnameditemns02", "L1:att", attrName); 140 } 141 @TestTargetNew( 142 level = TestLevel.PARTIAL, 143 notes = "Verifies that setNamedItemNS throws DOMException with WRONG_DOCUMENT_ERR code.", 144 method = "setNamedItemNS", 145 args = {org.w3c.dom.Node.class} 146 ) 147 public void testSetNamedItemNS3() throws Throwable { 148 149 Document doc; 150 Document docAlt; 151 NamedNodeMap attributes; 152 NamedNodeMap attributesAlt; 153 NodeList elementList; 154 NodeList elementListAlt; 155 Element element; 156 Element elementAlt; 157 Attr attr; 158 159 String nullNS = null; 160 161 doc = (Document) load("staffNS", builder); 162 elementList = doc.getElementsByTagNameNS("*", "address"); 163 element = (Element) elementList.item(1); 164 attributes = element.getAttributes(); 165 docAlt = (Document) load("staffNS", builder); 166 elementListAlt = docAlt.getElementsByTagNameNS("*", "address"); 167 elementAlt = (Element) elementListAlt.item(1); 168 attributesAlt = elementAlt.getAttributes(); 169 attr = (Attr) attributesAlt.getNamedItemNS(nullNS, "street"); 170 attributesAlt.removeNamedItemNS(nullNS, "street"); 171 172 { 173 boolean success = false; 174 try { 175 attributes.setNamedItemNS(attr); 176 } catch (DOMException ex) { 177 success = (ex.code == DOMException.WRONG_DOCUMENT_ERR); 178 } 179 assertTrue("throw_WRONG_DOCUMENT_ERR", success); 180 } 181 } 182 @TestTargetNew( 183 level = TestLevel.PARTIAL, 184 notes = "Verifies that setNamedItemNS throws DOMException with WRONG_DOCUMENT_ERR code.", 185 method = "setNamedItemNS", 186 args = {org.w3c.dom.Node.class} 187 ) 188 public void testSetNamedItemNS4() throws Throwable { 189 Document doc; 190 DOMImplementation domImpl; 191 Document docAlt; 192 DocumentType docType = null; 193 194 NamedNodeMap attributes; 195 NodeList elementList; 196 Element element; 197 Attr attrAlt; 198 199 String nullNS = null; 200 201 doc = (Document) load("staffNS", builder); 202 elementList = doc.getElementsByTagNameNS("*", "address"); 203 element = (Element) elementList.item(1); 204 attributes = element.getAttributes(); 205 domImpl = doc.getImplementation(); 206 docAlt = domImpl.createDocument(nullNS, "newDoc", docType); 207 attrAlt = docAlt.createAttributeNS(nullNS, "street"); 208 209 { 210 boolean success = false; 211 try { 212 attributes.setNamedItemNS(attrAlt); 213 } catch (DOMException ex) { 214 success = (ex.code == DOMException.WRONG_DOCUMENT_ERR); 215 } 216 assertTrue("throw_WRONG_DOCUMENT_ERR", success); 217 } 218 } 219 220 // Assumes validation. 221 // public void testSetNamedItemNS5() throws Throwable { 222 // Document doc; 223 // DocumentType docType; 224 // NamedNodeMap entities; 225 // NamedNodeMap notations; 226 // Entity entity; 227 // Notation notation; 228 // 229 // doc = (Document) load("staffNS", builder); 230 // docType = doc.getDoctype(); 231 // entities = docType.getEntities(); 232 // assertNotNull("entitiesNotNull", entities); 233 // notations = docType.getNotations(); 234 // assertNotNull("notationsNotNull", notations); 235 // entity = (Entity) entities.getNamedItem("ent1"); 236 // notation = (Notation) notations.getNamedItem("notation1"); 237 // 238 // { 239 // boolean success = false; 240 // try { 241 // entities.setNamedItemNS(entity); 242 // } catch (DOMException ex) { 243 // success = (ex.code == DOMException.NO_MODIFICATION_ALLOWED_ERR); 244 // } 245 // assertTrue("throw_NO_MODIFICATION_ALLOWED_ERR_entities", success); 246 // } 247 // 248 // { 249 // boolean success = false; 250 // try { 251 // notations.setNamedItemNS(notation); 252 // } catch (DOMException ex) { 253 // success = (ex.code == DOMException.NO_MODIFICATION_ALLOWED_ERR); 254 // } 255 // assertTrue("throw_NO_MODIFICATION_ALLOWED_ERR_notations", success); 256 // } 257 // } 258 @TestTargetNew( 259 level = TestLevel.PARTIAL, 260 notes = "Verifies that setNamedItemNS throws DOMException with INUSE_ATTRIBUTE_ERR code.", 261 method = "setNamedItemNS", 262 args = {org.w3c.dom.Node.class} 263 ) 264 public void testSetNamedItemNS6() throws Throwable { 265 Document doc; 266 NamedNodeMap attributes; 267 NodeList elementList; 268 Element element; 269 Attr attr; 270 271 doc = (Document) load("staffNS", builder); 272 elementList = doc.getElementsByTagNameNS("*", "address"); 273 element = (Element) elementList.item(0); 274 attributes = element.getAttributes(); 275 attr = (Attr) attributes.getNamedItemNS("http://www.usa.com", 276 "domestic"); 277 element = (Element) elementList.item(1); 278 attributes = element.getAttributes(); 279 280 { 281 boolean success = false; 282 try { 283 attributes.setNamedItemNS(attr); 284 } catch (DOMException ex) { 285 success = (ex.code == DOMException.INUSE_ATTRIBUTE_ERR); 286 } 287 assertTrue("namednodemapsetnameditemns06", success); 288 } 289 } 290 @TestTargetNew( 291 level = TestLevel.PARTIAL, 292 notes = "Verifies that setNamedItemNS throws DOMException with INUSE_ATTRIBUTE_ERR code.", 293 method = "setNamedItemNS", 294 args = {org.w3c.dom.Node.class} 295 ) 296 public void testSetNamedItemNS7() throws Throwable { 297 Document doc; 298 NamedNodeMap attributes; 299 NodeList elementList; 300 Element element; 301 Attr attr; 302 303 doc = (Document) load("staffNS", builder); 304 elementList = doc.getElementsByTagNameNS("*", "address"); 305 element = (Element) elementList.item(0); 306 attributes = element.getAttributes(); 307 attr = (Attr) attributes.getNamedItemNS("http://www.usa.com", 308 "domestic"); 309 element = (Element) elementList.item(1); 310 attributes = element.getAttributes(); 311 312 { 313 boolean success = false; 314 try { 315 attributes.setNamedItemNS(attr); 316 } catch (DOMException ex) { 317 success = (ex.code == DOMException.INUSE_ATTRIBUTE_ERR); 318 } 319 assertTrue("namednodemapsetnameditemns07", success); 320 } 321 } 322 @TestTargetNew( 323 level = TestLevel.PARTIAL, 324 notes = "Verifies that setNamedItemNS throws DOMException with INUSE_ATTRIBUTE_ERR code.", 325 method = "setNamedItemNS", 326 args = {org.w3c.dom.Node.class} 327 ) 328 public void testSetNamedItemNS8() throws Throwable { 329 Document doc; 330 NamedNodeMap attributes; 331 NodeList elementList; 332 Element element; 333 Attr attr; 334 335 doc = (Document) load("staffNS", builder); 336 elementList = doc.getElementsByTagNameNS("*", "address"); 337 element = (Element) elementList.item(0); 338 attributes = element.getAttributes(); 339 attr = (Attr) attributes.getNamedItemNS("http://www.usa.com", 340 "domestic"); 341 element = (Element) elementList.item(1); 342 attributes = element.getAttributes(); 343 344 { 345 boolean success = false; 346 try { 347 attributes.setNamedItemNS(attr); 348 } catch (DOMException ex) { 349 success = (ex.code == DOMException.INUSE_ATTRIBUTE_ERR); 350 } 351 assertTrue("namednodemapsetnameditemns08", success); 352 } 353 } 354 355 // Assumes validation. 356 // public void testSetNamedItemNS9() throws Throwable { 357 // Document doc; 358 // DocumentType docType; 359 // NamedNodeMap entities; 360 // NamedNodeMap notations; 361 // Attr attr; 362 // doc = (Document) load("staffNS", builder); 363 // docType = doc.getDoctype(); 364 // entities = docType.getEntities(); 365 // notations = docType.getNotations(); 366 // attr = doc.createAttributeNS("http://www.w3.org/DOM/Test", "test"); 367 // 368 // { 369 // boolean success = false; 370 // try { 371 // entities.setNamedItemNS(attr); 372 // } catch (DOMException ex) { 373 // success = (ex.code == DOMException.NO_MODIFICATION_ALLOWED_ERR); 374 // } 375 // assertTrue("throw_NO_MODIFICATION_ALLOWED_ERR_entities", success); 376 // } 377 // 378 // { 379 // boolean success = false; 380 // try { 381 // notations.setNamedItemNS(attr); 382 // } catch (DOMException ex) { 383 // success = (ex.code == DOMException.NO_MODIFICATION_ALLOWED_ERR); 384 // } 385 // assertTrue("throw_NO_MODIFICATION_ALLOWED_ERR_notations", success); 386 // } 387 // } 388 389 // Assumes validation. 390 // public void testSetNamedItemNS10() throws Throwable { 391 // Document doc; 392 // DocumentType docType; 393 // NamedNodeMap entities; 394 // NamedNodeMap attributes; 395 // Entity entity; 396 // 397 // Element element; 398 // NodeList elementList; 399 // 400 // doc = (Document) load("staffNS", builder); 401 // docType = doc.getDoctype(); 402 // entities = docType.getEntities(); 403 // assertNotNull("entitiesNotNull", entities); 404 // entity = (Entity) entities.getNamedItem("ent1"); 405 // elementList = doc.getElementsByTagNameNS("http://www.nist.gov", 406 // "address"); 407 // element = (Element) elementList.item(0); 408 // attributes = element.getAttributes(); 409 // 410 // { 411 // boolean success = false; 412 // try { 413 // attributes.setNamedItemNS(entity); 414 // } catch (DOMException ex) { 415 // success = (ex.code == DOMException.HIERARCHY_REQUEST_ERR); 416 // } 417 // assertTrue("throw_HIERARCHY_REQUEST_ERR", success); 418 // } 419 // } 420 421 // Assumes validation. 422 // public void testSetNamedItemNS11() throws Throwable { 423 // Document doc; 424 // DocumentType docType; 425 // NamedNodeMap notations; 426 // NamedNodeMap attributes; 427 // Notation notation; 428 // Element element; 429 // NodeList elementList; 430 // 431 // doc = (Document) load("staffNS", builder); 432 // docType = doc.getDoctype(); 433 // notations = docType.getNotations(); 434 // assertNotNull("notationsNotNull", notations); 435 // notation = (Notation) notations.getNamedItem("notation1"); 436 // elementList = doc.getElementsByTagNameNS("http://www.nist.gov", 437 // "address"); 438 // element = (Element) elementList.item(0); 439 // attributes = element.getAttributes(); 440 // 441 // { 442 // boolean success = false; 443 // try { 444 // attributes.setNamedItemNS(notation); 445 // } catch (DOMException ex) { 446 // success = (ex.code == DOMException.HIERARCHY_REQUEST_ERR); 447 // } 448 // assertTrue("throw_HIERARCHY_REQUEST_ERR", success); 449 // } 450 // } 451 } 452