1 /* 2 * Copyright (c) 2015, 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.util.Properties; 24 import javax.xml.transform.Templates; 25 import javax.xml.transform.Transformer; 26 import javax.xml.transform.TransformerFactory; 27 import javax.xml.transform.stream.StreamResult; 28 import javax.xml.transform.stream.StreamSource; 29 import jaxp.library.JAXPFileBaseTest; 30 import static jaxp.library.JAXPTestUtilities.compareWithGold; 31 import static jaxp.library.JAXPTestUtilities.filenameToURL; 32 import static org.apache.qetest.trax.TraxConst.GOLDEN_DIR; 33 import static org.apache.qetest.trax.TraxConst.XML_DIR; 34 import static jaxp.library.JAXPTestUtilities.getNextFile; 35 import org.apache.qetest.xslwrapper.TransformWrapper; 36 import org.apache.qetest.xslwrapper.TransformWrapperFactory; 37 import static org.testng.Assert.assertTrue; 38 import org.testng.annotations.DataProvider; 39 import org.testng.annotations.Test; 40 41 /** 42 * Unit test for Xalan-J 2.x. 43 * 44 * Developers should always run either the minitest or smoketest 45 * target before checking any code into the xml-xalan CVS 46 * repository. Running the minitest before checking in ensures 47 * that the Xalan CVS tree will always be in a compileable and 48 * at least basically functional state, thus ensuring a workable 49 * product for your fellow Xalan developers. Ensuring your code 50 * passes the smoketest target will also help the nightly GUMP 51 * runs to pass the smoketest as well and avoid 'nag' emails. 52 */ 53 public class Minitest extends JAXPFileBaseTest { 54 /** 55 * Constants matching parameter names/values. 56 */ 57 private static final String PARAM1S = "param1s"; 58 59 private static final String PARAM1N = "param1n"; 60 61 /** 62 * Basic systemId transforms and parameters plus API coverage. 63 * 64 * @throws Exception If any errors occur. 65 */ 66 @Test 67 public void test() throws Exception { 68 String xslFile = XML_DIR + "Minitest.xsl"; 69 String xmlFile = XML_DIR + "Minitest.xml"; 70 // Use separate output files for different versions, since 71 // some indenting rules are implemented differently 1.x/2.x 72 String goldFile = GOLDEN_DIR + "Minitest-xalanj2.out"; 73 74 String file1 = getNextFile(this.getClass()); 75 String file2 = getNextFile(this.getClass()); 76 77 try (FileOutputStream fos1 = new FileOutputStream(file1); 78 FileOutputStream fos2 = new FileOutputStream(file2);) { 79 TransformerFactory factory = TransformerFactory.newInstance(); 80 Templates templates = factory.newTemplates(new StreamSource(filenameToURL(xslFile))); 81 Transformer transformer = templates.newTransformer(); 82 transformer.transform(new StreamSource(filenameToURL(xmlFile)), 83 new StreamResult(fos1)); 84 compareWithGold(goldFile, file1); 85 86 // Validate transformer reuse 87 transformer.transform(new StreamSource(filenameToURL(xmlFile)), 88 new StreamResult(fos2)); 89 compareWithGold(goldFile, file2); 90 } 91 92 String paramXslFile = XML_DIR + "MinitestParam.xsl"; 93 String paramXmlFile = XML_DIR + "MinitestParam.xml"; 94 String paramGoldFile = GOLDEN_DIR + "MinitestParam.out"; 95 String file3 = getNextFile(this.getClass()); 96 try (FileOutputStream fos = new FileOutputStream(file3)) { 97 // Validate selected API's - primarily Parameters 98 TransformerFactory factory = TransformerFactory.newInstance(); 99 Templates paramTemplates = factory.newTemplates(new StreamSource(filenameToURL(paramXslFile))); 100 Transformer paramTransformer = paramTemplates.newTransformer(); 101 paramTransformer.setParameter(PARAM1S, "paramVal"); 102 assertTrue(paramTransformer.getParameter(PARAM1S) instanceof String); 103 104 // Verify simple re-set/get of a single parameter - new Integer 105 paramTransformer.setParameter(PARAM1S, 1234); // SPR SCUU4R3JGY - can't re-set 106 assertTrue(paramTransformer.getParameter(PARAM1S) instanceof Integer); 107 108 // Validate a transform with two params set 109 paramTransformer.setParameter(PARAM1N, "new-param1n-value"); 110 111 paramTransformer.transform(new StreamSource(filenameToURL(paramXmlFile)), 112 new StreamResult(fos)); 113 compareWithGold(paramGoldFile, file3); 114 // Validate params are still set after transform 115 assertTrue(paramTransformer.getParameter(PARAM1S) instanceof Integer); 116 assertTrue(paramTransformer.getParameter(PARAM1S) instanceof Integer); 117 } 118 } 119 120 /** 121 * Using different Transformer to transform xml files. 122 * Note wrapper here is mapping with wrapper according to 123 * TransformWrapperFactory.wrapperMapper 124 * 125 * @return an array that has the style-sheet file name, XML file name, 126 * golden validation file name and type of wrapper. 127 */ 128 @DataProvider 129 public Object[][] wrapperProvider() { 130 return new Object[][]{ 131 {"Minitest.xsl", "Minitest.xml", "Minitest-xalanj2.out", "trax.dom"}, 132 {"Minitest.xsl", "Minitest.xml", "Minitest-xalanj2.out", "trax.sax"}, 133 {"Minitest.xsl", "Minitest.xml", "Minitest-xalanj2.out", "trax.stream"}, 134 {"Minitest.xsl", "Minitest.xml", "Minitest-xalanj2.out", "trax.file"}, 135 {"Minitest.xsl", "Minitest.xml", "Minitest-xalanj2.out", "trax.systemId"}, 136 {"Minitest.xsl", "Minitest.xml", "Minitest-xalanj2.out", "trax.localPath"}, 137 {"Minitest.xsl", "Minitest.xml", "Minitest-xalanj2.out", "trax.systemId3"}, 138 {"MinitestPerf.xsl", "MinitestPerf.xml", "MinitestPerf.out", "trax.dom"}, 139 {"MinitestPerf.xsl", "MinitestPerf.xml", "MinitestPerf.out", "trax.sax"}, 140 {"MinitestPerf.xsl", "MinitestPerf.xml", "MinitestPerf.out", "trax.stream"}, 141 {"MinitestPerf.xsl", "MinitestPerf.xml", "MinitestPerf.out", "trax.file"}, 142 {"MinitestPerf.xsl", "MinitestPerf.xml", "MinitestPerf.out", "trax.systemId"}, 143 {"MinitestPerf.xsl", "MinitestPerf.xml", "MinitestPerf.out", "trax.localPath"}, 144 {"MinitestPerf.xsl", "MinitestPerf.xml", "MinitestPerf.out", "trax.systemId3"} 145 }; 146 } 147 148 /** 149 * Basic test for transformWrapper. Worker method to use a TransformWrapper 150 * to transform a file and checking the output. 151 * 152 * @param xslName stylesheet file. 153 * @param xmlName XML file for parsing. 154 * @param goldName golden file for checking. 155 * @param flavor Test favor. 156 * @throws Exception any exception in wrapper. 157 */ 158 @Test(dataProvider = "wrapperProvider") 159 public void testTransformWrapper(String xslName, String xmlName, 160 String goldName, String flavor) throws Exception { 161 String xslFile = XML_DIR + xslName; 162 String xmlFile = XML_DIR + xmlName; 163 String goldFile = GOLDEN_DIR + goldName; 164 String outputFile = getNextFile(this.getClass()); 165 166 TransformWrapper transformWrapper = TransformWrapperFactory.newWrapper(flavor); 167 transformWrapper.newProcessor(new Properties()); 168 if (null == xslFile) { 169 // presume it's an embedded test 170 transformWrapper.transformEmbedded(xmlFile, outputFile); 171 } else { 172 // presume it's a normal stylesheet test 173 transformWrapper.transform(xmlFile, xslFile, outputFile); 174 } 175 compareWithGold(goldFile, outputFile); 176 177 } 178 }