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.

AbstractRenderingTestCase.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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.render;
  19. import java.io.File;
  20. import java.io.InputStream;
  21. import java.io.OutputStream;
  22. import java.util.Map;
  23. import java.util.MissingResourceException;
  24. import javax.xml.transform.Source;
  25. import javax.xml.transform.Transformer;
  26. import javax.xml.transform.TransformerFactory;
  27. import javax.xml.transform.sax.SAXResult;
  28. import javax.xml.transform.stream.StreamSource;
  29. import org.apache.commons.io.FileUtils;
  30. import org.apache.commons.io.IOUtils;
  31. import org.apache.fop.apps.FOUserAgent;
  32. import org.apache.fop.apps.Fop;
  33. import org.apache.fop.apps.FopFactory;
  34. import org.apache.fop.apps.MimeConstants;
  35. /**
  36. * Abstract base class for rendering (output) verification tests.
  37. */
  38. public abstract class AbstractRenderingTestCase {
  39. private static final Map<String, String> MIME_MAP = new java.util.HashMap<String, String>();
  40. static {
  41. MIME_MAP.put(MimeConstants.MIME_PDF, ".pdf");
  42. MIME_MAP.put(MimeConstants.MIME_POSTSCRIPT, ".ps");
  43. MIME_MAP.put(MimeConstants.MIME_AFP, ".afp");
  44. }
  45. /** the JAXP TransformerFactory */
  46. protected TransformerFactory tFactory = TransformerFactory.newInstance();
  47. /** the FopFactory */
  48. protected FopFactory fopFactory = FopFactory.newInstance();
  49. /**
  50. * Renders a test file.
  51. * @param ua the user agent (with override set!)
  52. * @param resourceName the resource name for the FO file
  53. * @param suffix a suffix for the output filename
  54. * @param outputFormat MIME type of the requested output format
  55. * @return the output file
  56. * @throws Exception if an error occurs
  57. */
  58. protected File renderFile(FOUserAgent ua, String resourceName, String suffix,
  59. String outputFormat) throws Exception {
  60. String extension = MIME_MAP.get(outputFormat);
  61. assert extension != null;
  62. File outputFile = new File("build/test-results/" + resourceName + suffix + extension);
  63. File outputDir = outputFile.getParentFile();
  64. FileUtils.forceMkdir(outputDir);
  65. // Prepare input file
  66. InputStream in = getClass().getResourceAsStream(resourceName);
  67. if (in == null) {
  68. throw new MissingResourceException(resourceName + " not found in resources",
  69. getClass().getName(), null);
  70. }
  71. try {
  72. Source src = new StreamSource(in);
  73. // Create output file
  74. OutputStream out = new java.io.FileOutputStream(outputFile);
  75. out = new java.io.BufferedOutputStream(out);
  76. try {
  77. Fop fop = fopFactory.newFop(outputFormat, ua, out);
  78. SAXResult res = new SAXResult(fop.getDefaultHandler());
  79. Transformer transformer = tFactory.newTransformer();
  80. transformer.transform(src, res);
  81. } finally {
  82. IOUtils.closeQuietly(out);
  83. }
  84. } finally {
  85. IOUtils.closeQuietly(in);
  86. }
  87. return outputFile;
  88. }
  89. }