選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

BasePDFTestCase.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.pdf;
  19. import java.io.File;
  20. import javax.xml.transform.Source;
  21. import javax.xml.transform.Transformer;
  22. import javax.xml.transform.TransformerException;
  23. import javax.xml.transform.TransformerFactory;
  24. import javax.xml.transform.sax.SAXResult;
  25. import javax.xml.transform.stream.StreamSource;
  26. import org.xml.sax.SAXException;
  27. import org.apache.commons.io.FileUtils;
  28. import org.apache.commons.io.output.ByteArrayOutputStream;
  29. import org.apache.fop.AbstractFOPTestCase;
  30. import org.apache.fop.apps.FOUserAgent;
  31. import org.apache.fop.apps.Fop;
  32. import org.apache.fop.apps.FopFactory;
  33. import org.apache.fop.apps.MimeConstants;
  34. /**
  35. * Base class for automated tests that create PDF files
  36. */
  37. public class BasePDFTestCase extends AbstractFOPTestCase {
  38. /** the FopFactory */
  39. protected final FopFactory fopFactory = FopFactory.newInstance();
  40. /** the JAXP TransformerFactory */
  41. protected final TransformerFactory tFactory = TransformerFactory.newInstance();
  42. /**
  43. * Main constructor
  44. * @param name the name of the test case
  45. */
  46. protected BasePDFTestCase(String name) {
  47. super(name);
  48. init();
  49. }
  50. /**
  51. * initalizes the test
  52. */
  53. protected void init() {
  54. final File uc = getUserConfigFile();
  55. try {
  56. fopFactory.setUserConfig(uc);
  57. } catch (Exception e) {
  58. throw new RuntimeException("fopFactory.setUserConfig ("
  59. + uc.getAbsolutePath() + ") failed: " + e.getMessage());
  60. }
  61. }
  62. /**
  63. * Convert a test FO file to PDF
  64. * @param foFile the FO file
  65. * @param ua the preconfigured user agent
  66. * @param dumpPdfFile if true, dumps the generated PDF file to a file name (foFile).pdf
  67. * @return the generated PDF data
  68. * @throws Exception if the conversion fails
  69. */
  70. protected byte[] convertFO(File foFile, FOUserAgent ua, boolean dumpPdfFile)
  71. throws Exception {
  72. ByteArrayOutputStream baout = new ByteArrayOutputStream();
  73. Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, ua, baout);
  74. Transformer transformer = tFactory.newTransformer();
  75. Source src = new StreamSource(foFile);
  76. SAXResult res = new SAXResult(fop.getDefaultHandler());
  77. try {
  78. transformer.transform(src, res);
  79. final byte[] result = baout.toByteArray();
  80. if (dumpPdfFile) {
  81. final File outFile = new File(foFile.getParentFile(), foFile.getName() + ".pdf");
  82. FileUtils.writeByteArrayToFile(outFile, result);
  83. }
  84. return result;
  85. } catch (TransformerException e) {
  86. throw extractOriginalException(e);
  87. }
  88. }
  89. protected static Exception extractOriginalException(Exception e) {
  90. if (e.getCause() != null) {
  91. return extractOriginalException((Exception)e.getCause());
  92. } else if (e instanceof SAXException) {
  93. SAXException se = (SAXException)e;
  94. if (se.getException() != null) {
  95. return extractOriginalException(se.getException());
  96. }
  97. }
  98. return e;
  99. }
  100. /**
  101. * get FOP config File
  102. * @return user config file to be used for testing
  103. */
  104. protected File getUserConfigFile() {
  105. return new File("test/test.xconf");
  106. }
  107. }