--- /dev/null 2014-09-08 10:45:56.830930409 -0700 +++ new/test/javax/xml/jaxp/functional/org/apache/qetest/trax/TransformerAPITest.java 2014-12-31 11:40:31.407073292 -0800 @@ -0,0 +1,381 @@ +/* + * 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.FileOutputStream; +import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.util.Properties; +import javax.xml.transform.OutputKeys; +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.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 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 static org.testng.Assert.assertTrue; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +/** + * Basic API coverage test for the Transformer class of TRAX. + * This test focuses on coverage testing for the API's, and + * very brief functional testing. + */ +public class TransformerAPITest extends JAXPFileBaseTest { + /** + * Parameter names from TransformerAPIParam.xsl + */ + private static final String PARAM1S = "param1s"; + + /** + * Parameter names from TransformerAPIParam.xsl + */ + private static final String PARAM3S = "param3s"; + + /** + * Parameter names from TransformerAPIParam.xsl + */ + private static final String PARAM1N = "param1n"; + + /** + * TransformerFactory for creating Transfomer. + */ + private final TransformerFactory factory; + + /** + * Initiate Transformer factory. + */ + public TransformerAPITest() { + factory = TransformerFactory.newInstance(); + } + + /** + * Known outputFormat values from TransformerAPIOutputFormat.xsl . + */ + private static final String METHOD_VALUE = "xml"; + private static final String VERSION_VALUE ="123.45"; + private static final String ENCODING_VALUE ="UTF-16"; + private static final String STANDALONE_VALUE = "yes"; + private static final String DOCTYPE_PUBLIC_VALUE = "this-is-doctype-public"; + private static final String DOCTYPE_SYSTEM_VALUE = "this-is-doctype-system"; + private static final String CDATA_SECTION_ELEMENTS_VALUE = "cdataHere "; + private static final String INDENT_VALUE = "yes"; + private static final String MEDIA_TYPE_VALUE = "text/test/xml"; + private static final String OMIT_XML_DECLARATION_VALUE = "yes"; + + /** + * Basic API coverage tests for Transformer API. + * + * @throws TransformerConfigurationException When it is not possible to + * create a Transformer instance. + * @throws IOException if any I/O operation error. + * @throws TransformerException If an unrecoverable error occurs during the + * course of the transformation. + */ + @Test + public void testCase1() throws TransformerConfigurationException, + IOException, TransformerException { + String xmlFile = filenameToURL(XML_DIR + "TransformerAPIParam.xml"); + String xslFile = filenameToURL(XML_DIR + "TransformerAPIParam.xsl"); + String goldFile= GOLDEN_DIR + "TransformerAPIParam.out"; + + Transformer identityTransformer = factory.newTransformer(); + Templates templates = factory.newTemplates(new StreamSource(xslFile)); + assertNull(identityTransformer.getParameter("This-param-does-not-exist")); + + identityTransformer.setParameter("foo", "bar"); + assertNotNull(identityTransformer.getParameter("foo")); + + Transformer transformer = templates.newTransformer(); + // Default Transformer should not have any parameters. + assertNull(transformer.getParameter("This-param-does-not-exist")); + // Including params in the stylesheet, Javadoc says "This method does + // not return a default parameter value, which cannot be determined + // until the node context is evaluated during the transformation process". + assertNull(transformer.getParameter(PARAM1S)); + + // Verify simple set/get of a single parameter - Integer + transformer.setParameter(PARAM3S, 1234); + assertEquals(transformer.getParameter(PARAM3S), 1234); + + transformer.setParameter(PARAM3S, 99); // Had bug can't re-set + assertEquals(transformer.getParameter(PARAM3S), 99); + + // Verify simple re-set/get of a single parameter - now a new String + transformer.setParameter(PARAM3S, "new value3s"); + assertEquals(transformer.getParameter(PARAM3S), "new value3s"); + + transformer.setParameter(PARAM1S, "'test-param-1s'"); // note single quotes + transformer.setParameter(PARAM1N, 1234); + + + // Verify basic params actually affect transformation + // Use the transformer we set the params onto above! + String outputFile = getNextFile(this.getClass()); + try (FileOutputStream fos = new FileOutputStream(outputFile);) { + transformer.transform(new StreamSource(xmlFile), + new StreamResult(fos)); + } + assertTrue(compareWithGold(goldFile, outputFile)); + assertEquals(transformer.getParameter(PARAM1S), "'test-param-1s'"); + assertEquals(transformer.getParameter(PARAM1N), 1234); + } + + /** + * API coverage test of Transformer.set/getOutputProperty(). + * + * @param xslFile XSLT file we will use. + * @param xmlFile XML test file. + * @param goldFile golden validation file. + * @param expectedProperties expected result. + * @throws TransformerException When it is not possible to create a + * Transformer instance or an unrecoverable error occurs during + * the course of the transformation.. + * @throws IOException if an I/O error occurs reading from the file or a + * malformed or unmappable byte sequence is read. + */ + @Test(dataProvider="propertiesDataProvider") + public void testCase2(String xslFile, String xmlFile, String goldFile, + String[] expectedProperties) throws TransformerException, IOException { + Transformer transformer = factory.newTemplates(new StreamSource(xslFile)).newTransformer(); + Properties props = transformer.getOutputProperties(); + // Default uses UTF8 as encoding. + Charset cs = StandardCharsets.UTF_8; + for(int i = 0; i < expectedProperties.length; i = i + 2) { + assertEquals(props.get(expectedProperties[i]), + expectedProperties[i + 1]); + if(expectedProperties[i].equals(OutputKeys.ENCODING)) { + cs = Charset.forName(expectedProperties[i + 1]); + } + } + // Always do set/get testing on default Transformer. + Transformer defaultTransformer = factory.newTransformer(); + defaultTransformer.setOutputProperty(OutputKeys.METHOD, "text"); + assertEquals(defaultTransformer.getOutputProperty(OutputKeys.METHOD), "text"); + + String outputFile = getNextFile(this.getClass()); + transformer.transform(new StreamSource(xmlFile), new StreamResult(outputFile)); + assertTrue(compareWithGold(goldFile, outputFile, cs)); + } + + /** + * Data provider for testCase2. + * + * @return an array that has parameters XSL file,XML file, golden validation + * file, expected properties. + */ + @DataProvider + public Object[][] propertiesDataProvider(){ + return new Object[][]{ + {XML_DIR + "identity.xsl", + XML_DIR + "identity.xml", + GOLDEN_DIR + "identity.out", + new String[]{OutputKeys.METHOD, null, + OutputKeys.INDENT, null}}, + {XML_DIR + "TransformerAPIVar.xsl", + XML_DIR + "TransformerAPIVar.xml", + GOLDEN_DIR + "TransformerAPIVar.out", + new String[]{OutputKeys.METHOD, "xml", OutputKeys.INDENT, "no"}}, + {XML_DIR + "TransformerAPIOutputFormat.xsl", + XML_DIR + "TransformerAPIOutputFormat.xml", + GOLDEN_DIR + "TransformerAPIOutputFormatUTF16.out", + new String[]{ + OutputKeys.METHOD, METHOD_VALUE, + OutputKeys.VERSION, VERSION_VALUE, + OutputKeys.ENCODING, ENCODING_VALUE, + OutputKeys.STANDALONE, STANDALONE_VALUE, + OutputKeys.DOCTYPE_PUBLIC, DOCTYPE_PUBLIC_VALUE, + OutputKeys.DOCTYPE_SYSTEM, DOCTYPE_SYSTEM_VALUE, + OutputKeys.CDATA_SECTION_ELEMENTS, CDATA_SECTION_ELEMENTS_VALUE, + OutputKeys.INDENT, INDENT_VALUE, + OutputKeys.MEDIA_TYPE, MEDIA_TYPE_VALUE, + OutputKeys.OMIT_XML_DECLARATION, OMIT_XML_DECLARATION_VALUE}}, + {XML_DIR + "TransformerAPIHTMLFormat.xsl", + XML_DIR + "TransformerAPIHTMLFormat.xml", + GOLDEN_DIR + "TransformerAPIHTMLFormat.out", + new String[]{ + OutputKeys.METHOD, "html", OutputKeys.INDENT, null}} + }; + } + + /** + * API coverage test of Transformer.set/getOutputProperties() + * + * @throws TransformerConfigurationException Thrown in case of + * ServiceConfigurationError service configuration error or if the + * implementation is not available or cannot be instantiated. + */ + @Test + public void testCase3() throws TransformerConfigurationException { + Transformer identityTransformer = factory.newTransformer(); + assertNotNull(factory.newTemplates(new StreamSource(XML_DIR + "TransformerAPIOutputFormat.xsl"))); + assertNotNull(identityTransformer.getOutputProperties()); + identityTransformer.setOutputProperties(new Properties()); + assertEquals(identityTransformer.getOutputProperties(), new Properties()); + } + + /** + * Data provider for re-use Transformer test. + * + * @return an array that has parameters XSL file,XML file, golden validation + * file. + */ + @DataProvider + public Object[][] reuseTranfomerOnSameXMLProvider() { + return new Object[][]{ + {XML_DIR + "identity.xsl", + XML_DIR + "identity.xml", + GOLDEN_DIR + "identity.out"}, + {XML_DIR + "TransformerAPIVar.xsl", + XML_DIR + "TransformerAPIVar.xml", + GOLDEN_DIR + "TransformerAPIVar.out"}, + {XML_DIR + "TransformerAPIVar.xsl", + XML_DIR + "TransformerAPIVar2.xml", + GOLDEN_DIR + "TransformerAPIVar2.out"} + }; + } + + /** + * TRAX Transformer: cover transform() API and multiple Transformations from + * single transformer on same XML file with same style-sheet file. + * + * @param xslFile XSLT file we will use. + * @param xmlFile XML test file. + * @param goldFile golden validation file. + * @throws TransformerException When it is not possible to create a + * Transformer instance or an unrecoverable error occurs during + * the course of the transformation.. + * @throws IOException if an I/O error occurs reading from the file or a + * malformed or unmappable byte sequence is read. + */ + @Test(dataProvider = "reuseTranfomerOnSameXMLProvider") + public void reuseTransformerSameXMLTest(String xslFile, String xmlFile, + String goldFile) throws IOException, TransformerException { + Transformer reuseTransformer = factory.newTransformer(new StreamSource(xslFile)); + String outputFile = getNextFile(this.getClass()); + + // Re-use the transformer multiple times on XML file. + reuseTransformer.transform(new StreamSource(xmlFile), + new StreamResult(outputFile)); + assertTrue(compareWithGold(goldFile, outputFile)); + reuseTransformer.transform(new StreamSource(xmlFile), + new StreamResult(outputFile)); + assertTrue(compareWithGold(goldFile, outputFile)); + } + + /** + * Data provider for re-use Transformer test. + * Note: same style-sheet should be transformed two different XML files. + * + * @return an array that has parameters XSL file,two XML files and two + * golden validation files. + */ + @DataProvider + public Object[][] reuseTranfomerOnDiffXMLProvider() { + return new Object[][]{ + {XML_DIR + "TransformerAPIVar.xsl", + XML_DIR + "TransformerAPIVar.xml", + GOLDEN_DIR + "TransformerAPIVar.out", + XML_DIR + "TransformerAPIVar2.xml", + GOLDEN_DIR + "TransformerAPIVar2.out"} + }; + } + + /** + * TRAX Transformer: cover transform() API and multiple Transformations from + * single transformer on same XML file. + * + * @param xslFile XSLT file we will use. + * @param xmlFile1 First XML test file to be transformed. + * @param goldFile1 golden validation file for xmlFile1. + * @param xmlFile2 Second XML test file to be transformed. + * @param goldFile2 golden validation file for xmlFile2. + * @throws TransformerException When it is not possible to create a + * Transformer instance or an unrecoverable error occurs during + * the course of the transformation.. + * @throws IOException if an I/O error occurs reading from the file or a + * malformed or unmappable byte sequence is read. + */ + @Test(dataProvider = "reuseTranfomerOnDiffXMLProvider") + public void reuseTransformerDiffXMLTest(String xslFile, String xmlFile1, + String goldFile1, String xmlFile2, String goldFile2) + throws IOException, TransformerException { + Transformer reuseTransformer = factory.newTransformer(new StreamSource(xslFile)); + + // Transform first xml file and validate it. + String outputFile1 = getNextFile(this.getClass()); + reuseTransformer.transform(new StreamSource(xmlFile1), + new StreamResult(outputFile1)); + assertTrue(compareWithGold(goldFile1, outputFile1)); + + // Transform second xml file and validate it. + String outputFile2 = getNextFile(this.getClass()); + reuseTransformer.transform(new StreamSource(xmlFile2), + new StreamResult(outputFile2)); + assertTrue(compareWithGold(goldFile2, outputFile2)); + + // Transform first xml file and validate it again. + String outputFile3 = getNextFile(this.getClass()); + reuseTransformer.transform(new StreamSource(xmlFile1), + new StreamResult(outputFile3)); + assertTrue(compareWithGold(goldFile1, outputFile3)); + } + + /** + * Negative test on getOutputProperty. IllegalArgumentException If the + * property is not supported. + * + * @param notSupportedProperty an invalid property name. + * @throws TransformerConfigurationException Thrown in case of + * ServiceConfigurationError service configuration error or if the + * implementation is not available or cannot be instantiated. + */ + @Test(dataProvider = "bogusProperties", expectedExceptions = IllegalArgumentException.class) + public void bogusPropertyName(String notSupportedProperty) + throws TransformerConfigurationException{ + Transformer transformer = factory.newTransformer(); + transformer.getOutputProperty(notSupportedProperty); + } + + /** + * Data provider for bogusPropertyName negative tests. + * + * @return a parameters array that contains all invalid properties name. + */ + @DataProvider + public Object[][] bogusProperties() { + return new Object[][]{ + {"bogus-name"}, + {"bogus-{name}"} + }; + } +}