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