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