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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.tools;
  18. import java.io.File;
  19. import java.io.InputStream;
  20. import java.io.OutputStream;
  21. import java.util.Map;
  22. import javax.xml.parsers.DocumentBuilder;
  23. import javax.xml.parsers.DocumentBuilderFactory;
  24. import org.apache.avalon.framework.logger.AbstractLogEnabled;
  25. import org.apache.avalon.framework.logger.ConsoleLogger;
  26. import org.apache.fop.apps.Driver;
  27. import org.apache.fop.apps.FOFileHandler;
  28. import org.apache.fop.apps.FOUserAgent;
  29. import org.apache.fop.apps.InputHandler;
  30. import org.apache.fop.apps.XSLTInputHandler;
  31. import org.w3c.dom.Document;
  32. import org.w3c.dom.Node;
  33. import org.w3c.dom.NodeList;
  34. /**
  35. * TestConverter is used to process a set of tests specified in
  36. * a testsuite.
  37. * This class retrieves the data in the testsuite and uses FOP
  38. * to convert the xml and xsl file into either an xml representation
  39. * of the area tree or a pdf document.
  40. * The area tree can be used for automatic comparisons between different
  41. * versions of FOP or the pdf can be view for manual checking and
  42. * pdf rendering.
  43. *
  44. * Modified by Mark Lillywhite mark-fop@inomial.com to use the new Driver
  45. * interface.
  46. */
  47. public class TestConverter extends AbstractLogEnabled {
  48. private boolean failOnly = false;
  49. private boolean outputPDF = false;
  50. private File destdir;
  51. private File compare = null;
  52. private String baseDir = "./";
  53. private Map differ = new java.util.HashMap();
  54. /**
  55. * This main method can be used to run the test converter from
  56. * the command line.
  57. * This will take a specified testsuite xml and process all
  58. * tests in it.
  59. * The command line options are:
  60. * -b to set the base directory for where the testsuite and associated files are
  61. * -failOnly to process only the tests which are specified as fail in the test results
  62. * -pdf to output the result as pdf
  63. * @param args command-line arguments
  64. */
  65. public static void main(String[] args) {
  66. if (args == null || args.length == 0) {
  67. System.out.println("test suite file name required");
  68. }
  69. TestConverter tc = new TestConverter();
  70. String results = "results";
  71. String testFile = null;
  72. for (int count = 0; count < args.length; count++) {
  73. if (args[count].equals("-failOnly")) {
  74. tc.setFailOnly(true);
  75. } else if (args[count].equals("-pdf")) {
  76. tc.setOutputPDF(true);
  77. } else if (args[count].equals("-d")) {
  78. tc.setDebug(true);
  79. } else if (args[count].equals("-b")) {
  80. tc.setBaseDir(args[++count]);
  81. } else if (args[count].equals("-results")) {
  82. results = args[++count];
  83. } else {
  84. testFile = args[count];
  85. }
  86. }
  87. if (testFile == null) {
  88. System.out.println("test suite file name required");
  89. }
  90. tc.runTests(testFile, results, null);
  91. }
  92. /**
  93. * Construct a new TestConverter
  94. */
  95. public TestConverter() {
  96. enableLogging(new ConsoleLogger(ConsoleLogger.LEVEL_ERROR));
  97. }
  98. /**
  99. * Controls whether to generate PDF or XML.
  100. * @param pdf If True, PDF is generated, Area Tree XML otherwise.
  101. */
  102. public void setOutputPDF(boolean pdf) {
  103. outputPDF = pdf;
  104. }
  105. /**
  106. * Controls whether to process only the tests which are specified as fail
  107. * in the test results.
  108. * @param fail True if only fail tests should be processed
  109. */
  110. public void setFailOnly(boolean fail) {
  111. failOnly = fail;
  112. }
  113. /**
  114. * Sets the base directory.
  115. * @param str base directory
  116. */
  117. public void setBaseDir(String str) {
  118. baseDir = str;
  119. }
  120. /**
  121. * Controls whether to generate PDF or XML.
  122. * @param pdf If True, PDF is generated, Area Tree XML otherwise.
  123. */
  124. public void setDebug(boolean debug) {
  125. if (debug) {
  126. enableLogging(new ConsoleLogger(ConsoleLogger.LEVEL_DEBUG));
  127. }
  128. }
  129. /**
  130. * Run the Tests.
  131. * This runs the tests specified in the xml file fname.
  132. * The document is read as a dom and each testcase is covered.
  133. * @param fname filename of the input file
  134. * @param dest destination directory
  135. * @param compDir comparison directory
  136. * @return Map a Map containing differences
  137. */
  138. public Map runTests(String fname, String dest, String compDir) {
  139. getLogger().debug("running tests in file:" + fname);
  140. try {
  141. if (compDir != null) {
  142. compare = new File(baseDir + "/" + compDir);
  143. }
  144. destdir = new File(baseDir + "/" + dest);
  145. destdir.mkdirs();
  146. File f = new File(baseDir + "/" + fname);
  147. DocumentBuilderFactory factory =
  148. DocumentBuilderFactory.newInstance();
  149. DocumentBuilder db = factory.newDocumentBuilder();
  150. Document doc = db.parse(f);
  151. NodeList suitelist = doc.getChildNodes();
  152. if (suitelist.getLength() == 0) {
  153. return differ;
  154. }
  155. Node testsuite = null;
  156. testsuite = doc.getDocumentElement();
  157. if (testsuite.hasAttributes()) {
  158. String profile =
  159. testsuite.getAttributes().getNamedItem("profile").getNodeValue();
  160. getLogger().debug("testing test suite:" + profile);
  161. }
  162. NodeList testcases = testsuite.getChildNodes();
  163. for (int count = 0; count < testcases.getLength(); count++) {
  164. Node testcase = testcases.item(count);
  165. if (testcase.getNodeName().equals("testcases")) {
  166. runTestCase(testcase);
  167. }
  168. }
  169. } catch (Exception e) {
  170. getLogger().error("Error while running tests", e);
  171. }
  172. return differ;
  173. }
  174. /**
  175. * Run a test case.
  176. * This goes through a test case in the document.
  177. * A testcase can contain a test, a result or more test cases.
  178. * A test case is handled recursively otherwise the test is run.
  179. * @param tcase Test case node to run
  180. */
  181. protected void runTestCase(Node tcase) {
  182. if (tcase.hasAttributes()) {
  183. String profile =
  184. tcase.getAttributes().getNamedItem("profile").getNodeValue();
  185. getLogger().debug("testing profile:" + profile);
  186. }
  187. NodeList cases = tcase.getChildNodes();
  188. for (int count = 0; count < cases.getLength(); count++) {
  189. Node node = cases.item(count);
  190. String nodename = node.getNodeName();
  191. if (nodename.equals("testcases")) {
  192. runTestCase(node);
  193. } else if (nodename.equals("test")) {
  194. runTest(tcase, node);
  195. } else if (nodename.equals("result")) {
  196. //nop
  197. }
  198. }
  199. }
  200. /**
  201. * Run a particular test.
  202. * This runs a test defined by the xml and xsl documents.
  203. * If the test has a result specified it is checked.
  204. * This creates an XSLTInputHandler to provide the input
  205. * for FOP and writes the data out to an XML are tree.
  206. * @param testcase Test case to run
  207. * @param test Test
  208. */
  209. protected void runTest(Node testcase, Node test) {
  210. String id = test.getAttributes().getNamedItem("id").getNodeValue();
  211. Node result = locateResult(testcase, id);
  212. boolean pass = false;
  213. if (result != null) {
  214. String agreement =
  215. result.getAttributes().getNamedItem("agreement").getNodeValue();
  216. pass = agreement.equals("full");
  217. }
  218. if (pass && failOnly) {
  219. return;
  220. }
  221. String xml = test.getAttributes().getNamedItem("xml").getNodeValue();
  222. Node xslNode = test.getAttributes().getNamedItem("xsl");
  223. String xsl = null;
  224. if (xslNode != null) {
  225. xsl = xslNode.getNodeValue();
  226. }
  227. getLogger().debug("converting xml:" + xml + " and xsl:"
  228. + xsl + " to area tree");
  229. String res = xml;
  230. Node resNode = test.getAttributes().getNamedItem("results");
  231. if (resNode != null) {
  232. res = resNode.getNodeValue();
  233. }
  234. try {
  235. File xmlFile = new File(baseDir + "/" + xml);
  236. String baseURL = null;
  237. try {
  238. baseURL = xmlFile.getParentFile().toURL().toExternalForm();
  239. } catch (Exception e) {
  240. getLogger().error("Error setting base directory");
  241. }
  242. InputHandler inputHandler = null;
  243. if (xsl == null) {
  244. inputHandler = new FOFileHandler(xmlFile);
  245. } else {
  246. inputHandler = new XSLTInputHandler(xmlFile,
  247. new File(baseDir + "/"
  248. + xsl), null);
  249. }
  250. Driver driver = new Driver();
  251. FOUserAgent userAgent = new FOUserAgent();
  252. setupLogger(userAgent, "fop");
  253. userAgent.setBaseURL(baseURL);
  254. driver.setUserAgent(userAgent);
  255. if (outputPDF) {
  256. driver.setRenderer(Driver.RENDER_PDF);
  257. } else {
  258. driver.setRenderer(Driver.RENDER_XML);
  259. }
  260. Map rendererOptions = new java.util.HashMap();
  261. rendererOptions.put("fineDetail", new Boolean(false));
  262. rendererOptions.put("consistentOutput", new Boolean(true));
  263. driver.getRenderer().setOptions(rendererOptions);
  264. driver.getRenderer().setProducer("Testsuite Converter");
  265. String outname = res;
  266. if (outname.endsWith(".xml") || outname.endsWith(".pdf")) {
  267. outname = outname.substring(0, outname.length() - 4);
  268. }
  269. File outputFile = new File(destdir,
  270. outname + (outputPDF ? ".pdf" : ".at.xml"));
  271. outputFile.getParentFile().mkdirs();
  272. OutputStream outStream = new java.io.BufferedOutputStream(
  273. new java.io.FileOutputStream(outputFile));
  274. driver.setOutputStream(outStream);
  275. getLogger().debug("ddir:" + destdir + " on:" +
  276. outputFile.getName());
  277. driver.render(inputHandler);
  278. outStream.close();
  279. // check difference
  280. if (compare != null) {
  281. File f1 = new File(destdir, outname + ".at.xml");
  282. File f2 = new File(compare, outname + ".at.xml");
  283. if (!compareFiles(f1, f2)) {
  284. differ.put(outname + ".at.xml", new Boolean(pass));
  285. }
  286. }
  287. } catch (Exception e) {
  288. getLogger().error("Error while running tests", e);
  289. }
  290. }
  291. /**
  292. * Compare files.
  293. * @param f1 first file
  294. * @param f2 second file
  295. * @return true if equal
  296. */
  297. protected boolean compareFiles(File f1, File f2) {
  298. if (f1.length() != f2.length()) {
  299. return false;
  300. }
  301. try {
  302. InputStream is1 = new java.io.BufferedInputStream(new java.io.FileInputStream(f1));
  303. InputStream is2 = new java.io.BufferedInputStream(new java.io.FileInputStream(f2));
  304. while (true) {
  305. int ch1 = is1.read();
  306. int ch2 = is2.read();
  307. if (ch1 == ch2) {
  308. if (ch1 == -1) {
  309. return true;
  310. }
  311. } else {
  312. return false;
  313. }
  314. }
  315. } catch (Exception e) {
  316. getLogger().error("Error while comparing files", e);
  317. }
  318. return false;
  319. }
  320. private Node locateResult(Node testcase, String id) {
  321. NodeList cases = testcase.getChildNodes();
  322. for (int count = 0; count < cases.getLength(); count++) {
  323. Node node = cases.item(count);
  324. String nodename = node.getNodeName();
  325. if (nodename.equals("result")) {
  326. String resultid =
  327. node.getAttributes().getNamedItem("id").getNodeValue();
  328. if (id.equals(resultid)) {
  329. return node;
  330. }
  331. }
  332. }
  333. return null;
  334. }
  335. }