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.

PDFAConformanceTestCase.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright 2006 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.render.pdf;
  18. import java.io.File;
  19. import javax.xml.transform.Source;
  20. import javax.xml.transform.Transformer;
  21. import javax.xml.transform.TransformerFactory;
  22. import javax.xml.transform.sax.SAXResult;
  23. import javax.xml.transform.stream.StreamSource;
  24. import org.apache.commons.io.FileUtils;
  25. import org.apache.commons.io.output.ByteArrayOutputStream;
  26. import org.apache.fop.apps.FOUserAgent;
  27. import org.apache.fop.apps.Fop;
  28. import org.apache.fop.apps.FopFactory;
  29. import org.apache.fop.apps.MimeConstants;
  30. import org.apache.fop.pdf.PDFConformanceException;
  31. import junit.framework.TestCase;
  32. /**
  33. * Tests PDF/A-1 functionality.
  34. */
  35. public class PDFAConformanceTestCase extends TestCase {
  36. private TransformerFactory tFactory = TransformerFactory.newInstance();
  37. private FopFactory fopFactory = FopFactory.newInstance();
  38. private File foBaseDir = new File("test/xml/pdf-a");
  39. /**
  40. * Main constructor
  41. * @param name the name of the test case
  42. */
  43. public PDFAConformanceTestCase(String name) {
  44. super(name);
  45. try {
  46. fopFactory.setUserConfig(new File("test/test.xconf"));
  47. } catch (Exception e) {
  48. throw new RuntimeException("Configuring the FopFactory failed: " + e.getMessage());
  49. }
  50. }
  51. /**
  52. * Convert the test file
  53. * @param foFile the FO file
  54. * @param ua the preconfigured user agent
  55. * @throws Exception if the conversion fails
  56. */
  57. protected void convertFO(File foFile, FOUserAgent ua) throws Exception {
  58. ua.getRendererOptions().put("pdf-a-mode", "PDF/A-1b");
  59. File outFile = null;
  60. ByteArrayOutputStream baout = new ByteArrayOutputStream();
  61. Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, ua, baout);
  62. Transformer transformer = tFactory.newTransformer();
  63. Source src = new StreamSource(foFile);
  64. SAXResult res = new SAXResult(fop.getDefaultHandler());
  65. transformer.transform(src, res);
  66. if (true) {
  67. //Write to file for debugging
  68. outFile = new File(foFile.getParentFile(), foFile.getName() + ".pdf");
  69. FileUtils.writeByteArrayToFile(outFile, baout.toByteArray());
  70. }
  71. }
  72. /**
  73. * Test exception when PDF/A-1 is enabled and everything is as it should.
  74. * @throws Exception if the test fails
  75. */
  76. public void testAllOk() throws Exception {
  77. FOUserAgent ua = fopFactory.newFOUserAgent();
  78. File foFile = new File(foBaseDir, "minimal-pdf-a.fo");
  79. convertFO(foFile, ua);
  80. }
  81. /**
  82. * Test exception when PDF/A-1 is enabled together with encryption.
  83. * @throws Exception if the test fails
  84. */
  85. public void testNoEncryption() throws Exception {
  86. FOUserAgent ua = fopFactory.newFOUserAgent();
  87. ua.getRendererOptions().put("owner-password", "mypassword"); //To enabled encryption
  88. File foFile = new File(foBaseDir, "minimal-pdf-a.fo");
  89. try {
  90. convertFO(foFile, ua);
  91. fail("Expected PDFConformanceException. PDF/A-1 and PDF encryption don't go together.");
  92. } catch (PDFConformanceException e) {
  93. //Good!
  94. }
  95. }
  96. /**
  97. * Test exception when PDF/A-1 is enabled and a font is used which is not embedded.
  98. * @throws Exception if the test fails
  99. */
  100. public void testFontNotEmbedded() throws Exception {
  101. FOUserAgent ua = fopFactory.newFOUserAgent();
  102. File foFile = new File(foBaseDir, "base14-font.fo");
  103. try {
  104. convertFO(foFile, ua);
  105. fail("Expected PDFConformanceException. PDF/A-1 wants all fonts embedded.");
  106. } catch (PDFConformanceException e) {
  107. //Good!
  108. }
  109. }
  110. /**
  111. * Test exception when PDF/A-1 is enabled and an EPS is used.
  112. * @throws Exception if the test fails
  113. */
  114. public void testEPSUsage() throws Exception {
  115. FOUserAgent ua = fopFactory.newFOUserAgent();
  116. File foFile = new File(foBaseDir, "with-eps.fo");
  117. try {
  118. convertFO(foFile, ua);
  119. fail("Expected PDFConformanceException. PDF/A-1 does not allow PostScript XObjects.");
  120. } catch (PDFConformanceException e) {
  121. //Good!
  122. }
  123. }
  124. /**
  125. * Test exception when PDF/A-1 is enabled and images.
  126. * @throws Exception if the test fails
  127. */
  128. public void testImages() throws Exception {
  129. FOUserAgent ua = fopFactory.newFOUserAgent();
  130. File foFile = new File(foBaseDir, "with-rgb-images.fo");
  131. convertFO(foFile, ua);
  132. foFile = new File(foBaseDir, "with-cmyk-images.fo");
  133. try {
  134. convertFO(foFile, ua);
  135. fail("Expected PDFConformanceException. PDF/A-1 does not allow PostScript XObjects.");
  136. } catch (PDFConformanceException e) {
  137. //Good!
  138. }
  139. }
  140. }