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