Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

AbstractIntermediateTest.java 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.intermediate;
  19. import java.io.BufferedOutputStream;
  20. import java.io.File;
  21. import java.io.FileOutputStream;
  22. import java.io.IOException;
  23. import java.io.OutputStream;
  24. import javax.xml.transform.ErrorListener;
  25. import javax.xml.transform.Source;
  26. import javax.xml.transform.Templates;
  27. import javax.xml.transform.Transformer;
  28. import javax.xml.transform.TransformerException;
  29. import javax.xml.transform.dom.DOMSource;
  30. import org.custommonkey.xmlunit.XMLAssert;
  31. import org.junit.After;
  32. import org.junit.Before;
  33. import org.w3c.dom.Document;
  34. import org.xml.sax.SAXException;
  35. import org.apache.commons.io.IOUtils;
  36. import org.apache.commons.io.output.NullOutputStream;
  37. import org.apache.fop.apps.FOUserAgent;
  38. import org.apache.fop.apps.FopFactory;
  39. import org.apache.fop.apps.MimeConstants;
  40. import org.apache.fop.events.model.EventSeverity;
  41. import org.apache.fop.util.ConsoleEventListenerForTests;
  42. /**
  43. * Abstract base class for intermediate format tests.
  44. */
  45. public abstract class AbstractIntermediateTest {
  46. /** the test environment */
  47. protected static TestAssistant testAssistant = new TestAssistant();
  48. /** the FOP factory */
  49. protected FopFactory fopFactory;
  50. /** the directory containing the tests */
  51. protected File testDir = new File("test/layoutengine/standard-testcases");
  52. /** the output directory for any files generated by the tests */
  53. protected File outputDir;
  54. /** the test file */
  55. protected File testFile;
  56. /** the test document as DOM */
  57. protected Document testDoc;
  58. /** the intermediate format document as DOM */
  59. protected Document intermediate;
  60. /**
  61. * Constructor for the test suite that is used for each test file.
  62. * @param testFile the test file to run
  63. * @throws IOException if an I/O error occurs while loading the test case
  64. */
  65. public AbstractIntermediateTest(File testFile)
  66. throws IOException {
  67. this.testFile = testFile;
  68. }
  69. @Before
  70. public void setUp() throws Exception {
  71. setupOutputDirectory();
  72. this.testDoc = testAssistant.loadTestCase(testFile);
  73. this.fopFactory = testAssistant.getFopFactory(testDoc);
  74. intermediate = buildIntermediateDocument(testAssistant.getTestcase2FOStylesheet());
  75. if (outputDir != null) {
  76. testAssistant.saveDOM(intermediate, new File(outputDir,
  77. testFile.getName() + ".1" + getIntermediateFileExtension()));
  78. }
  79. }
  80. @After
  81. public void tearDown() throws Exception {
  82. //Release memory
  83. this.intermediate = null;
  84. this.fopFactory = null;
  85. this.testDoc = null;
  86. }
  87. /**
  88. * Returns the file extension for the intermediate file format.
  89. * @return the file extension
  90. */
  91. protected abstract String getIntermediateFileExtension();
  92. /**
  93. * Returns the MIME type for which to test or to mimic for the intermediate format.
  94. * @return the MIME type
  95. */
  96. protected String getTargetMIME() {
  97. return MimeConstants.MIME_PDF;
  98. }
  99. /**
  100. * Validates the intermediate format file.
  101. * @param doc the intermediate file
  102. * @throws IOException if an IO error occurs while loading the schema
  103. * @throws SAXException if a SAX-related exception (including a validation error) occurs
  104. */
  105. protected void validate(Document doc) throws SAXException, IOException {
  106. //nop by default
  107. }
  108. /**
  109. * Builds an intermediate format document from a source file.
  110. * @param templates the (optional) stylesheet
  111. * @return the intermediate format document as a DOM
  112. * @throws Exception if an error occurs while processing the document
  113. */
  114. protected abstract Document buildIntermediateDocument(Templates templates) throws Exception;
  115. /**
  116. * Creates a new FOP user agent.
  117. * @return the user agent
  118. */
  119. protected FOUserAgent createUserAgent() {
  120. FOUserAgent userAgent = fopFactory.newFOUserAgent();
  121. userAgent.getEventBroadcaster().addEventListener(
  122. new ConsoleEventListenerForTests(testFile.getName(), EventSeverity.FATAL));
  123. return userAgent;
  124. }
  125. /**
  126. * Sets up the output directory.
  127. */
  128. protected void setupOutputDirectory() {
  129. String s = System.getProperty("fop.intermediate.outdir");
  130. if (s != null && s.length() > 0) {
  131. outputDir = new File(s);
  132. outputDir.mkdirs();
  133. }
  134. }
  135. /**
  136. * Tests the area tree parser by running the parsed area tree again through the area tree
  137. * renderer. The source and result documents are compared to each other.
  138. * @throws Exception if the test fails
  139. */
  140. public void testParserToIntermediateFormat() throws Exception {
  141. validate(intermediate);
  142. Source src = new DOMSource(intermediate);
  143. Document doc = parseAndRenderToIntermediateFormat(src);
  144. if (outputDir != null) {
  145. File tgtFile = new File(outputDir, testFile.getName() + ".2"
  146. + getIntermediateFileExtension());
  147. testAssistant.saveDOM(doc, tgtFile);
  148. }
  149. XMLAssert.assertXMLEqual(intermediate, doc);
  150. }
  151. /**
  152. * Parses the intermediate file and renders it back to the intermediate format.
  153. * @param src the source for the intermediate file
  154. * @return a DOM Document with the re-created intermediate file
  155. * @throws Exception if an error occurs while processing the document
  156. */
  157. protected abstract Document parseAndRenderToIntermediateFormat(Source src) throws Exception;
  158. /**
  159. * Tests the area tree parser by sending the parsed area tree to the PDF Renderer. Some
  160. * errors might be caught by the PDFRenderer.
  161. * @throws Exception if the test fails
  162. */
  163. public void testParserToPDF() throws Exception {
  164. OutputStream out;
  165. if (outputDir != null) {
  166. File tgtFile = new File(outputDir, testFile.getName() + ".pdf");
  167. out = new FileOutputStream(tgtFile);
  168. out = new BufferedOutputStream(out);
  169. } else {
  170. out = new NullOutputStream();
  171. }
  172. try {
  173. Source src = new DOMSource(intermediate);
  174. parseAndRender(src, out);
  175. } finally {
  176. IOUtils.closeQuietly(out);
  177. }
  178. }
  179. /**
  180. * Parses and renders an intermediate format document to a final format.
  181. * @param src the source document
  182. * @param out the target output stream
  183. * @throws Exception if an error occurs while rendering the document
  184. */
  185. protected abstract void parseAndRender(Source src, OutputStream out)
  186. throws Exception;
  187. /**
  188. * Run the test.
  189. *
  190. * @throws Exception if an error occurs during the test
  191. */
  192. public abstract void runTest() throws Exception;
  193. /**
  194. * Sets an error listener which doesn't swallow errors like Xalan's default one.
  195. * @param transformer the transformer to set the error listener on
  196. */
  197. protected void setErrorListener(Transformer transformer) {
  198. transformer.setErrorListener(new ErrorListener() {
  199. public void error(TransformerException exception) throws TransformerException {
  200. throw exception;
  201. }
  202. public void fatalError(TransformerException exception) throws TransformerException {
  203. throw exception;
  204. }
  205. public void warning(TransformerException exception) throws TransformerException {
  206. //ignore
  207. }
  208. });
  209. }
  210. }