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

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