You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestConverter.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * $Id: TestConverter.java,v 1.23 2003/03/07 10:09:30 jeremias Exp $
  3. * ============================================================================
  4. * The Apache Software License, Version 1.1
  5. * ============================================================================
  6. *
  7. * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modifica-
  10. * tion, are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if any, must
  20. * include the following acknowledgment: "This product includes software
  21. * developed by the Apache Software Foundation (http://www.apache.org/)."
  22. * Alternately, this acknowledgment may appear in the software itself, if
  23. * and wherever such third-party acknowledgments normally appear.
  24. *
  25. * 4. The names "FOP" and "Apache Software Foundation" must not be used to
  26. * endorse or promote products derived from this software without prior
  27. * written permission. For written permission, please contact
  28. * apache@apache.org.
  29. *
  30. * 5. Products derived from this software may not be called "Apache", nor may
  31. * "Apache" appear in their name, without prior written permission of the
  32. * Apache Software Foundation.
  33. *
  34. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  35. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  36. * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  37. * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  38. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  39. * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  40. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  41. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  42. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  43. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. * ============================================================================
  45. *
  46. * This software consists of voluntary contributions made by many individuals
  47. * on behalf of the Apache Software Foundation and was originally created by
  48. * James Tauber <jtauber@jtauber.com>. For more information on the Apache
  49. * Software Foundation, please see <http://www.apache.org/>.
  50. */
  51. package org.apache.fop.tools;
  52. import org.apache.fop.apps.Driver;
  53. import org.apache.fop.apps.FOInputHandler;
  54. import org.apache.fop.apps.FOPException;
  55. import org.apache.fop.apps.InputHandler;
  56. import org.apache.fop.apps.XSLTInputHandler;
  57. import org.apache.fop.fo.FOUserAgent;
  58. import org.apache.avalon.framework.logger.ConsoleLogger;
  59. import org.apache.avalon.framework.logger.AbstractLogEnabled;
  60. import java.io.File;
  61. import java.io.InputStream;
  62. import java.util.Map;
  63. import javax.xml.parsers.DocumentBuilder;
  64. import javax.xml.parsers.DocumentBuilderFactory;
  65. import org.w3c.dom.Document;
  66. import org.w3c.dom.Node;
  67. import org.w3c.dom.NodeList;
  68. /**
  69. * TestConverter is used to process a set of tests specified in
  70. * a testsuite.
  71. * This class retrieves the data in the testsuite and uses FOP
  72. * to convert the xml and xsl file into either an xml representation
  73. * of the area tree or a pdf document.
  74. * The area tree can be used for automatic comparisons between different
  75. * versions of FOP or the pdf can be view for manual checking and
  76. * pdf rendering.
  77. *
  78. * Modified by Mark Lillywhite mark-fop@inomial.com to use the new Driver
  79. * interface.
  80. */
  81. public class TestConverter extends AbstractLogEnabled {
  82. private boolean failOnly = false;
  83. private boolean outputPDF = false;
  84. private File destdir;
  85. private File compare = null;
  86. private String baseDir = "./";
  87. private Map differ = new java.util.HashMap();
  88. /**
  89. * This main method can be used to run the test converter from
  90. * the command line.
  91. * This will take a specified testsuite xml and process all
  92. * tests in it.
  93. * The command line options are:
  94. * -b to set the base directory for where the testsuite and associated files are
  95. * -failOnly to process only the tests which are specified as fail in the test results
  96. * -pdf to output the result as pdf
  97. * @param args command-line arguments
  98. */
  99. public static void main(String[] args) {
  100. if (args == null || args.length == 0) {
  101. System.out.println("test suite file name required");
  102. }
  103. TestConverter tc = new TestConverter();
  104. tc.enableLogging(new ConsoleLogger(ConsoleLogger.LEVEL_ERROR));
  105. String testFile = null;
  106. for (int count = 0; count < args.length; count++) {
  107. if (args[count].equals("-failOnly")) {
  108. tc.setFailOnly(true);
  109. } else if (args[count].equals("-pdf")) {
  110. tc.setOutputPDF(true);
  111. } else if (args[count].equals("-b")) {
  112. tc.setBaseDir(args[count + 1]);
  113. } else {
  114. testFile = args[count];
  115. }
  116. }
  117. if (testFile == null) {
  118. System.out.println("test suite file name required");
  119. }
  120. tc.runTests(testFile, "results", null);
  121. }
  122. /**
  123. * Controls whether to generate PDF or XML.
  124. * @param pdf If True, PDF is generated, Area Tree XML otherwise.
  125. */
  126. public void setOutputPDF(boolean pdf) {
  127. outputPDF = pdf;
  128. }
  129. /**
  130. * Controls whether to process only the tests which are specified as fail
  131. * in the test results.
  132. * @param fail True if only fail tests should be processed
  133. */
  134. public void setFailOnly(boolean fail) {
  135. failOnly = fail;
  136. }
  137. /**
  138. * Sets the base directory.
  139. * @param str base directory
  140. */
  141. public void setBaseDir(String str) {
  142. baseDir = str;
  143. }
  144. /**
  145. * Run the Tests.
  146. * This runs the tests specified in the xml file fname.
  147. * The document is read as a dom and each testcase is covered.
  148. * @param fname filename of the input file
  149. * @param dest destination directory
  150. * @param compDir comparison directory
  151. * @return Map a Map containing differences
  152. */
  153. public Map runTests(String fname, String dest, String compDir) {
  154. getLogger().debug("running tests in file:" + fname);
  155. try {
  156. if (compDir != null) {
  157. compare = new File(baseDir + "/" + compDir);
  158. }
  159. destdir = new File(baseDir + "/" + dest);
  160. destdir.mkdirs();
  161. File f = new File(baseDir + "/" + fname);
  162. DocumentBuilderFactory factory =
  163. DocumentBuilderFactory.newInstance();
  164. DocumentBuilder db = factory.newDocumentBuilder();
  165. Document doc = db.parse(f);
  166. NodeList suitelist = doc.getChildNodes();
  167. if (suitelist.getLength() == 0) {
  168. return differ;
  169. }
  170. Node testsuite = null;
  171. testsuite = doc.getDocumentElement();
  172. if (testsuite.hasAttributes()) {
  173. String profile =
  174. testsuite.getAttributes().getNamedItem("profile").getNodeValue();
  175. getLogger().debug("testing test suite:" + profile);
  176. }
  177. NodeList testcases = testsuite.getChildNodes();
  178. for (int count = 0; count < testcases.getLength(); count++) {
  179. Node testcase = testcases.item(count);
  180. if (testcase.getNodeName().equals("testcases")) {
  181. runTestCase(testcase);
  182. }
  183. }
  184. } catch (Exception e) {
  185. getLogger().error("Error while running tests", e);
  186. }
  187. return differ;
  188. }
  189. /**
  190. * Run a test case.
  191. * This goes through a test case in the document.
  192. * A testcase can contain a test, a result or more test cases.
  193. * A test case is handled recursively otherwise the test is run.
  194. * @param tcase Test case node to run
  195. */
  196. protected void runTestCase(Node tcase) {
  197. if (tcase.hasAttributes()) {
  198. String profile =
  199. tcase.getAttributes().getNamedItem("profile").getNodeValue();
  200. getLogger().debug("testing profile:" + profile);
  201. }
  202. NodeList cases = tcase.getChildNodes();
  203. for (int count = 0; count < cases.getLength(); count++) {
  204. Node node = cases.item(count);
  205. String nodename = node.getNodeName();
  206. if (nodename.equals("testcases")) {
  207. runTestCase(node);
  208. } else if (nodename.equals("test")) {
  209. runTest(tcase, node);
  210. } else if (nodename.equals("result")) {
  211. //nop
  212. }
  213. }
  214. }
  215. /**
  216. * Run a particular test.
  217. * This runs a test defined by the xml and xsl documents.
  218. * If the test has a result specified it is checked.
  219. * This creates an XSLTInputHandler to provide the input
  220. * for FOP and writes the data out to an XML are tree.
  221. * @param testcase Test case to run
  222. * @param test Test
  223. */
  224. protected void runTest(Node testcase, Node test) {
  225. String id = test.getAttributes().getNamedItem("id").getNodeValue();
  226. Node result = locateResult(testcase, id);
  227. boolean pass = false;
  228. if (result != null) {
  229. String agreement =
  230. result.getAttributes().getNamedItem("agreement").getNodeValue();
  231. pass = agreement.equals("full");
  232. }
  233. if (pass && failOnly) {
  234. return;
  235. }
  236. String xml = test.getAttributes().getNamedItem("xml").getNodeValue();
  237. Node xslNode = test.getAttributes().getNamedItem("xsl");
  238. String xsl = null;
  239. if (xslNode != null) {
  240. xsl = xslNode.getNodeValue();
  241. }
  242. getLogger().debug("converting xml:" + xml + " and xsl:"
  243. + xsl + " to area tree");
  244. try {
  245. File xmlFile = new File(baseDir + "/" + xml);
  246. String baseURL = null;
  247. try {
  248. baseURL = xmlFile.getParentFile().toURL().toExternalForm();
  249. } catch (Exception e) {
  250. getLogger().error("Error setting base directory");
  251. }
  252. InputHandler inputHandler = null;
  253. if (xsl == null) {
  254. inputHandler = new FOInputHandler(xmlFile);
  255. } else {
  256. inputHandler = new XSLTInputHandler(xmlFile,
  257. new File(baseDir + "/"
  258. + xsl));
  259. }
  260. Driver driver = new Driver();
  261. setupLogger(driver, "fop");
  262. FOUserAgent userAgent = new FOUserAgent();
  263. userAgent.setBaseURL(baseURL);
  264. driver.setUserAgent(userAgent);
  265. if (outputPDF) {
  266. driver.setRenderer(Driver.RENDER_PDF);
  267. } else {
  268. driver.setRenderer(Driver.RENDER_XML);
  269. }
  270. Map rendererOptions = new java.util.HashMap();
  271. rendererOptions.put("fineDetail", new Boolean(false));
  272. rendererOptions.put("consistentOutput", new Boolean(true));
  273. driver.getRenderer().setOptions(rendererOptions);
  274. driver.getRenderer().setProducer("Testsuite Converter");
  275. String outname = xmlFile.getName();
  276. if (outname.endsWith(".xml")) {
  277. outname = outname.substring(0, outname.length() - 4);
  278. }
  279. driver.setOutputStream(new java.io.BufferedOutputStream(
  280. new java.io.FileOutputStream(new File(destdir,
  281. outname + (outputPDF ? ".pdf" : ".at.xml")))));
  282. getLogger().debug("ddir:" + destdir + " on:" + outname + ".pdf");
  283. driver.render(inputHandler);
  284. // check difference
  285. if (compare != null) {
  286. File f1 = new File(destdir, outname + ".at.xml");
  287. File f2 = new File(compare, outname + ".at.xml");
  288. if (!compareFiles(f1, f2)) {
  289. differ.put(outname + ".at.xml", new Boolean(pass));
  290. }
  291. }
  292. } catch (Exception e) {
  293. getLogger().error("Error while running tests", e);
  294. }
  295. }
  296. /**
  297. * Compare files.
  298. * @param f1 first file
  299. * @param f2 second file
  300. * @return true if equal
  301. */
  302. protected boolean compareFiles(File f1, File f2) {
  303. if (f1.length() != f2.length()) {
  304. return false;
  305. }
  306. try {
  307. InputStream is1 = new java.io.BufferedInputStream(new java.io.FileInputStream(f1));
  308. InputStream is2 = new java.io.BufferedInputStream(new java.io.FileInputStream(f2));
  309. while (true) {
  310. int ch1 = is1.read();
  311. int ch2 = is2.read();
  312. if (ch1 == ch2) {
  313. if (ch1 == -1) {
  314. return true;
  315. }
  316. } else {
  317. return false;
  318. }
  319. }
  320. } catch (Exception e) {
  321. getLogger().error("Error while comparing files", e);
  322. }
  323. return false;
  324. }
  325. private Node locateResult(Node testcase, String id) {
  326. NodeList cases = testcase.getChildNodes();
  327. for (int count = 0; count < cases.getLength(); count++) {
  328. Node node = cases.item(count);
  329. String nodename = node.getNodeName();
  330. if (nodename.equals("result")) {
  331. String resultid =
  332. node.getAttributes().getNamedItem("id").getNodeValue();
  333. if (id.equals(resultid)) {
  334. return node;
  335. }
  336. }
  337. }
  338. return null;
  339. }
  340. }