/* * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. */ /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.qetest.trax.dom; import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Templates; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMResult; import javax.xml.transform.dom.DOMSource; import jaxp.library.JAXPFileBaseTest; import static jaxp.library.JAXPTestUtilities.compareSerializeDOMWithGold; import static jaxp.library.JAXPTestUtilities.filenameToURL; import static org.apache.qetest.trax.TraxConst.GOLDEN_DIR; import static org.apache.qetest.trax.TraxConst.XML_DIR; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertNull; import static org.testng.Assert.assertTrue; import org.testng.annotations.Test; import org.w3c.dom.Node; import org.xml.sax.InputSource; import org.xml.sax.SAXException; /** * API Coverage test for the DOMSource class of TRAX. */ public class DOMSourceAPITest extends JAXPFileBaseTest { /** * Basic API coverage, constructor and set/get methods. * * @throws ParserConfigurationException if the implementation is not * available or cannot be instantiated. */ @Test public void testCase1() throws ParserConfigurationException { // Default no-arg ctor sets nothing (but needs special test for // creating new doc when being transformed) DOMSource defaultDOM = new DOMSource(); assertNull(defaultDOM.getNode()); assertNull(defaultDOM.getSystemId()); DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = dfactory.newDocumentBuilder(); Node n = docBuilder.newDocument(); DOMSource nodeDOM = new DOMSource(n); assertEquals(nodeDOM.getNode(), n); assertNull(nodeDOM.getSystemId()); DOMSource nodeDOMid = new DOMSource(n, "this-is-system-id"); assertEquals(nodeDOMid.getNode(), n); assertEquals(nodeDOMid.getSystemId(), "this-is-system-id"); DOMSource wackyDOM = new DOMSource(); Node n2 = docBuilder.newDocument(); wackyDOM.setNode(n2); assertEquals(wackyDOM.getNode(), n2); wackyDOM.setSystemId("another-system-id"); assertEquals(wackyDOM.getSystemId(), "another-system-id"); } /** * newTemplates on empty DOMSource throws TransformerConfigurationException. * * @throws ParserConfigurationException in case of ServiceConfigurationError * service configuration error or if the implementation is not * available or cannot be instantiated. * @throws TransformerConfigurationException Thrown in case of * ServiceConfigurationError service configuration error or if the * implementation is not available or cannot be instantiated. */ @Test(expectedExceptions = TransformerConfigurationException.class) public void negativeTestCase21() throws ParserConfigurationException, TransformerConfigurationException { // Startup a factory, create some nodes/DOMs TransformerFactory factory = TransformerFactory.newInstance(); DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); dfactory.setNamespaceAware(true); // A blank DOM as an input stylesheet - what should happen? DOMSource blankXSLDOM = new DOMSource(); factory.newTemplates(blankXSLDOM); } /** * newTransformer on empty DOMSource throws TransformerConfigurationException. * * @throws ParserConfigurationException if the implementation is not * available or cannot be instantiated. * @throws TransformerConfigurationException Thrown in case of * ServiceConfigurationError service configuration error or if the * implementation is not available or cannot be instantiated. */ @Test(expectedExceptions = TransformerConfigurationException.class) public void negativeTestCase22() throws ParserConfigurationException, TransformerConfigurationException { // Startup a factory, create some nodes/DOMs TransformerFactory factory = TransformerFactory.newInstance(); DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); dfactory.setNamespaceAware(true); // A blank DOM as an input stylesheet - what should happen? DOMSource blankXSLDOM = new DOMSource(); factory.newTransformer(blankXSLDOM); } /** * Validation with DOMSource on TransformerException. * * @throws TransformerException If an unrecoverable error occurs during the * course of the transformation. * @throws ParserConfigurationException if the implementation is not * available or cannot be instantiated. * @throws SAXException for SAX error. * @throws IOException if any I/O operation error. */ @Test(expectedExceptions = TransformerException.class) public void negativeTestCase24() throws TransformerException, ParserConfigurationException, SAXException, IOException { String xslFile = XML_DIR + "DOMImpIncl.xsl"; String xslImpInclURI = filenameToURL(xslFile); String xmlFile = XML_DIR + "DOMImpIncl.xml"; String xmlImpInclURI = filenameToURL(xmlFile); // Startup a factory, create some nodes/DOMs TransformerFactory factory = TransformerFactory.newInstance(); DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); dfactory.setNamespaceAware(true); DocumentBuilder docBuilder = dfactory.newDocumentBuilder(); Node xslImpInclNode = docBuilder.parse(new InputSource(xslImpInclURI)); Node xmlImpInclNode = docBuilder.parse(new InputSource(xmlImpInclURI)); DOMSource xslDOM = new DOMSource(xslImpInclNode); Transformer transformerXSL = factory.newTransformer(xslDOM); DOMSource xmlDOM = new DOMSource(xmlImpInclNode); DOMResult emptyResult = new DOMResult(); transformerXSL.transform(xmlDOM, emptyResult); } /** * Blank DOMSource should parse without error. * @throws ParserConfigurationException if the implementation is not * available or cannot be instantiated. * @throws SAXException for SAX error. * @throws IOException if any I/O operation error. * @throws TransformerException If an unrecoverable error occurs during the * course of the transformation. */ @Test public void positiveTestCase21() throws ParserConfigurationException, SAXException, IOException, TransformerException { String xslURI = filenameToURL(XML_DIR + "DOMTest.xsl"); String xmlURI = filenameToURL(XML_DIR + "DOMTest.xml"); String goldFile = GOLDEN_DIR + "DOMTest.out"; // Startup a factory, create some nodes/DOMs TransformerFactory factory = TransformerFactory.newInstance(); DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); dfactory.setNamespaceAware(true); DocumentBuilder docBuilder = dfactory.newDocumentBuilder(); Node xslNode = docBuilder.parse(new InputSource(xslURI)); Node xmlNode = docBuilder.parse(new InputSource(xmlURI)); // Try to get templates, transformerXSL from node DOMSource xslDOM = new DOMSource(xslNode); Templates templates = factory.newTemplates(xslDOM); assertNotNull(templates); Transformer transformerXSL = factory.newTransformer(xslDOM); assertNotNull(transformerXSL); // A blank DOM as an output of the transform - should auto-create a // source Document DOMSource xmlDOM = new DOMSource(xmlNode); Node outNode = docBuilder.newDocument(); DOMResult outResult = new DOMResult(outNode); transformerXSL.transform(xmlDOM, outResult); outNode = outResult.getNode(); assertNotNull(outNode); assertTrue(compareSerializeDOMWithGold(goldFile, outNode)); } /** * Validate DOMSource setting with systemid. * @throws ParserConfigurationException if the implementation is not * available or cannot be instantiated. * @throws SAXException for SAX error. * @throws IOException if any I/O operation error. * @throws TransformerException If an unrecoverable error occurs during the * course of the transformation. */ @Test public void positiveTestCase22() throws ParserConfigurationException, SAXException, IOException, TransformerException { String xslImpInclFile = XML_DIR + "DOMImpIncl.xsl"; String xslImpInclURI = filenameToURL(xslImpInclFile); String xmlImpInclFile = XML_DIR + "DOMImpIncl.xml"; String xmlImpInclURI = filenameToURL(xmlImpInclFile); String goldImpInclFile = GOLDEN_DIR + "DOMImpIncl.out"; // Startup a factory, create some nodes/DOMs TransformerFactory factory = TransformerFactory.newInstance(); DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); dfactory.setNamespaceAware(true); DocumentBuilder docBuilder = dfactory.newDocumentBuilder(); Node xslImpInclNode = docBuilder.parse(new InputSource(xslImpInclURI)); Node xmlImpInclNode = docBuilder.parse(new InputSource(xmlImpInclURI)); DOMSource xslDOM = new DOMSource(xslImpInclNode); // Note that inputName, xmlName are already URL'd xslDOM.setSystemId(xslImpInclURI); Transformer transformerXSL = factory.newTransformer(xslDOM); DOMSource xmlDOM = new DOMSource(xmlImpInclNode); // Do we really need to set SystemId on both XML and XSL? xmlDOM.setSystemId(xmlImpInclFile); DOMResult emptyResult = new DOMResult(); transformerXSL.transform(xmlDOM, emptyResult); Node outNode = emptyResult.getNode(); assertNotNull(outNode); assertTrue(compareSerializeDOMWithGold(goldImpInclFile, outNode)); } /** * Validate simple transform on DOMSource. * @throws TransformerException If an unrecoverable error occurs during the * course of the transformation. * @throws IOException if any I/O operation error. * @throws ParserConfigurationException if the implementation is not * available or cannot be instantiated. * @throws SAXException for SAX error. */ @Test public void positiveTestCase23() throws TransformerException, IOException, ParserConfigurationException, SAXException { String xslURI = filenameToURL(XML_DIR + "DOMTest.xsl"); String xmlURI = filenameToURL(XML_DIR + "DOMTest.xml"); String goldFile = GOLDEN_DIR + "DOMTest.out"; // Startup a factory, create some nodes/DOMs TransformerFactory factory = TransformerFactory.newInstance(); DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); dfactory.setNamespaceAware(true); DocumentBuilder docBuilder = dfactory.newDocumentBuilder(); Node xslNode = docBuilder.parse(new InputSource(xslURI)); Node xmlNode = docBuilder.parse(new InputSource(xmlURI)); // Try to get templates, transformerXSL from node DOMSource xslDOM = new DOMSource(xslNode); Templates templates = factory.newTemplates(xslDOM); assertNotNull(templates); Transformer transformerXSL = factory.newTransformer(xslDOM); assertNotNull(transformerXSL); // A simple DOM-DOM-DOM transform DOMSource xmlDOM = new DOMSource(xmlNode); Node outNode = docBuilder.newDocument(); DOMResult outDOM = new DOMResult(outNode); transformerXSL.transform(xmlDOM, outDOM); Node gotNode = outDOM.getNode(); assertNotNull(gotNode); assertTrue(compareSerializeDOMWithGold(goldFile, gotNode)); } /** * Test re-usage source for the stylesheet. It expects same result. * * @throws ParserConfigurationException if the implementation is not * available or cannot be instantiated. * @throws SAXException for SAX error. * @throws IOException if any I/O operation error. * @throws TransformerException If an unrecoverable error occurs during the * course of the transformation. */ @Test public void testCase3() throws ParserConfigurationException, SAXException, IOException, TransformerException { String xslFile = XML_DIR + "DOMTest.xsl"; String xmlFile = XML_DIR + "DOMTest.xml"; String goldFile = GOLDEN_DIR + "DOMTest.out"; String xslImpInclFile = XML_DIR + "DOMImpIncl.xsl"; String xmlImpInclFile = XML_DIR + "DOMImpIncl.xml"; String goldImpInclFile = GOLDEN_DIR + "DOMImpIncl.out"; // Startup a factory, create some nodes/DOMs TransformerFactory factory = TransformerFactory.newInstance(); DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); dfactory.setNamespaceAware(true); DocumentBuilder docBuilder = dfactory.newDocumentBuilder(); Node xslNode = docBuilder.parse(new InputSource(xslFile)); Node xmlNode = docBuilder.parse(new InputSource(xmlFile)); Node xslImpInclNode = docBuilder.parse(new InputSource(xslImpInclFile)); Node xmlImpInclNode = docBuilder.parse(new InputSource(xmlImpInclFile)); // Re-use DOMSource for stylesheet DOMSource xmlSource1 = new DOMSource(xmlNode); DOMResult result1 = new DOMResult(docBuilder.newDocument()); DOMSource xslSource = new DOMSource(xslNode); Transformer transformer1 = factory.newTransformer(xslSource); transformer1.transform(xmlSource1, result1); Node node1 = result1.getNode(); assertTrue(compareSerializeDOMWithGold(goldFile, node1)); // Use same Source for the stylesheet DOMSource xmlSource2 = new DOMSource(xmlNode); DOMResult result2 = new DOMResult(docBuilder.newDocument()); Transformer transformer2 = factory.newTransformer(xslSource); transformer2.transform(xmlSource2, result2); Node node2 = result2.getNode(); assertTrue(compareSerializeDOMWithGold(goldFile, node2)); // Re-use DOMSource for XML doc; with the same stylesheet DOMResult result3 = new DOMResult(docBuilder.newDocument()); Transformer transformer3 = factory.newTransformer(xslSource); transformer3.transform(xmlSource2, result3); Node node3 = result3.getNode(); assertTrue(compareSerializeDOMWithGold(goldFile, node3)); // Re-use DOMSource after setNode to different one DOMSource xmlSource = new DOMSource(xmlNode); // Use same Sources, but change Nodes for xml,xsl xmlSource.setNode(xmlImpInclNode); xmlSource.setSystemId(xmlImpInclFile); xslSource.setNode(xslImpInclNode); xslSource.setSystemId(xslImpInclFile); transformer2 = factory.newTransformer(xslSource); result2 = new DOMResult(docBuilder.newDocument()); transformer2.transform(xmlSource, result2); node2 = result2.getNode(); assertTrue(compareSerializeDOMWithGold(goldImpInclFile, node2)); } }