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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * $Id$
  3. * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
  4. * For details on use and redistribution please refer to the
  5. * LICENSE file included with these sources.
  6. */
  7. package org.apache.fop.tools;
  8. import org.apache.fop.apps.*;
  9. import org.apache.fop.configuration.*;
  10. import org.apache.avalon.framework.logger.ConsoleLogger;
  11. import org.apache.avalon.framework.logger.AbstractLogEnabled;
  12. import java.io.*;
  13. import java.util.*;
  14. import javax.xml.parsers.*;
  15. import org.w3c.dom.*;
  16. import org.xml.sax.XMLReader;
  17. import org.xml.sax.InputSource;
  18. import org.xml.sax.SAXException;
  19. import org.xml.sax.SAXParseException;
  20. /**
  21. * TestConverter is used to process a set of tests specified in
  22. * a testsuite.
  23. * This class retrieves the data in the testsuite and uses FOP
  24. * to convert the xml and xsl file into either an xml representation
  25. * of the area tree or a pdf document.
  26. * The area tree can be used for automatic comparisons between different
  27. * versions of FOP or the pdf can be view for manual checking and
  28. * pdf rendering.
  29. *
  30. * Modified by Mark Lillywhite mark-fop@inomial.com to use the new Driver
  31. * interface.
  32. */
  33. public class TestConverter extends AbstractLogEnabled {
  34. boolean failOnly = false;
  35. boolean outputPDF = false;
  36. File destdir;
  37. File compare = null;
  38. String baseDir = "./";
  39. HashMap differ = new HashMap();
  40. /**
  41. * This main method can be used to run the test converter from
  42. * the command line.
  43. * This will take a specified testsuite xml and process all
  44. * tests in it.
  45. * The command line options are:
  46. * -b to set the base directory for where the testsuite and associated files are
  47. * -failOnly to process only the tests which are specified as fail in the test results
  48. * -pdf to output the result as pdf
  49. */
  50. public static void main(String[] args) {
  51. if (args == null || args.length == 0) {
  52. System.out.println("test suite file name required");
  53. }
  54. TestConverter tc = new TestConverter();
  55. tc.enableLogging(new ConsoleLogger(ConsoleLogger.LEVEL_ERROR));
  56. String testFile = null;
  57. for (int count = 0; count < args.length; count++) {
  58. if (args[count].equals("-failOnly")) {
  59. tc.setFailOnly(true);
  60. } else if (args[count].equals("-pdf")) {
  61. tc.setOutputPDF(true);
  62. } else if (args[count].equals("-b")) {
  63. tc.setBaseDir(args[count + 1]);
  64. } else {
  65. testFile = args[count];
  66. }
  67. }
  68. if (testFile == null) {
  69. System.out.println("test suite file name required");
  70. }
  71. tc.runTests(testFile, "results", null);
  72. }
  73. public void setOutputPDF(boolean pdf) {
  74. outputPDF = pdf;
  75. }
  76. public void setFailOnly(boolean fail) {
  77. failOnly = fail;
  78. }
  79. public void setBaseDir(String str) {
  80. baseDir = str;
  81. }
  82. /**
  83. * Run the Tests.
  84. * This runs the tests specified in the xml file fname.
  85. * The document is read as a dom and each testcase is covered.
  86. */
  87. public HashMap runTests(String fname, String dest, String compDir) {
  88. getLogger().debug("running tests in file:" + fname);
  89. try {
  90. if (compDir != null) {
  91. compare = new File(baseDir + "/" + compDir);
  92. }
  93. destdir = new File(baseDir + "/" + dest);
  94. destdir.mkdirs();
  95. File f = new File(baseDir + "/" + fname);
  96. DocumentBuilderFactory factory =
  97. DocumentBuilderFactory.newInstance();
  98. DocumentBuilder db = factory.newDocumentBuilder();
  99. Document doc = db.parse(f);
  100. NodeList suitelist = doc.getChildNodes();
  101. if (suitelist.getLength() == 0) {
  102. return differ;
  103. }
  104. Node testsuite = null;
  105. testsuite = doc.getDocumentElement();
  106. if (testsuite.hasAttributes()) {
  107. String profile =
  108. testsuite.getAttributes().getNamedItem("profile").getNodeValue();
  109. getLogger().debug("testing test suite:" + profile);
  110. }
  111. NodeList testcases = testsuite.getChildNodes();
  112. for (int count = 0; count < testcases.getLength(); count++) {
  113. Node testcase = testcases.item(count);
  114. if (testcase.getNodeName().equals("testcases")) {
  115. runTestCase(testcase);
  116. }
  117. }
  118. } catch (Exception e) {
  119. getLogger().error("Error while running tests", e);
  120. }
  121. return differ;
  122. }
  123. /**
  124. * Run a test case.
  125. * This goes through a test case in the document.
  126. * A testcase can contain a test, a result or more test cases.
  127. * A test case is handled recursively otherwise the test is run.
  128. */
  129. protected void runTestCase(Node tcase) {
  130. if (tcase.hasAttributes()) {
  131. String profile =
  132. tcase.getAttributes().getNamedItem("profile").getNodeValue();
  133. getLogger().debug("testing profile:" + profile);
  134. }
  135. NodeList cases = tcase.getChildNodes();
  136. for (int count = 0; count < cases.getLength(); count++) {
  137. Node node = cases.item(count);
  138. String nodename = node.getNodeName();
  139. if (nodename.equals("testcases")) {
  140. runTestCase(node);
  141. } else if (nodename.equals("test")) {
  142. runTest(tcase, node);
  143. } else if (nodename.equals("result")) {}
  144. }
  145. }
  146. /**
  147. * Run a particular test.
  148. * This runs a test defined by the xml and xsl documents.
  149. * If the test has a result specified it is checked.
  150. * This creates an XSLTInputHandler to provide the input
  151. * for FOP and writes the data out to an XML are tree.
  152. */
  153. protected void runTest(Node testcase, Node test) {
  154. String id = test.getAttributes().getNamedItem("id").getNodeValue();
  155. Node result = locateResult(testcase, id);
  156. boolean pass = false;
  157. if (result != null) {
  158. String agreement =
  159. result.getAttributes().getNamedItem("agreement").getNodeValue();
  160. pass = agreement.equals("full");
  161. }
  162. if (pass && failOnly) {
  163. return;
  164. }
  165. String xml = test.getAttributes().getNamedItem("xml").getNodeValue();
  166. Node xslNode = test.getAttributes().getNamedItem("xsl");
  167. String xsl = null;
  168. if (xslNode != null) {
  169. xsl = xslNode.getNodeValue();
  170. }
  171. getLogger().debug("converting xml:" + xml + " and xsl:" +
  172. xsl + " to area tree");
  173. try {
  174. File xmlFile = new File(baseDir + "/" + xml);
  175. try {
  176. Configuration.put("baseDir",
  177. xmlFile.getParentFile().toURL().toExternalForm());
  178. } catch (Exception e) {
  179. getLogger().error("Error setting base directory");
  180. }
  181. InputHandler inputHandler = null;
  182. if (xsl == null) {
  183. inputHandler = new FOInputHandler(xmlFile);
  184. } else {
  185. inputHandler = new XSLTInputHandler(xmlFile,
  186. new File(baseDir + "/"
  187. + xsl));
  188. }
  189. XMLReader parser = inputHandler.getParser();
  190. setParserFeatures(parser);
  191. Driver driver = new Driver();
  192. setupLogger(driver, "fop");
  193. driver.initialize();
  194. if (outputPDF) {
  195. driver.setRenderer(Driver.RENDER_PDF);
  196. } else {
  197. driver.setRenderer(Driver.RENDER_XML);
  198. }
  199. HashMap rendererOptions = new HashMap();
  200. rendererOptions.put("fineDetail", new Boolean(false));
  201. rendererOptions.put("consistentOutput", new Boolean(true));
  202. driver.getRenderer().setOptions(rendererOptions);
  203. driver.getRenderer().setProducer("Testsuite Converter");
  204. String outname = xmlFile.getName();
  205. if (outname.endsWith(".xml")) {
  206. outname = outname.substring(0, outname.length() - 4);
  207. }
  208. driver.setOutputStream(new BufferedOutputStream(
  209. new FileOutputStream(new File(destdir,
  210. outname + (outputPDF ? ".pdf" : ".at.xml")))));
  211. getLogger().debug("ddir:" + destdir + " on:" + outname + ".pdf");
  212. driver.render(parser, inputHandler.getInputSource());
  213. // check difference
  214. if (compare != null) {
  215. File f1 = new File(destdir, outname + ".at.xml");
  216. File f2 = new File(compare, outname + ".at.xml");
  217. if (!compareFiles(f1, f2)) {
  218. differ.put(outname + ".at.xml", new Boolean(pass));
  219. }
  220. }
  221. } catch (Exception e) {
  222. getLogger().error("Error while running tests", e);
  223. }
  224. }
  225. /**
  226. * Compare files.
  227. * Returns true if equal.
  228. */
  229. protected boolean compareFiles(File f1, File f2) {
  230. if(f1.length() != f2.length()) {
  231. return false;
  232. }
  233. try {
  234. InputStream is1 = new BufferedInputStream(new FileInputStream(f1));
  235. InputStream is2 = new BufferedInputStream(new FileInputStream(f2));
  236. while (true) {
  237. int ch1 = is1.read();
  238. int ch2 = is2.read();
  239. if (ch1 == ch2) {
  240. if (ch1 == -1) {
  241. return true;
  242. }
  243. } else {
  244. return false;
  245. }
  246. }
  247. } catch (Exception e) {}
  248. return false;
  249. }
  250. public void setParserFeatures(XMLReader parser) throws FOPException {
  251. try {
  252. parser.setFeature("http://xml.org/sax/features/namespace-prefixes",
  253. true);
  254. } catch (SAXException e) {
  255. throw new FOPException("Error in setting up parser feature namespace-prefixes\n"
  256. + "You need a parser which supports SAX version 2", e);
  257. }
  258. }
  259. protected Node locateResult(Node testcase, String id) {
  260. NodeList cases = testcase.getChildNodes();
  261. for (int count = 0; count < cases.getLength(); count++) {
  262. Node node = cases.item(count);
  263. String nodename = node.getNodeName();
  264. if (nodename.equals("result")) {
  265. String resultid =
  266. node.getAttributes().getNamedItem("id").getNodeValue();
  267. if (id.equals(resultid)) {
  268. return node;
  269. }
  270. }
  271. }
  272. return null;
  273. }
  274. }