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     @Test
  95     public void testNormalize() throws Exception {
  96         Document document = createDOM("Node05.xml");
  97 
  98         Element root = document.getDocumentElement();
  99         
 100         Node node =  document.getElementsByTagName("title").item(0);
 101         node.appendChild(document.createTextNode("test"));
 102         root.normalize();
 103         assertEquals(node.getChildNodes().item(0).getNodeValue(), "Typographytest");
 104     }
 105 
 106     @Test
 107     public void testCloneNode() throws Exception {
 108         Document document = createDOMWithNS("Node02.xml");
 109 
 110         NodeList nodeList = document.getElementsByTagName("body");
 111         Node node = nodeList.item(0);
 112         Node cloneNode = node.cloneNode(true);
 113 
 114         assertTrue(node.isEqualNode(cloneNode));
 115         assertNotEquals(node, cloneNode);
 116 
 117         nodeList = document.getElementsByTagName("html");
 118         Node node2 = nodeList.item(0);
 119         node2.appendChild(cloneNode);
 120     }
 121 
 122     @Test
 123     public void testImportNode() throws Exception {
 124         Document document = createDOMWithNS("Node02.xml");
 125         Document otherDocument = createDOMWithNS("ElementSample01.xml");
 126 
 127         NodeList otherNodeList = otherDocument.getElementsByTagName("body");
 128         Node importedNode = otherNodeList.item(0);
 129         Node clone = importedNode.cloneNode(true);
 130         
 131         Node retNode = document.importNode(importedNode, true);
 132         assertTrue(clone.isEqualNode(importedNode)); //verify importedNode is not changed
 133         assertNotEquals(retNode, importedNode);
 134         assertTrue(importedNode.isEqualNode(retNode));
 135 
 136         retNode = document.importNode(importedNode, false);
 137         assertTrue(clone.isEqualNode(importedNode)); //verify importedNode is not changed
 138         assertEquals(retNode.getNodeName(), importedNode.getNodeName());
 139         assertFalse(importedNode.isEqualNode(retNode));
 140     }
 141 
 142     @Test
 143     public void testInsertBefore() throws Exception {
 144         Document document = createDOM("Node04.xml");
 145 
 146         Element parentElement = (Element) document.getElementsByTagName("to").item(0);
 147         Element element = (Element) document.getElementsByTagName("sender").item(0);
 148         parentElement.insertBefore(createTestDocumentFragment(document), element);
 149 
 150         String outputfile = USER_DIR + "dom" + separator + "InsertBefore.out";
 151         String goldfile = GOLDEN_DIR + "InsertBeforeGF.out";
 152         outputXml(document, outputfile);
 153         assertTrue(compareWithGold(goldfile, outputfile));
 154     }
 155 
 156     @Test
 157     public void testReplaceChild() throws Exception {
 158         Document document = createDOM("Node04.xml");
 159 
 160         Element parentElement = (Element) document.getElementsByTagName("to").item(0);
 161         Element element = (Element) document.getElementsByTagName("sender").item(0);
 162         parentElement.replaceChild(createTestDocumentFragment(document), element);
 163 
 164         String outputfile = USER_DIR + "dom" + separator + "ReplaceChild3.out";
 165         String goldfile = GOLDEN_DIR + "ReplaceChild3GF.out";
 166         outputXml(document, outputfile);
 167         assertTrue(compareWithGold(goldfile, outputfile));
 168     }
 169 
 170     /*
 171      * This test case checks for the replaceChild replacing a particular node
 172      * with a node which was created from a different document than the one
 173      * which is trying to use this method. It should throw a DOMException.
 174      */
 175     @Test(expectedExceptions = DOMException.class)
 176     public void testReplaceChildNeg() throws Exception {
 177         Document document = createDOM("Node04.xml");
 178         Document doc2 = createNewDocument();
 179 
 180         Element parentElement = (Element) document.getElementsByTagName("to").item(0);
 181         Element element = (Element) document.getElementsByTagName("sender").item(0);
 182         parentElement.replaceChild(createTestDocumentFragment(doc2), element);
 183     }
 184 
 185     private DocumentFragment createTestDocumentFragment(Document document) {
 186         DocumentFragment docFragment = document.createDocumentFragment();
 187         Element elem = document.createElement("dfElement");
 188         elem.appendChild(document.createTextNode("Text in it"));
 189         docFragment.appendChild(elem);
 190         return docFragment;
 191     }
 192 
 193     private void outputXml(Document document, String outputFileName) throws TransformerFactoryConfigurationError, TransformerException {
 194         DOMSource domSource = new DOMSource(document);
 195         Transformer transformer = TransformerFactory.newInstance().newTransformer();
 196         StreamResult streamResult = new StreamResult(new File(outputFileName));
 197         transformer.transform(domSource, streamResult);
 198     }
 199 }