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

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