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 22package tests.org.w3c.dom; 23 24import dalvik.annotation.TestTargets; 25import dalvik.annotation.TestLevel; 26import dalvik.annotation.TestTargetNew; 27import dalvik.annotation.TestTargetClass; 28 29import java.util.ArrayList; 30import java.util.List; 31 32import org.w3c.dom.Document; 33import org.w3c.dom.Attr; 34import org.w3c.dom.DOMException; 35import org.w3c.dom.DocumentType; 36import org.w3c.dom.DOMImplementation; 37 38import javax.xml.parsers.DocumentBuilder; 39 40/** 41 * The method createAttributeNS creates an attribute of the given qualified name 42 * and namespace URI 43 * 44 * Invoke the createAttributeNS method on this Document object with a null 45 * namespaceURI, and a qualifiedName without a prefix. This should return a 46 * valid Attr node object. 47 * 48 * @author IBM 49 * @author Neil Delima 50 * @see <a 51 * href="http://www.w3.org/TR/DOM-Level-2-Core/core">http://www.w3.org/TR/DOM-Level-2-Core/core</a> 52 * @see <a 53 * href="http://www.w3.org/TR/DOM-Level-2-Core/core#ID-DocCrAttrNS">http://www.w3.org/TR/DOM-Level-2-Core/core#ID-DocCrAttrNS</a> 54 */ 55@TestTargetClass(Document.class) 56public final class DocumentCreateAttributeNS extends DOMTestCase { 57 58 DOMDocumentBuilderFactory factory; 59 60 DocumentBuilder builder; 61 62 protected void setUp() throws Exception { 63 super.setUp(); 64 try { 65 factory = new DOMDocumentBuilderFactory(DOMDocumentBuilderFactory 66 .getConfiguration1()); 67 builder = factory.getBuilder(); 68 } catch (Exception e) { 69 fail("Unexpected exception" + e.getMessage()); 70 } 71 } 72 73 protected void tearDown() throws Exception { 74 factory = null; 75 builder = null; 76 super.tearDown(); 77 } 78 79 /** 80 * Runs the test case. 81 * 82 * @throws Throwable 83 * Any uncaught exception causes test to fail 84 */ 85 @TestTargetNew( 86 level = TestLevel.PARTIAL_COMPLETE, 87 notes = "Verifies positive functionality.", 88 method = "createAttributeNS", 89 args = {java.lang.String.class, java.lang.String.class} 90 ) 91 public void testCreateAttributeNS1() throws Throwable { 92 Document doc; 93 Attr attribute; 94 String namespaceURI = null; 95 96 String qualifiedName = "test"; 97 98 String nodeName; 99 100 doc = (Document) load("staffNS", builder); 101 attribute = doc.createAttributeNS(namespaceURI, qualifiedName); 102 nodeName = attribute.getNodeName(); 103 104 assertEquals("documentcreateattributeNS01", "test", nodeName); 105 } 106 107 /** 108 * Runs the test case. 109 * 110 * @throws Throwable 111 * Any uncaught exception causes test to fail 112 */ 113 @TestTargetNew( 114 level = TestLevel.PARTIAL_COMPLETE, 115 notes = "Verifies positive functionality.", 116 method = "createAttributeNS", 117 args = {java.lang.String.class, java.lang.String.class} 118 ) 119 public void testCreateAttributeNS2() throws Throwable { 120 Document doc; 121 Attr attribute1; 122 Attr attribute2; 123 String name; 124 String nodeName; 125 String nodeValue; 126 String prefix; 127 String namespaceURI; 128 doc = (Document) load("staffNS", builder); 129 attribute1 = doc.createAttributeNS( 130 "http://www.w3.org/XML/1998/namespace", "xml:xml"); 131 name = attribute1.getName(); 132 nodeName = attribute1.getNodeName(); 133 nodeValue = attribute1.getNodeValue(); 134 prefix = attribute1.getPrefix(); 135 namespaceURI = attribute1.getNamespaceURI(); 136 assertEquals("documentcreateattributeNS02_att1_name", "xml:xml", name); 137 assertEquals("documentcreateattributeNS02_att1_nodeName", "xml:xml", 138 nodeName); 139 assertEquals("documentcreateattributeNS02_att1_nodeValue", "", 140 nodeValue); 141 assertEquals("documentcreateattributeNS02_att1_prefix", "xml", prefix); 142 assertEquals("documentcreateattributeNS02_att1_namespaceURI", 143 "http://www.w3.org/XML/1998/namespace", namespaceURI); 144 attribute2 = doc.createAttributeNS("http://www.w3.org/2000/xmlns/", 145 "xmlns"); 146 name = attribute2.getName(); 147 nodeName = attribute2.getNodeName(); 148 nodeValue = attribute2.getNodeValue(); 149 prefix = attribute2.getPrefix(); 150 namespaceURI = attribute2.getNamespaceURI(); 151 assertEquals("documentcreateattributeNS02_att2_name", "xmlns", name); 152 assertEquals("documentcreateattributeNS02_att2_nodeName", "xmlns", 153 nodeName); 154 assertEquals("documentcreateattributeNS02_att2_nodeValue", "", 155 nodeValue); 156 assertEquals("documentcreateattributeNS02_att2_namespaceURI", 157 "http://www.w3.org/2000/xmlns/", namespaceURI); 158 } 159 @TestTargetNew( 160 level = TestLevel.PARTIAL_COMPLETE, 161 notes = "Verifies that createAttributeNS throws DOMException with INVALID_CHARACTER_ERR code.", 162 method = "createAttributeNS", 163 args = {java.lang.String.class, java.lang.String.class} 164 ) 165 public void testCreateAttributeNS3() throws Throwable { 166 Document doc; 167 168 String namespaceURI = "http://www.w3.org/DOM/Test/Level2"; 169 String qualifiedName; 170 List<String> qualifiedNames = new ArrayList<String>(); 171 qualifiedNames.add("/"); 172 qualifiedNames.add("//"); 173 qualifiedNames.add("\\"); 174 qualifiedNames.add(";"); 175 qualifiedNames.add("&"); 176 qualifiedNames.add("*"); 177 qualifiedNames.add("]]"); 178 qualifiedNames.add(">"); 179 qualifiedNames.add("<"); 180 181 doc = (Document) load("staffNS", builder); 182 for (int indexN1005A = 0; indexN1005A < qualifiedNames.size(); indexN1005A++) { 183 qualifiedName = (String) qualifiedNames.get(indexN1005A); 184 185 { 186 boolean success = false; 187 try { 188 doc.createAttributeNS(namespaceURI, qualifiedName); 189 } catch (DOMException ex) { 190 success = (ex.code == DOMException.INVALID_CHARACTER_ERR); 191 } 192 assertTrue("documentcreateattributeNS03", success); 193 } 194 } 195 } 196 @TestTargetNew( 197 level = TestLevel.PARTIAL_COMPLETE, 198 notes = "Verifies that createAttributeNS throws DOMException with NAMESPACE_ERR code.", 199 method = "createAttributeNS", 200 args = {java.lang.String.class, java.lang.String.class} 201 ) 202 public void testCreateAttributeNS4() throws Throwable { 203 Document doc; 204 205 String namespaceURI = "http://www.w3.org/DOM/Test/Level2"; 206 String qualifiedName; 207 List<String> qualifiedNames = new ArrayList<String>(); 208 qualifiedNames.add("_:"); 209 qualifiedNames.add(":0a"); 210 qualifiedNames.add(":"); 211 qualifiedNames.add("a:b:c"); 212 qualifiedNames.add("_::a"); 213 214 doc = (Document) load("staffNS", builder); 215 for (int indexN1004E = 0; indexN1004E < qualifiedNames.size(); indexN1004E++) { 216 qualifiedName = (String) qualifiedNames.get(indexN1004E); 217 218 { 219 220 // BEGIN android-changed 221 // Our exception priorities differ from the spec 222 try { 223 doc.createAttributeNS(namespaceURI, qualifiedName); 224 fail(); 225 } catch (DOMException ex) { 226 } 227 // END android-changed 228 } 229 } 230 } 231 @TestTargetNew( 232 level = TestLevel.PARTIAL_COMPLETE, 233 notes = "Verifies that createAttributeNS throws DOMException with NAMESPACE_ERR code.", 234 method = "createAttributeNS", 235 args = {java.lang.String.class, java.lang.String.class} 236 ) 237 public void testCreateAttributeNS5() throws Throwable { 238 Document doc; 239 Document newDoc; 240 DocumentType docType = null; 241 242 DOMImplementation domImpl; 243 244 String namespaceURI = null; 245 246 String qualifiedName = "abc:def"; 247 doc = (Document) load("staffNS", builder); 248 domImpl = doc.getImplementation(); 249 newDoc = domImpl.createDocument("http://www.w3.org/DOM/Test", 250 "dom:doc", docType); 251 252 { 253 boolean success = false; 254 try { 255 newDoc.createAttributeNS(namespaceURI, qualifiedName); 256 } catch (DOMException ex) { 257 success = (ex.code == DOMException.NAMESPACE_ERR); 258 } 259 assertTrue("documentcreateattributeNS05", success); 260 } 261 } 262 @TestTargetNew( 263 level = TestLevel.PARTIAL_COMPLETE, 264 notes = "Verifies that createAttributeNS throws DOMException with NAMESPACE_ERR code.", 265 method = "createElementNS", 266 args = {java.lang.String.class, java.lang.String.class} 267 ) 268 public void testCreateAttributeNS6() throws Throwable { 269 Document doc; 270 Document newDoc; 271 DocumentType docType = null; 272 273 DOMImplementation domImpl; 274 275 String namespaceURI = "http://www.w3.org/XML/1998 /namespace"; 276 String qualifiedName = "xml:root"; 277 doc = (Document) load("staffNS", builder); 278 domImpl = doc.getImplementation(); 279 newDoc = domImpl.createDocument("http://www.w3.org/DOM/Test", 280 "dom:doc", docType); 281 282 { 283 boolean success = false; 284 try { 285 newDoc.createAttributeNS(namespaceURI, qualifiedName); 286 } catch (DOMException ex) { 287 success = (ex.code == DOMException.NAMESPACE_ERR); 288 } 289 assertTrue("documentcreateattributeNS06", success); 290 } 291 } 292 @TestTargetNew( 293 level = TestLevel.PARTIAL_COMPLETE, 294 notes = "Verifies that createAttributeNS throws DOMException with NAMESPACE_ERR code.", 295 method = "createAttributeNS", 296 args = {java.lang.String.class, java.lang.String.class} 297 ) 298 public void testCreateAttributeNS7() throws Throwable { 299 Document doc; 300 301 String namespaceURI = "http://www.W3.org/2000/xmlns"; 302 String qualifiedName = "xmlns"; 303 doc = (Document) load("staffNS", builder); 304 305 { 306 boolean success = false; 307 try { 308 doc.createAttributeNS(namespaceURI, qualifiedName); 309 } catch (DOMException ex) { 310 success = (ex.code == DOMException.NAMESPACE_ERR); 311 } 312 assertTrue("documentcreateattributeNS07", success); 313 } 314 } 315} 316