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

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