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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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.fop.apps.Driver;
  25. import org.apache.fop.apps.FOFileHandler;
  26. import org.apache.fop.apps.FOUserAgent;
  27. import org.apache.fop.apps.InputHandler;
  28. import org.apache.fop.apps.XSLTInputHandler;
  29. import org.w3c.dom.Document;
  30. import org.w3c.dom.Node;
  31. import org.w3c.dom.NodeList;
  32. import org.apache.commons.logging.Log;
  33. import org.apache.commons.logging.impl.SimpleLog;
  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 {
  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. * logging instance
  56. */
  57. protected Log logger = null;
  58. /**
  59. * Sets the Commons-Logging instance for this class
  60. * @param logger The Commons-Logging instance
  61. */
  62. public void setLogger(Log logger) {
  63. this.logger = logger;
  64. }
  65. /**
  66. * Returns the Commons-Logging instance for this class
  67. * @return The Commons-Logging instance
  68. */
  69. protected Log getLogger() {
  70. return logger;
  71. }
  72. /**
  73. * This main method can be used to run the test converter from
  74. * the command line.
  75. * This will take a specified testsuite xml and process all
  76. * tests in it.
  77. * The command line options are:
  78. * -b to set the base directory for where the testsuite and associated files are
  79. * -failOnly to process only the tests which are specified as fail in the test results
  80. * -pdf to output the result as pdf
  81. * @param args command-line arguments
  82. */
  83. public static void main(String[] args) {
  84. if (args == null || args.length == 0) {
  85. System.out.println("test suite file name required");
  86. }
  87. TestConverter tc = new TestConverter();
  88. String results = "results";
  89. String testFile = null;
  90. for (int count = 0; count < args.length; count++) {
  91. if (args[count].equals("-failOnly")) {
  92. tc.setFailOnly(true);
  93. } else if (args[count].equals("-pdf")) {
  94. tc.setOutputPDF(true);
  95. } else if (args[count].equals("-d")) {
  96. tc.setDebug(true);
  97. } else if (args[count].equals("-b")) {
  98. tc.setBaseDir(args[++count]);
  99. } else if (args[count].equals("-results")) {
  100. results = args[++count];
  101. } else {
  102. testFile = args[count];
  103. }
  104. }
  105. if (testFile == null) {
  106. System.out.println("test suite file name required");
  107. }
  108. tc.runTests(testFile, results, null);
  109. }
  110. /**
  111. * Construct a new TestConverter
  112. */
  113. public TestConverter() {
  114. SimpleLog log = new SimpleLog("FOP/Test");
  115. log.setLevel(SimpleLog.LOG_LEVEL_ERROR);
  116. setLogger(log);
  117. }
  118. /**
  119. * Controls whether to generate PDF or XML.
  120. * @param pdf If True, PDF is generated, Area Tree XML otherwise.
  121. */
  122. public void setOutputPDF(boolean pdf) {
  123. outputPDF = pdf;
  124. }
  125. /**
  126. * Controls whether to process only the tests which are specified as fail
  127. * in the test results.
  128. * @param fail True if only fail tests should be processed
  129. */
  130. public void setFailOnly(boolean fail) {
  131. failOnly = fail;
  132. }
  133. /**
  134. * Sets the base directory.
  135. * @param str base directory
  136. */
  137. public void setBaseDir(String str) {
  138. baseDir = str;
  139. }
  140. /**
  141. * Controls whether to generate PDF or XML.
  142. * @param pdf If True, PDF is generated, Area Tree XML otherwise.
  143. */
  144. public void setDebug(boolean debug) {
  145. if (debug) {
  146. SimpleLog log = new SimpleLog("FOP/Test");
  147. log.setLevel(SimpleLog.LOG_LEVEL_DEBUG);
  148. setLogger(log);
  149. }
  150. }
  151. /**
  152. * Run the Tests.
  153. * This runs the tests specified in the xml file fname.
  154. * The document is read as a dom and each testcase is covered.
  155. * @param fname filename of the input file
  156. * @param dest destination directory
  157. * @param compDir comparison directory
  158. * @return Map a Map containing differences
  159. */
  160. public Map runTests(String fname, String dest, String compDir) {
  161. logger.debug("running tests in file:" + fname);
  162. try {
  163. if (compDir != null) {
  164. compare = new File(baseDir + "/" + compDir);
  165. }
  166. destdir = new File(baseDir + "/" + dest);
  167. destdir.mkdirs();
  168. File f = new File(baseDir + "/" + fname);
  169. DocumentBuilderFactory factory =
  170. DocumentBuilderFactory.newInstance();
  171. DocumentBuilder db = factory.newDocumentBuilder();
  172. Document doc = db.parse(f);
  173. NodeList suitelist = doc.getChildNodes();
  174. if (suitelist.getLength() == 0) {
  175. return differ;
  176. }
  177. Node testsuite = null;
  178. testsuite = doc.getDocumentElement();
  179. if (testsuite.hasAttributes()) {
  180. String profile =
  181. testsuite.getAttributes().getNamedItem("profile").getNodeValue();
  182. logger.debug("testing test suite:" + profile);
  183. }
  184. NodeList testcases = testsuite.getChildNodes();
  185. for (int count = 0; count < testcases.getLength(); count++) {
  186. Node testcase = testcases.item(count);
  187. if (testcase.getNodeName().equals("testcases")) {
  188. runTestCase(testcase);
  189. }
  190. }
  191. } catch (Exception e) {
  192. logger.error("Error while running tests", e);
  193. }
  194. return differ;
  195. }
  196. /**
  197. * Run a test case.
  198. * This goes through a test case in the document.
  199. * A testcase can contain a test, a result or more test cases.
  200. * A test case is handled recursively otherwise the test is run.
  201. * @param tcase Test case node to run
  202. */
  203. protected void runTestCase(Node tcase) {
  204. if (tcase.hasAttributes()) {
  205. String profile =
  206. tcase.getAttributes().getNamedItem("profile").getNodeValue();
  207. logger.debug("testing profile:" + profile);
  208. }
  209. NodeList cases = tcase.getChildNodes();
  210. for (int count = 0; count < cases.getLength(); count++) {
  211. Node node = cases.item(count);
  212. String nodename = node.getNodeName();
  213. if (nodename.equals("testcases")) {
  214. runTestCase(node);
  215. } else if (nodename.equals("test")) {
  216. runTest(tcase, node);
  217. } else if (nodename.equals("result")) {
  218. //nop
  219. }
  220. }
  221. }
  222. /**
  223. * Run a particular test.
  224. * This runs a test defined by the xml and xsl documents.
  225. * If the test has a result specified it is checked.
  226. * This creates an XSLTInputHandler to provide the input
  227. * for FOP and writes the data out to an XML are tree.
  228. * @param testcase Test case to run
  229. * @param test Test
  230. */
  231. protected void runTest(Node testcase, Node test) {
  232. String id = test.getAttributes().getNamedItem("id").getNodeValue();
  233. Node result = locateResult(testcase, id);
  234. boolean pass = false;
  235. if (result != null) {
  236. String agreement =
  237. result.getAttributes().getNamedItem("agreement").getNodeValue();
  238. pass = agreement.equals("full");
  239. }
  240. if (pass && failOnly) {
  241. return;
  242. }
  243. String xml = test.getAttributes().getNamedItem("xml").getNodeValue();
  244. Node xslNode = test.getAttributes().getNamedItem("xsl");
  245. String xsl = null;
  246. if (xslNode != null) {
  247. xsl = xslNode.getNodeValue();
  248. }
  249. logger.debug("converting xml:" + xml + " and xsl:"
  250. + xsl + " to area tree");
  251. String res = xml;
  252. Node resNode = test.getAttributes().getNamedItem("results");
  253. if (resNode != null) {
  254. res = resNode.getNodeValue();
  255. }
  256. try {
  257. File xmlFile = new File(baseDir + "/" + xml);
  258. String baseURL = null;
  259. try {
  260. baseURL = xmlFile.getParentFile().toURL().toExternalForm();
  261. } catch (Exception e) {
  262. logger.error("Error setting base directory");
  263. }
  264. InputHandler inputHandler = null;
  265. if (xsl == null) {
  266. inputHandler = new FOFileHandler(xmlFile);
  267. } else {
  268. inputHandler = new XSLTInputHandler(xmlFile,
  269. new File(baseDir + "/"
  270. + xsl), null);
  271. }
  272. Driver driver = new Driver();
  273. FOUserAgent userAgent = new FOUserAgent();
  274. userAgent.setBaseURL(baseURL);
  275. driver.setUserAgent(userAgent);
  276. if (outputPDF) {
  277. driver.setRenderer(Driver.RENDER_PDF);
  278. } else {
  279. driver.setRenderer(Driver.RENDER_XML);
  280. }
  281. userAgent.getRendererOptions().put("fineDetail", new Boolean(false));
  282. userAgent.getRendererOptions().put("consistentOutput", new Boolean(true));
  283. driver.getRenderer().setProducer("Testsuite Converter");
  284. String outname = res;
  285. if (outname.endsWith(".xml") || outname.endsWith(".pdf")) {
  286. outname = outname.substring(0, outname.length() - 4);
  287. }
  288. File outputFile = new File(destdir,
  289. outname + (outputPDF ? ".pdf" : ".at.xml"));
  290. outputFile.getParentFile().mkdirs();
  291. OutputStream outStream = new java.io.BufferedOutputStream(
  292. new java.io.FileOutputStream(outputFile));
  293. driver.setOutputStream(outStream);
  294. logger.debug("ddir:" + destdir + " on:" +
  295. outputFile.getName());
  296. driver.render(inputHandler);
  297. outStream.close();
  298. // check difference
  299. if (compare != null) {
  300. File f1 = new File(destdir, outname + ".at.xml");
  301. File f2 = new File(compare, outname + ".at.xml");
  302. if (!compareFiles(f1, f2)) {
  303. differ.put(outname + ".at.xml", new Boolean(pass));
  304. }
  305. }
  306. } catch (Exception e) {
  307. getLogger().error("Error while running tests", e);
  308. }
  309. }
  310. /**
  311. * Compare files.
  312. * @param f1 first file
  313. * @param f2 second file
  314. * @return true if equal
  315. */
  316. protected boolean compareFiles(File f1, File f2) {
  317. if (f1.length() != f2.length()) {
  318. return false;
  319. }
  320. try {
  321. InputStream is1 = new java.io.BufferedInputStream(new java.io.FileInputStream(f1));
  322. InputStream is2 = new java.io.BufferedInputStream(new java.io.FileInputStream(f2));
  323. while (true) {
  324. int ch1 = is1.read();
  325. int ch2 = is2.read();
  326. if (ch1 == ch2) {
  327. if (ch1 == -1) {
  328. return true;
  329. }
  330. } else {
  331. return false;
  332. }
  333. }
  334. } catch (Exception e) {
  335. logger.error("Error while comparing files", e);
  336. }
  337. return false;
  338. }
  339. private Node locateResult(Node testcase, String id) {
  340. NodeList cases = testcase.getChildNodes();
  341. for (int count = 0; count < cases.getLength(); count++) {
  342. Node node = cases.item(count);
  343. String nodename = node.getNodeName();
  344. if (nodename.equals("result")) {
  345. String resultid =
  346. node.getAttributes().getNamedItem("id").getNodeValue();
  347. if (id.equals(resultid)) {
  348. return node;
  349. }
  350. }
  351. }
  352. return null;
  353. }
  354. }