1 /* 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3 */ 4 /* 5 * Licensed to the Apache Software Foundation (ASF) under one or more 6 * contributor license agreements. See the NOTICE file distributed with 7 * this work for additional information regarding copyright ownership. 8 * The ASF licenses this file to You under the Apache License, Version 2.0 9 * (the "License"); you may not use this file except in compliance with 10 * the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 */ 20 package org.apache.qetest.trax; 21 22 import java.io.FileOutputStream; 23 import java.io.IOException; 24 import java.util.Properties; 25 import javax.xml.transform.Templates; 26 import javax.xml.transform.Transformer; 27 import javax.xml.transform.TransformerException; 28 import javax.xml.transform.TransformerFactory; 29 import javax.xml.transform.stream.StreamResult; 30 import javax.xml.transform.stream.StreamSource; 31 import jaxp.library.JAXPFileBaseTest; 32 import static jaxp.library.JAXPTestUtilities.compareWithGold; 33 import static jaxp.library.JAXPTestUtilities.filenameToURL; 34 import static org.apache.qetest.trax.TraxConst.GOLDEN_DIR; 35 import static org.apache.qetest.trax.TraxConst.XML_DIR; 36 import static jaxp.library.JAXPTestUtilities.getNextFile; 37 import org.apache.qetest.xslwrapper.TransformWrapper; 38 import org.apache.qetest.xslwrapper.TransformWrapperFactory; 39 import static org.testng.Assert.assertTrue; 40 import org.testng.annotations.DataProvider; 41 import org.testng.annotations.Test; 42 43 /** 44 * Unit test for Xalan-J 2.x. 45 * 46 * Developers should always run either the minitest or smoketest 47 * target before checking any code into the xml-xalan CVS 48 * repository. Running the minitest before checking in ensures 49 * that the Xalan CVS tree will always be in a compileable and 50 * at least basically functional state, thus ensuring a workable 51 * product for your fellow Xalan developers. Ensuring your code 52 * passes the smoketest target will also help the nightly GUMP 53 * runs to pass the smoketest as well and avoid 'nag' emails. 54 */ 55 public class Minitest extends JAXPFileBaseTest { 56 /** 57 * Constants matching parameter names/values. 58 */ 59 private static final String PARAM1S = "param1s"; 60 61 private static final String PARAM1N = "param1n"; 62 63 /** 64 * Basic systemId transforms and parameters plus API coverage. 65 * 66 * @throws TransformerException If an unrecoverable error occurs during the 67 * course of the transformation. 68 * @throws IOException if any I/O operation error. 69 */ 70 @Test 71 public void test() throws TransformerException, IOException { 72 String xslFile = XML_DIR + "Minitest.xsl"; 73 String xmlFile = XML_DIR + "Minitest.xml"; 74 // Use separate output files for different versions, since 75 // some indenting rules are implemented differently 1.x/2.x 76 String goldFile = GOLDEN_DIR + "Minitest-xalanj2.out"; 77 78 String file1 = getNextFile(this.getClass()); 79 String file2 = getNextFile(this.getClass()); 80 81 try (FileOutputStream fos1 = new FileOutputStream(file1); 82 FileOutputStream fos2 = new FileOutputStream(file2);) { 83 TransformerFactory factory = TransformerFactory.newInstance(); 84 Templates templates = factory.newTemplates(new StreamSource(filenameToURL(xslFile))); 85 Transformer transformer = templates.newTransformer(); 86 transformer.transform(new StreamSource(filenameToURL(xmlFile)), 87 new StreamResult(fos1)); 88 compareWithGold(goldFile, file1); 89 90 // Validate transformer reuse 91 transformer.transform(new StreamSource(filenameToURL(xmlFile)), 92 new StreamResult(fos2)); 93 compareWithGold(goldFile, file2); 94 } 95 96 String paramXslFile = XML_DIR + "MinitestParam.xsl"; 97 String paramXmlFile = XML_DIR + "MinitestParam.xml"; 98 String paramGoldFile = GOLDEN_DIR + "MinitestParam.out"; 99 String file3 = getNextFile(this.getClass()); 100 try (FileOutputStream fos = new FileOutputStream(file3)) { 101 // Validate selected API's - primarily Parameters 102 TransformerFactory factory = TransformerFactory.newInstance(); 103 Templates paramTemplates = factory.newTemplates(new StreamSource(filenameToURL(paramXslFile))); 104 Transformer paramTransformer = paramTemplates.newTransformer(); 105 paramTransformer.setParameter(PARAM1S, "paramVal"); 106 assertTrue(paramTransformer.getParameter(PARAM1S) instanceof String); 107 108 // Verify simple re-set/get of a single parameter - new Integer 109 paramTransformer.setParameter(PARAM1S, 1234); // SPR SCUU4R3JGY - can't re-set 110 assertTrue(paramTransformer.getParameter(PARAM1S) instanceof Integer); 111 112 // Validate a transform with two params set 113 paramTransformer.setParameter(PARAM1N, "new-param1n-value"); 114 115 paramTransformer.transform(new StreamSource(filenameToURL(paramXmlFile)), 116 new StreamResult(fos)); 117 compareWithGold(paramGoldFile, file3); 118 // Validate params are still set after transform 119 assertTrue(paramTransformer.getParameter(PARAM1S) instanceof Integer); 120 assertTrue(paramTransformer.getParameter(PARAM1S) instanceof Integer); 121 } 122 } 123 124 @DataProvider 125 public Object[][] wrapperProvider() { 126 return new Object[][]{ 127 {"Minitest.xsl", "Minitest.xml", "Minitest-xalanj2.out", "trax.dom"}, 128 {"Minitest.xsl", "Minitest.xml", "Minitest-xalanj2.out", "trax.sax"}, 129 {"Minitest.xsl", "Minitest.xml", "Minitest-xalanj2.out", "trax.stream"}, 130 {"Minitest.xsl", "Minitest.xml", "Minitest-xalanj2.out", "trax.file"}, 131 {"Minitest.xsl", "Minitest.xml", "Minitest-xalanj2.out", "trax.systemId"}, 132 {"Minitest.xsl", "Minitest.xml", "Minitest-xalanj2.out", "trax.localPath"}, 133 {"Minitest.xsl", "Minitest.xml", "Minitest-xalanj2.out", "trax.systemId3"}, 134 {"MinitestPerf.xsl", "MinitestPerf.xml", "MinitestPerf.out", "trax.dom"}, 135 {"MinitestPerf.xsl", "MinitestPerf.xml", "MinitestPerf.out", "trax.sax"}, 136 {"MinitestPerf.xsl", "MinitestPerf.xml", "MinitestPerf.out", "trax.stream"}, 137 {"MinitestPerf.xsl", "MinitestPerf.xml", "MinitestPerf.out", "trax.file"}, 138 {"MinitestPerf.xsl", "MinitestPerf.xml", "MinitestPerf.out", "trax.systemId"}, 139 {"MinitestPerf.xsl", "MinitestPerf.xml", "MinitestPerf.out", "trax.localPath"}, 140 {"MinitestPerf.xsl", "MinitestPerf.xml", "MinitestPerf.out", "trax.systemId3"} 141 }; 142 } 143 144 /** 145 * Basic test for transformWrapper. Worker method to use a TransformWrapper 146 * to transform a file and checking the output. 147 * 148 * @param xslName stylesheet file. 149 * @param xmlName XML file for parsing. 150 * @param goldName golden file for checking. 151 * @param flavor Test favor. 152 * @throws Exception any exception in wrapper. 153 */ 154 @Test(dataProvider = "wrapperProvider") 155 public void testTransformWrapper(String xslName, String xmlName, 156 String goldName, String flavor) throws Exception { 157 String xslFile = XML_DIR + xslName; 158 String xmlFile = XML_DIR + xmlName; 159 String goldFile = GOLDEN_DIR + goldName; 160 String outputFile = getNextFile(this.getClass()); 161 162 TransformWrapper transformWrapper = TransformWrapperFactory.newWrapper(flavor); 163 transformWrapper.newProcessor(new Properties()); 164 if (null == xslFile) { 165 // presume it's an embedded test 166 transformWrapper.transformEmbedded(xmlFile, outputFile); 167 } else { 168 // presume it's a normal stylesheet test 169 transformWrapper.transform(xmlFile, xslFile, outputFile); 170 } 171 compareWithGold(goldFile, outputFile); 172 173 } 174 }