--- /dev/null 2014-09-08 10:45:56.830930409 -0700 +++ new/test/javax/xml/jaxp/functional/org/apache/qetest/trax/sax/SAXTransformerFactoryAPITest.java 2014-12-31 11:41:13.983139548 -0800 @@ -0,0 +1,589 @@ +/* + * 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.sax; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.parsers.SAXParserFactory; +import javax.xml.transform.Result; +import javax.xml.transform.TransformerConfigurationException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.sax.SAXSource; +import javax.xml.transform.sax.SAXTransformerFactory; +import javax.xml.transform.sax.TransformerHandler; +import javax.xml.transform.stream.StreamResult; +import javax.xml.transform.stream.StreamSource; +import jaxp.library.JAXPFileBaseTest; +import static jaxp.library.JAXPTestUtilities.FILE_SEP; +import static jaxp.library.JAXPTestUtilities.compareWithGold; +import static jaxp.library.JAXPTestUtilities.filenameToURL; +import org.apache.qetest.trax.CheckingErrorListener; +import static org.apache.qetest.trax.TraxConst.GOLDEN_DIR; +import static org.apache.qetest.trax.TraxConst.XML_DIR; +import static jaxp.library.JAXPTestUtilities.getNextFile; +import static org.testng.Assert.assertTrue; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import org.xml.sax.XMLReader; + +/** + * API Coverage test for SAXTransformerFactory. + */ +public class SAXTransformerFactoryAPITest extends JAXPFileBaseTest { + /** + * Test XSLT embedded file. + */ + private static final String CITIES_INCLUDE_FILE = XML_DIR + "impincl" + + FILE_SEP + "citiesinclude.xsl"; + + /** + * Test XSLT file. + */ + private static final String XSLT_FILE = XML_DIR + "cities.xsl"; + + /** + * Test XML file. + */ + private static final String XML_FILE = XML_DIR + "cities.xml"; + + /** + * Test golden verification file. + */ + private static final String GOLDEN_FILE = GOLDEN_DIR + "cities.out"; + + @DataProvider + public Object[][] parameters(){ + return new Object[][]{ + {XML_FILE, XSLT_FILE, GOLDEN_FILE} + }; + } + + /** + * This tests newTransformerhandler() method which takes StreamSource as + * argument. + * + * @param xmlFile XML test file name. + * @param xslFile XSLT test file name. + * @param goldFile golden file name. + * @throws TransformerConfigurationException Thrown in case of + * ServiceConfigurationError service configuration error or if the + * implementation is not available or cannot be instantiated. + * @throws IOException if any I/O operation error. + * @throws SAXException for SAX error. + * @throws ParserConfigurationException if the implementation is not + * available or cannot be instantiated. + */ + @Test(dataProvider = "parameters") + public void SAXTFactoryTest001(String xmlFile, String xslFile, String goldFile) + throws TransformerConfigurationException, IOException, + SAXException, ParserConfigurationException { + String outputFile = getNextFile(this.getClass()); + TransformerFactory tfactory = TransformerFactory.newInstance(); + try (FileOutputStream fos = new FileOutputStream(outputFile)) { + XMLReader reader = getJAXPXMLReader(); + SAXTransformerFactory saxTFactory = (SAXTransformerFactory) tfactory; + TransformerHandler handler = saxTFactory.newTransformerHandler( + new StreamSource(filenameToURL(xslFile))); + // Send results out to the next output name + handler.setResult(new StreamResult(fos)); + reader.setContentHandler(handler); + // Log what output is about to be created + reader.parse(filenameToURL(xmlFile)); + } + assertTrue(compareWithGold(goldFile, outputFile)); + } + /** + * This tests newTransformerhandler() method which takes SAXSource as + * argument. + * + * @param xmlFile XML test file name. + * @param xslFile XSLT test file name. + * @param goldFile golden file name. + * @throws TransformerConfigurationException Thrown in case of + * ServiceConfigurationError service configuration error or if the + * implementation is not available or cannot be instantiated. + * @throws IOException if any I/O operation error. + * @throws SAXException for SAX error. + * @throws ParserConfigurationException if the implementation is not + * available or cannot be instantiated. + */ + @Test(dataProvider = "parameters") + public void SAXTFactoryTest002(String xmlFile, String xslFile, String goldFile) + throws TransformerConfigurationException, IOException, + SAXException, ParserConfigurationException { + String outputFile = getNextFile(this.getClass()); + TransformerFactory tfactory = TransformerFactory.newInstance(); + try (FileOutputStream fos = new FileOutputStream(outputFile); + FileInputStream fis = new FileInputStream(xslFile);) { + XMLReader reader = getJAXPXMLReader(); + SAXTransformerFactory saxTFactory = (SAXTransformerFactory) tfactory; + SAXSource ss = new SAXSource(); + ss.setInputSource(new InputSource(fis)); + TransformerHandler handler = saxTFactory.newTransformerHandler(ss); + + handler.setResult(new StreamResult(fos)); + reader.setContentHandler(handler); + reader.parse(filenameToURL(xmlFile)); + } + assertTrue(compareWithGold(goldFile, outputFile)); + } + + /** + * This tests newTransformerhandler() method which takes DOMSource as + * argument. No relative URIs used. + * + * @param xmlFile XML test file name. + * @param xslFile XSLT test file name. + * @param goldFile golden file name. + * @throws TransformerConfigurationException Thrown in case of + * ServiceConfigurationError service configuration error or if the + * implementation is not available or cannot be instantiated. + * @throws IOException if any I/O operation error. + * @throws SAXException for SAX error. + * @throws ParserConfigurationException if the implementation is not + * available or cannot be instantiated. + */ + @Test(dataProvider = "parameters") + public void SAXTFactoryTest003(String xmlFile, String xslFile, String goldFile) + throws TransformerConfigurationException, IOException, + SAXException, ParserConfigurationException { + String outputFile = getNextFile(this.getClass()); + TransformerFactory tfactory = TransformerFactory.newInstance(); + CheckingErrorListener loggingErrListener = new CheckingErrorListener(); + tfactory.setErrorListener(loggingErrListener); + try (FileOutputStream fos = new FileOutputStream(outputFile)) { + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + dbf.setNamespaceAware(true); // Ensure we get namespaces! May be required for some Xerces versions + DocumentBuilder docBuilder = dbf.newDocumentBuilder(); + DOMSource domSource = new DOMSource((Node)docBuilder.parse(new File(xslFile))); + SAXTransformerFactory saxTFactory = (SAXTransformerFactory)tfactory; + TransformerHandler handler = saxTFactory.newTransformerHandler(domSource); + handler.setResult(new StreamResult(fos)); + XMLReader reader = getJAXPXMLReader(); + reader.setContentHandler(handler); + reader.parse(filenameToURL(xmlFile)); + } + assertTrue(compareWithGold(goldFile, outputFile)); + } + /** + * This tests newTransformerhandler() method which takes DOMSource as + * argument. Here a relative URI is used in citiesinclude.xsl file. + * setSystemId is not used for DOMSource. It should throw an exception. + * + * @param xmlFile XML test file name. + * @param xslFile XSLT test file name. + * @param goldFile golden file name. + * @throws TransformerConfigurationException Thrown in case of + * ServiceConfigurationError service configuration error or if the + * implementation is not available or cannot be instantiated. + * @throws IOException if any I/O operation error. + * @throws SAXException for SAX error. + * @throws ParserConfigurationException if the implementation is not + * available or cannot be instantiated. + */ + @Test(dataProvider = "parameters", expectedExceptions = TransformerConfigurationException.class) + public void SAXTFactoryTest004(String xmlFile, String xslFile, String goldFile) + throws TransformerConfigurationException, IOException, + SAXException, ParserConfigurationException { + String outputFile = getNextFile(this.getClass()); + TransformerFactory tfactory = TransformerFactory.newInstance(); + try (FileOutputStream fos = new FileOutputStream(outputFile)) { + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + // Ensure we get namespaces! May be required for some Xerces versions + dbf.setNamespaceAware(true); + DocumentBuilder docBuilder = dbf.newDocumentBuilder(); + // note specific file name used + Node node = (Node) docBuilder.parse(new File(CITIES_INCLUDE_FILE)); + DOMSource domSource = new DOMSource(node); + // setSystemId is not used for DOMSource. expecting Exception. + SAXTransformerFactory saxTFactory = (SAXTransformerFactory) tfactory; + TransformerHandler handler = saxTFactory.newTransformerHandler(domSource); + handler.setResult(new StreamResult(fos)); + + XMLReader reader = getJAXPXMLReader(); + reader.setContentHandler(handler); + + // Log what output is about to be created + reader.parse(filenameToURL(xmlFile)); + } + } + + /** + * Tests newTransformerhandler() method which takes DOMSource as argument. + * Here a relative URI is used in citiesinclude.xsl file. setSystemId is + * used for DOMSource. It should run well. + * + * @param xmlFile XML test file name. + * @param xslFile XSLT test file name. + * @param goldFile golden file name. + * @throws TransformerConfigurationException Thrown in case of + * ServiceConfigurationError service configuration error or if the + * implementation is not available or cannot be instantiated. + * @throws IOException if any I/O operation error. + * @throws SAXException for SAX error. + * @throws ParserConfigurationException if the implementation is not + * available or cannot be instantiated. + */ + @Test(dataProvider = "parameters") + public void SAXTFactoryTest005(String xmlFile, String xslFile, String goldFile) + throws TransformerConfigurationException, IOException, + SAXException, ParserConfigurationException { + String outputFile = getNextFile(this.getClass()); + TransformerFactory tfactory = TransformerFactory.newInstance(); + try (FileOutputStream fos = new FileOutputStream(outputFile)) { + SAXTransformerFactory saxTFactory = (SAXTransformerFactory) tfactory; + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + dbf.setNamespaceAware(true); + DocumentBuilder docBuilder = dbf.newDocumentBuilder(); + + Document document = docBuilder.parse(new File(CITIES_INCLUDE_FILE)); + DOMSource domSource = new DOMSource((Node) document); + domSource.setSystemId(filenameToURL(CITIES_INCLUDE_FILE)); + TransformerHandler handler = saxTFactory.newTransformerHandler(domSource); + handler.setResult(new StreamResult(fos)); + XMLReader reader = getJAXPXMLReader(); + reader.setContentHandler(handler); + reader.parse(filenameToURL(xmlFile)); + } + // Validate the output by comparing against gold + assertTrue(compareWithGold(goldFile, outputFile)); + } + /** + * This tests newTransformerhandler() method which takes StreamSource as + * argument. + * + * @param xmlFile XML test file name. + * @param xslFile XSLT test file name. + * @param goldFile golden file name. + * @throws TransformerConfigurationException Thrown in case of + * ServiceConfigurationError service configuration error or if the + * implementation is not available or cannot be instantiated. + * @throws IOException if any I/O operation error. + * @throws SAXException for SAX error. + * @throws ParserConfigurationException if the implementation is not + * available or cannot be instantiated. + */ + @Test(dataProvider = "parameters") + public void SAXTFactoryTest006(String xmlFile, String xslFile, String goldFile) + throws TransformerConfigurationException, IOException, + SAXException, ParserConfigurationException { + String outputFile = getNextFile(this.getClass()); + TransformerFactory tfactory = TransformerFactory.newInstance(); + try (FileOutputStream fos = new FileOutputStream(outputFile)) { + XMLReader reader = getJAXPXMLReader(); + + SAXTransformerFactory saxTFactory = (SAXTransformerFactory) tfactory; + TransformerHandler handler = saxTFactory.newTransformerHandler( + new StreamSource(filenameToURL(xslFile))); + // Send results out to the next output name + Result result = new StreamResult(fos); + + handler.setResult(result); + reader.setContentHandler(handler); + reader.parse(filenameToURL(xmlFile)); + } + assertTrue(compareWithGold(goldFile, outputFile)); + } + + /** + * This tests newTransformerhandler() method which takes StreamSource as + * argument. + * + * @param xmlFile XML test file name. + * @param xslFile XSLT test file name. + * @param goldFile golden file name. + * @throws TransformerConfigurationException Thrown in case of + * ServiceConfigurationError service configuration error or if the + * implementation is not available or cannot be instantiated. + * @throws IOException if any I/O operation error. + * @throws SAXException for SAX error. + * @throws ParserConfigurationException if the implementation is not + * available or cannot be instantiated. + */ + @Test(dataProvider = "parameters") + public void SAXTFactoryTest007(String xmlFile, String xslFile, String goldFile) + throws TransformerConfigurationException, IOException, + SAXException, ParserConfigurationException { + String outputFile = getNextFile(this.getClass()); + TransformerFactory tfactory = TransformerFactory.newInstance(); + try (FileOutputStream fos = new FileOutputStream(outputFile)) { + XMLReader reader = getJAXPXMLReader(); + + SAXTransformerFactory saxTFactory = (SAXTransformerFactory) tfactory; + TransformerHandler handler = saxTFactory.newTransformerHandler( + new StreamSource(filenameToURL(xslFile))); + // Send results out to the next output name + Result result = new StreamResult(fos); + + handler.setResult(result); + reader.setContentHandler(handler); + reader.parse(filenameToURL(xmlFile)); + } + assertTrue(compareWithGold(goldFile, outputFile)); + } + + /** + * This tests newTransformerhandler() method which takes StreamSource as + * argument. + * + * @param xmlFile XML test file name. + * @param xslFile XSLT test file name. + * @param goldFile golden file name. + * @throws TransformerConfigurationException Thrown in case of + * ServiceConfigurationError service configuration error or if the + * implementation is not available or cannot be instantiated. + * @throws IOException if any I/O operation error. + * @throws SAXException for SAX error. + * @throws ParserConfigurationException if the implementation is not + * available or cannot be instantiated. + */ + @Test(dataProvider = "parameters") + public void SAXTFactoryTest008(String xmlFile, String xslFile, String goldFile) + throws TransformerConfigurationException, IOException, + SAXException, ParserConfigurationException { + String outputFile = getNextFile(this.getClass()); + TransformerFactory tfactory = TransformerFactory.newInstance(); + try (FileOutputStream fos = new FileOutputStream(outputFile)) { + XMLReader reader = getJAXPXMLReader(); + + SAXTransformerFactory saxTFactory = (SAXTransformerFactory) tfactory; + TransformerHandler handler = saxTFactory.newTransformerHandler( + new StreamSource(filenameToURL(xslFile))); + // Send results out to the next output name + Result result = new StreamResult(fos); + + handler.setResult(result); + reader.setContentHandler(handler); + reader.parse(filenameToURL(xmlFile)); + } + assertTrue(compareWithGold(goldFile, outputFile)); + } + + /** + * This tests newTransformerhandler() method which takes StreamSource as + * argument. + * + * @param xmlFile XML test file name. + * @param xslFile XSLT test file name. + * @param goldFile golden file name. + * @throws TransformerConfigurationException Thrown in case of + * ServiceConfigurationError service configuration error or if the + * implementation is not available or cannot be instantiated. + * @throws IOException if any I/O operation error. + * @throws SAXException for SAX error. + * @throws ParserConfigurationException if the implementation is not + * available or cannot be instantiated. + */ + @Test(dataProvider = "parameters") + public void SAXTFactoryTest009(String xmlFile, String xslFile, String goldFile) + throws TransformerConfigurationException, IOException, + SAXException, ParserConfigurationException { + String outputFile = getNextFile(this.getClass()); + TransformerFactory tfactory = TransformerFactory.newInstance(); + try (FileOutputStream fos = new FileOutputStream(outputFile)) { + XMLReader reader = getJAXPXMLReader(); + + SAXTransformerFactory saxTFactory = (SAXTransformerFactory) tfactory; + TransformerHandler handler = saxTFactory.newTransformerHandler( + new StreamSource(filenameToURL(xslFile))); + // Send results out to the next output name + Result result = new StreamResult(fos); + + handler.setResult(result); + reader.setContentHandler(handler); + reader.parse(filenameToURL(xmlFile)); + } + assertTrue(compareWithGold(goldFile, outputFile)); + } + + /** + * This tests newTransformerhandler() method which takes StreamSource as + * argument. + * + * @param xmlFile XML test file name. + * @param xslFile XSLT test file name. + * @param goldFile golden file name. + * @throws TransformerConfigurationException Thrown in case of + * ServiceConfigurationError service configuration error or if the + * implementation is not available or cannot be instantiated. + * @throws IOException if any I/O operation error. + * @throws SAXException for SAX error. + * @throws ParserConfigurationException if the implementation is not + * available or cannot be instantiated. + */ + @Test(dataProvider = "parameters") + public void SAXTFactoryTest010(String xmlFile, String xslFile, String goldFile) + throws TransformerConfigurationException, IOException, + SAXException, ParserConfigurationException { + String outputFile = getNextFile(this.getClass()); + TransformerFactory tfactory = TransformerFactory.newInstance(); + try (FileOutputStream fos = new FileOutputStream(outputFile)) { + XMLReader reader = getJAXPXMLReader(); + + SAXTransformerFactory saxTFactory = (SAXTransformerFactory) tfactory; + TransformerHandler handler = saxTFactory.newTransformerHandler( + new StreamSource(filenameToURL(xslFile))); + // Send results out to the next output name + Result result = new StreamResult(fos); + + handler.setResult(result); + reader.setContentHandler(handler); + reader.parse(filenameToURL(xmlFile)); + } + assertTrue(compareWithGold(goldFile, outputFile)); + } + + /** + * This tests newTransformerhandler() method which takes StreamSource as + * argument. + * + * @param xmlFile XML test file name. + * @param xslFile XSLT test file name. + * @param goldFile golden file name. + * @throws TransformerConfigurationException Thrown in case of + * ServiceConfigurationError service configuration error or if the + * implementation is not available or cannot be instantiated. + * @throws IOException if any I/O operation error. + * @throws SAXException for SAX error. + * @throws ParserConfigurationException if the implementation is not + * available or cannot be instantiated. + */ + @Test(dataProvider = "parameters") + public void SAXTFactoryTest011(String xmlFile, String xslFile, String goldFile) + throws TransformerConfigurationException, IOException, + SAXException, ParserConfigurationException { + String outputFile = getNextFile(this.getClass()); + TransformerFactory tfactory = TransformerFactory.newInstance(); + try (FileOutputStream fos = new FileOutputStream(outputFile)) { + XMLReader reader = getJAXPXMLReader(); + + SAXTransformerFactory saxTFactory = (SAXTransformerFactory) tfactory; + TransformerHandler handler = saxTFactory.newTransformerHandler( + new StreamSource(filenameToURL(xslFile))); + // Send results out to the next output name + Result result = new StreamResult(fos); + + handler.setResult(result); + reader.setContentHandler(handler); + reader.parse(filenameToURL(xmlFile)); + } + assertTrue(compareWithGold(goldFile, outputFile)); + } + + /** + * This tests newTransformerhandler() method which takes StreamSource as + * argument. + * + * @param xmlFile XML test file name. + * @param xslFile XSLT test file name. + * @param goldFile golden file name. + * @throws TransformerConfigurationException Thrown in case of + * ServiceConfigurationError service configuration error or if the + * implementation is not available or cannot be instantiated. + * @throws IOException if any I/O operation error. + * @throws SAXException for SAX error. + * @throws ParserConfigurationException if the implementation is not + * available or cannot be instantiated. + */ + @Test(dataProvider = "parameters") + public void SAXTFactoryTest012(String xmlFile, String xslFile, String goldFile) + throws TransformerConfigurationException, IOException, + SAXException, ParserConfigurationException { + String outputFile = getNextFile(this.getClass()); + TransformerFactory tfactory = TransformerFactory.newInstance(); + try (FileOutputStream fos = new FileOutputStream(outputFile)) { + XMLReader reader = getJAXPXMLReader(); + + SAXTransformerFactory saxTFactory = (SAXTransformerFactory) tfactory; + TransformerHandler handler = saxTFactory.newTransformerHandler( + new StreamSource(filenameToURL(xslFile))); + // Send results out to the next output name + Result result = new StreamResult(fos); + + handler.setResult(result); + reader.setContentHandler(handler); + reader.parse(filenameToURL(xmlFile)); + } + assertTrue(compareWithGold(goldFile, outputFile)); + } + + /** + * This tests newTransformerhandler() method which takes StreamSource as + * argument. + * + * @param xmlFile XML test file name. + * @param xslFile XSLT test file name. + * @param goldFile golden file name. + * @throws TransformerConfigurationException Thrown in case of + * ServiceConfigurationError service configuration error or if the + * implementation is not available or cannot be instantiated. + * @throws IOException if any I/O operation error. + * @throws SAXException for SAX error. + * @throws ParserConfigurationException if the implementation is not + * available or cannot be instantiated. + */ + @Test(dataProvider = "parameters") + public void SAXTFactoryTest013(String xmlFile, String xslFile, String goldFile) + throws TransformerConfigurationException, IOException, + SAXException, ParserConfigurationException { + String outputFile = getNextFile(this.getClass()); + TransformerFactory tfactory = TransformerFactory.newInstance(); + try (FileOutputStream fos = new FileOutputStream(outputFile)) { + XMLReader reader = getJAXPXMLReader(); + + SAXTransformerFactory saxTFactory = (SAXTransformerFactory) tfactory; + TransformerHandler handler = saxTFactory.newTransformerHandler( + new StreamSource(filenameToURL(xslFile))); + // Send results out to the next output name + Result result = new StreamResult(fos); + + handler.setResult(result); + reader.setContentHandler(handler); + reader.parse(filenameToURL(xmlFile)); + } + assertTrue(compareWithGold(goldFile, outputFile)); + } + + /** + * Worker method to get an XMLReader. + * + * @return a new XMLReader for use, with setNamespaceAware(true) + * @throws ParserConfigurationException if a parser cannot be created which + * satisfies the requested configuration. + * @throws SAXException for SAX errors. + */ + private XMLReader getJAXPXMLReader() + throws SAXException, ParserConfigurationException { + // Be sure to use the JAXP methods only! + SAXParserFactory factory = SAXParserFactory.newInstance(); + factory.setNamespaceAware(true); + return factory.newSAXParser().getXMLReader(); + } +}