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.

VersionControllerTestCase.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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.pdf;
  19. import static org.junit.Assert.assertEquals;
  20. import static org.junit.Assert.assertNull;
  21. import static org.junit.Assert.fail;
  22. import org.junit.Before;
  23. import org.junit.Test;
  24. /**
  25. * A test class for {@link VersionController}.
  26. */
  27. public class VersionControllerTestCase {
  28. private PDFDocument doc;
  29. @Before
  30. public void setUp() {
  31. doc = new PDFDocument("test");
  32. }
  33. @Test
  34. public void testGetVersion() {
  35. // These do the same thing
  36. for (Version version : Version.values()) {
  37. if (version.compareTo(Version.V1_4) >= 0) {
  38. VersionController fixedVC = VersionController.getFixedVersionController(version);
  39. assertEquals(version, fixedVC.getPDFVersion());
  40. }
  41. VersionController dynamicVC = VersionController.getDynamicVersionController(version,
  42. doc);
  43. assertEquals(version, dynamicVC.getPDFVersion());
  44. }
  45. }
  46. /**
  47. * Tests that the setter methods work at setting the underlying version.
  48. * Here there is a disparity between the two objects, the fixed version will
  49. * throw an exception if the setter is invoked. The dynamic version will
  50. * allow the version to be changed, if the new version is greater than the
  51. * version already set.
  52. */
  53. @Test
  54. public void testSetVersion() {
  55. // Create every type of expected PDFVersion
  56. for (Version originalVersion : Version.values()) {
  57. // Compare against every type of Version
  58. for (Version setVersion : Version.values()) {
  59. testDynamicController(originalVersion, setVersion);
  60. testFixedController(originalVersion, setVersion);
  61. }
  62. }
  63. }
  64. /**
  65. * The fixed implementation will throw an exception if an attempt is made to change its
  66. * version.
  67. *
  68. * @param originalVersion the version given to the constructor when PDFVersion instantiated
  69. * @param setVersion the version being set
  70. */
  71. private void testFixedController(Version originalVersion, Version setVersion) {
  72. if (originalVersion.compareTo(Version.V1_4) >= 0) {
  73. VersionController fixedVC = VersionController
  74. .getFixedVersionController(originalVersion);
  75. try {
  76. fixedVC.setPDFVersion(setVersion);
  77. fail("The FixedVersionController should throw an exception if an attempt to change "
  78. + "the version is made");
  79. } catch (IllegalStateException e) {
  80. // PASS
  81. }
  82. // Changes are NOT allowed, the original version is immutable
  83. assertEquals(originalVersion, fixedVC.getPDFVersion());
  84. // The document version is NEVER changed
  85. assertEquals(Version.V1_4, doc.getPDFVersion());
  86. // the /Version parameter shouldn't be present in the document catalog
  87. assertNull(doc.getRoot().get("Version"));
  88. } else {
  89. try {
  90. VersionController.getFixedVersionController(originalVersion);
  91. fail("Versions < 1.4 aren't allowed.");
  92. } catch (IllegalArgumentException e) {
  93. // PASS
  94. }
  95. }
  96. }
  97. /**
  98. * The dynamic implementation allows the version to be changed. However, the version given in
  99. * the constructor will be the version set in the header of the PDF document. Any change to the
  100. * version will then be made in the document catalog.
  101. *
  102. * @param originalVersion the version given to the constructor when PDFVersion instantiated
  103. * @param setVersion the version being set
  104. */
  105. private void testDynamicController(Version originalVersion, Version setVersion) {
  106. VersionController testSubj = VersionController.getDynamicVersionController(originalVersion,
  107. doc);
  108. testSubj.setPDFVersion(setVersion);
  109. PDFName nameVersion = new PDFName(setVersion.toString());
  110. if (originalVersion.compareTo(setVersion) < 0) {
  111. versionShouldChange(setVersion, testSubj, nameVersion);
  112. } else {
  113. versionShouldNotChange(originalVersion, testSubj);
  114. }
  115. doc.getRoot().put("Version", null);
  116. }
  117. private void versionShouldNotChange(Version originalVersion, VersionController testSubj) {
  118. assertEquals(originalVersion, testSubj.getPDFVersion());
  119. assertNull(doc.getRoot().get("Version"));
  120. }
  121. private void versionShouldChange(Version setVersion, VersionController testSubj,
  122. PDFName nameVersion) {
  123. assertEquals(setVersion, testSubj.getPDFVersion());
  124. assertEquals(nameVersion.toString(), doc.getRoot().get("Version").toString());
  125. }
  126. }