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 * @test 46 * @library /javax/xml/jaxp/libs 47 * @run testng/othervm -DrunSecMngr=true org.w3c.dom.ptests.DocumentTest 48 * @run testng/othervm org.w3c.dom.ptests.DocumentTest 49 * @summary Test createAttributeNS, getElementsByTagNameNS and createElementNS method of Document 50 */ 51 @Listeners({jaxp.library.FilePolicy.class}) 52 public class DocumentTest { 53 54 @DataProvider(name = "invalid-nsuri") 55 public Object[][] getInvalidNamespaceURI() { 56 return new Object[][] { 57 { " ", "xml:novel" }, //blank 58 { "hello", "xml:novel" }, //unqualified 59 { null, "xml:novel" }, //null 60 { "", "xmlns:novel" } };//empty 61 } 62 63 /* 64 * Test for createAttributeNS method: verifies that DOMException is thrown 65 * if reserved prefixes are used with an arbitrary namespace name. 66 */ 67 @Test(dataProvider = "invalid-nsuri", expectedExceptions = DOMException.class) 68 public void testCreateAttributeNSNeg(String namespaceURI, String name) throws Exception { 69 Document document = createDOMWithNS("DocumentTest01.xml"); 70 document.createAttributeNS(namespaceURI, name); 71 } 72 73 @DataProvider(name = "valid-nsuri") 74 public Object[][] getValidNamespaceURI() { 75 return new Object[][] { 76 { XML_NS_URI, "xml:novel" }, 77 { XMLNS_ATTRIBUTE_NS_URI, "xmlns:novel" }, 78 { "urn:BooksAreUs.org:BookInfo", "attributeNew"}, 79 { "urn:BooksAreUs.org:BookInfonew", "attributeNew"} }; 80 } 81 82 /* 83 * Verify the Attr from createAttributeNS. 84 */ 85 @Test(dataProvider = "valid-nsuri") 86 public void testCreateAttributeNS(String namespaceURI, String name) throws Exception { 87 Document document = createDOMWithNS("DocumentTest01.xml"); 88 Attr attr = document.createAttributeNS(namespaceURI, name); 89 assertEquals(attr.getNamespaceURI(), namespaceURI); 90 assertEquals(attr.getName(), name); 91 } 92 93 @DataProvider(name = "elementName") 94 public Object[][] getElementName() { 95 return new Object[][] { 96 { "author", 1 }, 97 { "b:author", 0 } }; 98 } 99 100 /* 101 * Verify the NodeList from getElementsByTagNameNS. 102 */ 103 @Test(dataProvider = "elementName") 104 public void testGetElementsByTagNameNS(String localName, int number) throws Exception { 105 Document document = createDOMWithNS("DocumentTest01.xml"); 106 NodeList nodeList = document.getElementsByTagNameNS("urn:BooksAreUs.org:BookInfo", localName); 107 assertEquals(nodeList.getLength(), number); 108 } 109 110 /* 111 * Test for createElementNS method: verifies that DOMException is thrown 112 * if reserved prefixes are used with an arbitrary namespace name. 113 */ 114 @Test(dataProvider = "invalid-nsuri") 115 public void testCreateElementNSNeg(String namespaceURI, String name) throws Exception { 116 Document document = createDOMWithNS("DocumentTest01.xml"); 117 try { 118 document.createElementNS(namespaceURI, name); 119 fail(DOMEXCEPTION_EXPECTED); 120 } catch (DOMException e) { 121 assertEquals(e.code, NAMESPACE_ERR); 122 } 123 } 124 125 /* 126 * Test createElementNS method works as the spec. 127 */ 128 @Test 129 public void testCreateElementNS() throws Exception { 130 final String nsURI = "http://www.books.com"; 131 final String name = "b:novel"; 132 final String localName = "novel"; 133 Document document = createDOMWithNS("DocumentTest01.xml"); 134 Element element = document.createElementNS(nsURI, name); 135 assertEquals(element.getNamespaceURI(), nsURI); 136 assertEquals(element.getNodeName(), name); 137 assertEquals(element.getLocalName(), localName); 138 } 139 140 /* 141 * Test createAttributeNS and then append it with setAttributeNode. 142 */ 143 @Test 144 public void testAddNewAttributeNode() throws Exception { 145 Document document = createDOMWithNS("DocumentTest01.xml"); 146 147 NodeList nodeList = document.getElementsByTagNameNS("http://www.w3.org/TR/REC-html40", "body"); 148 NodeList childList = nodeList.item(0).getChildNodes(); 149 Element child = (Element) childList.item(1); 150 Attr a = document.createAttributeNS("urn:BooksAreUs.org:BookInfo", "attributeNew"); 151 child.setAttributeNode(a); 152 assertNotNull(child.getAttributeNodeNS("urn:BooksAreUs.org:BookInfo", "attributeNew")); 153 } 154 155 /* 156 * Test createElementNS and then append it with appendChild. 157 */ 158 @Test 159 public void testAddNewElement() throws Exception { 160 Document document = createDOMWithNS("DocumentTest01.xml"); 161 162 NodeList nodeList = document.getElementsByTagNameNS("http://www.w3.org/TR/REC-html40", "body"); 163 NodeList childList = nodeList.item(0).getChildNodes(); 164 Element child = (Element) childList.item(1); 165 Element elem = document.createElementNS("urn:BooksAreUs.org:BookInfonew", "newElement"); 166 assertNotNull(child.appendChild(elem)); 167 } 168 169 /* 170 * Test createElement with unqualified xml name. 171 */ 172 @Test(expectedExceptions = DOMException.class) 173 public void testCreateElementNeg() throws Exception { 174 Document doc = createNewDocument(); 175 doc.createElement("!nc$%^*(!"); 176 } 177 } 178