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.

PDFRendererConfiguratorTestCase.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 junit.framework.AssertionFailedError;
  21. import junit.framework.TestCase;
  22. import org.apache.fop.apps.FOPException;
  23. import org.apache.fop.apps.FOUserAgent;
  24. import org.apache.fop.apps.FopFactory;
  25. import org.apache.fop.events.Event;
  26. import org.apache.fop.events.EventListener;
  27. import org.apache.fop.pdf.PDFEncryptionParams;
  28. /**
  29. * Tests that encryption length is properly set up.
  30. */
  31. public class PDFRendererConfiguratorTestCase extends TestCase {
  32. private FOUserAgent foUserAgent;
  33. private PDFDocumentHandler documentHandler;
  34. private boolean eventTriggered;
  35. private class EncryptionEventFilter implements EventListener {
  36. private final int specifiedEncryptionLength;
  37. private final int correctedEncryptionLength;
  38. EncryptionEventFilter(int specifiedEncryptionLength, int correctedEncryptionLength) {
  39. this.specifiedEncryptionLength = specifiedEncryptionLength;
  40. this.correctedEncryptionLength = correctedEncryptionLength;
  41. }
  42. public void processEvent(Event event) {
  43. assertEquals(PDFEventProducer.class.getName() + ".incorrectEncryptionLength",
  44. event.getEventID());
  45. assertEquals(specifiedEncryptionLength, event.getParam("originalValue"));
  46. assertEquals(correctedEncryptionLength, event.getParam("correctedValue"));
  47. eventTriggered = true;
  48. }
  49. }
  50. /**
  51. * Non-multiple of 8 should be rounded.
  52. *
  53. * @throws Exception if an error occurs
  54. */
  55. public void testRoundUp() throws Exception {
  56. runTest("roundUp", 55, 56);
  57. }
  58. /**
  59. * Non-multiple of 8 should be rounded.
  60. *
  61. * @throws Exception if an error occurs
  62. */
  63. public void testRoundDown() throws Exception {
  64. runTest("roundDown", 67, 64);
  65. }
  66. /**
  67. * Encryption length must be at least 40.
  68. *
  69. * @throws Exception if an error occurs
  70. */
  71. public void testBelow40() throws Exception {
  72. runTest("below40", 32, 40);
  73. }
  74. /**
  75. * Encryption length must be at most 128.
  76. *
  77. * @throws Exception if an error occurs
  78. */
  79. public void testAbove128() throws Exception {
  80. runTest("above128", 233, 128);
  81. }
  82. /**
  83. * A correct value must be properly set up.
  84. *
  85. * @throws Exception if an error occurs
  86. */
  87. public void testCorrectValue() throws Exception {
  88. givenAConfigurationFile("correct", new EventListener() {
  89. public void processEvent(Event event) {
  90. throw new AssertionFailedError("No event was expected");
  91. }
  92. });
  93. whenCreatingAndConfiguringDocumentHandler();
  94. thenEncryptionLengthShouldBe(128);
  95. }
  96. private void runTest(String configFilename,
  97. final int specifiedEncryptionLength,
  98. final int correctedEncryptionLength) throws Exception {
  99. givenAConfigurationFile(configFilename,
  100. new EncryptionEventFilter(specifiedEncryptionLength, correctedEncryptionLength));
  101. whenCreatingAndConfiguringDocumentHandler();
  102. assertTrue(eventTriggered);
  103. }
  104. private void givenAConfigurationFile(String filename, EventListener eventListener)
  105. throws Exception {
  106. FopFactory fopFactory = FopFactory.newInstance();
  107. fopFactory.setUserConfig(new File("test/resources/org/apache/fop/render/pdf/"
  108. + filename + ".xconf"));
  109. foUserAgent = fopFactory.newFOUserAgent();
  110. foUserAgent.getEventBroadcaster().addEventListener(eventListener);
  111. }
  112. private void whenCreatingAndConfiguringDocumentHandler() throws FOPException {
  113. PDFDocumentHandlerMaker maker = new PDFDocumentHandlerMaker();
  114. documentHandler = (PDFDocumentHandler) maker.makeIFDocumentHandler(foUserAgent);
  115. new PDFRendererConfigurator(foUserAgent).configure(documentHandler);
  116. }
  117. private void thenEncryptionLengthShouldBe(int expectedEncryptionLength) {
  118. PDFEncryptionParams encryptionParams = documentHandler.getPDFUtil().getEncryptionParams();
  119. assertEquals(expectedEncryptionLength, encryptionParams.getEncryptionLengthInBits());
  120. }
  121. }