Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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