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.

PDFDictionaryTestCase.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 java.io.ByteArrayOutputStream;
  20. import java.io.IOException;
  21. import org.junit.Before;
  22. import org.junit.Test;
  23. import static org.junit.Assert.assertEquals;
  24. import static org.junit.Assert.assertNull;
  25. import static org.junit.Assert.fail;
  26. import org.apache.commons.io.output.CountingOutputStream;
  27. /**
  28. * Test case for {@link PDFDictionary}.
  29. */
  30. public class PDFDictionaryTestCase extends PDFObjectTestCase {
  31. /** The test subject */
  32. private PDFDictionary pdfDictUnderTest;
  33. private PDFArray testArray;
  34. private PDFNumber testNumber;
  35. /** The order in which these objects are put into the dictionary MUST be maintained. */
  36. private String expectedOutput = "<<\n"
  37. + " /String (TestValue)\n"
  38. + " /int 10\n"
  39. + " /double 3.1\n"
  40. + " /array [1 (two) 20]\n"
  41. + " /number 20\n"
  42. + " /null null\n"
  43. + ">>";
  44. @Before
  45. public void setUp() {
  46. // A PDFNumber for testing, this DOES have a parent
  47. testNumber = new PDFNumber();
  48. testNumber.setParent(parent);
  49. testNumber.setNumber(20);
  50. // An array for testing, this DOES NOT have a parent
  51. testArray = new PDFArray();
  52. testArray.add(1);
  53. testArray.add("two");
  54. testArray.add(testNumber);
  55. // Populating the dictionary with a parent, document and the various objects
  56. pdfDictUnderTest = new PDFDictionary(parent);
  57. pdfDictUnderTest.setDocument(doc);
  58. pdfDictUnderTest.put("String", "TestValue");
  59. pdfDictUnderTest.put("int", 10);
  60. pdfDictUnderTest.put("double", 3.1);
  61. pdfDictUnderTest.put("array", testArray);
  62. pdfDictUnderTest.put("number", testNumber);
  63. // null is a valid PDF object
  64. pdfDictUnderTest.put("null", null);
  65. // test that the interface is maintained
  66. pdfObjectUnderTest = pdfDictUnderTest;
  67. }
  68. /**
  69. * Tests put() - tests that the object is put into the dictionary and it is handled if it is a
  70. * {@link PDFObject}.
  71. */
  72. @Test
  73. public void testPut() {
  74. // The "put()" commands have already been done in setUp(), so just test them.
  75. assertEquals("TestValue", pdfDictUnderTest.get("String"));
  76. assertEquals(10, pdfDictUnderTest.get("int"));
  77. assertEquals(3.1, pdfDictUnderTest.get("double"));
  78. // With PDFObjects, if they DO NOT have a parent, the dict becomes their parent.
  79. assertEquals(testArray, pdfDictUnderTest.get("array"));
  80. assertEquals(pdfDictUnderTest, testArray.getParent());
  81. // With PDFObjects, if they DO have a parent, the dict DOES NOT change the parent object.
  82. assertEquals(testNumber, pdfDictUnderTest.get("number"));
  83. // Test it doesn't explode when we try to get a non-existent entry
  84. assertNull(pdfDictUnderTest.get("Not in dictionary"));
  85. // Tests that we can over-write objects
  86. pdfDictUnderTest.put("array", 10);
  87. assertEquals(10, pdfDictUnderTest.get("array"));
  88. // Test that nulls are handled appropriately
  89. assertNull(pdfDictUnderTest.get("null"));
  90. }
  91. /**
  92. * Tests get() - tests that objects can be properly retrieved from the dictionary.
  93. */
  94. @Test
  95. public void testGet() {
  96. // Tested fairly comprehensively in testPut().
  97. }
  98. /**
  99. * Tests writeDictionary() - tests that the dictionary is properly written to the output-stream.
  100. */
  101. @Test
  102. public void testWriteDictionary() {
  103. // Ensure that the objects stored in the dictionary are streamed in the correct format.
  104. ByteArrayOutputStream outStream = new ByteArrayOutputStream();
  105. CountingOutputStream cout = new CountingOutputStream(outStream);
  106. StringBuilder textBuffer = new StringBuilder();
  107. try {
  108. pdfDictUnderTest.writeDictionary(cout, textBuffer);
  109. PDFDocument.flushTextBuffer(textBuffer, cout);
  110. assertEquals(expectedOutput, outStream.toString());
  111. } catch (IOException e) {
  112. fail("IOException: " + e.getMessage());
  113. }
  114. }
  115. /**
  116. * Tests output() - test that this object can write itself to an output stream.
  117. * @throws IOException error caused by I/O
  118. */
  119. @Test
  120. public void testOutput() throws IOException {
  121. testOutputStreams(expectedOutput, pdfDictUnderTest);
  122. }
  123. }