1 2 /* 3 This Java source file was generated by test-to-java.xsl 4 and is a derived work from the source document. 5 The source document contained the following notice: 6 7 8 9 Copyright (c) 2001 World Wide Web Consortium, 10 (Massachusetts Institute of Technology, Institut National de 11 Recherche en Informatique et en Automatique, Keio University). All 12 Rights Reserved. This program is distributed under the W3C's Software 13 Intellectual Property License. This program is distributed in the 14 hope that it will be useful, but WITHOUT ANY WARRANTY; without even 15 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 16 PURPOSE. 17 18 See W3C License http://www.w3.org/Consortium/Legal/ for more details. 19 20 21 */ 22 23 package tests.org.w3c.dom; 24 25 import dalvik.annotation.TestTargets; 26 import dalvik.annotation.TestLevel; 27 import dalvik.annotation.TestTargetNew; 28 import dalvik.annotation.TestTargetClass; 29 30 import org.w3c.dom.NamedNodeMap; 31 import org.w3c.dom.Document; 32 import org.w3c.dom.Node; 33 import org.w3c.dom.NodeList; 34 import org.w3c.dom.DOMException; 35 36 import javax.xml.parsers.DocumentBuilder; 37 38 /** 39 * The "setNamedItemNS(arg)" method for a 40 * NamedNodeMap should raise INUSE_ATTRIBUTE_ERR DOMException if 41 * arg is an Attr that is already an attribute of another Element object. 42 * 43 * Retrieve an attr node from the third "address" element whose local name 44 * is "domestic" and namespaceURI is "http://www.netzero.com". 45 * Invoke method setNamedItemNS(arg) on the map of the first "address" element with 46 * arg being the attr node from above. Method should raise 47 * INUSE_ATTRIBUTE_ERR DOMException. 48 * @author NIST 49 * @author Mary Brady 50 * @see <a href="http://www.w3.org/TR/DOM-Level-2-Core/core#xpointer(id('ID-258A00AF')/constant[@name='INUSE_ATTRIBUTE_ERR'])">http://www.w3.org/TR/DOM-Level-2-Core/core#xpointer(id('ID-258A00AF')/constant[@name='INUSE_ATTRIBUTE_ERR'])</a> 51 * @see <a href="http://www.w3.org/TR/DOM-Level-2-Core/core#ID-setNamedItemNS">http://www.w3.org/TR/DOM-Level-2-Core/core#ID-setNamedItemNS</a> 52 * @see <a href="http://www.w3.org/TR/DOM-Level-2-Core/core#xpointer(id('ID-setNamedItemNS')/raises/exception[@name='DOMException']/descr/p[substring-before(.,':')='INUSE_ATTRIBUTE_ERR'])">http://www.w3.org/TR/DOM-Level-2-Core/core#xpointer(id('ID-setNamedItemNS')/raises/exception[@name='DOMException']/descr/p[substring-before(.,':')='INUSE_ATTRIBUTE_ERR'])</a> 53 */ 54 @TestTargetClass(NamedNodeMap.class) 55 public final class SetNamedItemNS extends DOMTestCase { 56 57 DOMDocumentBuilderFactory factory; 58 59 DocumentBuilder builder; 60 61 protected void setUp() throws Exception { 62 super.setUp(); 63 try { 64 factory = new DOMDocumentBuilderFactory(DOMDocumentBuilderFactory 65 .getConfiguration2()); 66 builder = factory.getBuilder(); 67 } catch (Exception e) { 68 fail("Unexpected exception" + e.getMessage()); 69 } 70 } 71 72 protected void tearDown() throws Exception { 73 factory = null; 74 builder = null; 75 super.tearDown(); 76 } 77 78 /** 79 * Runs the test case. 80 * @throws Throwable Any uncaught exception causes test to fail 81 */ 82 @TestTargetNew( 83 level = TestLevel.PARTIAL_COMPLETE, 84 notes = "Verifies DOMException with INUSE_ATTRIBUTE_ERR code.", 85 method = "setNamedItemNS", 86 args = {org.w3c.dom.Node.class} 87 ) 88 public void testSetNamedItemNS1() throws Throwable { 89 Document doc; 90 NodeList elementList; 91 Node anotherElement; 92 NamedNodeMap anotherMap; 93 Node arg; 94 Node testAddress; 95 NamedNodeMap map; 96 97 doc = (Document) load("staffNS", builder); 98 elementList = doc.getElementsByTagName("address"); 99 anotherElement = elementList.item(2); 100 anotherMap = anotherElement.getAttributes(); 101 arg = anotherMap.getNamedItemNS("http://www.netzero.com", "domestic"); 102 testAddress = elementList.item(0); 103 map = testAddress.getAttributes(); 104 105 { 106 boolean success = false; 107 try { 108 map.setNamedItemNS(arg); 109 } catch (DOMException ex) { 110 success = (ex.code == DOMException.INUSE_ATTRIBUTE_ERR); 111 } 112 assertTrue("throw_INUSE_ATTRIBUTE_ERR", success); 113 } 114 } 115 @TestTargetNew( 116 level = TestLevel.PARTIAL_COMPLETE, 117 notes = "Verifies DOMException with WRONG_DOCUMENT_ERR code.", 118 method = "setNamedItemNS", 119 args = {org.w3c.dom.Node.class} 120 ) 121 public void testSetNamedItemNS2() throws Throwable { 122 String namespaceURI = "http://www.usa.com"; 123 String qualifiedName = "dmstc:domestic"; 124 Document doc; 125 Document anotherDoc; 126 Node arg; 127 NodeList elementList; 128 Node testAddress; 129 NamedNodeMap attributes; 130 131 doc = (Document) load("staffNS", builder); 132 anotherDoc = (Document) load("staffNS", builder); 133 arg = anotherDoc.createAttributeNS(namespaceURI, qualifiedName); 134 arg.setNodeValue("Maybe"); 135 elementList = doc.getElementsByTagName("address"); 136 testAddress = elementList.item(0); 137 attributes = testAddress.getAttributes(); 138 139 { 140 boolean success = false; 141 try { 142 attributes.setNamedItemNS(arg); 143 } catch (DOMException ex) { 144 success = (ex.code == DOMException.WRONG_DOCUMENT_ERR); 145 } 146 assertTrue("throw_WRONG_DOCUMENT_ERR", success); 147 } 148 } 149 @TestTargetNew( 150 level = TestLevel.PARTIAL_COMPLETE, 151 notes = "Verifies positive fnctionality.", 152 method = "getNamedItemNS", 153 args = {java.lang.String.class, java.lang.String.class} 154 ) 155 public void testSetNamedItemNS3() throws Throwable { 156 String namespaceURI = "http://www.nist.gov"; 157 String qualifiedName = "prefix:newAttr"; 158 Document doc; 159 Node arg; 160 NodeList elementList; 161 Node testAddress; 162 NamedNodeMap attributes; 163 Node retnode; 164 String value; 165 166 doc = (Document) load("staffNS", builder); 167 arg = doc.createAttributeNS(namespaceURI, qualifiedName); 168 arg.setNodeValue("newValue"); 169 elementList = doc.getElementsByTagName("address"); 170 testAddress = elementList.item(0); 171 attributes = testAddress.getAttributes(); 172 attributes.setNamedItemNS(arg); 173 retnode = attributes.getNamedItemNS(namespaceURI, "newAttr"); 174 value = retnode.getNodeValue(); 175 assertEquals("throw_Equals", "newValue", value); 176 } 177 178 // Assumes validation. 179 // public void testSetNamedItemNS4() throws Throwable { 180 // String namespaceURI = "http://www.w3.org/2000/xmlns/"; 181 // String localName = "local1"; 182 // Document doc; 183 // NodeList elementList; 184 // Node testAddress; 185 // NodeList nList; 186 // Node child; 187 // NodeList n2List; 188 // Node child2; 189 // NamedNodeMap attributes; 190 // Node arg; 191 // 192 // int nodeType; 193 // doc = (Document) load("staffNS", builder); 194 // elementList = doc.getElementsByTagName("gender"); 195 // testAddress = elementList.item(2); 196 // nList = testAddress.getChildNodes(); 197 // child = nList.item(0); 198 // nodeType = (int) child.getNodeType(); 199 // 200 // if (1 == nodeType) { 201 // child = doc.createEntityReference("ent4"); 202 // assertNotNull("createdEntRefNotNull", child); 203 // } 204 // n2List = child.getChildNodes(); 205 // child2 = n2List.item(0); 206 // assertNotNull("notnull", child2); 207 // attributes = child2.getAttributes(); 208 // arg = attributes.getNamedItemNS(namespaceURI, localName); 209 // 210 // { 211 // boolean success = false; 212 // try { 213 // attributes.setNamedItemNS(arg); 214 // } catch (DOMException ex) { 215 // success = (ex.code == DOMException.NO_MODIFICATION_ALLOWED_ERR); 216 // } 217 // assertTrue("throw_NO_MODIFICATION_ALLOWED_ERR", success); 218 // } 219 // } 220 @TestTargetNew( 221 level = TestLevel.PARTIAL_COMPLETE, 222 notes = "Verifies positive fnctionality.", 223 method = "setNamedItemNS", 224 args = {org.w3c.dom.Node.class} 225 ) 226 public void testSetNamedItemNS5() throws Throwable { 227 String namespaceURI = "http://www.usa.com"; 228 String qualifiedName = "dmstc:domestic"; 229 Document doc; 230 Node arg; 231 NodeList elementList; 232 Node testAddress; 233 NamedNodeMap attributes; 234 Node retnode; 235 String value; 236 doc = (Document) load("staffNS", builder); 237 arg = doc.createAttributeNS(namespaceURI, qualifiedName); 238 arg.setNodeValue("newValue"); 239 elementList = doc.getElementsByTagName("address"); 240 testAddress = elementList.item(0); 241 attributes = testAddress.getAttributes(); 242 retnode = attributes.setNamedItemNS(arg); 243 value = retnode.getNodeValue(); 244 assertEquals("throw_Equals", "Yes", value); 245 } 246 247 } 248 249