/* * 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.IOException; import javax.xml.transform.Result; import javax.xml.transform.Source; 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.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.CLASS_DIR; import static jaxp.library.JAXPTestUtilities.compareWithGold; 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 jaxp.library.JAXPTestUtilities.getNextFile; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertNull; import org.testng.annotations.Test; /** * API Coverage test for the TransformerHandler class of TRAX. */ public class TransformerHandlerAPITest extends JAXPFileBaseTest { /** * Nonsense systemId for various tests. */ private static final String NONSENSE_SYSTEMID = "file:///nonsense/system/id/"; /** * IllegalArgumentExceptionis thrown if result set as null. * * @throws TransformerConfigurationException Thrown in case of * ServiceConfigurationError service configuration error or if the * implementation is not available or cannot be instantiated. */ @Test(expectedExceptions = IllegalArgumentException.class) public void negativeCase1() throws TransformerConfigurationException { String outputFile = CLASS_DIR + "TransformerHandlerAPITest01.out"; // Validate API's for an identity transformer // No public constructor available: you must always ask // a SAXTransformerFactory to give you one SAXTransformerFactory saxFactory = (SAXTransformerFactory) TransformerFactory.newInstance(); // Basic construction of identity transformer TransformerHandler tHandler = saxFactory.newTransformerHandler(); // set/getSystemId API coverage tHandler.setSystemId(NONSENSE_SYSTEMID); tHandler.setSystemId(null); // setResult API coverage Result unusedResult = new StreamResult(outputFile); tHandler.setResult(unusedResult); tHandler.setResult(null); } /** * IllegalArgumentExceptionis thrown if result set as null. * * @throws TransformerConfigurationException Thrown in case of * ServiceConfigurationError service configuration error or if the * implementation is not available or cannot be instantiated. */ @Test(expectedExceptions = IllegalArgumentException.class) public void negativeCase2() throws TransformerConfigurationException { String outputFile = getNextFile(this.getClass()); // Validate API's for an identity transformer // No public constructor available: you must always ask // a SAXTransformerFactory to give you one SAXTransformerFactory saxFactory = (SAXTransformerFactory) TransformerFactory.newInstance(); // Basic construction of identity transformer TransformerHandler tHandler = saxFactory.newTransformerHandler(); // set/getSystemId API coverage tHandler.setSystemId(NONSENSE_SYSTEMID); tHandler.setSystemId(null); // setResult API coverage Result unusedResult = new StreamResult(outputFile); tHandler.setResult(unusedResult); tHandler.setResult(null); } /** * IllegalArgumentExceptionis thrown if result set as null. * * @throws TransformerConfigurationException Thrown in case of * ServiceConfigurationError service configuration error or if the * implementation is not available or cannot be instantiated. */ @Test(expectedExceptions = IllegalArgumentException.class) public void negativeCase3() throws TransformerConfigurationException { String xslURI = filenameToURL(XML_DIR + "SAXTest.xsl"); String outputFile = getNextFile(this.getClass()); // Validate API's for a 'real' transformer, which is different code SAXTransformerFactory saxFactory = (SAXTransformerFactory) TransformerFactory.newInstance(); // Basic construction of identity transformer TransformerHandler tHandler = saxFactory.newTransformerHandler(new StreamSource(xslURI)); assertNotNull(tHandler); // getTemplates API coverage - simple Transformer transformer = tHandler.getTransformer(); assertNotNull(transformer); // set/getSystemId API coverage tHandler.setSystemId(NONSENSE_SYSTEMID); assertEquals(tHandler.getSystemId(), NONSENSE_SYSTEMID); tHandler.setSystemId(null); assertNull(tHandler.getSystemId()); // setResult API coverage Result unusedResult = new StreamResult(outputFile); tHandler.setResult(unusedResult); tHandler.setResult(null); } /** * Basic functionality of TransformerHandler. * * @throws TransformerException If an unrecoverable error occurs during the * course of the transformation. * @throws IOException if any I/O operation error. */ public void testCase2() throws TransformerException, IOException { String xslURI = filenameToURL(XML_DIR + "SAXTest.xsl"); String xmlURI = XML_DIR + "SAXTest.xml"; String goldFile = GOLDEN_DIR + "SAXTest.out"; String outputURI = filenameToURL(CLASS_DIR + "SAXTest.out"); SAXTransformerFactory saxFactory = (SAXTransformerFactory) TransformerFactory.newInstance(); // Validate an identity transformerHandler is valid and performs as an // identity stylesheet TransformerHandler transformerHandler = saxFactory.newTransformerHandler(); Transformer transformer = transformerHandler.getTransformer(); assertNotNull(transformer); transformer.transform(new StreamSource(xmlURI), new StreamResult(xslURI)); compareWithGold(xmlURI, outputURI); // Validate newTransformerHandler(Source) works transformer = transformerHandler.getTransformer(); assertNotNull(transformer); transformer.transform(new StreamSource(xmlURI), new StreamResult(outputURI)); compareWithGold(goldFile, outputURI); // Validate newTransformerHandler(Templates) works Source xslSource = new StreamSource(xslURI); Templates otherTemplates = saxFactory.newTemplates(xslSource); transformerHandler = saxFactory.newTransformerHandler(otherTemplates); transformer = transformerHandler.getTransformer(); assertNotNull(transformer); transformer.transform(new StreamSource(xmlURI), new StreamResult(outputURI)); compareWithGold(goldFile, outputURI); } }