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.

PDFObjectTestCase.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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.assertFalse;
  21. import static org.junit.Assert.assertTrue;
  22. import static org.junit.Assert.fail;
  23. import java.io.ByteArrayOutputStream;
  24. import java.io.IOException;
  25. import org.junit.Before;
  26. import org.junit.Test;
  27. /**
  28. * Tests the PDFObject class.
  29. */
  30. public class PDFObjectTestCase {
  31. /** The document behind this object */
  32. protected final PDFDocument doc = new PDFDocument("test");
  33. /** The parent of this object */
  34. protected final PDFObject parent = new DummyPDFObject();
  35. /** The test subject */
  36. protected PDFObject pdfObjectUnderTest;
  37. private static class DummyPDFObject extends PDFObject {
  38. };
  39. @Before
  40. public void setUp() {
  41. pdfObjectUnderTest = new DummyPDFObject();
  42. pdfObjectUnderTest.setDocument(doc);
  43. pdfObjectUnderTest.setParent(parent);
  44. }
  45. /**
  46. * Tests setObjectNumber()
  47. */
  48. @Test
  49. public void testSetObjectNumber() {
  50. pdfObjectUnderTest.setObjectNumber(1);
  51. assertEquals(1, pdfObjectUnderTest.getObjectNumber());
  52. pdfObjectUnderTest.setObjectNumber(5);
  53. assertEquals(5, pdfObjectUnderTest.getObjectNumber());
  54. }
  55. /**
  56. * Tests hasObjectNumber() - returns the object number of the underlying PDF object.
  57. */
  58. @Test
  59. public void testHasObjectNumber() {
  60. assertFalse(pdfObjectUnderTest.hasObjectNumber());
  61. pdfObjectUnderTest.setObjectNumber(1);
  62. assertTrue(pdfObjectUnderTest.hasObjectNumber());
  63. }
  64. /**
  65. * Tests getGeneration() - returns the generation number of the underlying PDF object.
  66. */
  67. @Test
  68. public void testGetGeneration() {
  69. // Default should be 0
  70. assertEquals(0, pdfObjectUnderTest.getGeneration());
  71. // apparently there is no way to set this to anything other than 0
  72. }
  73. /**
  74. * Tests setDocument() - returns the document to which this object is bound.
  75. */
  76. @Test
  77. public void testSetDocument() {
  78. assertEquals(doc, pdfObjectUnderTest.getDocument());
  79. // assign a different document to the object and test (this should be immutable but isn't)
  80. PDFDocument anotherDoc = new PDFDocument("another test");
  81. pdfObjectUnderTest.setDocument(anotherDoc);
  82. assertEquals(anotherDoc, pdfObjectUnderTest.getDocument());
  83. }
  84. /**
  85. * Tests setParent() - assigns the object a parent.
  86. */
  87. @Test
  88. public void testSetParent() {
  89. assertEquals(parent, pdfObjectUnderTest.getParent());
  90. // assign another parent (this probably shouldn't me mutable)
  91. DummyPDFObject anotherParent = new DummyPDFObject();
  92. pdfObjectUnderTest.setParent(anotherParent);
  93. assertEquals(anotherParent, pdfObjectUnderTest.getParent());
  94. }
  95. /**
  96. * Test getObjectID() - returns the PDF object ID.
  97. */
  98. @Test
  99. public void testGetObjectID() {
  100. pdfObjectUnderTest.setObjectNumber(10);
  101. // String is of the format "<object#> <generation#> obj\n"
  102. assertEquals("10 0 obj\n", pdfObjectUnderTest.getObjectID());
  103. }
  104. /**
  105. * Test referencePDF() - returns a {@link String} in PDF format to reference this object.
  106. */
  107. @Test
  108. public void testReferencePDF() {
  109. try {
  110. pdfObjectUnderTest.referencePDF();
  111. fail("The object number is not set, an exception should be thrown");
  112. } catch (IllegalArgumentException e) {
  113. // PASS
  114. }
  115. pdfObjectUnderTest.setObjectNumber(10);
  116. // Referencing this object is in the format "<obj#> <gen#> R"
  117. assertEquals("10 0 R", pdfObjectUnderTest.referencePDF());
  118. }
  119. /**
  120. * Test makeReference() - returns this object represented as a {@link PDFReference}.
  121. */
  122. @Test
  123. public void testMakeReference() {
  124. // Not very intelligent but, there's not much to test here
  125. pdfObjectUnderTest.setObjectNumber(10);
  126. PDFReference ref = pdfObjectUnderTest.makeReference();
  127. assertEquals(pdfObjectUnderTest.getObjectNumber(), ref.getObjectNumber());
  128. assertEquals(pdfObjectUnderTest, ref.getObject());
  129. assertEquals(pdfObjectUnderTest.referencePDF(), ref.toString());
  130. }
  131. /**
  132. * Tests PDF object references.
  133. * @throws Exception if an error occurs
  134. */
  135. @Test
  136. public void testReference() throws Exception {
  137. PDFDictionary dict = new PDFDictionary();
  138. dict.setObjectNumber(7);
  139. PDFReference ref = dict.makeReference();
  140. assertEquals(ref.getObjectNumber(), 7);
  141. assertEquals(ref.getGeneration(), 0);
  142. assertEquals(ref.toString(), "7 0 R");
  143. ref = new PDFReference("8 0 R");
  144. assertEquals(ref.getObjectNumber(), 8);
  145. assertEquals(ref.getGeneration(), 0);
  146. assertEquals(ref.toString(), "8 0 R");
  147. }
  148. /**
  149. * A generic method to test output() for sub-classes of (@link PDFObject}. The expected String
  150. * should be formatted such that the object number and object descriptor aren't printed i.e.
  151. * for a simple integer object in PDF:
  152. * <pre>
  153. * 1 0 obj ** ommited from expectedString
  154. * 10
  155. * endobj ** ommited from expectedString
  156. * </pre>
  157. * Thus the expected string would be "10".
  158. * @param expectedString the string that is expected.
  159. * @param object the object being tested
  160. * @throws IOException error with I/O
  161. */
  162. protected void testOutputStreams(String expectedString, PDFObject object) throws IOException {
  163. // Test both with and without object numbers
  164. ByteArrayOutputStream outStream = new ByteArrayOutputStream();
  165. // Ensure that
  166. object.setObjectNumber(0);
  167. assertEquals(expectedString.length(), object.output(outStream));
  168. assertEquals(expectedString, outStream.toString());
  169. outStream.reset();
  170. object.setObjectNumber(1);
  171. // Test the length of the output string is returned correctly.
  172. assertEquals(expectedString.length(), object.output(outStream));
  173. assertEquals(expectedString, outStream.toString());
  174. }
  175. }