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 org.w3c.dom.Element; 25 import org.w3c.dom.Document; 26 import org.w3c.dom.Node; 27 import org.w3c.dom.NodeList; 28 import org.w3c.dom.DOMException; 29 import org.w3c.dom.Attr; 30 31 import javax.xml.parsers.DocumentBuilder; 32 33 /** 34 * The "setAttributeNS(namespaceURI,qualifiedName,Value)" method raises a 35 * INVALID_CHARACTER_ERR DOMException if the specified prefix contains an 36 * illegal character. 37 * 38 * Attempt to add a new attribute on the first employee node. An exception 39 * should be raised since the "qualifiedName" has an invalid character. 40 * 41 * @author NIST 42 * @author Mary Brady 43 * @see <a 44 * href="http://www.w3.org/TR/DOM-Level-2-Core/core#xpointer(id('ID-258A00AF')/constant[@name='INVALID_CHARACTER_ERR'])">http://www.w3.org/TR/DOM-Level-2-Core/core#xpointer(id('ID-258A00AF')/constant[@name='INVALID_CHARACTER_ERR'])</a> 45 * @see <a 46 * 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> 47 * @see <a 48 * href="http://www.w3.org/TR/DOM-Level-2-Core/core#xpointer(id('ID-ElSetAttrNS')/raises/exception[@name='DOMException']/descr/p[substring-before(.,':')='INVALID_CHARACTER_ERR'])">http://www.w3.org/TR/DOM-Level-2-Core/core#xpointer(id('ID-ElSetAttrNS')/raises/exception[@name='DOMException']/descr/p[substring-before(.,':')='INVALID_CHARACTER_ERR'])</a> 49 */ 50 public final class SetAttributeNS extends DOMTestCase { 51 52 DOMDocumentBuilderFactory factory; 53 54 DocumentBuilder builder; 55 56 protected void setUp() throws Exception { 57 super.setUp(); 58 try { 59 factory = new DOMDocumentBuilderFactory(DOMDocumentBuilderFactory 60 .getConfiguration1()); 61 builder = factory.getBuilder(); 62 } catch (Exception e) { 63 fail("Unexpected exception" + e.getMessage()); 64 } 65 } 66 67 protected void tearDown() throws Exception { 68 factory = null; 69 builder = null; 70 super.tearDown(); 71 } 72 73 /** 74 * Runs the test case. 75 * 76 * @throws Throwable 77 * Any uncaught exception causes test to fail 78 */ 79 public void testSetAttributeNS1() throws Throwable { 80 String namespaceURI = "http://www.nist.gov"; 81 String qualifiedName = "emp:qual?name"; 82 Document doc; 83 NodeList elementList; 84 Node testAddr; 85 doc = (Document) load("staffNS", builder); 86 elementList = doc.getElementsByTagName("employee"); 87 testAddr = elementList.item(0); 88 89 { 90 boolean success = false; 91 try { 92 ((Element) /* Node */testAddr).setAttributeNS(namespaceURI, 93 qualifiedName, "newValue"); 94 } catch (DOMException ex) { 95 success = (ex.code == DOMException.INVALID_CHARACTER_ERR); 96 } 97 assertTrue("throw_INVALID_CHARACTER_ERR", success); 98 } 99 } 100 public void testSetAttributeNS2() throws Throwable { 101 String namespaceURI = "http://www.nist.gov"; 102 String qualifiedName = "emp:"; 103 Document doc; 104 NodeList elementList; 105 Node testAddr; 106 doc = (Document) load("staffNS", builder); 107 elementList = doc.getElementsByTagName("emp:employee"); 108 testAddr = elementList.item(0); 109 110 { 111 // BEGIN android-changed 112 // Our exception priorities differ from the spec 113 try { 114 ((Element) /* Node */testAddr).setAttributeNS(namespaceURI, 115 qualifiedName, "newValue"); 116 fail(); 117 } catch (DOMException ex) { 118 } 119 // END android-changed 120 } 121 } 122 123 // Assumes validation. 124 // public void testSetAttributeNS3() throws Throwable { 125 // String namespaceURI = "www.xyz.com"; 126 // String qualifiedName = "emp:local1"; 127 // Document doc; 128 // NodeList genderList; 129 // Node gender; 130 // NodeList genList; 131 // Node gen; 132 // NodeList gList; 133 // Element genElement; 134 // int nodeType; 135 // doc = (Document) load("staffNS", builder); 136 // genderList = doc.getElementsByTagName("gender"); 137 // gender = genderList.item(2); 138 // genList = gender.getChildNodes(); 139 // gen = genList.item(0); 140 // nodeType = (int) gen.getNodeType(); 141 // 142 // if (1 == nodeType) { 143 // gen = doc.createEntityReference("ent4"); 144 // assertNotNull("createdEntRefNotNull", gen); 145 // } 146 // gList = gen.getChildNodes(); 147 // genElement = (Element) gList.item(0); 148 // assertNotNull("notnull", genElement); 149 // 150 // { 151 // boolean success = false; 152 // try { 153 // genElement.setAttributeNS(namespaceURI, qualifiedName, 154 // "newValue"); 155 // } catch (DOMException ex) { 156 // success = (ex.code == DOMException.NO_MODIFICATION_ALLOWED_ERR); 157 // } 158 // assertTrue("throw_NO_MODIFICATION_ALLOWED_ERR", success); 159 // } 160 // } 161 public void testSetAttributeNS4() throws Throwable { 162 Document doc; 163 NodeList elementList; 164 Node testAddr; 165 Attr addrAttr; 166 String resultAttr; 167 String resultNamespaceURI; 168 String resultLocalName; 169 String resultPrefix; 170 doc = (Document) load("staffNS", builder); 171 elementList = doc.getElementsByTagName("emp:address"); 172 testAddr = elementList.item(0); 173 assertNotNull("empAddrNotNull", testAddr); 174 ((Element) /* Node */testAddr).setAttributeNS("http://www.nist.gov", 175 "newprefix:zone", "newValue"); 176 addrAttr = ((Element) /* Node */testAddr).getAttributeNodeNS( 177 "http://www.nist.gov", "zone"); 178 resultAttr = ((Element) /* Node */testAddr).getAttributeNS( 179 "http://www.nist.gov", "zone"); 180 assertEquals("attrValue", "newValue", resultAttr); 181 resultNamespaceURI = addrAttr.getNamespaceURI(); 182 assertEquals("nsuri", "http://www.nist.gov", resultNamespaceURI); 183 resultLocalName = addrAttr.getLocalName(); 184 assertEquals("lname", "zone", resultLocalName); 185 resultPrefix = addrAttr.getPrefix(); 186 assertEquals("prefix", "newprefix", resultPrefix); 187 } 188 189 public void testSetAttributeNS5() throws Throwable { 190 String localName = "newAttr"; 191 String namespaceURI = "http://www.newattr.com"; 192 String qualifiedName = "emp:newAttr"; 193 Document doc; 194 NodeList elementList; 195 Node testAddr; 196 197 String resultAttr; 198 doc = (Document) load("staffNS", builder); 199 elementList = doc.getElementsByTagName("emp:address"); 200 testAddr = elementList.item(0); 201 assertNotNull("empAddrNotNull", testAddr); 202 ((Element) /* Node */testAddr).setAttributeNS(namespaceURI, 203 qualifiedName, "<newValue>"); 204 resultAttr = ((Element) /* Node */testAddr).getAttributeNS( 205 namespaceURI, localName); 206 assertEquals("throw_Equals", "<newValue>", resultAttr); 207 } 208 public void testSetAttributeNS6() throws Throwable { 209 String namespaceURI = "http://www.nist.gov"; 210 String qualifiedName = "xml:qualifiedName"; 211 Document doc; 212 NodeList elementList; 213 Node testAddr; 214 doc = (Document) load("staffNS", builder); 215 elementList = doc.getElementsByTagName("employee"); 216 testAddr = elementList.item(0); 217 218 { 219 boolean success = false; 220 try { 221 ((Element) /* Node */testAddr).setAttributeNS(namespaceURI, 222 qualifiedName, "newValue"); 223 } catch (DOMException ex) { 224 success = (ex.code == DOMException.NAMESPACE_ERR); 225 } 226 assertTrue("throw_NAMESPACE_ERR", success); 227 } 228 } 229 public void testSetAttributeNS7() throws Throwable { 230 String namespaceURI = "http://www.nist.gov"; 231 String qualifiedName = "xmlns"; 232 Document doc; 233 NodeList elementList; 234 Node testAddr; 235 doc = (Document) load("staffNS", builder); 236 elementList = doc.getElementsByTagName("employee"); 237 testAddr = elementList.item(0); 238 239 { 240 boolean success = false; 241 try { 242 ((Element) /* Node */testAddr).setAttributeNS(namespaceURI, 243 qualifiedName, "newValue"); 244 } catch (DOMException ex) { 245 success = (ex.code == DOMException.NAMESPACE_ERR); 246 } 247 assertTrue("throw_NAMESPACE_ERR", success); 248 } 249 } 250 public void testSetAttributeNS9() throws Throwable { 251 String localName = "newAttr"; 252 String namespaceURI = "http://www.newattr.com"; 253 String qualifiedName = "emp:newAttr"; 254 Document doc; 255 NodeList elementList; 256 Node testAddr; 257 Attr addrAttr; 258 String resultAttr; 259 String resultNamespaceURI; 260 String resultLocalName; 261 String resultPrefix; 262 doc = (Document) load("staffNS", builder); 263 elementList = doc.getElementsByTagName("emp:address"); 264 testAddr = elementList.item(0); 265 assertNotNull("empAddrNotNull", testAddr); 266 ((Element) /* Node */testAddr).setAttributeNS(namespaceURI, 267 qualifiedName, "newValue"); 268 addrAttr = ((Element) /* Node */testAddr).getAttributeNodeNS( 269 namespaceURI, localName); 270 resultAttr = ((Element) /* Node */testAddr).getAttributeNS( 271 namespaceURI, localName); 272 assertEquals("attrValue", "newValue", resultAttr); 273 resultNamespaceURI = addrAttr.getNamespaceURI(); 274 assertEquals("nsuri", "http://www.newattr.com", resultNamespaceURI); 275 resultLocalName = addrAttr.getLocalName(); 276 assertEquals("lname", "newAttr", resultLocalName); 277 resultPrefix = addrAttr.getPrefix(); 278 assertEquals("prefix", "emp", resultPrefix); 279 } 280 public void testSetAttributeNS10() throws Throwable { 281 String namespaceURI = "http://www.example.gov"; 282 Document doc; 283 NodeList elementList; 284 Node testAddr; 285 doc = (Document) load("hc_staff", builder); 286 elementList = doc.getElementsByTagName("em"); 287 testAddr = elementList.item(0); 288 289 { 290 // BEGIN android-changed 291 // Our exception priorities differ from the spec 292 try { 293 ((Element) /* Node */testAddr).setAttributeNS(namespaceURI, "", 294 "newValue"); 295 fail(); 296 } catch (DOMException ex) { 297 } 298 // END android-changed 299 } 300 } 301 } 302