1 /* 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3 */ 4 /* 5 * Licensed to the Apache Software Foundation (ASF) under one or more 6 * contributor license agreements. See the NOTICE file distributed with 7 * this work for additional information regarding copyright ownership. 8 * The ASF licenses this file to You under the Apache License, Version 2.0 9 * (the "License"); you may not use this file except in compliance with 10 * the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 */ 20 package org.apache.qetest.trax.dom; 21 22 import java.io.IOException; 23 import javax.xml.parsers.DocumentBuilder; 24 import javax.xml.parsers.DocumentBuilderFactory; 25 import javax.xml.parsers.ParserConfigurationException; 26 import javax.xml.transform.Templates; 27 import javax.xml.transform.Transformer; 28 import javax.xml.transform.TransformerConfigurationException; 29 import javax.xml.transform.TransformerException; 30 import javax.xml.transform.TransformerFactory; 31 import javax.xml.transform.dom.DOMResult; 32 import javax.xml.transform.dom.DOMSource; 33 import jaxp.library.JAXPFileBaseTest; 34 import static jaxp.library.JAXPTestUtilities.compareSerializeDOMWithGold; 35 import static jaxp.library.JAXPTestUtilities.filenameToURL; 36 import static org.apache.qetest.trax.TraxConst.GOLDEN_DIR; 37 import static org.apache.qetest.trax.TraxConst.XML_DIR; 38 import static org.testng.Assert.assertEquals; 39 import static org.testng.Assert.assertNotNull; 40 import static org.testng.Assert.assertNull; 41 import static org.testng.Assert.assertTrue; 42 import org.testng.annotations.Test; 43 import org.w3c.dom.Node; 44 import org.xml.sax.InputSource; 45 import org.xml.sax.SAXException; 46 47 /** 48 * API Coverage test for the DOMSource class of TRAX. 49 */ 50 public class DOMSourceAPITest extends JAXPFileBaseTest { 51 /** 52 * Basic API coverage, constructor and set/get methods. 53 * 54 * @throws ParserConfigurationException if the implementation is not 55 * available or cannot be instantiated. 56 */ 57 @Test 58 public void testCase1() throws ParserConfigurationException { 59 // Default no-arg ctor sets nothing (but needs special test for 60 // creating new doc when being transformed) 61 DOMSource defaultDOM = new DOMSource(); 62 assertNull(defaultDOM.getNode()); 63 assertNull(defaultDOM.getSystemId()); 64 DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); 65 DocumentBuilder docBuilder = dfactory.newDocumentBuilder(); 66 Node n = docBuilder.newDocument(); 67 DOMSource nodeDOM = new DOMSource(n); 68 assertEquals(nodeDOM.getNode(), n); 69 assertNull(nodeDOM.getSystemId()); 70 71 DOMSource nodeDOMid = new DOMSource(n, "this-is-system-id"); 72 assertEquals(nodeDOMid.getNode(), n); 73 assertEquals(nodeDOMid.getSystemId(), "this-is-system-id"); 74 75 DOMSource wackyDOM = new DOMSource(); 76 Node n2 = docBuilder.newDocument(); 77 wackyDOM.setNode(n2); 78 assertEquals(wackyDOM.getNode(), n2); 79 80 wackyDOM.setSystemId("another-system-id"); 81 assertEquals(wackyDOM.getSystemId(), "another-system-id"); 82 } 83 84 /** 85 * newTemplates on empty DOMSource throws TransformerConfigurationException. 86 * 87 * @throws ParserConfigurationException in case of ServiceConfigurationError 88 * service configuration error or if the implementation is not 89 * available or cannot be instantiated. 90 * @throws TransformerConfigurationException Thrown in case of 91 * ServiceConfigurationError service configuration error or if the 92 * implementation is not available or cannot be instantiated. 93 */ 94 @Test(expectedExceptions = TransformerConfigurationException.class) 95 public void negativeTestCase21() throws ParserConfigurationException, 96 TransformerConfigurationException { 97 // Startup a factory, create some nodes/DOMs 98 TransformerFactory factory = TransformerFactory.newInstance(); 99 DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); 100 dfactory.setNamespaceAware(true); 101 // A blank DOM as an input stylesheet - what should happen? 102 DOMSource blankXSLDOM = new DOMSource(); 103 factory.newTemplates(blankXSLDOM); 104 } 105 106 /** 107 * newTransformer on empty DOMSource throws TransformerConfigurationException. 108 * 109 * @throws ParserConfigurationException if the implementation is not 110 * available or cannot be instantiated. 111 * @throws TransformerConfigurationException Thrown in case of 112 * ServiceConfigurationError service configuration error or if the 113 * implementation is not available or cannot be instantiated. 114 */ 115 @Test(expectedExceptions = TransformerConfigurationException.class) 116 public void negativeTestCase22() throws ParserConfigurationException, 117 TransformerConfigurationException { 118 // Startup a factory, create some nodes/DOMs 119 TransformerFactory factory = TransformerFactory.newInstance(); 120 DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); 121 dfactory.setNamespaceAware(true); 122 // A blank DOM as an input stylesheet - what should happen? 123 DOMSource blankXSLDOM = new DOMSource(); 124 factory.newTransformer(blankXSLDOM); 125 } 126 127 /** 128 * Validation with DOMSource on TransformerException. 129 * 130 * @throws TransformerException If an unrecoverable error occurs during the 131 * course of the transformation. 132 * @throws ParserConfigurationException if the implementation is not 133 * available or cannot be instantiated. 134 * @throws SAXException for SAX error. 135 * @throws IOException if any I/O operation error. 136 */ 137 @Test(expectedExceptions = TransformerException.class) 138 public void negativeTestCase24() throws TransformerException, 139 ParserConfigurationException, SAXException, IOException { 140 String xslFile = XML_DIR + "DOMImpIncl.xsl"; 141 String xslImpInclURI = filenameToURL(xslFile); 142 String xmlFile = XML_DIR + "DOMImpIncl.xml"; 143 String xmlImpInclURI = filenameToURL(xmlFile); 144 145 // Startup a factory, create some nodes/DOMs 146 TransformerFactory factory = TransformerFactory.newInstance(); 147 DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); 148 dfactory.setNamespaceAware(true); 149 DocumentBuilder docBuilder = dfactory.newDocumentBuilder(); 150 Node xslImpInclNode = docBuilder.parse(new InputSource(xslImpInclURI)); 151 Node xmlImpInclNode = docBuilder.parse(new InputSource(xmlImpInclURI)); 152 153 DOMSource xslDOM = new DOMSource(xslImpInclNode); 154 Transformer transformerXSL = factory.newTransformer(xslDOM); 155 DOMSource xmlDOM = new DOMSource(xmlImpInclNode); 156 DOMResult emptyResult = new DOMResult(); 157 transformerXSL.transform(xmlDOM, emptyResult); 158 } 159 160 /** 161 * Blank DOMSource should parse without error. 162 * @throws ParserConfigurationException if the implementation is not 163 * available or cannot be instantiated. 164 * @throws SAXException for SAX error. 165 * @throws IOException if any I/O operation error. 166 * @throws TransformerException If an unrecoverable error occurs during the 167 * course of the transformation. 168 */ 169 @Test 170 public void positiveTestCase21() throws ParserConfigurationException, 171 SAXException, IOException, TransformerException { 172 String xslURI = filenameToURL(XML_DIR + "DOMTest.xsl"); 173 String xmlURI = filenameToURL(XML_DIR + "DOMTest.xml"); 174 String goldFile = GOLDEN_DIR + "DOMTest.out"; 175 176 // Startup a factory, create some nodes/DOMs 177 TransformerFactory factory = TransformerFactory.newInstance(); 178 DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); 179 dfactory.setNamespaceAware(true); 180 DocumentBuilder docBuilder = dfactory.newDocumentBuilder(); 181 Node xslNode = docBuilder.parse(new InputSource(xslURI)); 182 Node xmlNode = docBuilder.parse(new InputSource(xmlURI)); 183 // Try to get templates, transformerXSL from node 184 DOMSource xslDOM = new DOMSource(xslNode); 185 Templates templates = factory.newTemplates(xslDOM); 186 assertNotNull(templates); 187 Transformer transformerXSL = factory.newTransformer(xslDOM); 188 assertNotNull(transformerXSL); 189 190 // A blank DOM as an output of the transform - should auto-create a 191 // source Document 192 DOMSource xmlDOM = new DOMSource(xmlNode); 193 Node outNode = docBuilder.newDocument(); 194 DOMResult outResult = new DOMResult(outNode); 195 transformerXSL.transform(xmlDOM, outResult); 196 outNode = outResult.getNode(); 197 assertNotNull(outNode); 198 assertTrue(compareSerializeDOMWithGold(goldFile, outNode)); 199 } 200 201 /** 202 * Validate DOMSource setting with systemid. 203 * @throws ParserConfigurationException if the implementation is not 204 * available or cannot be instantiated. 205 * @throws SAXException for SAX error. 206 * @throws IOException if any I/O operation error. 207 * @throws TransformerException If an unrecoverable error occurs during the 208 * course of the transformation. 209 */ 210 @Test 211 public void positiveTestCase22() throws ParserConfigurationException, 212 SAXException, IOException, TransformerException { 213 String xslImpInclFile = XML_DIR + "DOMImpIncl.xsl"; 214 String xslImpInclURI = filenameToURL(xslImpInclFile); 215 String xmlImpInclFile = XML_DIR + "DOMImpIncl.xml"; 216 String xmlImpInclURI = filenameToURL(xmlImpInclFile); 217 String goldImpInclFile = GOLDEN_DIR + "DOMImpIncl.out"; 218 219 // Startup a factory, create some nodes/DOMs 220 TransformerFactory factory = TransformerFactory.newInstance(); 221 DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); 222 dfactory.setNamespaceAware(true); 223 DocumentBuilder docBuilder = dfactory.newDocumentBuilder(); 224 Node xslImpInclNode = docBuilder.parse(new InputSource(xslImpInclURI)); 225 Node xmlImpInclNode = docBuilder.parse(new InputSource(xmlImpInclURI)); 226 227 DOMSource xslDOM = new DOMSource(xslImpInclNode); 228 // Note that inputName, xmlName are already URL'd 229 xslDOM.setSystemId(xslImpInclURI); 230 Transformer transformerXSL = factory.newTransformer(xslDOM); 231 DOMSource xmlDOM = new DOMSource(xmlImpInclNode); 232 // Do we really need to set SystemId on both XML and XSL? 233 xmlDOM.setSystemId(xmlImpInclFile); 234 DOMResult emptyResult = new DOMResult(); 235 transformerXSL.transform(xmlDOM, emptyResult); 236 Node outNode = emptyResult.getNode(); 237 assertNotNull(outNode); 238 assertTrue(compareSerializeDOMWithGold(goldImpInclFile, outNode)); 239 } 240 241 /** 242 * Validate simple transform on DOMSource. 243 * @throws TransformerException If an unrecoverable error occurs during the 244 * course of the transformation. 245 * @throws IOException if any I/O operation error. 246 * @throws ParserConfigurationException if the implementation is not 247 * available or cannot be instantiated. 248 * @throws SAXException for SAX error. 249 */ 250 @Test 251 public void positiveTestCase23() throws TransformerException, IOException, 252 ParserConfigurationException, SAXException { 253 String xslURI = filenameToURL(XML_DIR + "DOMTest.xsl"); 254 String xmlURI = filenameToURL(XML_DIR + "DOMTest.xml"); 255 String goldFile = GOLDEN_DIR + "DOMTest.out"; 256 257 // Startup a factory, create some nodes/DOMs 258 TransformerFactory factory = TransformerFactory.newInstance(); 259 DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); 260 dfactory.setNamespaceAware(true); 261 DocumentBuilder docBuilder = dfactory.newDocumentBuilder(); 262 Node xslNode = docBuilder.parse(new InputSource(xslURI)); 263 Node xmlNode = docBuilder.parse(new InputSource(xmlURI)); 264 // Try to get templates, transformerXSL from node 265 DOMSource xslDOM = new DOMSource(xslNode); 266 Templates templates = factory.newTemplates(xslDOM); 267 assertNotNull(templates); 268 Transformer transformerXSL = factory.newTransformer(xslDOM); 269 assertNotNull(transformerXSL); 270 271 // A simple DOM-DOM-DOM transform 272 DOMSource xmlDOM = new DOMSource(xmlNode); 273 Node outNode = docBuilder.newDocument(); 274 DOMResult outDOM = new DOMResult(outNode); 275 transformerXSL.transform(xmlDOM, outDOM); 276 Node gotNode = outDOM.getNode(); 277 assertNotNull(gotNode); 278 assertTrue(compareSerializeDOMWithGold(goldFile, gotNode)); 279 } 280 281 /** 282 * Test re-usage source for the stylesheet. It expects same result. 283 * 284 * @throws ParserConfigurationException if the implementation is not 285 * available or cannot be instantiated. 286 * @throws SAXException for SAX error. 287 * @throws IOException if any I/O operation error. 288 * @throws TransformerException If an unrecoverable error occurs during the 289 * course of the transformation. 290 */ 291 @Test 292 public void testCase3() throws ParserConfigurationException, SAXException, 293 IOException, TransformerException { 294 String xslFile = XML_DIR + "DOMTest.xsl"; 295 String xmlFile = XML_DIR + "DOMTest.xml"; 296 String goldFile = GOLDEN_DIR + "DOMTest.out"; 297 298 String xslImpInclFile = XML_DIR + "DOMImpIncl.xsl"; 299 String xmlImpInclFile = XML_DIR + "DOMImpIncl.xml"; 300 String goldImpInclFile = GOLDEN_DIR + "DOMImpIncl.out"; 301 302 // Startup a factory, create some nodes/DOMs 303 TransformerFactory factory = TransformerFactory.newInstance(); 304 DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); 305 dfactory.setNamespaceAware(true); 306 DocumentBuilder docBuilder = dfactory.newDocumentBuilder(); 307 Node xslNode = docBuilder.parse(new InputSource(xslFile)); 308 Node xmlNode = docBuilder.parse(new InputSource(xmlFile)); 309 Node xslImpInclNode = docBuilder.parse(new InputSource(xslImpInclFile)); 310 Node xmlImpInclNode = docBuilder.parse(new InputSource(xmlImpInclFile)); 311 312 // Re-use DOMSource for stylesheet 313 DOMSource xmlSource1 = new DOMSource(xmlNode); 314 DOMResult result1 = new DOMResult(docBuilder.newDocument()); 315 DOMSource xslSource = new DOMSource(xslNode); 316 Transformer transformer1 = factory.newTransformer(xslSource); 317 transformer1.transform(xmlSource1, result1); 318 Node node1 = result1.getNode(); 319 assertTrue(compareSerializeDOMWithGold(goldFile, node1)); 320 // Use same Source for the stylesheet 321 DOMSource xmlSource2 = new DOMSource(xmlNode); 322 DOMResult result2 = new DOMResult(docBuilder.newDocument()); 323 Transformer transformer2 = factory.newTransformer(xslSource); 324 transformer2.transform(xmlSource2, result2); 325 Node node2 = result2.getNode(); 326 assertTrue(compareSerializeDOMWithGold(goldFile, node2)); 327 328 // Re-use DOMSource for XML doc; with the same stylesheet 329 DOMResult result3 = new DOMResult(docBuilder.newDocument()); 330 Transformer transformer3 = factory.newTransformer(xslSource); 331 transformer3.transform(xmlSource2, result3); 332 Node node3 = result3.getNode(); 333 assertTrue(compareSerializeDOMWithGold(goldFile, node3)); 334 335 // Re-use DOMSource after setNode to different one 336 DOMSource xmlSource = new DOMSource(xmlNode); 337 // Use same Sources, but change Nodes for xml,xsl 338 xmlSource.setNode(xmlImpInclNode); 339 xmlSource.setSystemId(xmlImpInclFile); 340 xslSource.setNode(xslImpInclNode); 341 xslSource.setSystemId(xslImpInclFile); 342 transformer2 = factory.newTransformer(xslSource); 343 result2 = new DOMResult(docBuilder.newDocument()); 344 transformer2.transform(xmlSource, result2); 345 node2 = result2.getNode(); 346 assertTrue(compareSerializeDOMWithGold(goldImpInclFile, node2)); 347 } 348 }