Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

PDFRendererConfiguratorTestCase.java 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 java.io.IOException;
  21. import java.io.OutputStream;
  22. import java.net.URI;
  23. import java.util.List;
  24. import java.util.Map;
  25. import org.junit.Test;
  26. import org.xml.sax.SAXException;
  27. import org.apache.xmlgraphics.util.MimeConstants;
  28. import org.apache.fop.apps.AbstractRendererConfiguratorTest;
  29. import org.apache.fop.apps.FopConfBuilder.RendererConfBuilder;
  30. import org.apache.fop.apps.FopFactory;
  31. import org.apache.fop.apps.PDFRendererConfBuilder;
  32. import org.apache.fop.events.Event;
  33. import org.apache.fop.events.EventListener;
  34. import org.apache.fop.pdf.PDFAMode;
  35. import org.apache.fop.pdf.PDFEncryptionParams;
  36. import org.apache.fop.pdf.PDFXMode;
  37. import org.apache.fop.pdf.Version;
  38. import org.apache.fop.render.pdf.PDFRendererConfig.PDFRendererConfigParser;
  39. import static org.junit.Assert.assertEquals;
  40. import static org.junit.Assert.assertTrue;
  41. import static org.mockito.Mockito.mock;
  42. import static org.mockito.Mockito.times;
  43. import static org.mockito.Mockito.verify;
  44. import static org.mockito.Mockito.when;
  45. /**
  46. * Tests that encryption length is properly set up.
  47. */
  48. public class PDFRendererConfiguratorTestCase extends
  49. AbstractRendererConfiguratorTest<PDFRendererConfigurator, PDFRendererConfBuilder> {
  50. private boolean eventTriggered;
  51. private PDFRenderingUtil pdfUtil;
  52. public PDFRendererConfiguratorTestCase() {
  53. super(MimeConstants.MIME_PDF, PDFRendererConfBuilder.class, PDFDocumentHandler.class);
  54. }
  55. @Override
  56. protected PDFRendererConfigurator createConfigurator() {
  57. return new PDFRendererConfigurator(userAgent, new PDFRendererConfigParser());
  58. }
  59. @Override
  60. public void setUpDocumentHandler() {
  61. pdfUtil = new PDFRenderingUtil(userAgent);
  62. when(((PDFDocumentHandler) docHandler).getPDFUtil()).thenReturn(pdfUtil);
  63. }
  64. private void parseConfig(RendererConfBuilder builder, EventListener listener)
  65. throws SAXException, IOException {
  66. parseConfigWithUtil(builder, listener, false);
  67. }
  68. private void parseConfigWithUtil(RendererConfBuilder builder, EventListener listener,
  69. boolean mockUtil) throws SAXException, IOException {
  70. userAgent = FopFactory.newInstance(
  71. new File(".").toURI(), builder.endRendererConfig().build()).newFOUserAgent();
  72. userAgent.getEventBroadcaster().addEventListener(listener);
  73. if (mockUtil) {
  74. this.pdfUtil = mock(PDFRenderingUtil.class);
  75. when(((PDFDocumentHandler) docHandler).getPDFUtil()).thenReturn(pdfUtil);
  76. } else {
  77. setUpDocumentHandler();
  78. }
  79. sut = createConfigurator();
  80. sut.configure(docHandler);
  81. }
  82. private void parseConfigMockUtil(RendererConfBuilder builder)
  83. throws SAXException, IOException {
  84. parseConfigWithUtil(builder, null, true);
  85. }
  86. /**
  87. * Non-multiple of 8 should be rounded.
  88. *
  89. * @throws Exception if an error occurs
  90. */
  91. @Test
  92. public void testRoundUp() throws Exception {
  93. testEncryptionAndEvent(55, 56);
  94. }
  95. /**
  96. * Non-multiple of 8 should be rounded.
  97. *
  98. * @throws Exception if an error occurs
  99. */
  100. @Test
  101. public void testRoundDown() throws Exception {
  102. testEncryptionAndEvent(67, 64);
  103. }
  104. /**
  105. * Encryption length must be at least 40.
  106. *
  107. * @throws Exception if an error occurs
  108. */
  109. @Test
  110. public void testBelow40() throws Exception {
  111. testEncryptionAndEvent(32, 40);
  112. }
  113. /**
  114. * Encryption length must be at most 128.
  115. *
  116. * @throws Exception if an error occurs
  117. */
  118. @Test
  119. public void testAbove128() throws Exception {
  120. testEncryptionAndEvent(233, 128);
  121. }
  122. /**
  123. * A correct value must be properly set up.
  124. *
  125. * @throws Exception if an error occurs
  126. */
  127. @Test
  128. public void testCorrectValue() throws Exception {
  129. runEncryptionTest(128, 128);
  130. }
  131. private void testEncryptionAndEvent(int specifiedEncryptionLength,
  132. int actualEncryptionLength) throws Exception {
  133. runEncryptionTest(specifiedEncryptionLength, actualEncryptionLength);
  134. assertTrue(eventTriggered);
  135. }
  136. private void runEncryptionTest(int specifiedEncryptionLength, int actualEncryptionLength)
  137. throws Exception {
  138. parseConfig(createBuilder().startEncryptionParams()
  139. .setEncryptionLength(specifiedEncryptionLength)
  140. .endEncryptionParams(),
  141. new EncryptionEventFilter(specifiedEncryptionLength, actualEncryptionLength));
  142. thenEncryptionLengthShouldBe(actualEncryptionLength);
  143. }
  144. private void thenEncryptionLengthShouldBe(int expectedEncryptionLength) {
  145. PDFEncryptionParams encryptionParams = pdfUtil.getEncryptionParams();
  146. assertEquals(expectedEncryptionLength, encryptionParams.getEncryptionLengthInBits());
  147. }
  148. private class EncryptionEventFilter implements EventListener {
  149. private final int specifiedEncryptionLength;
  150. private final int correctedEncryptionLength;
  151. EncryptionEventFilter(int specifiedEncryptionLength, int correctedEncryptionLength) {
  152. this.specifiedEncryptionLength = specifiedEncryptionLength;
  153. this.correctedEncryptionLength = correctedEncryptionLength;
  154. }
  155. public void processEvent(Event event) {
  156. assertEquals(PDFEventProducer.class.getName() + ".incorrectEncryptionLength",
  157. event.getEventID());
  158. assertEquals(specifiedEncryptionLength, event.getParam("originalValue"));
  159. assertEquals(correctedEncryptionLength, event.getParam("correctedValue"));
  160. eventTriggered = true;
  161. }
  162. }
  163. @Test
  164. public void testFilterMaps() throws Exception {
  165. parseConfig(createBuilder().createFilterList("image", "flate", "ascii-85"));
  166. OutputStream outStream = mock(OutputStream.class);
  167. Map<String, List<String>> filterMap = pdfUtil.setupPDFDocument(outStream).getFilterMap();
  168. assertEquals("flate", filterMap.get("image").get(0));
  169. assertEquals("ascii-85", filterMap.get("image").get(1));
  170. }
  171. @Test
  172. public void testPDFAMode() throws Exception {
  173. parseConfigMockUtil(createBuilder().setPDFAMode(PDFAMode.DISABLED.getName()));
  174. // DISABLED is the default setting, it doesn't need to be set
  175. verify(pdfUtil, times(0)).setAMode(PDFAMode.DISABLED);
  176. parseConfigMockUtil(createBuilder().setPDFAMode(PDFAMode.PDFA_1A.getName()));
  177. verify(pdfUtil, times(1)).setAMode(PDFAMode.PDFA_1A);
  178. parseConfigMockUtil(createBuilder().setPDFAMode(PDFAMode.PDFA_1B.getName()));
  179. verify(pdfUtil, times(1)).setAMode(PDFAMode.PDFA_1B);
  180. }
  181. @Test
  182. public void testPDFXMode() throws Exception {
  183. parseConfigMockUtil(createBuilder().setPDFXMode(PDFXMode.DISABLED.getName()));
  184. // DISABLED is the default setting, it doesn't need to be set
  185. verify(pdfUtil, times(0)).setXMode(PDFXMode.DISABLED);
  186. parseConfigMockUtil(createBuilder().setPDFXMode(PDFXMode.PDFX_3_2003.getName()));
  187. verify(pdfUtil, times(1)).setXMode(PDFXMode.PDFX_3_2003);
  188. }
  189. @Test
  190. public void testSetProfile() throws Exception {
  191. String testString = "this.uri.is.purely.for.testing.and.has.no.contextual.meaning";
  192. parseConfigMockUtil(createBuilder().setOutputProfile(testString));
  193. verify(pdfUtil).setOutputProfileURI(URI.create(testString));
  194. }
  195. @Test
  196. public void testDisableSRGBColourspace() throws Exception {
  197. parseConfigMockUtil(createBuilder().disableSRGBColorSpace(true));
  198. verify(pdfUtil).setDisableSRGBColorSpace(true);
  199. parseConfigMockUtil(createBuilder().disableSRGBColorSpace(false));
  200. verify(pdfUtil, times(0)).setDisableSRGBColorSpace(false);
  201. }
  202. @Test
  203. public void testPDFVersion() throws Exception {
  204. for (Version version : Version.values()) {
  205. parseConfigMockUtil(createBuilder().setPDFVersion(version.toString()));
  206. verify(pdfUtil).setPDFVersion(version);
  207. }
  208. }
  209. }