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