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.

CrossReferenceStreamTestCase.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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.xref;
  19. import java.io.ByteArrayOutputStream;
  20. import java.io.DataOutputStream;
  21. import java.io.IOException;
  22. import java.util.ArrayList;
  23. import java.util.Arrays;
  24. import java.util.Collections;
  25. import java.util.List;
  26. import org.junit.Test;
  27. public class CrossReferenceStreamTestCase extends CrossReferenceObjectTest {
  28. private List<Long> uncompressedObjectOffsets;
  29. private List<CompressedObjectReference> compressedObjectReferences;
  30. @Test
  31. public void testWithNoOffset() throws IOException {
  32. List<Long> emptyList = Collections.emptyList();
  33. test(emptyList);
  34. }
  35. @Test
  36. public void testWithOffsets() throws IOException {
  37. test(new ArrayList<Long>(Arrays.asList(0L, 1L, 2L, 3L, 4L)));
  38. }
  39. @Test
  40. public void testWithBigOffsets() throws IOException {
  41. test(new ArrayList<Long>(Arrays.asList(0xffL, 0xffffL, 0xffffffffL, 0xffffffffffffffffL)));
  42. }
  43. @Test
  44. public void testWithObjectStreams1() throws IOException {
  45. List<CompressedObjectReference> compressedObjectReferences =
  46. Arrays.asList(new CompressedObjectReference(2, 1, 0));
  47. test(Arrays.asList(0L, null), compressedObjectReferences);
  48. }
  49. @Test
  50. public void testWithObjectStreams2() throws IOException {
  51. int numIndirectObjects = 2;
  52. int numCompressedObjects = 1;
  53. List<Long> indirectObjectOffsets
  54. = new ArrayList<Long>(numIndirectObjects + numCompressedObjects);
  55. for (long i = 0; i < numIndirectObjects; i++) {
  56. indirectObjectOffsets.add(i);
  57. }
  58. List<CompressedObjectReference> compressedObjectReferences
  59. = new ArrayList<CompressedObjectReference>();
  60. for (int index = 0; index < numCompressedObjects; index++) {
  61. indirectObjectOffsets.add(null);
  62. int obNum = numIndirectObjects + index + 1;
  63. compressedObjectReferences.add(new CompressedObjectReference(obNum,
  64. numIndirectObjects, index));
  65. }
  66. test(indirectObjectOffsets, compressedObjectReferences);
  67. }
  68. private void test(List<Long> indirectObjectOffsets) throws IOException {
  69. List<CompressedObjectReference> compressedObjectReferences = Collections.emptyList();
  70. test(indirectObjectOffsets, compressedObjectReferences);
  71. }
  72. private void test(List<Long> indirectObjectOffsets,
  73. List<CompressedObjectReference> compressedObjectReferences) throws IOException {
  74. this.uncompressedObjectOffsets = indirectObjectOffsets;
  75. this.compressedObjectReferences = compressedObjectReferences;
  76. runTest();
  77. }
  78. @Override
  79. protected CrossReferenceObject createCrossReferenceObject() {
  80. return new CrossReferenceStream(pdfDocument,
  81. uncompressedObjectOffsets.size() + 1,
  82. trailerDictionary,
  83. STARTXREF,
  84. uncompressedObjectOffsets,
  85. compressedObjectReferences);
  86. }
  87. @Override
  88. protected byte[] createExpectedCrossReferenceData() throws IOException {
  89. List<ObjectReference> objectReferences
  90. = new ArrayList<ObjectReference>(uncompressedObjectOffsets.size());
  91. for (Long offset : uncompressedObjectOffsets) {
  92. objectReferences.add(offset == null ? null : new UncompressedObjectReference(offset));
  93. }
  94. for (CompressedObjectReference ref : compressedObjectReferences) {
  95. objectReferences.set(ref.getObjectNumber() - 1, ref);
  96. }
  97. int maxObjectNumber = objectReferences.size() + 1;
  98. ByteArrayOutputStream stream = new ByteArrayOutputStream();
  99. StringBuilder expected = new StringBuilder(256);
  100. expected.append(maxObjectNumber + " 0 obj\n")
  101. .append("<<\n")
  102. .append(" /Root 1 0 R\n")
  103. .append(" /Info 2 0 R\n")
  104. .append(" /ID [<0123456789ABCDEF> <0123456789ABCDEF>]\n")
  105. .append(" /Type /XRef\n")
  106. .append(" /Size ").append(Integer.toString(maxObjectNumber + 1)).append('\n')
  107. .append(" /W [1 8 2]\n")
  108. .append(" /Length ").append(Integer.toString((maxObjectNumber + 1) * 11 + 1)).append('\n')
  109. .append(">>\n")
  110. .append("stream\n");
  111. stream.write(getBytes(expected));
  112. DataOutputStream data = new DataOutputStream(stream);
  113. data.write(new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, (byte) 0xff, (byte) 0xff});
  114. for (ObjectReference objectReference : objectReferences) {
  115. objectReference.output(data);
  116. }
  117. data.write(1);
  118. data.writeLong(STARTXREF);
  119. data.write(0);
  120. data.write(0);
  121. data.close();
  122. stream.write(getBytes("\nendstream\nendobj\n"));
  123. return stream.toByteArray();
  124. }
  125. }