--- /dev/null 2014-09-08 10:45:56.830930409 -0700 +++ new/test/javax/xml/jaxp/functional/org/apache/qetest/trax/sax/TransformerHandlerTest.java 2014-12-31 11:41:14.784140796 -0800 @@ -0,0 +1,202 @@ +/* + * 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.FilePermission; +import java.io.IOException; +import java.io.StringReader; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.stream.Collectors; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerConfigurationException; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +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.JAXPBaseTest; +import static jaxp.library.JAXPTestUtilities.CLASS_DIR; +import static org.apache.qetest.trax.TraxConst.XML_DIR; +import static jaxp.library.JAXPTestUtilities.getNextFile; +import org.apache.qetest.xsl.CheckingSAXErrorHandler; +import static org.testng.Assert.assertEquals; +import org.testng.annotations.AfterGroups; +import org.testng.annotations.BeforeGroups; +import org.testng.annotations.Test; +import org.xml.sax.SAXException; +import org.xml.sax.SAXParseException; + +/** + * Basic functionality test for the TransformerHandler class of TRAX. + */ +public class TransformerHandlerTest extends JAXPBaseTest { + + /** + * Un-formed XML string(note mismatched tags). + */ + private static final String XML_ERROR_STR + = "" + + ""; + + /** + * Well-formed XML string. + */ + private static final String XML_GOOD_STR + = "" + + "Hello world"; + + /** + * XSLT string. + */ + private static final String XSL_STR + = "" + + "" + + "" + + "" + + "" + + ""; + + /** + * Golden validation string. + */ + private static final String GOLDEN_STR + = "Hello world"; + + /** + * Save system property for restoring. + */ + @BeforeGroups (groups = {"readLocalFiles"}) + public void setFilePermissions() { + setPermissions(new FilePermission(XML_DIR + "/-", "read"), + new FilePermission(CLASS_DIR + "/-", "read, write")); + } + + /** + * Restore the system property. + */ + @AfterGroups (groups = {"readLocalFiles"}) + public void restoreFilePermissions() { + setPermissions(); + } + + /** + * Transformer.transform on a not well-formed XML throws TransformerException. + * + * @throws TransformerException If an unrecoverable error occurs during the + * course of the transformation. + */ + @Test(groups = {"readLocalFiles"}, expectedExceptions = TransformerException.class) + public void negativeCase1() throws TransformerException { + // No public constructor available: you must always ask + // a SAXTransformerFactory to give you one + SAXTransformerFactory saxFactory + = (SAXTransformerFactory) TransformerFactory.newInstance(); + try (StringReader sr1 = new StringReader(XSL_STR); + StringReader sr2 = new StringReader(XML_ERROR_STR)) { + Transformer reusedTransformer = saxFactory.newTransformer( + new StreamSource(sr1)); + reusedTransformer.transform(new StreamSource(sr2), + new StreamResult(getNextFile(TransformerHandlerTest.class))); + } + } + + /** + * Reader.read on a not well-formed XML throws TransformerException. + * + * @throws SAXException for SAX errors. + * @throws TransformerConfigurationException Thrown in case of {@linkplain + * java.util.ServiceConfigurationError service configuration error} or if + * the implementation is not available or cannot be instantiated. + * @throws ParserConfigurationException if the implementation is not + * available or cannot be instantiated. + * @throws IOException if any I/O operation failed. + */ + @Test(groups = {"readLocalFiles"}, expectedExceptions = SAXParseException.class) + public void testCase2() throws SAXException, TransformerConfigurationException, + ParserConfigurationException, IOException { + SAXTransformerFactory saxFactory + = (SAXTransformerFactory) TransformerFactory.newInstance(); + try(StringReader sr1 = new StringReader(XSL_STR); + StringReader sr2 = new StringReader(XML_ERROR_STR);) { + TransformerHandler thandler = saxFactory.newTransformerHandler( + new StreamSource(sr1)); + SAXParserFactory spf1 = SAXParserFactory.newInstance(); + spf1.setNamespaceAware(true); + SAXParser parser1 = spf1.newSAXParser(); + org.xml.sax.XMLReader reader = parser1.getXMLReader(); + reader.setContentHandler(thandler); + reader.setErrorHandler(new CheckingSAXErrorHandler()); + thandler.setResult(new StreamResult(getNextFile(TransformerHandlerTest.class))); + reader.parse(new org.xml.sax.InputSource(sr2)); + } + } + + /** + * Re-transform several times with XMLReader and Transformer. No exception + * should be thrown. + * + * @throws TransformerException If an unrecoverable error occurs during the + * course of the transformation. + * @throws ParserConfigurationException Thrown in case of + * ServiceConfigurationError service configuration error or if + * the implementation is not available or cannot be instantiated. + * @throws SAXException for SAX errors. + * @throws IOException if any I/O operation failed. + */ + @Test(groups = {"readLocalFiles"}) + public void testCase3() throws TransformerException, ParserConfigurationException, + SAXException, IOException { + SAXTransformerFactory saxFactory + = (SAXTransformerFactory) TransformerFactory.newInstance(); + String outputFile1 = getNextFile(this.getClass()); + String outputFile2 = getNextFile(this.getClass()); + try(StringReader sr1 = new StringReader(XSL_STR); + StringReader sr2 = new StringReader(XML_GOOD_STR); + StringReader sr3 = new StringReader(XSL_STR); + StringReader sr4 = new StringReader(XML_GOOD_STR)) { + TransformerHandler thandler = saxFactory.newTransformerHandler( + new StreamSource(sr1)); + SAXParserFactory spf = SAXParserFactory.newInstance(); + spf.setNamespaceAware(true); + SAXParser parser = spf.newSAXParser(); + org.xml.sax.XMLReader reader = parser.getXMLReader(); + thandler.setResult(new StreamResult(outputFile1)); + reader.setContentHandler(thandler); + reader.setErrorHandler(new CheckingSAXErrorHandler()); + reader.parse(new org.xml.sax.InputSource(sr2)); + + Transformer reusedTransformer = TransformerFactory.newInstance() + .newTransformer(new StreamSource(sr3)); + reusedTransformer.transform(new StreamSource(sr4), + new StreamResult(outputFile2)); + } + String outputStr1 = Files.readAllLines(Paths.get(outputFile1)).stream() + .collect(Collectors.joining(System.getProperty("line.separator"))); + String outputStr2 = Files.readAllLines(Paths.get(outputFile1)).stream() + .collect(Collectors.joining(System.getProperty("line.separator"))); + assertEquals(outputStr1, GOLDEN_STR); + assertEquals(outputStr2, GOLDEN_STR); + } +}