diff options
author | Vincent Hennebert <vhennebert@apache.org> | 2012-03-19 15:56:19 +0000 |
---|---|---|
committer | Vincent Hennebert <vhennebert@apache.org> | 2012-03-19 15:56:19 +0000 |
commit | c1653ad47548cff23a614559544fe0b0aec3522d (patch) | |
tree | 07a0ab82acf33280d8560bf0e5d33fe683415f1b /test/java/org | |
parent | 0e6c4648996b5bfd38330129fcdfaaa72f86b09c (diff) | |
download | xmlgraphics-fop-c1653ad47548cff23a614559544fe0b0aec3522d.tar.gz xmlgraphics-fop-c1653ad47548cff23a614559544fe0b0aec3522d.zip |
Bugzilla #51385: Added configuration option to set the version of the output PDF document.
Patch by Mehdi Houshmand, applied with minor modifications
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1302518 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'test/java/org')
-rw-r--r-- | test/java/org/apache/fop/pdf/VersionControllerTestCase.java | 143 | ||||
-rw-r--r-- | test/java/org/apache/fop/pdf/VersionTestCase.java | 89 |
2 files changed, 232 insertions, 0 deletions
diff --git a/test/java/org/apache/fop/pdf/VersionControllerTestCase.java b/test/java/org/apache/fop/pdf/VersionControllerTestCase.java new file mode 100644 index 000000000..74637c91f --- /dev/null +++ b/test/java/org/apache/fop/pdf/VersionControllerTestCase.java @@ -0,0 +1,143 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id$ */ + +package org.apache.fop.pdf; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.fail; + +import org.junit.Before; +import org.junit.Test; + + +/** + * A test class for {@link VersionController}. + */ +public class VersionControllerTestCase { + + private PDFDocument doc; + + @Before + public void setUp() { + doc = new PDFDocument("test"); + } + + @Test + public void testGetVersion() { + // These do the same thing + for (Version version : Version.values()) { + if (version.compareTo(Version.V1_4) >= 0) { + VersionController fixedVC = VersionController.getFixedVersionController(version); + assertEquals(version, fixedVC.getPDFVersion()); + } + + VersionController dynamicVC = VersionController.getDynamicVersionController(version, + doc); + assertEquals(version, dynamicVC.getPDFVersion()); + } + } + + /** + * Tests that the setter methods work at setting the underlying version. + * Here there is a disparity between the two objects, the fixed version will + * throw an exception if the setter is invoked. The dynamic version will + * allow the version to be changed, if the new version is greater than the + * version already set. + */ + @Test + public void testSetVersion() { + // Create every type of expected PDFVersion + for (Version originalVersion : Version.values()) { + // Compare against every type of Version + for (Version setVersion : Version.values()) { + testDynamicController(originalVersion, setVersion); + testFixedController(originalVersion, setVersion); + } + + } + } + + /** + * The fixed implementation will throw an exception if an attempt is made to change its + * version. + * + * @param originalVersion the version given to the constructor when PDFVersion instantiated + * @param setVersion the version being set + */ + private void testFixedController(Version originalVersion, Version setVersion) { + if (originalVersion.compareTo(Version.V1_4) >= 0) { + VersionController fixedVC = VersionController + .getFixedVersionController(originalVersion); + try { + fixedVC.setPDFVersion(setVersion); + fail("The FixedVersionController should throw an exception if an attempt to change " + + "the version is made"); + } catch (IllegalStateException e) { + // PASS + } + // Changes are NOT allowed, the original version is immutable + assertEquals(originalVersion, fixedVC.getPDFVersion()); + // The document version is NEVER changed + assertEquals(Version.V1_4, doc.getPDFVersion()); + // the /Version parameter shouldn't be present in the document catalog + assertNull(doc.getRoot().get("Version")); + } else { + try { + VersionController.getFixedVersionController(originalVersion); + fail("Versions < 1.4 aren't allowed."); + } catch (IllegalArgumentException e) { + // PASS + } + } + } + + /** + * The dynamic implementation allows the version to be changed. However, the version given in + * the constructor will be the version set in the header of the PDF document. Any change to the + * version will then be made in the document catalog. + * + * @param originalVersion the version given to the constructor when PDFVersion instantiated + * @param setVersion the version being set + */ + private void testDynamicController(Version originalVersion, Version setVersion) { + VersionController testSubj = VersionController.getDynamicVersionController(originalVersion, + doc); + testSubj.setPDFVersion(setVersion); + PDFName nameVersion = new PDFName(setVersion.toString()); + + if (originalVersion.compareTo(setVersion) < 0) { + versionShouldChange(setVersion, testSubj, nameVersion); + } else { + versionShouldNotChange(originalVersion, testSubj); + } + doc.getRoot().put("Version", null); + } + + private void versionShouldNotChange(Version originalVersion, VersionController testSubj) { + assertEquals(originalVersion, testSubj.getPDFVersion()); + assertNull(doc.getRoot().get("Version")); + } + + private void versionShouldChange(Version setVersion, VersionController testSubj, + PDFName nameVersion) { + assertEquals(setVersion, testSubj.getPDFVersion()); + assertEquals(nameVersion.toString(), doc.getRoot().get("Version").toString()); + } +} diff --git a/test/java/org/apache/fop/pdf/VersionTestCase.java b/test/java/org/apache/fop/pdf/VersionTestCase.java new file mode 100644 index 000000000..9c90f0966 --- /dev/null +++ b/test/java/org/apache/fop/pdf/VersionTestCase.java @@ -0,0 +1,89 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id$ */ + +package org.apache.fop.pdf; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +/** + * This is a test case for ({@link Version}. + */ +public class VersionTestCase { + + /** + * Test the <code>getValue()</code> method. This should return {@link Version} given a + * {@link String}. + */ + @Test + public void testGetValue() { + int index = 0; + for (Version version : Version.values()) { + assertEquals(version, Version.getValueOf("1." + index++)); + } + } + + @Test(expected = IllegalArgumentException.class) + public void testGetValueIllegalArgument() { + Version.getValueOf("blah"); + } + + /** + * Tests that the <code>toString()</method> method returns the PDF version string of the proper + * format. + */ + @Test + public void testToString() { + // Test all the normal values + int index = 0; + for (Version version : Version.values()) { + assertTrue(version.toString().equals("1." + index++)); + } + } + + /** + * Tests that the <code>compareTo()</code> contract is obeyed. + */ + @Test + public void testCompareTo() { + // Ensure that the implicit comparison contract is satisfied + Version[] expected = { + Version.V1_0, + Version.V1_1, + Version.V1_2, + Version.V1_3, + Version.V1_4, + Version.V1_5, + Version.V1_6, + Version.V1_7 + }; + + Version[] actual = Version.values(); + + for (int i = 0; i < actual.length - 1; i++) { + assertEquals(-1, actual[i].compareTo(expected[i + 1])); + + assertEquals(0, actual[i].compareTo(expected[i])); + + assertEquals(1, actual[i + 1].compareTo(expected[i])); + } + } +} |