--- /dev/null 2014-09-08 10:45:56.830930409 -0700 +++ new/test/javax/xml/jaxp/functional/org/apache/qetest/trax/ErrorListenerTest.java 2014-12-31 11:40:29.783070767 -0800 @@ -0,0 +1,245 @@ +/* + * 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; + +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.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.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.compareWithGold; +import static jaxp.library.JAXPTestUtilities.filenameToURL; +import static jaxp.library.JAXPTestUtilities.getNextFile; +import static org.apache.qetest.CheckingHandler.Expectation.ITEM_CHECKFAIL; +import static org.apache.qetest.trax.CheckingErrorListener.THROW_NEVER; +import static org.apache.qetest.trax.CheckingErrorListener.TYPE_FATALERROR; +import static org.apache.qetest.trax.CheckingErrorListener.TYPE_WARNING; +import static org.apache.qetest.trax.TraxConst.GOLDEN_DIR; +import static org.apache.qetest.trax.TraxConst.XML_DIR; +import org.apache.qetest.xsl.CheckingSAXErrorHandler; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; +import org.w3c.dom.Node; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import org.xml.sax.XMLReader; + +/** + * Verify that ErrorListeners are called properly from Transformers. Also + * verifies basic Transformer behavior after a stylesheet with errors has been + * built. Note: parts of this test may rely on specific Xalan functionality, in + * that with the specific errors I've chosen, Xalan can actually continue to + * process the stylesheet, even though it had an error. XSLTC mode may either + * throw slighly different kinds of errors, or may not be able to continue after + * the error (we should investigate changing this test to just verify common + * things, and then check the rest into the xalanj2 directory). + */ +public class ErrorListenerTest extends JAXPFileBaseTest { + /** + * Expect fatal when transformation by default. + */ + private static final int TEMPLATES_EXPECTED_TYPE = TYPE_FATALERROR; + + /** + * Expect warning when transformation by default. + */ + private static final int TRANSFORM_EXPECTED_TYPE = TYPE_WARNING; + + /** + * Data provide provides test XML, stylesheet and output files name. + * @return an array has XML, stylesheet and output files name. + */ + @DataProvider + public Object[][] testFiles(){ + return new Object[][]{ + {XML_DIR + "ErrorListenerTest.xml", + XML_DIR + "ErrorListenerTest.xsl", + GOLDEN_DIR + "ErrorListenerTest.out"} + }; + } + + /** + * TransformerFactory Set/Get ErrorListener test. + * + * @param xmlFile XML test file name. + * @param xslFile XSLT test file name. + * @param goldFile golden file name. + * @throws IOException if any I/O operation error. + * @throws TransformerException If an unrecoverable error occurs during the + * course of the transformation. + */ + @Test(dataProvider = "testFiles") + public void testCase1(String xmlFile, String xslFile, String goldFile) + throws IOException, TransformerException { + String outputFile = getNextFile(this.getClass()); + + CheckingErrorListener chkErrorListener = new CheckingErrorListener(THROW_NEVER); + + TransformerFactory factory = TransformerFactory.newInstance(); + // Set the errorListener and validate it + factory.setErrorListener(chkErrorListener); + + // Attempt to build templates from known-bad stylesheet + // Validate known errors in stylesheet building + chkErrorListener.setExpected(TEMPLATES_EXPECTED_TYPE, + ITEM_CHECKFAIL); + Templates templates = factory.newTemplates(new StreamSource(filenameToURL(xslFile))); + // Clear out any setExpected or counters + chkErrorListener.reset(); + + // This stylesheet will still work, even though errors + // were detected during it's building. Note that + // future versions of Xalan or other processors may + // not be able to continue here... + Transformer transformer = templates.newTransformer(); + + // Set the errorListener and validate it + transformer.setErrorListener(chkErrorListener); + + // Validate the first xsl:message call in the stylesheet + chkErrorListener.setExpected(TRANSFORM_EXPECTED_TYPE, + ITEM_CHECKFAIL); + transformer.transform(new StreamSource(filenameToURL(xmlFile)), + new StreamResult(outputFile)); + // Clear out any setExpected or counters + chkErrorListener.reset(); + compareWithGold(goldFile, outputFile); + } + + /** + * Build a bad style-sheet/do a transform with DOMs. Verify that the + * ErrorListener is called properly. Primarily using SAXSources. + * + * @param xmlFile test XML filename. + * @param xslFile test style-sheet filename. + * @param goldFile golden verification filename. + * + * @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 = "testFiles") + public void testCase2(String xmlFile, String xslFile, String goldFile) + throws SAXException, ParserConfigurationException, + TransformerConfigurationException, IOException { + String outputFile = getNextFile(this.getClass()); + + CheckingErrorListener loggingErrorListener = new CheckingErrorListener(THROW_NEVER); + // assumes SAXSource.feature! + SAXTransformerFactory saxFactory = (SAXTransformerFactory)TransformerFactory.newInstance(); + + // Set the errorListener and validate it + saxFactory.setErrorListener(loggingErrorListener); + + // Use the JAXP way to get an XMLReader + XMLReader reader = SAXParserFactory.newInstance().newSAXParser().getXMLReader(); + InputSource is = new InputSource(filenameToURL(xslFile)); + + // Attempt to build templates from known-bad stylesheet + // Validate known errors in stylesheet building + loggingErrorListener.setExpected(TEMPLATES_EXPECTED_TYPE, + ITEM_CHECKFAIL); + TransformerHandler handler = saxFactory.newTransformerHandler(new SAXSource(is)); + // Clear out any setExpected or counters + loggingErrorListener.reset(); + + // This stylesheet will still work, even though errors + // were detected during it's building. Note that + // future versions of Xalan or other processors may + // not be able to continue here... + + // Create a result and setup SAX parsing 'tree' + Result result = new StreamResult(outputFile); + handler.setResult(result); + reader.setContentHandler(handler); + + CheckingSAXErrorHandler loggingSAXErrorHandler + = new CheckingSAXErrorHandler(CheckingSAXErrorHandler.THROW_NEVER); + reader.setErrorHandler(loggingSAXErrorHandler); + + // Validate the first xsl:message call in the stylesheet + loggingErrorListener.setExpected(TRANSFORM_EXPECTED_TYPE, + ITEM_CHECKFAIL); + reader.parse(filenameToURL(xmlFile)); + // Clear out any setExpected or counters + loggingErrorListener.reset(); + loggingSAXErrorHandler.reset(); + compareWithGold(goldFile, outputFile); + } + + /** + * Build a bad style-sheet/do a transform with DOMs. Verify that the + * ErrorListener is called properly. Primarily using DOMSources. + * + * @param xmlFile test XML filename. + * @param xslFile test style-sheet filename. + * @param goldFile golden verification filename. + * @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 = "testFiles") + public void testCase3(String xmlFile, String xslFile, String goldFile) + throws ParserConfigurationException, SAXException, IOException, + TransformerException { + String outputFile = getNextFile(this.getClass()); + CheckingErrorListener chkErrorListener = new CheckingErrorListener(THROW_NEVER); + // Startup a DOM factory, create some nodes/DOMs + DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); + dfactory.setNamespaceAware(true); + DocumentBuilder docBuilder = dfactory.newDocumentBuilder(); + Node xslNode = docBuilder.parse(new InputSource(filenameToURL(xslFile))); + Node xmlNode = docBuilder.parse(new InputSource(filenameToURL(xmlFile))); + + // Create a transformer factory with an error listener + TransformerFactory factory = TransformerFactory.newInstance(); + factory.setErrorListener(chkErrorListener); + + // Attempt to build templates from known-bad stylesheet + // Validate known errors in stylesheet building + Templates templates = factory.newTemplates(new DOMSource(xslNode)); + // Clear out any setExpected or counters + Transformer transformer = templates.newTransformer(); + transformer.setErrorListener(chkErrorListener); + transformer.transform(new DOMSource(xmlNode), new StreamResult(outputFile)); + compareWithGold(goldFile, outputFile); + } +}