/* * 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.util.Properties; import javax.xml.transform.Templates; import javax.xml.transform.Transformer; 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 org.apache.qetest.xslwrapper.TransformWrapper; import org.apache.qetest.xslwrapper.TransformWrapperFactory; import static org.testng.Assert.assertTrue; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; /** * Unit test for Xalan-J 2.x. * * Developers should always run either the minitest or smoketest * target before checking any code into the xml-xalan CVS * repository. Running the minitest before checking in ensures * that the Xalan CVS tree will always be in a compileable and * at least basically functional state, thus ensuring a workable * product for your fellow Xalan developers. Ensuring your code * passes the smoketest target will also help the nightly GUMP * runs to pass the smoketest as well and avoid 'nag' emails. */ public class Minitest extends JAXPFileBaseTest { /** * Constants matching parameter names/values. */ private static final String PARAM1S = "param1s"; private static final String PARAM1N = "param1n"; /** * Basic systemId transforms and parameters plus API coverage. * * @throws TransformerException If an unrecoverable error occurs during the * course of the transformation. * @throws IOException if any I/O operation error. */ @Test public void test() throws TransformerException, IOException { String xslFile = XML_DIR + "Minitest.xsl"; String xmlFile = XML_DIR + "Minitest.xml"; // Use separate output files for different versions, since // some indenting rules are implemented differently 1.x/2.x String goldFile = GOLDEN_DIR + "Minitest-xalanj2.out"; String file1 = getNextFile(this.getClass()); String file2 = getNextFile(this.getClass()); try (FileOutputStream fos1 = new FileOutputStream(file1); FileOutputStream fos2 = new FileOutputStream(file2);) { TransformerFactory factory = TransformerFactory.newInstance(); Templates templates = factory.newTemplates(new StreamSource(filenameToURL(xslFile))); Transformer transformer = templates.newTransformer(); transformer.transform(new StreamSource(filenameToURL(xmlFile)), new StreamResult(fos1)); compareWithGold(goldFile, file1); // Validate transformer reuse transformer.transform(new StreamSource(filenameToURL(xmlFile)), new StreamResult(fos2)); compareWithGold(goldFile, file2); } String paramXslFile = XML_DIR + "MinitestParam.xsl"; String paramXmlFile = XML_DIR + "MinitestParam.xml"; String paramGoldFile = GOLDEN_DIR + "MinitestParam.out"; String file3 = getNextFile(this.getClass()); try (FileOutputStream fos = new FileOutputStream(file3)) { // Validate selected API's - primarily Parameters TransformerFactory factory = TransformerFactory.newInstance(); Templates paramTemplates = factory.newTemplates(new StreamSource(filenameToURL(paramXslFile))); Transformer paramTransformer = paramTemplates.newTransformer(); paramTransformer.setParameter(PARAM1S, "paramVal"); assertTrue(paramTransformer.getParameter(PARAM1S) instanceof String); // Verify simple re-set/get of a single parameter - new Integer paramTransformer.setParameter(PARAM1S, 1234); // SPR SCUU4R3JGY - can't re-set assertTrue(paramTransformer.getParameter(PARAM1S) instanceof Integer); // Validate a transform with two params set paramTransformer.setParameter(PARAM1N, "new-param1n-value"); paramTransformer.transform(new StreamSource(filenameToURL(paramXmlFile)), new StreamResult(fos)); compareWithGold(paramGoldFile, file3); // Validate params are still set after transform assertTrue(paramTransformer.getParameter(PARAM1S) instanceof Integer); assertTrue(paramTransformer.getParameter(PARAM1S) instanceof Integer); } } @DataProvider public Object[][] wrapperProvider() { return new Object[][]{ {"Minitest.xsl", "Minitest.xml", "Minitest-xalanj2.out", "trax.dom"}, {"Minitest.xsl", "Minitest.xml", "Minitest-xalanj2.out", "trax.sax"}, {"Minitest.xsl", "Minitest.xml", "Minitest-xalanj2.out", "trax.stream"}, {"Minitest.xsl", "Minitest.xml", "Minitest-xalanj2.out", "trax.file"}, {"Minitest.xsl", "Minitest.xml", "Minitest-xalanj2.out", "trax.systemId"}, {"Minitest.xsl", "Minitest.xml", "Minitest-xalanj2.out", "trax.localPath"}, {"Minitest.xsl", "Minitest.xml", "Minitest-xalanj2.out", "trax.systemId3"}, {"MinitestPerf.xsl", "MinitestPerf.xml", "MinitestPerf.out", "trax.dom"}, {"MinitestPerf.xsl", "MinitestPerf.xml", "MinitestPerf.out", "trax.sax"}, {"MinitestPerf.xsl", "MinitestPerf.xml", "MinitestPerf.out", "trax.stream"}, {"MinitestPerf.xsl", "MinitestPerf.xml", "MinitestPerf.out", "trax.file"}, {"MinitestPerf.xsl", "MinitestPerf.xml", "MinitestPerf.out", "trax.systemId"}, {"MinitestPerf.xsl", "MinitestPerf.xml", "MinitestPerf.out", "trax.localPath"}, {"MinitestPerf.xsl", "MinitestPerf.xml", "MinitestPerf.out", "trax.systemId3"} }; } /** * Basic test for transformWrapper. Worker method to use a TransformWrapper * to transform a file and checking the output. * * @param xslName stylesheet file. * @param xmlName XML file for parsing. * @param goldName golden file for checking. * @param flavor Test favor. * @throws Exception any exception in wrapper. */ @Test(dataProvider = "wrapperProvider") public void testTransformWrapper(String xslName, String xmlName, String goldName, String flavor) throws Exception { String xslFile = XML_DIR + xslName; String xmlFile = XML_DIR + xmlName; String goldFile = GOLDEN_DIR + goldName; String outputFile = getNextFile(this.getClass()); TransformWrapper transformWrapper = TransformWrapperFactory.newWrapper(flavor); transformWrapper.newProcessor(new Properties()); if (null == xslFile) { // presume it's an embedded test transformWrapper.transformEmbedded(xmlFile, outputFile); } else { // presume it's a normal stylesheet test transformWrapper.transform(xmlFile, xslFile, outputFile); } compareWithGold(goldFile, outputFile); } }