/* * Copyright (c) 2015, 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.StringReader; import java.nio.file.Files; import java.nio.file.Paths; import java.util.stream.Collectors; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import javax.xml.transform.Transformer; 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.JAXPFileBaseTest; import static jaxp.library.JAXPTestUtilities.getNextFile; import org.apache.qetest.xsl.CheckingSAXErrorHandler; import static org.testng.Assert.assertEquals; import org.testng.annotations.Test; import org.xml.sax.SAXParseException; /** * Basic functionality test for the TransformerHandler class of TRAX. */ public class TransformerHandlerTest extends JAXPFileBaseTest { /** * 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"; /** * Transformer.transform on a not well-formed XML throws TransformerException. * * @throws TransformerException If an unrecoverable error occurs during the * course of the transformation. */ @Test(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 Exception If any errors occur. */ @Test(expectedExceptions = SAXParseException.class) public void testCase2() throws Exception { 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 Exception If any errors occur. */ @Test public void testCase3() throws Exception { 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); } }