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 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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.assertArrayEquals;
  20. import static org.junit.Assert.assertEquals;
  21. import static org.junit.Assert.assertFalse;
  22. import static org.junit.Assert.assertTrue;
  23. import java.io.ByteArrayOutputStream;
  24. import java.io.IOException;
  25. import org.junit.Before;
  26. import org.junit.Test;
  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("stream\n".getBytes("US-ASCII"));
  105. stream.write(createSampleData());
  106. stream.write("\nendstream".getBytes("US-ASCII"));
  107. return stream.toByteArray();
  108. }
  109. }