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.

PDFStreamTestCase.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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.assertArrayEquals;
  24. import static org.junit.Assert.assertEquals;
  25. import static org.junit.Assert.assertFalse;
  26. import static org.junit.Assert.assertTrue;
  27. public class PDFStreamTestCase {
  28. private PDFStream stream;
  29. @Before
  30. public void createStream() {
  31. stream = new PDFStream();
  32. stream.setObjectNumber(1);
  33. PDFDocument pdfDocument = new PDFDocument("Apache FOP");
  34. stream.setDocument(pdfDocument);
  35. }
  36. @Test
  37. public void testFilterSetup() {
  38. testGetFilterList();
  39. testSetupFilterList();
  40. }
  41. private void testGetFilterList() {
  42. PDFFilterList filterList = stream.getFilterList();
  43. assertFalse(filterList.isInitialized());
  44. assertEquals(0, filterList.getFilters().size());
  45. }
  46. private void testSetupFilterList() {
  47. stream.setupFilterList();
  48. PDFFilterList filterList = stream.getFilterList();
  49. assertTrue(filterList.isInitialized());
  50. assertEquals(1, filterList.getFilters().size());
  51. PDFFilter filter = filterList.getFilters().get(0);
  52. assertEquals("/FlateDecode", filter.getName());
  53. }
  54. @Test
  55. public void customFilter() {
  56. PDFFilterList filters = stream.getFilterList();
  57. filters.addFilter("null");
  58. assertTrue(filters.isInitialized());
  59. assertEquals(1, filters.getFilters().size());
  60. PDFFilter filter = filters.getFilters().get(0);
  61. assertEquals("", filter.getName());
  62. }
  63. @Test
  64. public void testStream() throws IOException {
  65. PDFFilterList filters = stream.getFilterList();
  66. filters.addFilter("null");
  67. byte[] bytes = createSampleData();
  68. stream.setData(bytes);
  69. ByteArrayOutputStream actual = new ByteArrayOutputStream();
  70. stream.outputRawStreamData(actual);
  71. assertArrayEquals(bytes, actual.toByteArray());
  72. }
  73. @Test
  74. public void testEncodeStream() throws IOException {
  75. PDFFilterList filters = stream.getFilterList();
  76. filters.addFilter("null");
  77. byte[] bytes = createSampleData();
  78. stream.setData(bytes);
  79. ByteArrayOutputStream actual = new ByteArrayOutputStream();
  80. StreamCache streamCache = stream.encodeStream();
  81. streamCache.outputContents(actual);
  82. assertArrayEquals(bytes, actual.toByteArray());
  83. }
  84. @Test
  85. public void testEncodeAndWriteStream() throws IOException {
  86. PDFFilterList filters = stream.getFilterList();
  87. filters.addFilter("null");
  88. byte[] bytes = createSampleData();
  89. stream.setData(bytes);
  90. ByteArrayOutputStream actual = new ByteArrayOutputStream();
  91. PDFNumber number = new PDFNumber();
  92. stream.encodeAndWriteStream(actual, number);
  93. assertArrayEquals(createSampleStreamData(), actual.toByteArray());
  94. }
  95. private byte[] createSampleData() {
  96. byte[] bytes = new byte[10];
  97. for (int i = 0; i < 10; i++) {
  98. bytes[i] = (byte) i;
  99. }
  100. return bytes;
  101. }
  102. private byte[] createSampleStreamData() throws IOException {
  103. ByteArrayOutputStream stream = new ByteArrayOutputStream();
  104. stream.write("\nstream\n".getBytes("US-ASCII"));
  105. stream.write(createSampleData());
  106. stream.write("\nendstream".getBytes("US-ASCII"));
  107. return stream.toByteArray();
  108. }
  109. @Test
  110. public void testHash() throws IOException {
  111. assertFalse(getStreamHash(65025).equals(getStreamHash(127076)));
  112. }
  113. private String getStreamHash(int i) throws IOException {
  114. PDFStream stream = new PDFStream();
  115. String txt = "1 0 0 -1 0 790.866 cm\n"
  116. + "q\n"
  117. + "0 g\n"
  118. + "BT\n"
  119. + "/F1 12 Tf\n"
  120. + "1 0 0 -1 0 10.26599979 Tm [(" + i + ")] TJ\n"
  121. + "ET\n";
  122. String img = "q\n"
  123. + "126.734001 0 0 -38.244999 0 54.294998 cm\n"
  124. + "/Im2 Do\n"
  125. + "Q\n";
  126. if (i % 2 == 0) {
  127. stream.add(txt + img + "Q\n");
  128. } else {
  129. stream.add(txt + "Q\n");
  130. }
  131. return stream.streamHashCode();
  132. }
  133. }