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 * TemplatesAPITest.java 25 * 26 */ 27 package org.apache.qetest.trax; 28 29 import android.platform.test.annotations.FlakyTest; 30 import java.io.File; 31 import java.util.Properties; 32 33 import javax.xml.transform.OutputKeys; 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.OutputNameManager; 42 import org.apache.qetest.QetestUtils; 43 import org.apache.qetest.xsl.XSLTestfileInfo; 44 import org.apache.xml.utils.DefaultErrorHandler; 45 import org.junit.Test; 46 47 //------------------------------------------------------------------------- 48 49 /** 50 * Basic API coverage test for the Templates class of TRAX. 51 * @author shane_curcuru@lotus.com 52 */ 53 public class TemplatesAPITest extends FileBasedTest 54 { 55 56 /** 57 * Cheap-o filename for various output files. 58 * 59 */ 60 protected OutputNameManager outNames; 61 62 /** Cheap-o filename set for both API tests and exampleSimple. */ 63 protected XSLTestfileInfo simpleTest = new XSLTestfileInfo(); 64 65 /** Name of a stylesheet with xsl:output HTML. */ 66 protected String outputFormatXSL = null; 67 68 /** System property name javax.xml.transform.TransformerFactory. */ 69 public static final String TRAX_PROCESSOR_XSLT = "javax.xml.transform.TransformerFactory"; 70 71 /** Known outputFormat property name from outputFormatTest */ 72 public static final String OUTPUT_FORMAT_NAME = OutputKeys.CDATA_SECTION_ELEMENTS; 73 74 /** Known outputFormat property value from outputFormatTest */ 75 public static final String OUTPUT_FORMAT_VALUE = "cdataHere"; 76 77 /** NEEDSDOC Field TRAX_SUBDIR */ 78 public static final String TRAX_SUBDIR = "trax"; 79 80 /** Default ctor initializes test name, comment, numTestCases. */ TemplatesAPITest()81 public TemplatesAPITest() 82 { 83 84 numTestCases = 1; // REPLACE_num 85 testName = "TemplatesAPITest"; 86 testComment = "Basic API coverage test for the Templates class of TRAX"; 87 } 88 89 /** 90 * Initialize this test - Set names of xml/xsl test files, cache system property. 91 * 92 * NEEDSDOC @param p 93 * 94 * NEEDSDOC ($objectName$) @return 95 */ doTestFileInit(Properties p)96 public boolean doTestFileInit(Properties p) 97 { 98 99 // Used for all tests; just dump files in xapi subdir 100 File outSubDir = new File(outputDir + File.separator + TRAX_SUBDIR); 101 102 if (!outSubDir.mkdirs()) 103 reporter.logWarningMsg("Could not create output dir: " 104 + outSubDir); 105 106 outNames = new OutputNameManager(outputDir + File.separator + TRAX_SUBDIR 107 + File.separator + testName, ".out"); 108 109 // Used for API coverage and exampleSimple 110 String testBasePath = inputDir + File.separator + TRAX_SUBDIR 111 + File.separator; 112 String goldBasePath = goldDir + File.separator + TRAX_SUBDIR 113 + File.separator; 114 115 simpleTest.xmlName = QetestUtils.filenameToURL(testBasePath + "TransformerAPIParam.xml"); 116 simpleTest.inputName = QetestUtils.filenameToURL(testBasePath + "TransformerAPIParam.xsl"); 117 simpleTest.goldName = goldBasePath + "TransformerAPIParam.out"; 118 outputFormatXSL = QetestUtils.filenameToURL(testBasePath + "TransformerAPIOutputFormat.xsl"); 119 120 reporter.logInfoMsg(TRAX_PROCESSOR_XSLT + " property is: " 121 + System.getProperty(TRAX_PROCESSOR_XSLT)); 122 try 123 { 124 TransformerFactory tf = TransformerFactory.newInstance(); 125 if (!(tf.getFeature(StreamSource.FEATURE) 126 && tf.getFeature(StreamResult.FEATURE))) 127 { // The rest of this test relies on Streams 128 reporter.logErrorMsg("Streams not supported! Some tests may be invalid!"); 129 } 130 } 131 catch (Throwable t) 132 { 133 reporter.checkFail( 134 "Problem creating factory; Some tests may be invalid!"); 135 reporter.logThrowable(reporter.ERRORMSG, t, 136 "Problem creating factory; Some tests may be invalid!"); 137 } 138 139 return true; 140 } 141 142 143 /** 144 * TRAX Templates: cover newTransformer(), 145 * getOutputProperties() APIs and basic functionality. 146 * 147 * NEEDSDOC ($objectName$) @return 148 */ testCase1()149 public boolean testCase1() 150 { 151 reporter.testCaseInit("TRAX Templates: cover APIs and basic functionality"); 152 153 TransformerFactory factory = null; 154 try 155 { 156 factory = TransformerFactory.newInstance(); 157 factory.setErrorListener(new DefaultErrorHandler()); 158 } 159 catch (Exception e) 160 { 161 reporter.checkFail( 162 "Problem creating Processor; cannot continue testcase"); 163 reporter.logThrowable(reporter.ERRORMSG, e, 164 "Problem creating Processor"); 165 return true; 166 } 167 168 try 169 { 170 // Cover APIs newTransformer(), getOutputProperties() 171 Templates templates = 172 factory.newTemplates(new StreamSource(simpleTest.inputName)); 173 Transformer transformer = templates.newTransformer(); 174 175 reporter.check((transformer != null), true, 176 "newTransformer() is non-null for " 177 + simpleTest.inputName); 178 179 Properties outputFormat = templates.getOutputProperties(); 180 181 reporter.check((outputFormat != null), true, 182 "getOutputProperties() is non-null for " 183 + simpleTest.inputName); 184 reporter.logHashtable(reporter.STATUSMSG, outputFormat, 185 "getOutputProperties for " + simpleTest.inputName); 186 187 // Check that the local stylesheet.getProperty has default set, cf. getOutputProperties javadoc 188 reporter.check(("xml".equals(outputFormat.getProperty(OutputKeys.METHOD))), true, simpleTest.inputName + ".op.getProperty(" 189 + OutputKeys.METHOD + ") is default value, act: " + outputFormat.getProperty(OutputKeys.METHOD)); 190 // Check that the local stylesheet.get has nothing set, cf. getOutputProperties javadoc 191 reporter.check((null == outputFormat.get(OutputKeys.METHOD)), true, simpleTest.inputName + ".op.get(" 192 + OutputKeys.METHOD + ") is null value, act: " + outputFormat.get(OutputKeys.METHOD)); 193 194 // Check that the local stylesheet.getProperty has default set, cf. getOutputProperties javadoc 195 reporter.check(("no".equals(outputFormat.getProperty(OutputKeys.INDENT))), true, simpleTest.inputName + ".op.getProperty(" 196 + OutputKeys.INDENT + ") is default value, act: " + outputFormat.getProperty(OutputKeys.INDENT)); 197 // Check that the local stylesheet.get has nothing set, cf. getOutputProperties javadoc 198 reporter.check((null == (outputFormat.get(OutputKeys.INDENT))), true, simpleTest.inputName + ".op.get(" 199 + OutputKeys.INDENT + ") is null value, act: " + outputFormat.get(OutputKeys.INDENT)); 200 } 201 catch (Exception e) 202 { 203 reporter.checkErr("newTransformer/getOutputProperties threw: " 204 + e.toString()); 205 reporter.logThrowable(reporter.STATUSMSG, e, 206 "newTransformer/getOutputProperties threw:"); 207 } 208 209 try 210 { 211 Templates templates2 = 212 factory.newTemplates(new StreamSource(outputFormatXSL)); 213 Properties outputFormat2 = templates2.getOutputProperties(); 214 215 reporter.check((outputFormat2 != null), true, 216 "getOutputProperties() is non-null for " 217 + outputFormatXSL); 218 reporter.logHashtable(reporter.STATUSMSG, outputFormat2, 219 "getOutputProperties for " + outputFormatXSL); 220 221 String tmp = outputFormat2.getProperty(OUTPUT_FORMAT_NAME); // SPR SCUU4RXSG5 - has extra space 222 if (OUTPUT_FORMAT_VALUE.equals(tmp)) // Use if so we can put out id with checkPass/checkFail lines 223 reporter.checkPass("outputProperties " + OUTPUT_FORMAT_NAME + " has known value ?" + tmp + "?", "SCUU4RXSG5"); 224 else 225 reporter.checkFail("outputProperties " + OUTPUT_FORMAT_NAME + " has known value ?" + tmp + "?", "SCUU4RXSG5"); 226 227 tmp = outputFormat2.getProperty("omit-xml-declaration"); 228 reporter.check(tmp, "yes", "outputProperties omit-xml-declaration has known value ?" + tmp + "?"); 229 } 230 catch (Exception e) 231 { 232 reporter.checkErr("outputFormat() is html... threw: " 233 + e.toString()); 234 reporter.logThrowable(reporter.STATUSMSG, e, 235 "outputFormat() is html... threw:"); 236 } 237 reporter.logTraceMsg("Functionality of Transformers covered in TransformerAPITest, elsewhere"); 238 reporter.testCaseClose(); 239 240 return true; 241 } 242 243 244 /** 245 * Convenience method to print out usage information - update if needed. 246 * 247 * NEEDSDOC ($objectName$) @return 248 */ usage()249 public String usage() 250 { 251 252 return ("Common [optional] options supported by TemplatesAPITest:\n" 253 + "(Note: assumes inputDir=.\\tests\\api)\n" 254 + "-processorClassname classname.of.processor (to override setPlatformDefaultProcessor to Xalan 2.x)\n" 255 + super.usage()); 256 } 257 258 259 /** 260 * Main method to run test from the command line - can be left alone. 261 * @param args command line argument array 262 */ main(String[] args)263 public static void main(String[] args) 264 { 265 266 TemplatesAPITest app = new TemplatesAPITest(); 267 268 app.doMain(args); 269 } 270 271 // Android-added: Run main method as a JUnit test case. 272 @FlakyTest(bugId = 292520220) 273 @Test main()274 public void main() { 275 main(new String[0]); 276 } 277 } 278