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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 org.junit.Before;
  20. import org.junit.Test;
  21. import static org.junit.Assert.assertEquals;
  22. import static org.junit.Assert.assertNull;
  23. import static org.junit.Assert.fail;
  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. if (setVersion.compareTo(originalVersion) != 0) {
  78. fail("The FixedVersionController should throw an exception if an attempt to change "
  79. + "the version is made");
  80. }
  81. } catch (IllegalStateException e) {
  82. // PASS
  83. }
  84. // Changes are NOT allowed, the original version is immutable
  85. assertEquals(originalVersion, fixedVC.getPDFVersion());
  86. // The document version is NEVER changed
  87. assertEquals(Version.V1_4, doc.getPDFVersion());
  88. // the /Version parameter shouldn't be present in the document catalog
  89. assertNull(doc.getRoot().get("Version"));
  90. } else {
  91. try {
  92. VersionController.getFixedVersionController(originalVersion);
  93. fail("Versions < 1.4 aren't allowed.");
  94. } catch (IllegalArgumentException e) {
  95. // PASS
  96. }
  97. }
  98. }
  99. /**
  100. * The dynamic implementation allows the version to be changed. However, the version given in
  101. * the constructor will be the version set in the header of the PDF document. Any change to the
  102. * version will then be made in the document catalog.
  103. *
  104. * @param originalVersion the version given to the constructor when PDFVersion instantiated
  105. * @param setVersion the version being set
  106. */
  107. private void testDynamicController(Version originalVersion, Version setVersion) {
  108. VersionController testSubj = VersionController.getDynamicVersionController(originalVersion,
  109. doc);
  110. testSubj.setPDFVersion(setVersion);
  111. PDFName nameVersion = new PDFName(setVersion.toString());
  112. if (originalVersion.compareTo(setVersion) < 0) {
  113. versionShouldChange(setVersion, testSubj, nameVersion);
  114. } else {
  115. versionShouldNotChange(originalVersion, testSubj);
  116. }
  117. doc.getRoot().put("Version", null);
  118. }
  119. private void versionShouldNotChange(Version originalVersion, VersionController testSubj) {
  120. assertEquals(originalVersion, testSubj.getPDFVersion());
  121. assertNull(doc.getRoot().get("Version"));
  122. }
  123. private void versionShouldChange(Version setVersion, VersionController testSubj,
  124. PDFName nameVersion) {
  125. assertEquals(setVersion, testSubj.getPDFVersion());
  126. assertEquals(nameVersion.toString(), doc.getRoot().get("Version").toString());
  127. }
  128. }