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