1 /* 2 * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package org.w3c.dom.ptests; 24 25 import static javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI; 26 import static javax.xml.XMLConstants.XML_NS_URI; 27 import static org.testng.Assert.assertEquals; 28 import static org.testng.Assert.assertNotNull; 29 import static org.testng.Assert.fail; 30 import static org.w3c.dom.DOMException.NAMESPACE_ERR; 31 import static org.w3c.dom.ptests.DOMTestUtil.DOMEXCEPTION_EXPECTED; 32 import static org.w3c.dom.ptests.DOMTestUtil.createDOMWithNS; 33 import static org.w3c.dom.ptests.DOMTestUtil.createNewDocument; 34 35 import org.testng.annotations.DataProvider; 36 import org.testng.annotations.Listeners; 37 import org.testng.annotations.Test; 38 import org.w3c.dom.Attr; 39 import org.w3c.dom.DOMException; 40 import org.w3c.dom.Document; 41 import org.w3c.dom.Element; 42 import org.w3c.dom.NodeList; 43 44 /* 45 * @summary Test createAttributeNS, getElementsByTagNameNS and createElementNS method of Document 46 */ 47 @Listeners({jaxp.library.FilePolicy.class}) 48 public class DocumentTest { 49 50 @DataProvider(name = "invalid-nsuri") 51 public Object[][] getInvalidNamespaceURI() { 52 return new Object[][] { 53 { " ", "xml:novel" }, //blank 54 { "hello", "xml:novel" }, //unqualified 55 { null, "xml:novel" }, //null 56 { "", "xmlns:novel" } };//empty 57 } 58 59 /* 60 * Test for createAttributeNS method: verifies that DOMException is thrown 61 * if reserved prefixes are used with an arbitrary namespace name. 62 */ 63 @Test(dataProvider = "invalid-nsuri", expectedExceptions = DOMException.class) 64 public void testCreateAttributeNSNeg(String namespaceURI, String name) throws Exception { 65 Document document = createDOMWithNS("DocumentTest01.xml"); 66 document.createAttributeNS(namespaceURI, name); 67 } 68 69 @DataProvider(name = "valid-nsuri") 70 public Object[][] getValidNamespaceURI() { 71 return new Object[][] { 72 { XML_NS_URI, "xml:novel" }, 73 { XMLNS_ATTRIBUTE_NS_URI, "xmlns:novel" }, 74 { "urn:BooksAreUs.org:BookInfo", "attributeNew"}, 75 { "urn:BooksAreUs.org:BookInfonew", "attributeNew"} }; 76 } 77 78 /* 79 * Verify the Attr from createAttributeNS. 80 */ 81 @Test(dataProvider = "valid-nsuri") 82 public void testCreateAttributeNS(String namespaceURI, String name) throws Exception { 83 Document document = createDOMWithNS("DocumentTest01.xml"); 84 Attr attr = document.createAttributeNS(namespaceURI, name); 85 assertEquals(attr.getNamespaceURI(), namespaceURI); 86 assertEquals(attr.getName(), name); 87 } 88 89 @DataProvider(name = "elementName") 90 public Object[][] getElementName() { 91 return new Object[][] { 92 { "author", 1 }, 93 { "b:author", 0 } }; 94 } 95 96 /* 97 * Verify the NodeList from getElementsByTagNameNS. 98 */ 99 @Test(dataProvider = "elementName") 100 public void testGetElementsByTagNameNS(String localName, int number) throws Exception { 101 Document document = createDOMWithNS("DocumentTest01.xml"); 102 NodeList nodeList = document.getElementsByTagNameNS("urn:BooksAreUs.org:BookInfo", localName); 103 assertEquals(nodeList.getLength(), number); 104 } 105 106 /* 107 * Test for createElementNS method: verifies that DOMException is thrown 108 * if reserved prefixes are used with an arbitrary namespace name. 109 */ 110 @Test(dataProvider = "invalid-nsuri") 111 public void testCreateElementNSNeg(String namespaceURI, String name) throws Exception { 112 Document document = createDOMWithNS("DocumentTest01.xml"); 113 try { 114 document.createElementNS(namespaceURI, name); 115 fail(DOMEXCEPTION_EXPECTED); 116 } catch (DOMException e) { 117 assertEquals(e.code, NAMESPACE_ERR); 118 } 119 } 120 121 /* 122 * Test createElementNS method works as the spec. 123 */ 124 @Test 125 public void testCreateElementNS() throws Exception { 126 final String nsURI = "http://www.books.com"; 127 final String name = "b:novel"; 128 final String localName = "novel"; 129 Document document = createDOMWithNS("DocumentTest01.xml"); 130 Element element = document.createElementNS(nsURI, name); 131 assertEquals(element.getNamespaceURI(), nsURI); 132 assertEquals(element.getNodeName(), name); 133 assertEquals(element.getLocalName(), localName); 134 } 135 136 /* 137 * Test createAttributeNS and then append it with setAttributeNode. 138 */ 139 @Test 140 public void testAddNewAttributeNode() throws Exception { 141 Document document = createDOMWithNS("DocumentTest01.xml"); 142 143 NodeList nodeList = document.getElementsByTagNameNS("http://www.w3.org/TR/REC-html40", "body"); 144 NodeList childList = nodeList.item(0).getChildNodes(); 145 Element child = (Element) childList.item(1); 146 Attr a = document.createAttributeNS("urn:BooksAreUs.org:BookInfo", "attributeNew"); 147 child.setAttributeNode(a); 148 assertNotNull(child.getAttributeNodeNS("urn:BooksAreUs.org:BookInfo", "attributeNew")); 149 } 150 151 /* 152 * Test createElementNS and then append it with appendChild. 153 */ 154 @Test 155 public void testAddNewElement() throws Exception { 156 Document document = createDOMWithNS("DocumentTest01.xml"); 157 158 NodeList nodeList = document.getElementsByTagNameNS("http://www.w3.org/TR/REC-html40", "body"); 159 NodeList childList = nodeList.item(0).getChildNodes(); 160 Element child = (Element) childList.item(1); 161 Element elem = document.createElementNS("urn:BooksAreUs.org:BookInfonew", "newElement"); 162 assertNotNull(child.appendChild(elem)); 163 } 164 165 /* 166 * Test createElement with unqualified xml name. 167 */ 168 @Test(expectedExceptions = DOMException.class) 169 public void testCreateElementNeg() throws Exception { 170 Document doc = createNewDocument(); 171 doc.createElement("!nc$%^*(!"); 172 } 173 }