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.

AbstractPDFStreamTestCase.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 java.io.OutputStream;
  22. import org.junit.Before;
  23. import org.junit.Test;
  24. import static org.junit.Assert.assertEquals;
  25. /**
  26. * Test case for {@link AbstractPDFStream}.
  27. */
  28. public class AbstractPDFStreamTestCase extends PDFObjectTestCase {
  29. private AbstractPDFStream abstractStream;
  30. private String textData = "This is an arbitrary string for testing.";
  31. private static byte[] encodedBytes;
  32. static {
  33. int[] encoded = { 0x78, 0x9c, 0x0b, 0xc9, 0xc8, 0x2c, 0x56, 0x00, 0xa2, 0xc4, 0x3c, 0x85,
  34. 0xc4, 0xa2, 0xa4, 0xcc, 0x92, 0xa2, 0xc4, 0xa2, 0x4a, 0x85, 0xe2, 0x92, 0xa2, 0xcc,
  35. 0xbc, 0x74, 0x85, 0xb4, 0xfc, 0x22, 0x85, 0x92, 0xd4, 0xe2, 0x12, 0x20, 0x5b, 0x0f,
  36. 0x00, 0x2d, 0x2b, 0x0e, 0xde, 0x0a };
  37. encodedBytes = new byte[encoded.length];
  38. int i = 0;
  39. for (int in : encoded) {
  40. encodedBytes[i++] = (byte) (in & 0xff);
  41. }
  42. }
  43. private String startStream = "<< /Length 5 0 R /Filter /FlateDecode >>\n"
  44. + "stream\n";
  45. private String endStream = "endstream";
  46. @Before
  47. public void setUp() {
  48. abstractStream = new AbstractPDFStream() {
  49. @Override
  50. protected void outputRawStreamData(OutputStream out) throws IOException {
  51. out.write(textData.getBytes());
  52. }
  53. @Override
  54. protected int getSizeHint() throws IOException {
  55. return textData.length();
  56. }
  57. };
  58. abstractStream.setDocument(doc);
  59. abstractStream.setParent(parent);
  60. pdfObjectUnderTest = abstractStream;
  61. }
  62. /**
  63. * Tests output() - ensure that this object is correctly formatted to the output stream.
  64. * @throws IOException if an I/O error occurs
  65. */
  66. @Test
  67. public void testOutput() throws IOException {
  68. // This differs from most other objects, if the object number = 0 an exception is thrown
  69. ByteArrayOutputStream outStream = new ByteArrayOutputStream();
  70. abstractStream.setObjectNumber(1);
  71. ByteArrayOutputStream expectedStream = new ByteArrayOutputStream();
  72. expectedStream.write(startStream.getBytes());
  73. expectedStream.write(encodedBytes);
  74. expectedStream.write(endStream.getBytes());
  75. assertEquals(expectedStream.size(), abstractStream.output(outStream));
  76. assertEquals(expectedStream.toString(), outStream.toString());
  77. }
  78. }