1 /* 2 * Copyright (c) 2003, 2016, 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 jaxp.library.JAXPTestUtilities.compareWithGold; 26 import static jaxp.library.JAXPTestUtilities.tryRunWithTmpPermission; 27 import static org.testng.Assert.assertEquals; 28 import static org.testng.Assert.assertFalse; 29 import static org.testng.Assert.assertNotEquals; 30 import static org.testng.Assert.assertTrue; 31 import static org.w3c.dom.ptests.DOMTestUtil.GOLDEN_DIR; 32 import static org.w3c.dom.ptests.DOMTestUtil.createDOM; 33 import static org.w3c.dom.ptests.DOMTestUtil.createDOMWithNS; 34 import static org.w3c.dom.ptests.DOMTestUtil.createNewDocument; 35 36 import java.io.File; 37 import java.util.PropertyPermission; 38 39 import javax.xml.transform.Transformer; 40 import javax.xml.transform.TransformerException; 41 import javax.xml.transform.TransformerFactory; 42 import javax.xml.transform.TransformerFactoryConfigurationError; 43 import javax.xml.transform.dom.DOMSource; 44 import javax.xml.transform.stream.StreamResult; 45 46 import org.testng.annotations.DataProvider; 47 import org.testng.annotations.Listeners; 48 import org.testng.annotations.Test; 49 import org.w3c.dom.DOMException; 50 import org.w3c.dom.Document; 51 import org.w3c.dom.DocumentFragment; 52 import org.w3c.dom.Element; 53 import org.w3c.dom.Node; 54 import org.w3c.dom.NodeList; 55 56 /* 57 * @test 58 * @library /javax/xml/jaxp/libs 59 * @run testng/othervm -DrunSecMngr=true org.w3c.dom.ptests.NodeTest 60 * @run testng/othervm org.w3c.dom.ptests.NodeTest 61 * @summary Test Node interface 62 */ 63 @Listeners({jaxp.library.FilePolicy.class}) 64 public class NodeTest { 65 @DataProvider(name = "feature-supported") 66 public Object[][] getFeatureSupportedList() throws Exception { 67 Document document = createDOMWithNS("Node01.xml"); 68 Node node = document.getElementsByTagName("body").item(0); 69 return new Object[][] { 70 { node, "XML", "2.0", true }, 71 { node, "HTML", "2.0", false }, 72 { node, "Views", "2.0", false }, 73 { node, "StyleSheets", "2.0", false }, 74 { node, "CSS", "2.0", false }, 75 { node, "CSS2", "2.0", false }, 76 { node, "Events", "2.0", true }, 77 { node, "UIEvents", "2.0", false }, 78 { node, "MouseEvents", "2.0", false }, 79 { node, "HTMLEvents", "2.0", false }, 80 { node, "Traversal", "2.0", true }, 81 { node, "Range", "2.0", true } }; 82 } 83 84 /* 85 * Verify Node for feature supporting. 86 */ 87 @Test(dataProvider = "feature-supported") 88 public void testHasFeature(Node node, String feature, String version, boolean supported) { 89 assertEquals(node.isSupported(feature, version), supported); 90 } 91 92 /* 93 * Test normalize method will merge adjacent Text nodes. 94 */ 95 @Test 96 public void testNormalize() throws Exception { 97 Document document = createDOM("Node05.xml"); 98 99 Element root = document.getDocumentElement(); 100 101 Node node = document.getElementsByTagName("title").item(0); 102 node.appendChild(document.createTextNode("test")); 103 root.normalize(); 104 assertEquals(node.getChildNodes().item(0).getNodeValue(), "Typographytest"); 105 } 106 107 /* 108 * Test cloneNode deeply, and the clone node can be appended on the same document. 109 */ 110 @Test 111 public void testCloneNode() throws Exception { 112 Document document = createDOMWithNS("Node02.xml"); 113 114 NodeList nodeList = document.getElementsByTagName("body"); 115 Node node = nodeList.item(0); 116 Node cloneNode = node.cloneNode(true); 117 118 assertTrue(node.isEqualNode(cloneNode)); 119 assertNotEquals(node, cloneNode); 120 121 nodeList = document.getElementsByTagName("html"); 122 Node node2 = nodeList.item(0); 123 node2.appendChild(cloneNode); 124 } 125 126 /* 127 * Test importing node from one document to another. 128 */ 129 @Test 130 public void testImportNode() throws Exception { 131 Document document = createDOMWithNS("Node02.xml"); 132 Document otherDocument = createDOMWithNS("ElementSample01.xml"); 133 134 NodeList otherNodeList = otherDocument.getElementsByTagName("body"); 135 Node importedNode = otherNodeList.item(0); 136 Node clone = importedNode.cloneNode(true); 137 138 Node retNode = document.importNode(importedNode, true); 139 assertTrue(clone.isEqualNode(importedNode)); //verify importedNode is not changed 140 assertNotEquals(retNode, importedNode); 141 assertTrue(importedNode.isEqualNode(retNode)); 142 143 retNode = document.importNode(importedNode, false); 144 assertTrue(clone.isEqualNode(importedNode)); //verify importedNode is not changed 145 assertEquals(retNode.getNodeName(), importedNode.getNodeName()); 146 assertFalse(importedNode.isEqualNode(retNode)); 147 } 148 149 /* 150 * Test inserting a document fragment before a particular node. 151 */ 152 @Test 153 public void testInsertBefore() throws Exception { 154 Document document = createDOM("Node04.xml"); 155 156 Element parentElement = (Element) document.getElementsByTagName("to").item(0); 157 Element element = (Element) document.getElementsByTagName("sender").item(0); 158 parentElement.insertBefore(createTestDocumentFragment(document), element); 159 160 String outputfile = "InsertBefore.out"; 161 String goldfile = GOLDEN_DIR + "InsertBeforeGF.out"; 162 tryRunWithTmpPermission(() -> outputXml(document, outputfile), new PropertyPermission("user.dir", "read")); 163 assertTrue(compareWithGold(goldfile, outputfile)); 164 } 165 166 167 /* 168 * Test replacing a particular node with a document fragment. 169 */ 170 @Test 171 public void testReplaceChild() throws Exception { 172 Document document = createDOM("Node04.xml"); 173 174 Element parentElement = (Element) document.getElementsByTagName("to").item(0); 175 Element element = (Element) document.getElementsByTagName("sender").item(0); 176 parentElement.replaceChild(createTestDocumentFragment(document), element); 177 178 String outputfile = "ReplaceChild3.out"; 179 String goldfile = GOLDEN_DIR + "ReplaceChild3GF.out"; 180 tryRunWithTmpPermission(() -> outputXml(document, outputfile), new PropertyPermission("user.dir", "read")); 181 assertTrue(compareWithGold(goldfile, outputfile)); 182 } 183 184 /* 185 * This test case checks for the replaceChild replacing a particular node 186 * with a node which was created from a different document than the one 187 * which is trying to use this method. It should throw a DOMException. 188 */ 189 @Test(expectedExceptions = DOMException.class) 190 public void testReplaceChildNeg() throws Exception { 191 Document document = createDOM("Node04.xml"); 192 Document doc2 = createNewDocument(); 193 194 Element parentElement = (Element) document.getElementsByTagName("to").item(0); 195 Element element = (Element) document.getElementsByTagName("sender").item(0); 196 parentElement.replaceChild(createTestDocumentFragment(doc2), element); 197 } 198 199 private DocumentFragment createTestDocumentFragment(Document document) { 200 DocumentFragment docFragment = document.createDocumentFragment(); 201 Element elem = document.createElement("dfElement"); 202 elem.appendChild(document.createTextNode("Text in it")); 203 docFragment.appendChild(elem); 204 return docFragment; 205 } 206 207 private void outputXml(Document document, String outputFileName) throws TransformerFactoryConfigurationError, TransformerException { 208 DOMSource domSource = new DOMSource(document); 209 Transformer transformer = TransformerFactory.newInstance().newTransformer(); 210 StreamResult streamResult = new StreamResult(new File(outputFileName)); 211 transformer.transform(domSource, streamResult); 212 } 213 }