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     /*
 121      * Test createElementNS method works as the spec.
 122      */
 123     @Test
 124     public void testCreateElementNS() throws Exception {
 125         final String nsURI = "http://www.books.com";
 126         final String name = "b:novel";
 127         final String localName = "novel";
 128         Document document = createDOMWithNS("DocumentTest01.xml");
 129         Element element = document.createElementNS(nsURI, name);
 130         assertEquals(element.getNamespaceURI(), nsURI);
 131         assertEquals(element.getNodeName(), name);
 132         assertEquals(element.getLocalName(), localName);
 133     }
 134 
 135     /*
 136      * Test createAttributeNS and then append it with setAttributeNode.
 137      */
 138     @Test
 139     public void testAddNewAttributeNode() throws Exception {
 140         Document document = createDOMWithNS("DocumentTest01.xml");
 141 
 142         NodeList nodeList = document.getElementsByTagNameNS("http://www.w3.org/TR/REC-html40", "body");
 143         NodeList childList = nodeList.item(0).getChildNodes();
 144         Element child = (Element) childList.item(1);
 145         Attr a = document.createAttributeNS("urn:BooksAreUs.org:BookInfo", "attributeNew");
 146         child.setAttributeNode(a);
 147         assertNotNull(child.getAttributeNodeNS("urn:BooksAreUs.org:BookInfo", "attributeNew"));
 148     }
 149 
 150     /*
 151      * Test createElementNS and then append it with appendChild.
 152      */
 153     @Test
 154     public void testAddNewElement() throws Exception {
 155         Document document = createDOMWithNS("DocumentTest01.xml");
 156 
 157         NodeList nodeList = document.getElementsByTagNameNS("http://www.w3.org/TR/REC-html40", "body");
 158         NodeList childList = nodeList.item(0).getChildNodes();
 159         Element child = (Element) childList.item(1);
 160         Element elem = document.createElementNS("urn:BooksAreUs.org:BookInfonew", "newElement");
 161         assertNotNull(child.appendChild(elem));
 162     }
 163 
 164     /*
 165      * Test createElement with unqualified xml name.
 166      */
 167     @Test(expectedExceptions = DOMException.class)
 168     public void testCreateElementNeg() throws Exception {
 169         Document doc = createNewDocument();
 170         doc.createElement("!nc$%^*(!");
 171     }
 172 }