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 * URIResolverTest.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.Templates; 34 import javax.xml.transform.Transformer; 35 import javax.xml.transform.TransformerFactory; 36 import javax.xml.transform.stream.StreamResult; 37 import javax.xml.transform.stream.StreamSource; 38 39 import org.apache.qetest.FileBasedTest; 40 import org.apache.qetest.Logger; 41 import org.apache.qetest.OutputNameManager; 42 import org.apache.qetest.QetestUtils; 43 import org.apache.qetest.xsl.XSLTestfileInfo; 44 import org.junit.Test; 45 46 //------------------------------------------------------------------------- 47 48 /** 49 * Verify that URIResolvers are called properly. 50 * @author shane_curcuru@lotus.com 51 * @version $Id$ 52 */ 53 public class URIResolverTest extends FileBasedTest 54 { 55 56 /** Provide sequential output names automatically. */ 57 protected OutputNameManager outNames; 58 59 60 /** 61 * A simple stylesheet with errors for testing in various flavors. 62 * Must be coordinated with templatesExpectedType/Value, 63 * transformExpectedType/Value. 64 */ 65 protected XSLTestfileInfo testFileInfo = new XSLTestfileInfo(); 66 67 /** Subdir name under test\tests\api for files. */ 68 public static final String TRAX_SUBDIR = "trax"; 69 70 /** Just initialize test name, comment, numTestCases. */ URIResolverTest()71 public URIResolverTest() 72 { 73 numTestCases = 1; // REPLACE_num 74 testName = "URIResolverTest"; 75 testComment = "Verify that URIResolvers are called properly."; 76 } 77 78 79 /** 80 * Initialize this test 81 * 82 * @param p Properties to initialize from (if needed) 83 * @return false if we should abort the test; true otherwise 84 */ doTestFileInit(Properties p)85 public boolean doTestFileInit(Properties p) 86 { 87 // Used for all tests; just dump files in trax subdir 88 File outSubDir = new File(outputDir + File.separator + TRAX_SUBDIR); 89 if (!outSubDir.mkdirs()) 90 reporter.logWarningMsg("Could not create output dir: " + outSubDir); 91 outNames = new OutputNameManager(outputDir + File.separator + TRAX_SUBDIR 92 + File.separator + testName, ".out"); 93 94 95 String testBasePath = inputDir 96 + File.separator 97 + TRAX_SUBDIR 98 + File.separator; 99 String goldBasePath = goldDir 100 + File.separator 101 + TRAX_SUBDIR 102 + File.separator; 103 104 testFileInfo.inputName = testBasePath + "URIResolverTest.xsl"; 105 testFileInfo.xmlName = testBasePath + "URIResolverTest.xml"; 106 testFileInfo.goldName = goldBasePath + "URIResolverTest.out"; 107 108 return true; 109 } 110 111 112 /** 113 * Build a stylesheet/do a transform with lots of URIs to resolve. 114 * Verify that the URIResolver is called properly. 115 * @return false if we should abort the test; true otherwise 116 */ testCase1()117 public boolean testCase1() 118 { 119 reporter.testCaseInit("Build a stylesheet/do a transform with lots of URIs to resolve"); 120 LoggingURIResolver loggingURIResolver = new LoggingURIResolver((Logger)reporter); 121 reporter.logTraceMsg("loggingURIResolver originally setup:" + loggingURIResolver.getQuickCounters()); 122 123 TransformerFactory factory = null; 124 Templates templates = null; 125 Transformer transformer = null; 126 try 127 { 128 factory = TransformerFactory.newInstance(); 129 // Set the URIResolver and validate it 130 factory.setURIResolver(loggingURIResolver); 131 reporter.check((factory.getURIResolver() == loggingURIResolver), 132 true, "set/getURIResolver on factory"); 133 134 // Validate various URI's to be resolved during stylesheet 135 // build with the loggingURIResolver 136 String[] expectedXslUris = 137 { 138 "{" + QetestUtils.filenameToURL(testFileInfo.inputName) + "}" + "impincl/SystemIdImport.xsl", 139 "{" + QetestUtils.filenameToURL(testFileInfo.inputName) + "}" + "impincl/SystemIdInclude.xsl" 140 }; 141 loggingURIResolver.setExpected(expectedXslUris); 142 // Note that we don't currently have a default URIResolver, 143 // so the LoggingURIResolver class will just attempt 144 // to use the SystemIDResolver class instead 145 // loggingURIResolver.setDefaultHandler(savedURIResolver); 146 reporter.logInfoMsg("About to factory.newTemplates(" + QetestUtils.filenameToURL(testFileInfo.inputName) + ")"); 147 templates = factory.newTemplates(new StreamSource(QetestUtils.filenameToURL(testFileInfo.inputName))); 148 reporter.logTraceMsg("loggingURIResolver after newTemplates:" + loggingURIResolver.getQuickCounters()); 149 150 // Clear out any setExpected or counters 151 loggingURIResolver.reset(); 152 153 transformer = templates.newTransformer(); 154 reporter.logTraceMsg("default transformer's getURIResolver is: " + transformer.getURIResolver()); 155 // Set the URIResolver and validate it 156 transformer.setURIResolver(loggingURIResolver); 157 reporter.check((transformer.getURIResolver() == loggingURIResolver), 158 true, "set/getURIResolver on transformer"); 159 160 // Validate various URI's to be resolved during transform 161 // time with the loggingURIResolver 162 String[] expectedXmlUris = 163 { 164 "{" + QetestUtils.filenameToURL(testFileInfo.inputName) + "}" + "../impincl/SystemIdImport.xsl", 165 "{" + QetestUtils.filenameToURL(testFileInfo.inputName) + "}" + "impincl/SystemIdImport.xsl", 166 "{" + QetestUtils.filenameToURL(testFileInfo.inputName) + "}" + "systemid/impincl/SystemIdImport.xsl", 167 }; 168 169 //@todo Bugzilla#2425 every document() call is resolved twice twice - two fails caused below MOVED to SmoketestOuttakes.java 02-Nov-01 -sc 170 reporter.logWarningMsg("Bugzilla#2425 every document() call is resolved twice twice - two fails caused below MOVED to SmoketestOuttakes.java 02-Nov-01 -sc"); 171 // loggingURIResolver.setExpected(expectedXmlUris); 172 //@todo Bugzilla#2425 every document() call is resolved twice twice - two fails caused below MOVED to SmoketestOuttakes.java 02-Nov-01 -sc 173 174 reporter.logTraceMsg("about to transform(...)"); 175 transformer.transform(new StreamSource(QetestUtils.filenameToURL(testFileInfo.xmlName)), 176 new StreamResult(outNames.nextName())); 177 reporter.logTraceMsg("after transform(...)"); 178 // Clear out any setExpected or counters 179 loggingURIResolver.reset(); 180 181 // Validate the actual output file as well: in this case, 182 // the stylesheet should still work 183 fileChecker.check(reporter, 184 new File(outNames.currentName()), 185 new File(testFileInfo.goldName), 186 "transform of URI-filled xsl into: " + outNames.currentName()); 187 } 188 catch (Throwable t) 189 { 190 reporter.checkFail("URIResolver test unexpectedly threw: " + t.toString()); 191 reporter.logThrowable(Logger.ERRORMSG, t, "URIResolver test unexpectedly threw"); 192 } 193 194 reporter.testCaseClose(); 195 return true; 196 } 197 198 199 /** 200 * Convenience method to print out usage information - update if needed. 201 * @return String denoting usage of this test class 202 */ usage()203 public String usage() 204 { 205 return ("Common [optional] options supported by URIResolverTest:\n" 206 + "(Note: assumes inputDir=.\\tests\\api)\n" 207 + super.usage()); // Grab our parent classes usage as well 208 } 209 210 211 /** 212 * Main method to run test from the command line - can be left alone. 213 * @param args command line argument array 214 */ main(String[] args)215 public static void main(String[] args) 216 { 217 URIResolverTest app = new URIResolverTest(); 218 app.doMain(args); 219 } 220 221 // Android-added: Run main method as a JUnit test case. 222 @FlakyTest(bugId = 292520220) 223 @Test main()224 public void main() { 225 main(new String[0]); 226 } 227 } 228