# HG changeset patch
# User joehw
# Date 1429160069 25200
# Node ID 3ea7da18938d3a941d560f816d13c7abda8d18fe
# Parent 59622fefbbf685ff517103f1fc7b68c52cd6e25c
8051559: Convert JAXP function tests: org.w3c.dom to jtreg (testng) tests
Reviewed-by: lancea, joehw
Contributed-by: frank.yuan@oracle.com
diff -r 59622fefbbf6 -r 3ea7da18938d jaxp/test/javax/xml/jaxp/functional/org/w3c/dom/ptests/AbstractCharacterDataTest.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jaxp/test/javax/xml/jaxp/functional/org/w3c/dom/ptests/AbstractCharacterDataTest.java Wed Apr 15 21:54:29 2015 -0700
@@ -0,0 +1,231 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package org.w3c.dom.ptests;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.fail;
+import static org.w3c.dom.DOMException.INDEX_SIZE_ERR;
+import static org.w3c.dom.ptests.DOMTestUtil.DOMEXCEPTION_EXPECTED;
+
+import java.io.IOException;
+
+import javax.xml.parsers.ParserConfigurationException;
+
+import jaxp.library.JAXPFileBaseTest;
+
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+import org.w3c.dom.CharacterData;
+import org.w3c.dom.DOMException;
+import org.xml.sax.SAXException;
+
+/*
+ * @summary common test for the CharacterData Interface
+ */
+public abstract class AbstractCharacterDataTest extends JAXPFileBaseTest {
+ @DataProvider(name = "data-for-length")
+ public Object[][] getDataForTestLength() {
+ return new Object[][] {
+ { "", 0 },
+ { "test", 4 } };
+ }
+
+ /*
+ * Verify getLength method works as the spec, for an empty string, should
+ * return zero
+ */
+ @Test(dataProvider = "data-for-length")
+ public void testGetLength(String text, int length) throws Exception {
+ CharacterData cd = createCharacterData(text);
+ assertEquals(cd.getLength(), length);
+
+ }
+
+ /*
+ * Test appendData method and verify by getData method.
+ */
+ @Test
+ public void testAppendData() throws Exception {
+ CharacterData cd = createCharacterData("DOM");
+ cd.appendData("2");
+ assertEquals(cd.getData(), "DOM2");
+
+ }
+
+ @DataProvider(name = "data-for-delete")
+ public Object[][] getDataForTestDelete() {
+ return new Object[][] {
+ { "DOM", 2, 1, "DO" },
+ { "DOM", 0, 2, "M" },
+ { "DOM", 2, 3, "DO" } };
+ }
+
+ /*
+ * Verify deleteData method works as the spec.
+ */
+ @Test(dataProvider = "data-for-delete")
+ public void testDeleteData(String text, int offset, int count, String result) throws Exception {
+ CharacterData cd = createCharacterData(text);
+ cd.deleteData(offset, count);
+ assertEquals(cd.getData(), result);
+ }
+
+ @DataProvider(name = "data-for-replace")
+ public Object[][] getDataForTestReplace() {
+ return new Object[][] {
+ { "DOM", 0, 3, "SAX", "SAX" },
+ { "DOM", 1, 1, "AA", "DAAM" },
+ { "DOM", 1, 2, "A", "DA" },
+ { "DOM", 2, 2, "SAX", "DOSAX" } };
+ }
+
+ /*
+ * Verify replaceData method works as the spec.
+ */
+ @Test(dataProvider = "data-for-replace")
+ public void testReplaceData(String text, int offset, int count, String arg, String result) throws Exception {
+ CharacterData cd = createCharacterData(text);
+ cd.replaceData(offset, count, arg);
+ assertEquals(cd.getData(), result);
+ }
+
+ @DataProvider(name = "data-for-replace-neg")
+ public Object[][] getDataForTestReplaceNeg() {
+ return new Object[][] {
+ { "DOM", -1, 3, "SAX" }, //offset if neg
+ { "DOM", 0, -1, "SAX" }, //count is neg
+ { "DOM", 4, 1, "SAX" } };//offset is greater than length
+ }
+
+ /*
+ * Test for replaceData method: verifies that DOMException with
+ * INDEX_SIZE_ERR is thrown if offset or count is out of the bound.
+ */
+ @Test(dataProvider = "data-for-replace-neg")
+ public void testReplaceDataNeg(String text, int offset, int count, String arg) throws Exception {
+ CharacterData cd = createCharacterData(text);
+ try {
+ cd.replaceData(offset, count, arg);
+ fail(DOMEXCEPTION_EXPECTED);
+ } catch (DOMException e) {
+ assertEquals(e.code, INDEX_SIZE_ERR);
+ }
+ }
+
+ @DataProvider(name = "data-for-insert")
+ public Object[][] getDataForTestInsert() {
+ return new Object[][] {
+ { "DOM", 0, "SAX", "SAXDOM" },
+ { "DOM", 3, "SAX", "DOMSAX" } };
+ }
+
+ /*
+ * Verify insertData method works as the spec.
+ */
+ @Test(dataProvider = "data-for-insert")
+ public void testInsertData(String text, int offset, String arg, String result) throws Exception {
+ CharacterData cd = createCharacterData(text);
+ cd.insertData(offset, arg);
+ assertEquals(cd.getData(), result);
+ }
+
+ @DataProvider(name = "data-for-insert-neg")
+ public Object[][] getDataForTestInsertNeg() {
+ return new Object[][] {
+ { "DOM", -1 }, //offset is neg
+ { "DOM", 4 } };//offset is greater than length
+ }
+
+ /*
+ * Test for insertData method: verifies that DOMException with
+ * INDEX_SIZE_ERR is thrown if offset is out of the bound.
+ */
+ @Test(dataProvider = "data-for-insert-neg")
+ public void testInsertDataNeg(String text, int offset) throws Exception {
+ CharacterData cd = createCharacterData(text);
+ try {
+ cd.insertData(offset, "TEST");
+ fail(DOMEXCEPTION_EXPECTED);
+ } catch (DOMException e) {
+ assertEquals(e.code, INDEX_SIZE_ERR);
+ }
+ }
+
+ /*
+ * Test setData method and verify by getData method.
+ */
+ @Test
+ public void testSetData() throws Exception {
+ CharacterData cd = createCharacterData("DOM");
+ cd.setData("SAX");
+ assertEquals(cd.getData(), "SAX");
+ }
+
+ @DataProvider(name = "data-for-substring")
+ public Object[][] getDataForTestSubstring() {
+ return new Object[][] {
+ { "DOM Level 2", 0, 3, "DOM" },
+ { "DOM", 0, 3, "DOM" },
+ { "DOM", 2, 5, "M" } };
+ }
+
+ /*
+ * Verify substringData method works as the spec.
+ */
+ @Test(dataProvider = "data-for-substring")
+ public void testSubstringData(String text, int offset, int count, String result) throws Exception {
+ CharacterData cd = createCharacterData(text);
+ String retStr = cd.substringData(offset, count);
+ assertEquals(retStr, result);
+ }
+
+ @DataProvider(name = "data-for-substring-neg")
+ public Object[][] getDataForTestSubstringNeg() {
+ return new Object[][] {
+ { "DOM Level 2", -1, 3 }, //offset is neg
+ { "DOM", 0, -1 }, //count is neg
+ { "DOM", 3, 1 } }; //offset exceeds length
+ }
+
+ /*
+ * Test for substringData method: verifies that DOMException with
+ * INDEX_SIZE_ERR is thrown if offset or count is out of the bound.
+ */
+ @Test(dataProvider = "data-for-substring-neg")
+ public void testSubstringDataNeg(String text, int offset, int count) throws Exception {
+ CharacterData cd = createCharacterData(text);
+ try {
+ cd.substringData(offset, count);
+ fail(DOMEXCEPTION_EXPECTED);
+ } catch (DOMException e) {
+ assertEquals(e.code, INDEX_SIZE_ERR);
+ }
+
+ }
+
+ /*
+ * Return a concrete CharacterData instance.
+ */
+ abstract protected CharacterData createCharacterData(String text) throws IOException, SAXException, ParserConfigurationException;
+
+}
diff -r 59622fefbbf6 -r 3ea7da18938d jaxp/test/javax/xml/jaxp/functional/org/w3c/dom/ptests/AttrTest.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jaxp/test/javax/xml/jaxp/functional/org/w3c/dom/ptests/AttrTest.java Wed Apr 15 21:54:29 2015 -0700
@@ -0,0 +1,148 @@
+/*
+ * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package org.w3c.dom.ptests;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertNull;
+import static org.testng.Assert.assertTrue;
+import static org.w3c.dom.ptests.DOMTestUtil.createDOM;
+import jaxp.library.JAXPFileBaseTest;
+
+import org.testng.annotations.Test;
+import org.w3c.dom.Attr;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NamedNodeMap;
+
+
+/*
+ * @summary Test for the Attr Interface
+ */
+public class AttrTest extends JAXPFileBaseTest {
+ /*
+ * Verify getName method against both existing Attr and new Attr.
+ */
+ @Test
+ public void testGetName() throws Exception {
+ Document document = createDOM("Attr01.xml");
+ //test a new created Attr
+ Attr attr = document.createAttribute("newAttribute");
+ assertEquals(attr.getName(), "newAttribute");
+
+ //test a Attr loaded from xml file
+ Element elemNode = (Element) document.getElementsByTagName("book").item(1);
+ Attr attr2 = (Attr) elemNode.getAttributes().item(0);
+ assertEquals(attr2.getName(), "category1");
+ }
+
+ /*
+ * Verify getOwnerElement method against both existing Attr and new Attr.
+ */
+ @Test
+ public void testGetOwnerElement() throws Exception {
+ Document document = createDOM("Attr01.xml");
+
+ //test Attr loaded from xml file
+ Element elemNode = (Element) document.getElementsByTagName("book").item(1);
+ NamedNodeMap nnMap = elemNode.getAttributes();
+ for (int i = 0; i < nnMap.getLength(); i++) {
+ Attr attr = (Attr) nnMap.item(i);
+ assertEquals(attr.getOwnerElement().getNodeName(), "book");
+ }
+
+ //test an Attr without owner node
+ Attr attr = document.createAttribute("newAttribute");
+ assertNull(attr.getOwnerElement());
+
+ }
+
+ /*
+ * Verify getSpecified method works as the spec.
+ */
+ @Test
+ public void testGetSpecified1() throws Exception {
+ Document document = createDOM("Attr01.xml");
+
+ Element elemNode = (Element) document.getElementsByTagName("book").item(1);
+ Attr attr = elemNode.getAttributeNode("category1");
+ assertTrue(attr.getSpecified());
+
+ }
+
+ /*
+ * In this xml file, the dtd has the value for the attrribute, but the xml
+ * element does not specify the value for the attrribute, as per the spec it
+ * should return false.
+ */
+ @Test
+ public void testGetSpecified2() throws Exception {
+
+ Document document = createDOM("Attr2.xml");
+ Element elemNode = (Element) document.getElementsByTagName("Name").item(0);
+ Attr attr = elemNode.getAttributeNode("type");
+
+ assertFalse(attr.getSpecified());
+ }
+
+ /*
+ * Creating a new attribute, the owner element is null since the attribute
+ * has just been created, getSpecified should return true.
+ */
+ @Test
+ public void testNewCreatedAttribute() throws Exception {
+ Document document = createDOM("Attr01.xml");
+ Attr attr = document.createAttribute("newAttribute");
+ assertTrue(attr.getSpecified());
+ assertNull(attr.getOwnerElement());
+
+ }
+
+ /*
+ * The xml file includes the dtd having the IMPLIED value for the attrribute
+ * and the xml element does not specify the value. As per the spec it should
+ * not be seen as a part of the structure model hence getAttributeNode
+ * rerurn null if the attribute is even found.
+ */
+ @Test
+ public void testIMPLIEDAttribute() throws Exception {
+ Document document = createDOM("Attr3.xml");
+ Element elemNode = (Element) document.getElementsByTagName("Name").item(0);
+ Attr attr = elemNode.getAttributeNode("type");
+ assertNull(attr);
+ }
+
+ /*
+ * Test setValue method and verify by getValue method.
+ */
+ @Test
+ public void testSetValue() throws Exception {
+ Document document = createDOM("Attr01.xml");
+ Attr attr = document.createAttribute("newAttribute");
+ attr.setValue("newVal");
+ assertEquals(attr.getValue(), "newVal");
+
+ }
+
+}
diff -r 59622fefbbf6 -r 3ea7da18938d jaxp/test/javax/xml/jaxp/functional/org/w3c/dom/ptests/CommentTest.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jaxp/test/javax/xml/jaxp/functional/org/w3c/dom/ptests/CommentTest.java Wed Apr 15 21:54:29 2015 -0700
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package org.w3c.dom.ptests;
+
+import static org.w3c.dom.ptests.DOMTestUtil.createNewDocument;
+
+import java.io.IOException;
+
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.w3c.dom.CharacterData;
+import org.w3c.dom.Document;
+import org.xml.sax.SAXException;
+
+/*
+ * @summary Test for Comment implementation returned by Document.createComment(String)
+ */
+public class CommentTest extends AbstractCharacterDataTest {
+ @Override
+ protected CharacterData createCharacterData(String text) throws IOException, SAXException, ParserConfigurationException {
+ Document document = createNewDocument();
+ return document.createComment(text);
+ }
+}
diff -r 59622fefbbf6 -r 3ea7da18938d jaxp/test/javax/xml/jaxp/functional/org/w3c/dom/ptests/DocumentTest.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jaxp/test/javax/xml/jaxp/functional/org/w3c/dom/ptests/DocumentTest.java Wed Apr 15 21:54:29 2015 -0700
@@ -0,0 +1,172 @@
+/*
+ * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package org.w3c.dom.ptests;
+
+import static javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
+import static javax.xml.XMLConstants.XML_NS_URI;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.fail;
+import static org.w3c.dom.DOMException.NAMESPACE_ERR;
+import static org.w3c.dom.ptests.DOMTestUtil.DOMEXCEPTION_EXPECTED;
+import static org.w3c.dom.ptests.DOMTestUtil.createDOMWithNS;
+import static org.w3c.dom.ptests.DOMTestUtil.createNewDocument;
+import jaxp.library.JAXPFileBaseTest;
+
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+import org.w3c.dom.Attr;
+import org.w3c.dom.DOMException;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+
+/*
+ * @summary Test createAttributeNS, getElementsByTagNameNS and createElementNS method of Document
+ */
+public class DocumentTest extends JAXPFileBaseTest {
+
+ @DataProvider(name = "invalid-nsuri")
+ public Object[][] getInvalidNamespaceURI() {
+ return new Object[][] {
+ { " ", "xml:novel" }, //blank
+ { "hello", "xml:novel" }, //unqualified
+ { null, "xml:novel" }, //null
+ { "", "xmlns:novel" } };//empty
+ }
+
+ /*
+ * Test for createAttributeNS method: verifies that DOMException is thrown
+ * if reserved prefixes are used with an arbitrary namespace name.
+ */
+ @Test(dataProvider = "invalid-nsuri", expectedExceptions = DOMException.class)
+ public void testCreateAttributeNSNeg(String namespaceURI, String name) throws Exception {
+ Document document = createDOMWithNS("DocumentTest01.xml");
+ document.createAttributeNS(namespaceURI, name);
+ }
+
+ @DataProvider(name = "valid-nsuri")
+ public Object[][] getValidNamespaceURI() {
+ return new Object[][] {
+ { XML_NS_URI, "xml:novel" },
+ { XMLNS_ATTRIBUTE_NS_URI, "xmlns:novel" },
+ { "urn:BooksAreUs.org:BookInfo", "attributeNew"},
+ { "urn:BooksAreUs.org:BookInfonew", "attributeNew"} };
+ }
+
+ /*
+ * Verify the Attr from createAttributeNS.
+ */
+ @Test(dataProvider = "valid-nsuri")
+ public void testCreateAttributeNS(String namespaceURI, String name) throws Exception {
+ Document document = createDOMWithNS("DocumentTest01.xml");
+ Attr attr = document.createAttributeNS(namespaceURI, name);
+ assertEquals(attr.getNamespaceURI(), namespaceURI);
+ assertEquals(attr.getName(), name);
+ }
+
+ @DataProvider(name = "elementName")
+ public Object[][] getElementName() {
+ return new Object[][] {
+ { "author", 1 },
+ { "b:author", 0 } };
+ }
+
+ /*
+ * Verify the NodeList from getElementsByTagNameNS.
+ */
+ @Test(dataProvider = "elementName")
+ public void testGetElementsByTagNameNS(String localName, int number) throws Exception {
+ Document document = createDOMWithNS("DocumentTest01.xml");
+ NodeList nodeList = document.getElementsByTagNameNS("urn:BooksAreUs.org:BookInfo", localName);
+ assertEquals(nodeList.getLength(), number);
+ }
+
+ /*
+ * Test for createElementNS method: verifies that DOMException is thrown
+ * if reserved prefixes are used with an arbitrary namespace name.
+ */
+ @Test(dataProvider = "invalid-nsuri")
+ public void testCreateElementNSNeg(String namespaceURI, String name) throws Exception {
+ Document document = createDOMWithNS("DocumentTest01.xml");
+ try {
+ document.createElementNS(namespaceURI, name);
+ fail(DOMEXCEPTION_EXPECTED);
+ } catch (DOMException e) {
+ assertEquals(e.code, NAMESPACE_ERR);
+ }
+ }
+
+ /*
+ * Test createElementNS method works as the spec.
+ */
+ @Test
+ public void testCreateElementNS() throws Exception {
+ final String nsURI = "http://www.books.com";
+ final String name = "b:novel";
+ final String localName = "novel";
+ Document document = createDOMWithNS("DocumentTest01.xml");
+ Element element = document.createElementNS(nsURI, name);
+ assertEquals(element.getNamespaceURI(), nsURI);
+ assertEquals(element.getNodeName(), name);
+ assertEquals(element.getLocalName(), localName);
+ }
+
+ /*
+ * Test createAttributeNS and then append it with setAttributeNode.
+ */
+ @Test
+ public void testAddNewAttributeNode() throws Exception {
+ Document document = createDOMWithNS("DocumentTest01.xml");
+
+ NodeList nodeList = document.getElementsByTagNameNS("http://www.w3.org/TR/REC-html40", "body");
+ NodeList childList = nodeList.item(0).getChildNodes();
+ Element child = (Element) childList.item(1);
+ Attr a = document.createAttributeNS("urn:BooksAreUs.org:BookInfo", "attributeNew");
+ child.setAttributeNode(a);
+ assertNotNull(child.getAttributeNodeNS("urn:BooksAreUs.org:BookInfo", "attributeNew"));
+ }
+
+ /*
+ * Test createElementNS and then append it with appendChild.
+ */
+ @Test
+ public void testAddNewElement() throws Exception {
+ Document document = createDOMWithNS("DocumentTest01.xml");
+
+ NodeList nodeList = document.getElementsByTagNameNS("http://www.w3.org/TR/REC-html40", "body");
+ NodeList childList = nodeList.item(0).getChildNodes();
+ Element child = (Element) childList.item(1);
+ Element elem = document.createElementNS("urn:BooksAreUs.org:BookInfonew", "newElement");
+ assertNotNull(child.appendChild(elem));
+ }
+
+ /*
+ * Test createElement with unqualified xml name.
+ */
+ @Test(expectedExceptions = DOMException.class)
+ public void testCreateElementNeg() throws Exception {
+ Document doc = createNewDocument();
+ doc.createElement("!nc$%^*(!");
+ }
+}
diff -r 59622fefbbf6 -r 3ea7da18938d jaxp/test/javax/xml/jaxp/functional/org/w3c/dom/ptests/DocumentTypeTest.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jaxp/test/javax/xml/jaxp/functional/org/w3c/dom/ptests/DocumentTypeTest.java Wed Apr 15 21:54:29 2015 -0700
@@ -0,0 +1,89 @@
+/*
+ * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package org.w3c.dom.ptests;
+
+import static org.testng.Assert.assertEquals;
+import static org.w3c.dom.ptests.DOMTestUtil.createDOM;
+import jaxp.library.JAXPFileBaseTest;
+
+import org.testng.Assert;
+import org.testng.annotations.Test;
+import org.w3c.dom.DocumentType;
+import org.w3c.dom.NamedNodeMap;
+
+/*
+ * @summary Test DocumentType
+ */
+public class DocumentTypeTest extends JAXPFileBaseTest {
+
+ /*
+ * Test testGetEntities method, and verify the entity items.
+ */
+ @Test
+ public void testGetEntities() throws Exception {
+ DocumentType documentType = createDOM("DocumentType01.xml").getDoctype();
+ NamedNodeMap namedNodeMap = documentType.getEntities();
+ // should return both external and internal. Parameter entities are not
+ // contained. Duplicates are discarded.
+ assertEquals(namedNodeMap.getLength(), 3);
+ assertEquals(namedNodeMap.item(0).getNodeName(), "author");
+ assertEquals(namedNodeMap.item(1).getNodeName(), "test");
+ assertEquals(namedNodeMap.item(2).getNodeName(), "writer");
+ }
+
+ /*
+ * Test getNotations method, and verify the notation items.
+ */
+ @Test
+ public void testGetNotations() throws Exception {
+ DocumentType documentType = createDOM("DocumentType03.xml").getDoctype();
+ NamedNodeMap nm = documentType.getNotations();
+ assertEquals(nm.getLength(), 2); // should return 2 because the notation
+ // name is repeated and
+ // it considers only the first
+ // occurence
+ assertEquals(nm.item(0).getNodeName(), "gs");
+ assertEquals(nm.item(1).getNodeName(), "name");
+ }
+
+ /*
+ * Test getName method.
+ */
+ @Test
+ public void testGetName() throws Exception {
+ DocumentType documentType = createDOM("DocumentType03.xml").getDoctype();
+ assertEquals(documentType.getName(), "note");
+ }
+
+ /*
+ * Test getSystemId and getPublicId method.
+ */
+ @Test
+ public void testGetSystemId() throws Exception {
+ DocumentType documentType = createDOM("DocumentType05.xml").getDoctype();
+ assertEquals(documentType.getSystemId(), "DocumentBuilderImpl02.dtd");
+ Assert.assertNull(documentType.getPublicId());
+ }
+
+}
diff -r 59622fefbbf6 -r 3ea7da18938d jaxp/test/javax/xml/jaxp/functional/org/w3c/dom/ptests/DomImplementationTest.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jaxp/test/javax/xml/jaxp/functional/org/w3c/dom/ptests/DomImplementationTest.java Wed Apr 15 21:54:29 2015 -0700
@@ -0,0 +1,127 @@
+/*
+ * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package org.w3c.dom.ptests;
+
+import static org.testng.Assert.assertEquals;
+import static org.w3c.dom.ptests.DOMTestUtil.createNewDocument;
+
+import javax.xml.parsers.ParserConfigurationException;
+
+import jaxp.library.JAXPBaseTest;
+
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+import org.w3c.dom.DOMImplementation;
+import org.w3c.dom.Document;
+import org.w3c.dom.DocumentType;
+
+/*
+ * @summary Test DomImplementation API
+ */
+public class DomImplementationTest extends JAXPBaseTest {
+ /*
+ * Test createDocument method with a namespace uri, qualified name and null
+ * for the doctype
+ */
+ @Test
+ public void testCreateDocument() throws ParserConfigurationException {
+ final String nsURI = "http://www.document.com";
+ final String name = "document:localName";
+ DOMImplementation domImpl = getDOMImplementation();
+ Document document = domImpl.createDocument(nsURI, name, null);
+ assertEquals(document.getDocumentElement().getNamespaceURI(), nsURI);
+ assertEquals(document.getDocumentElement().getNodeName(), name);
+ }
+
+ /*
+ * Test createDocumentType method with name, public id and system id.
+ */
+ @Test
+ public void testCreateDocumentType01() throws ParserConfigurationException {
+ final String name = "document:localName";
+ final String publicId = "pubid";
+ final String systemId = "sysid";
+
+ DOMImplementation domImpl = getDOMImplementation();
+ DocumentType documentType = domImpl.createDocumentType(name, publicId, systemId);
+ verifyDocumentType(documentType, name, publicId, systemId);
+ }
+
+
+ /*
+ * Test createDocument method using a DocumentType, verify the document will
+ * take that Doctype.
+ */
+ @Test
+ public void testCreateDocumentType02() throws ParserConfigurationException {
+ final String name = "document:localName";
+ final String publicId = "-//W3C//DTD HTML 4.0 Transitional//EN";
+ final String systemId = "http://www.w3.org/TR/REC-html40/loose.dtd";
+ DOMImplementation domImpl = getDOMImplementation();
+
+ DocumentType documentType = domImpl.createDocumentType(name, publicId, systemId);
+ Document document = domImpl.createDocument("http://www.document.com", "document:localName", documentType);
+ verifyDocumentType(document.getDoctype(), name, publicId, systemId);
+ }
+
+ @DataProvider(name = "feature-supported")
+ public Object[][] getFeatureSupportedList() throws ParserConfigurationException {
+ DOMImplementation impl = getDOMImplementation();
+ return new Object[][] {
+ { impl, "XML", "2.0", true },
+ { impl, "HTML", "2.0", false },
+ { impl, "Views", "2.0", false },
+ { impl, "StyleSheets", "2.0", false },
+ { impl, "CSS", "2.0", false },
+ { impl, "CSS2", "2.0", false },
+ { impl, "Events", "2.0", true },
+ { impl, "UIEvents", "2.0", false },
+ { impl, "MouseEvents", "2.0", false },
+ { impl, "HTMLEvents", "2.0", false },
+ { impl, "Traversal", "2.0", true },
+ { impl, "Range", "2.0", true },
+ { impl, "Core", "2.0", true },
+ { impl, "XML", "", true } };
+ }
+
+
+ /*
+ * Verify DOMImplementation for feature supporting.
+ */
+ @Test(dataProvider = "feature-supported")
+ public void testHasFeature(DOMImplementation impl, String feature, String version, boolean isSupported) {
+ assertEquals(impl.hasFeature(feature,version), isSupported);
+ }
+
+
+ private DOMImplementation getDOMImplementation() throws ParserConfigurationException {
+ return createNewDocument().getImplementation();
+ }
+
+
+ private void verifyDocumentType(DocumentType documentType, String name, String publicId, String systemId) {
+ assertEquals(documentType.getPublicId(), publicId);
+ assertEquals(documentType.getSystemId(), systemId);
+ assertEquals(documentType.getName(), name);
+ }
+}
diff -r 59622fefbbf6 -r 3ea7da18938d jaxp/test/javax/xml/jaxp/functional/org/w3c/dom/ptests/ElementTest.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jaxp/test/javax/xml/jaxp/functional/org/w3c/dom/ptests/ElementTest.java Wed Apr 15 21:54:29 2015 -0700
@@ -0,0 +1,253 @@
+/*
+ * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package org.w3c.dom.ptests;
+
+import static javax.xml.XMLConstants.XML_NS_URI;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNull;
+import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.fail;
+import static org.w3c.dom.DOMException.INUSE_ATTRIBUTE_ERR;
+import static org.w3c.dom.ptests.DOMTestUtil.DOMEXCEPTION_EXPECTED;
+import static org.w3c.dom.ptests.DOMTestUtil.createDOM;
+import static org.w3c.dom.ptests.DOMTestUtil.createDOMWithNS;
+import static org.w3c.dom.ptests.DOMTestUtil.createNewDocument;
+
+import java.io.StringReader;
+
+import javax.xml.parsers.DocumentBuilderFactory;
+
+import jaxp.library.JAXPFileBaseTest;
+
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+import org.w3c.dom.Attr;
+import org.w3c.dom.DOMException;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.xml.sax.InputSource;
+
+/*
+ * @summary Test for the methods of Element Interface
+ */
+public class ElementTest extends JAXPFileBaseTest {
+ @Test
+ public void testGetAttributeNS() throws Exception {
+ Document document = createDOMWithNS("ElementSample01.xml");
+ Element elemNode = (Element) document.getElementsByTagName("book").item(0);
+ String s = elemNode.getAttributeNS("urn:BooksAreUs.org:BookInfo", "category");
+ assertEquals(s, "research");
+ }
+
+ @Test
+ public void testGetAttributeNodeNS() throws Exception {
+ Document document = createDOMWithNS("ElementSample01.xml");
+ Element elemNode = (Element) document.getElementsByTagName("book").item(0);
+ Attr attr = elemNode.getAttributeNodeNS("urn:BooksAreUs.org:BookInfo", "category");
+ assertEquals(attr.getValue(), "research");
+
+ }
+
+ /*
+ * Test getAttributeNode to get a Attr and then remove it successfully by
+ * removeAttributeNode.
+ */
+ @Test
+ public void testRemoveAttributeNode() throws Exception {
+ Document document = createDOMWithNS("ElementSample01.xml");
+ Element elemNode = (Element) document.getElementsByTagName("book").item(1);
+ Attr attr = elemNode.getAttributeNode("category1");
+ assertEquals(attr.getValue(), "research");
+
+ assertEquals(elemNode.getTagName(), "book");
+ elemNode.removeAttributeNode(attr);
+ assertEquals(elemNode.getAttribute("category1"), "");
+ }
+
+ /*
+ * Test removing an Attribute Node with removeAttributeNS(String
+ * namespaceURI, String localName).
+ */
+ @Test
+ public void testRemoveAttributeNS() throws Exception {
+ final String nsURI = "urn:BooksAreUs.org:BookInfo";
+ final String localName = "category";
+ Document document = createDOMWithNS("ElementSample01.xml");
+ Element elemNode = (Element) document.getElementsByTagName("book").item(0);
+ elemNode.removeAttributeNS(nsURI, localName);
+
+ assertNull(elemNode.getAttributeNodeNS(nsURI, localName));
+ }
+
+ /*
+ * Test getFirstChild and getLastChild.
+ */
+ @Test
+ public void testGetChild() throws Exception {
+ Document document = createDOMWithNS("ElementSample01.xml");
+ Element elemNode = (Element) document.getElementsByTagName("b:aaa").item(0);
+ elemNode.normalize();
+ Node firstChild = elemNode.getFirstChild();
+ Node lastChild = elemNode.getLastChild();
+ assertEquals(firstChild.getNodeValue(), "fjfjf");
+ assertEquals(lastChild.getNodeValue(), "fjfjf");
+ }
+
+ /*
+ * Test setAttributeNode with an Attr from createAttribute.
+ */
+ @Test
+ public void testSetAttributeNode() throws Exception {
+ final String attrName = "myAttr";
+ final String attrValue = "attrValue";
+ Document document = createDOM("ElementSample02.xml");
+ Element elemNode = document.createElement("pricetag2");
+ Attr myAttr = document.createAttribute(attrName);
+ myAttr.setValue(attrValue);
+
+ assertNull(elemNode.setAttributeNode(myAttr));
+ assertEquals(elemNode.getAttribute(attrName), attrValue);
+ }
+
+ @DataProvider(name = "attribute")
+ public Object[][] getAttributeData() {
+ return new Object[][] {
+ { "thisisname", "thisisitsvalue" },
+ { "style", "font-Family" } };
+ }
+
+ @Test(dataProvider = "attribute")
+ public void testSetAttribute(String name, String value) throws Exception {
+ Document document = createDOM("ElementSample02.xml");
+ Element elemNode = document.createElement("pricetag2");
+ elemNode.setAttribute(name, value);
+ assertEquals(elemNode.getAttribute(name), value);
+ }
+
+ /*
+ * Negative test for setAttribute, null is not a valid name.
+ */
+ @Test(expectedExceptions = DOMException.class)
+ public void testSetAttributeNeg() throws Exception {
+ Document document = createDOM("ElementSample02.xml");
+ Element elemNode = document.createElement("pricetag2");
+ elemNode.setAttribute(null, null);
+ }
+
+ /*
+ * Test setAttributeNode, newAttr can't be an attribute of another Element
+ * object, must explicitly clone Attr nodes to re-use them in other
+ * elements.
+ */
+ @Test
+ public void testDuplicateAttributeNode() throws Exception {
+ final String name = "testAttrName";
+ final String value = "testAttrValue";
+ Document document = createNewDocument();
+ Attr attr = document.createAttribute(name);
+ attr.setValue(value);
+
+ Element element1 = document.createElement("AFirstElement");
+ element1.setAttributeNode(attr);
+ Element element2 = document.createElement("ASecondElement");
+ Attr attr2 = (Attr) attr.cloneNode(true);
+ element2.setAttributeNode(attr2);
+ assertEquals(element1.getAttribute(name), element2.getAttribute(name));
+
+ Element element3 = document.createElement("AThirdElement");
+ try {
+ element3.setAttributeNode(attr);
+ fail(DOMEXCEPTION_EXPECTED);
+ } catch (DOMException doe) {
+ assertEquals(doe.code, INUSE_ATTRIBUTE_ERR);
+ }
+ }
+
+ /*
+ * If not setting the namsepace aware method of DocumentBuilderFactory to
+ * true, can't retrieve element by namespace and local name.
+ */
+ @Test
+ public void testNamespaceAware() throws Exception {
+ Document document = createDOM("ElementSample02.xml");
+
+ NodeList nl = document.getElementsByTagNameNS("urn:BooksAreUs.org:BookInfo", "author");
+ assertNull(nl.item(0));
+
+ nl = document.getDocumentElement().getElementsByTagNameNS("urn:BooksAreUs.org:BookInfo", "author");
+ assertNull(nl.item(0));
+ }
+
+ @DataProvider(name = "nsattribute")
+ public Object[][] getNSAttributeData() {
+ return new Object[][] {
+ { "h:html", "html", "attrValue" },
+ { "b:style", "style", "attrValue" } };
+ }
+
+ /*
+ * setAttributeNodeNS and verify it with getAttributeNS.
+ */
+ @Test(dataProvider = "nsattribute")
+ public void testSetAttributeNodeNS(String qualifiedName, String localName, String value) throws Exception {
+ Document document = createDOM("ElementSample03.xml");
+ Element elemNode = document.createElement("pricetag2");
+ Attr myAttr = document.createAttributeNS(XML_NS_URI, qualifiedName);
+ myAttr.setValue(value);
+ assertNull(elemNode.setAttributeNodeNS(myAttr));
+ assertEquals(elemNode.getAttributeNS(XML_NS_URI, localName), value);
+ }
+
+ @Test
+ public void testHasAttributeNS() throws Exception {
+ Document document = createDOMWithNS("ElementSample04.xml");
+ NodeList nodeList = document.getElementsByTagName("body");
+ NodeList childList = nodeList.item(0).getChildNodes();
+ Element child = (Element) childList.item(7);
+ assertTrue(child.hasAttributeNS("urn:BooksAreUs.org:BookInfo", "style"));
+ }
+
+ @Test
+ public void testToString() throws Exception {
+ final String xml =
+ ""
+ + ""
+ + "
Welcome to the world of typography! Here is a book that you may find useful.
+Welcome to the world of typography! Here is a book that you may find useful.
+Welcome to the world of typography! Here is a book that you may find useful.
+Welcome to the world of typography! Here is a book that you may find useful.
+Welcome to the world of typography! Here is a book that you may find useful.
+Welcome to the world of typography! Here is a book that you may find useful.
+Welcome to the world of typography! Here is a book that you may find useful.
+Welcome to the world of typography! Here is a book that you may find useful.
+Welcome to the world of typography! Here is a book that you may find useful.
+Welcome to the world of typography! Here is a book that you may find useful.
+