1 package tests.org.w3c.dom; 2 3 import org.w3c.dom.Document; 4 import org.w3c.dom.Element; 5 import org.w3c.dom.DOMException; 6 import org.w3c.dom.DocumentType; 7 import org.w3c.dom.DOMImplementation; 8 9 import javax.xml.parsers.DocumentBuilder; 10 11 /** 12 * The method createElementNS creates an element of the given valid 13 * qualifiedName and NamespaceURI. 14 * 15 * Invoke the createElementNS method on this Document object with a valid 16 * namespaceURI and qualifiedName. Check if a valid Element object is returned 17 * with the same node attributes. 18 * 19 * @author IBM 20 * @author Neil Delima 21 * @see <a 22 * href="http://www.w3.org/TR/DOM-Level-2-Core/core">http://www.w3.org/TR/DOM-Level-2-Core/core</a> 23 * @see <a 24 * href="http://www.w3.org/TR/DOM-Level-2-Core/core#ID-DocCrElNS">http://www.w3.org/TR/DOM-Level-2-Core/core#ID-DocCrElNS</a> 25 */ 26 public final class DocumentCreateElementNS extends DOMTestCase { 27 28 DOMDocumentBuilderFactory factory; 29 30 DocumentBuilder builder; 31 setUp()32 protected void setUp() throws Exception { 33 super.setUp(); 34 try { 35 factory = new DOMDocumentBuilderFactory(DOMDocumentBuilderFactory 36 .getConfiguration1()); 37 builder = factory.getBuilder(); 38 } catch (Exception e) { 39 fail("Unexpected exception" + e.getMessage()); 40 } 41 } 42 tearDown()43 protected void tearDown() throws Exception { 44 factory = null; 45 builder = null; 46 super.tearDown(); 47 } 48 49 /** 50 * Runs the test case. 51 * 52 * @throws Throwable 53 * Any uncaught exception causes test to fail 54 */ testCreateElementNS1()55 public void testCreateElementNS1() throws Throwable { 56 Document doc; 57 Element element; 58 String namespaceURI = "http://www.w3.org/DOM/Test/level2"; 59 String qualifiedName = "XML:XML"; 60 String nodeName; 61 String nsURI; 62 String localName; 63 String prefix; 64 String tagName; 65 doc = (Document) load("staffNS", builder); 66 element = doc.createElementNS(namespaceURI, qualifiedName); 67 nodeName = element.getNodeName(); 68 nsURI = element.getNamespaceURI(); 69 localName = element.getLocalName(); 70 prefix = element.getPrefix(); 71 tagName = element.getTagName(); 72 assertEquals("documentcreateelementNS01_nodeName", "XML:XML", nodeName); 73 assertEquals("documentcreateelementNS01_namespaceURI", 74 "http://www.w3.org/DOM/Test/level2", nsURI); 75 assertEquals("documentcreateelementNS01_localName", "XML", localName); 76 assertEquals("documentcreateelementNS01_prefix", "XML", prefix); 77 assertEquals("documentcreateelementNS01_tagName", "XML:XML", tagName); 78 } testCreateElementNS2()79 public void testCreateElementNS2() throws Throwable { 80 Document doc; 81 82 String namespaceURI = null; 83 84 String qualifiedName = "^^"; 85 doc = (Document) load("staffNS", builder); 86 87 { 88 boolean success = false; 89 try { 90 doc.createElementNS(namespaceURI, qualifiedName); 91 } catch (DOMException ex) { 92 success = (ex.code == DOMException.INVALID_CHARACTER_ERR); 93 } 94 assertTrue("documentcreateelementNS02", success); 95 } 96 } testCreateElementNS5()97 public void testCreateElementNS5() throws Throwable { 98 Document doc; 99 100 String namespaceURI = null; 101 102 String qualifiedName = "null:xml"; 103 doc = (Document) load("staffNS", builder); 104 105 { 106 boolean success = false; 107 try { 108 doc.createElementNS(namespaceURI, qualifiedName); 109 } catch (DOMException ex) { 110 success = (ex.code == DOMException.NAMESPACE_ERR); 111 } 112 assertTrue("documentcreateelementNS05", success); 113 } 114 } testCreateElementNS6()115 public void testCreateElementNS6() throws Throwable { 116 Document doc; 117 Document newDoc; 118 DocumentType docType = null; 119 120 DOMImplementation domImpl; 121 122 String namespaceURI = "http://www.w3.org/xml/1998/namespace "; 123 String qualifiedName = "xml:root"; 124 doc = (Document) load("staffNS", builder); 125 domImpl = doc.getImplementation(); 126 newDoc = domImpl.createDocument("http://www.w3.org/DOM/Test", 127 "dom:doc", docType); 128 129 { 130 boolean success = false; 131 try { 132 newDoc.createElementNS(namespaceURI, qualifiedName); 133 } catch (DOMException ex) { 134 success = (ex.code == DOMException.NAMESPACE_ERR); 135 } 136 assertTrue("documentcreateelementNS06", success); 137 } 138 } 139 } 140