1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 /* 19 * $Id$ 20 */ 21 22 /* 23 * 24 * OutputSettingsTest.java 25 * 26 */ 27 package org.apache.qetest.xalanj2; 28 29 import android.platform.test.annotations.FlakyTest; 30 import java.io.File; 31 import java.util.Properties; 32 33 import javax.xml.transform.Result; 34 import javax.xml.transform.Templates; 35 import javax.xml.transform.Transformer; 36 import javax.xml.transform.TransformerFactory; 37 import javax.xml.transform.stream.StreamResult; 38 import javax.xml.transform.stream.StreamSource; 39 40 import org.apache.qetest.FileBasedTest; 41 import org.apache.qetest.Logger; 42 import org.apache.qetest.OutputNameManager; 43 import org.apache.qetest.QetestUtils; 44 import org.apache.qetest.xsl.XSLTestfileInfo; 45 import org.junit.Test; 46 47 //------------------------------------------------------------------------- 48 49 /** 50 * Verify xalan:-specific output properties. 51 * This test is similar to trax.OutputPropertiesTest but tests 52 * some Xalan-J 2.2.x+ specific features for the xalan: namespace, 53 * like: indent-amount, content-handler, entities, 54 * use-url-escaping, and omit-meta-tag. 55 * 56 * @author shane_curcuru@lotus.com 57 * @version $Id$ 58 */ 59 public class OutputSettingsTest extends FileBasedTest 60 { 61 62 /** Used for generating names of actual output files. */ 63 protected OutputNameManager outNames; 64 65 /** Default OutputSettingsTest.xml/xsl file pair. */ 66 protected XSLTestfileInfo xmlFileInfo = new XSLTestfileInfo(); 67 68 /** OutputEntities.xml/xsl/ent file pair. */ 69 protected XSLTestfileInfo entFileInfo = new XSLTestfileInfo(); 70 71 /** Just initialize test name, comment, numTestCases. */ OutputSettingsTest()72 public OutputSettingsTest() 73 { 74 numTestCases = 2; // REPLACE_num 75 testName = "OutputSettingsTest"; 76 testComment = "Verify xalan:-specific output properties"; 77 } 78 79 80 /** 81 * Initialize this test - Set names of xml/xsl test files, etc. 82 * 83 * @param p Properties to initialize from (if needed) 84 * @return false if we should abort the test; true otherwise 85 */ doTestFileInit(Properties p)86 public boolean doTestFileInit(Properties p) 87 { 88 // Used for all tests; just dump files in trax subdir 89 File outSubDir = new File(outputDir + File.separator + "xalanj2"); 90 if (!outSubDir.mkdirs()) 91 reporter.logWarningMsg("Possible problem creating output dir: " + outSubDir); 92 // Initialize an output name manager to that dir with .out extension 93 outNames = new OutputNameManager(outputDir + File.separator + "xalanj2" 94 + File.separator + testName, ".out"); 95 96 String testBasePath = inputDir 97 + File.separator 98 + "xalanj2" 99 + File.separator; 100 String goldBasePath = goldDir 101 + File.separator 102 + "xalanj2" 103 + File.separator; 104 105 xmlFileInfo.inputName = testBasePath + "OutputSettingsXML.xsl"; 106 xmlFileInfo.xmlName = testBasePath + "OutputSettingsXML.xml"; 107 // Only root of the output gold name 108 xmlFileInfo.goldName = goldBasePath + "OutputSettingsXML"; 109 110 // xsl file references OutputEntities.ent 111 entFileInfo.inputName = testBasePath + "OutputEntities.xsl"; 112 entFileInfo.xmlName = testBasePath + "identity.xml"; 113 entFileInfo.goldName = goldBasePath + "OutputEntities.out"; 114 return true; 115 } 116 117 118 /** 119 * Verify xalan:entities output property. 120 * @return false if we should abort the test; true otherwise 121 */ testCase1()122 public boolean testCase1() 123 { 124 reporter.testCaseInit("Verify xalan:entities output property"); 125 TransformerFactory factory = null; 126 Templates templates = null; 127 128 try 129 { 130 // Process stylesheet with replaced entities 131 factory = TransformerFactory.newInstance(); 132 reporter.logInfoMsg("entFileInfo newTemplates(" + QetestUtils.filenameToURL(entFileInfo.inputName) + ")"); 133 templates = factory.newTemplates(new StreamSource(QetestUtils.filenameToURL(entFileInfo.inputName))); 134 reporter.logHashtable(Logger.STATUSMSG, 135 templates.getOutputProperties(), "entFileInfo templates output properties"); 136 137 // Process the file once with default properties 138 Transformer transformer = templates.newTransformer(); 139 Result result = new StreamResult(outNames.nextName()); 140 reporter.logInfoMsg("(1)replaced entities transform(" + QetestUtils.filenameToURL(entFileInfo.xmlName) 141 + ", " + outNames.currentName() + ")"); 142 transformer.transform(new StreamSource(QetestUtils.filenameToURL(entFileInfo.xmlName)), result); 143 // Validate the default transform to base gold file 144 fileChecker.check(reporter, 145 new File(outNames.currentName()), 146 new File(entFileInfo.goldName), 147 "(1)replaced entities transform into: " + outNames.currentName() 148 + " gold: " + entFileInfo.goldName); 149 150 } 151 catch (Throwable t) 152 { 153 reporter.checkErr("(1)replaced entities threw:" + t.toString()); 154 reporter.logThrowable(reporter.ERRORMSG, t, "(1)replaced entities threw "); 155 return true; 156 } 157 reporter.testCaseClose(); 158 return true; 159 } 160 161 /** 162 * Verify xalan:indent-amount output property. 163 * @return false if we should abort the test; true otherwise 164 */ testCase2()165 public boolean testCase2() 166 { 167 reporter.testCaseInit("Verify xalan:indent-amount output property"); 168 TransformerFactory factory = null; 169 Templates templates = null; 170 try 171 { 172 // Process simple XML output stylesheet 173 factory = TransformerFactory.newInstance(); 174 reporter.logInfoMsg("xmlFileInfo newTemplates(" + QetestUtils.filenameToURL(xmlFileInfo.inputName) + ")"); 175 templates = factory.newTemplates(new StreamSource(QetestUtils.filenameToURL(xmlFileInfo.inputName))); 176 reporter.logHashtable(Logger.STATUSMSG, 177 templates.getOutputProperties(), "xmlFileInfo templates output properties"); 178 179 // Process the file once with default properties 180 Transformer transformer = templates.newTransformer(); 181 Result result = new StreamResult(outNames.nextName()); 182 reporter.logInfoMsg("(2)xml transform(" + QetestUtils.filenameToURL(xmlFileInfo.xmlName) 183 + ", " + outNames.currentName() + ")"); 184 transformer.transform(new StreamSource(QetestUtils.filenameToURL(xmlFileInfo.xmlName)), result); 185 // Validate the default transform to base gold file 186 fileChecker.check(reporter, 187 new File(outNames.currentName()), 188 new File(xmlFileInfo.goldName + ".out"), 189 "(2)xml transform into: " + outNames.currentName() 190 + " gold: " + xmlFileInfo.goldName + ".out"); 191 192 // Set Xalan-specific output property 193 reporter.logInfoMsg("setOutputProperty({http://xml.apache.org/xslt}indent-amount, 2)"); 194 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); 195 196 result = new StreamResult(outNames.nextName()); 197 reporter.logInfoMsg("(2)xml-2 transform(" + QetestUtils.filenameToURL(xmlFileInfo.xmlName) 198 + ", " + outNames.currentName() + ")"); 199 transformer.transform(new StreamSource(QetestUtils.filenameToURL(xmlFileInfo.xmlName)), result); 200 // Validate the default transform to base gold file 201 fileChecker.check(reporter, 202 new File(outNames.currentName()), 203 new File(xmlFileInfo.goldName + "-2.out"), 204 "(2)xml-2 transform into: " + outNames.currentName() 205 + " gold: " + xmlFileInfo.goldName + "-2.out"); 206 reporter.logHashtable(Logger.STATUSMSG, 207 transformer.getOutputProperties(), "xml-2 transformer output properties"); 208 209 // Set Xalan-specific output property 210 reporter.logInfoMsg("setOutputProperty({http://xml.apache.org/xslt}indent-amount, 12)"); 211 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "12"); 212 213 result = new StreamResult(outNames.nextName()); 214 reporter.logInfoMsg("(2)xml-12 transform(" + QetestUtils.filenameToURL(xmlFileInfo.xmlName) 215 + ", " + outNames.currentName() + ")"); 216 transformer.transform(new StreamSource(QetestUtils.filenameToURL(xmlFileInfo.xmlName)), result); 217 // Validate the default transform to base gold file 218 fileChecker.check(reporter, 219 new File(outNames.currentName()), 220 new File(xmlFileInfo.goldName + "-12.out"), 221 "(2)xml-12 transform into: " + outNames.currentName() 222 + " gold: " + xmlFileInfo.goldName + "-12.out"); 223 reporter.logHashtable(Logger.STATUSMSG, 224 transformer.getOutputProperties(), "xml-12 transformer output properties"); 225 } 226 catch (Throwable t) 227 { 228 reporter.checkErr("(2)xml stylesheet threw:" + t.toString()); 229 reporter.logThrowable(reporter.ERRORMSG, t, "(2)xml stylesheet threw "); 230 return true; 231 } 232 reporter.testCaseClose(); 233 return true; 234 } 235 236 /** 237 * Convenience method to print out usage information - update if needed. 238 * @return String denoting usage of this test class 239 */ usage()240 public String usage() 241 { 242 return ("Common [optional] options supported by OutputSettingsTest:\n" 243 + "(Note: assumes inputDir=tests\\api)\n" 244 + super.usage()); // Grab our parent classes usage as well 245 } 246 247 /** 248 * Main method to run test from the command line - can be left alone. 249 * @param args command line argument array 250 */ main(String[] args)251 public static void main(String[] args) 252 { 253 OutputSettingsTest app = new OutputSettingsTest(); 254 app.doMain(args); 255 } 256 257 // Android-added: Run main method as a JUnit test case. 258 @FlakyTest(bugId = 292520220) 259 @Test main()260 public void main() { 261 main(new String[0]); 262 } 263 } 264