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