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.

AbstractIntermediateTest.java 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 java.net.MalformedURLException;
  25. import javax.xml.transform.ErrorListener;
  26. import javax.xml.transform.Source;
  27. import javax.xml.transform.Templates;
  28. import javax.xml.transform.Transformer;
  29. import javax.xml.transform.TransformerException;
  30. import javax.xml.transform.dom.DOMSource;
  31. import org.custommonkey.xmlunit.XMLAssert;
  32. import org.junit.After;
  33. import org.junit.Before;
  34. import org.w3c.dom.Document;
  35. import org.xml.sax.SAXException;
  36. import org.apache.commons.io.IOUtils;
  37. import org.apache.commons.io.output.NullOutputStream;
  38. import org.apache.fop.apps.FOUserAgent;
  39. import org.apache.fop.apps.FopFactory;
  40. import org.apache.fop.apps.MimeConstants;
  41. import org.apache.fop.events.model.EventSeverity;
  42. import org.apache.fop.util.ConsoleEventListenerForTests;
  43. /**
  44. * Abstract base class for intermediate format tests.
  45. */
  46. public abstract class AbstractIntermediateTest {
  47. /** the test environment */
  48. protected static TestAssistant testAssistant = new TestAssistant();
  49. /** the FOP factory */
  50. protected FopFactory fopFactory;
  51. /** the directory containing the tests */
  52. protected File testDir = new File("test/layoutengine/standard-testcases");
  53. /** the output directory for any files generated by the tests */
  54. protected File outputDir;
  55. /** the test file */
  56. protected File testFile;
  57. /** the test document as DOM */
  58. protected Document testDoc;
  59. /** the intermediate format document as DOM */
  60. protected Document intermediate;
  61. /**
  62. * Constructor for the test suite that is used for each test file.
  63. * @param testFile the test file to run
  64. * @throws IOException if an I/O error occurs while loading the test case
  65. */
  66. public AbstractIntermediateTest(File testFile)
  67. throws IOException {
  68. this.testFile = testFile;
  69. }
  70. @Before
  71. public void setUp() throws Exception {
  72. setupOutputDirectory();
  73. this.testDoc = testAssistant.loadTestCase(testFile);
  74. this.fopFactory = testAssistant.getFopFactory(testDoc);
  75. intermediate = buildIntermediateDocument(testAssistant.getTestcase2FOStylesheet());
  76. if (outputDir != null) {
  77. testAssistant.saveDOM(intermediate, new File(outputDir,
  78. testFile.getName() + ".1" + getIntermediateFileExtension()));
  79. }
  80. }
  81. @After
  82. public void tearDown() throws Exception {
  83. //Release memory
  84. this.intermediate = null;
  85. this.fopFactory = null;
  86. this.testDoc = null;
  87. }
  88. /**
  89. * Returns the file extension for the intermediate file format.
  90. * @return the file extension
  91. */
  92. protected abstract String getIntermediateFileExtension();
  93. /**
  94. * Returns the MIME type for which to test or to mimic for the intermediate format.
  95. * @return the MIME type
  96. */
  97. protected String getTargetMIME() {
  98. return MimeConstants.MIME_PDF;
  99. }
  100. /**
  101. * Validates the intermediate format file.
  102. * @param doc the intermediate file
  103. * @throws IOException if an IO error occurs while loading the schema
  104. * @throws SAXException if a SAX-related exception (including a validation error) occurs
  105. */
  106. protected void validate(Document doc) throws SAXException, IOException {
  107. //nop by default
  108. }
  109. /**
  110. * Builds an intermediate format document from a source file.
  111. * @param templates the (optional) stylesheet
  112. * @return the intermediate format document as a DOM
  113. * @throws Exception if an error occurs while processing the document
  114. */
  115. protected abstract Document buildIntermediateDocument(Templates templates) throws Exception;
  116. /**
  117. * Creates a new FOP user agent.
  118. * @return the user agent
  119. */
  120. protected FOUserAgent createUserAgent() {
  121. FOUserAgent userAgent = fopFactory.newFOUserAgent();
  122. try {
  123. userAgent.setBaseURL(testDir.toURI().toURL().toExternalForm());
  124. userAgent.getEventBroadcaster().addEventListener(
  125. new ConsoleEventListenerForTests(testFile.getName(), EventSeverity.FATAL));
  126. } catch (MalformedURLException e) {
  127. // Shouldn't happen
  128. throw new AssertionError();
  129. }
  130. return userAgent;
  131. }
  132. /**
  133. * Sets up the output directory.
  134. */
  135. protected void setupOutputDirectory() {
  136. String s = System.getProperty("fop.intermediate.outdir");
  137. if (s != null && s.length() > 0) {
  138. outputDir = new File(s);
  139. outputDir.mkdirs();
  140. }
  141. }
  142. /**
  143. * Tests the area tree parser by running the parsed area tree again through the area tree
  144. * renderer. The source and result documents are compared to each other.
  145. * @throws Exception if the test fails
  146. */
  147. public void testParserToIntermediateFormat() throws Exception {
  148. validate(intermediate);
  149. Source src = new DOMSource(intermediate);
  150. Document doc = parseAndRenderToIntermediateFormat(src);
  151. if (outputDir != null) {
  152. File tgtFile = new File(outputDir, testFile.getName() + ".2"
  153. + getIntermediateFileExtension());
  154. testAssistant.saveDOM(doc, tgtFile);
  155. }
  156. XMLAssert.assertXMLEqual(intermediate, doc);
  157. }
  158. /**
  159. * Parses the intermediate file and renders it back to the intermediate format.
  160. * @param src the source for the intermediate file
  161. * @return a DOM Document with the re-created intermediate file
  162. * @throws Exception if an error occurs while processing the document
  163. */
  164. protected abstract Document parseAndRenderToIntermediateFormat(Source src) throws Exception;
  165. /**
  166. * Tests the area tree parser by sending the parsed area tree to the PDF Renderer. Some
  167. * errors might be caught by the PDFRenderer.
  168. * @throws Exception if the test fails
  169. */
  170. public void testParserToPDF() throws Exception {
  171. OutputStream out;
  172. if (outputDir != null) {
  173. File tgtFile = new File(outputDir, testFile.getName() + ".pdf");
  174. out = new FileOutputStream(tgtFile);
  175. out = new BufferedOutputStream(out);
  176. } else {
  177. out = new NullOutputStream();
  178. }
  179. try {
  180. Source src = new DOMSource(intermediate);
  181. parseAndRender(src, out);
  182. } finally {
  183. IOUtils.closeQuietly(out);
  184. }
  185. }
  186. /**
  187. * Parses and renders an intermediate format document to a final format.
  188. * @param src the source document
  189. * @param out the target output stream
  190. * @throws Exception if an error occurs while rendering the document
  191. */
  192. protected abstract void parseAndRender(Source src, OutputStream out)
  193. throws Exception;
  194. /**
  195. * Run the test.
  196. *
  197. * @throws Exception if an error occurs during the test
  198. */
  199. public abstract void runTest() throws Exception;
  200. /**
  201. * Sets an error listener which doesn't swallow errors like Xalan's default one.
  202. * @param transformer the transformer to set the error listener on
  203. */
  204. protected void setErrorListener(Transformer transformer) {
  205. transformer.setErrorListener(new ErrorListener() {
  206. public void error(TransformerException exception) throws TransformerException {
  207. throw exception;
  208. }
  209. public void fatalError(TransformerException exception) throws TransformerException {
  210. throw exception;
  211. }
  212. public void warning(TransformerException exception) throws TransformerException {
  213. //ignore
  214. }
  215. });
  216. }
  217. }