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.

ObjectStreamTestCase.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 java.io.ByteArrayOutputStream;
  21. import java.io.IOException;
  22. import java.util.Arrays;
  23. import java.util.List;
  24. import org.junit.Before;
  25. import org.junit.Test;
  26. public class ObjectStreamTestCase {
  27. private static final String OBJECT_CONTENT = "<<\n /Foo True\n /Bar False\n>>\n";
  28. private PDFDocument pdfDocument;
  29. private ObjectStream objectStream;
  30. private List<MockCompressedObject> compressedObjects;
  31. @Before
  32. public void setUp() throws Exception {
  33. pdfDocument = new PDFDocument("PDFObjectStreamTestCase");
  34. objectStream = new ObjectStream();
  35. pdfDocument.assignObjectNumber(objectStream);
  36. compressedObjects = Arrays.asList(new MockCompressedObject(), new MockCompressedObject());
  37. }
  38. @Test
  39. public void testSingleObjectStream() throws IOException {
  40. populateObjectStream();
  41. testOutput();
  42. }
  43. @Test
  44. public void testObjectStreamCollection() throws IOException {
  45. objectStream = new ObjectStream(objectStream);
  46. pdfDocument.assignObjectNumber(objectStream);
  47. populateObjectStream();
  48. testOutput();
  49. }
  50. @Test(expected = IllegalStateException.class)
  51. public void directObjectsAreNotAllowed() throws Exception {
  52. objectStream.addObject(new MockCompressedObject());
  53. }
  54. @Test(expected = NullPointerException.class)
  55. public void nullObjectsAreNotAllowed() throws Exception {
  56. objectStream.addObject(null);
  57. }
  58. private void testOutput() throws IOException {
  59. String expected = getExpectedOutput();
  60. String actual = getActualOutput();
  61. assertEquals(expected, actual);
  62. }
  63. private void populateObjectStream() {
  64. for (MockCompressedObject obj : compressedObjects) {
  65. pdfDocument.assignObjectNumber(obj);
  66. objectStream.addObject(obj);
  67. }
  68. }
  69. private String getExpectedOutput() {
  70. int numObs = compressedObjects.size();
  71. int objectStreamNumber = objectStream.getObjectNumber();
  72. int offsetsLength = 9;
  73. StringBuilder expected = new StringBuilder();
  74. expected.append("<<\n");
  75. ObjectStream previous = (ObjectStream) objectStream.get("Extends");
  76. if (previous != null) {
  77. expected.append(" /Extends ").append(previous.getObjectNumber()).append(" 0 R\n");
  78. }
  79. expected.append(" /Type /ObjStm\n")
  80. .append(" /N ").append(numObs).append("\n")
  81. .append(" /First ").append(offsetsLength).append('\n')
  82. .append(" /Length ").append(OBJECT_CONTENT.length() * 2 + offsetsLength + 1).append('\n')
  83. .append(">>\n")
  84. .append("stream\n");
  85. int offset = 0;
  86. int num = 1;
  87. for (PDFObject ob : compressedObjects) {
  88. expected.append(objectStreamNumber + num++).append(' ').append(offset).append('\n');
  89. offset += ob.toPDFString().length();
  90. }
  91. for (PDFObject ob : compressedObjects) {
  92. expected.append(ob.toPDFString());
  93. }
  94. expected.append("\nendstream");
  95. return expected.toString();
  96. }
  97. private String getActualOutput() throws IOException {
  98. ByteArrayOutputStream actual = new ByteArrayOutputStream();
  99. objectStream.getFilterList().setDisableAllFilters(true);
  100. objectStream.output(actual);
  101. return actual.toString("US-ASCII");
  102. }
  103. private static class MockCompressedObject extends PDFObject implements CompressedObject {
  104. @Override
  105. protected String toPDFString() {
  106. return OBJECT_CONTENT;
  107. }
  108. }
  109. }