@@ -45,23 +45,31 @@ This file is part of the iText (R) project.
45
45
import com .itextpdf .kernel .pdf .xobject .PdfImageXObject ;
46
46
import com .itextpdf .kernel .utils .CompareTool ;
47
47
import com .itextpdf .test .ExtendedITextTest ;
48
+
48
49
import java .io .IOException ;
49
50
import java .nio .charset .StandardCharsets ;
50
51
51
52
import com .itextpdf .test .annotations .type .IntegrationTest ;
53
+
54
+ import java .util .Collections ;
52
55
import org .junit .Assert ;
53
- import org .junit .Before ;
56
+ import org .junit .BeforeClass ;
57
+ import org .junit .Rule ;
54
58
import org .junit .Test ;
55
59
import org .junit .experimental .categories .Category ;
60
+ import org .junit .rules .ExpectedException ;
56
61
57
62
@ Category (IntegrationTest .class )
58
63
public class PdfStreamTest extends ExtendedITextTest {
59
64
60
65
public static final String sourceFolder = "./src/test/resources/com/itextpdf/kernel/pdf/PdfStreamTest/" ;
61
66
public static final String destinationFolder = "./target/test/com/itextpdf/kernel/pdf/PdfStreamTest/" ;
62
67
63
- @ Before
64
- public void before () {
68
+ @ Rule
69
+ public ExpectedException junitExpectedException = ExpectedException .none ();
70
+
71
+ @ BeforeClass
72
+ public static void before () {
65
73
createOrClearDestinationFolder (destinationFolder );
66
74
}
67
75
@@ -108,4 +116,83 @@ public void runLengthEncodingTest01() throws IOException {
108
116
Assert .assertArrayEquals (imgBytes1 , cmpImgBytes1 );
109
117
Assert .assertArrayEquals (imgBytes2 , cmpImgBytes2 );
110
118
}
119
+
120
+ @ Test
121
+ public void indirectRefInFilterAndNoTaggedPdfTest () throws IOException {
122
+ String inFile = sourceFolder + "indirectRefInFilterAndNoTaggedPdf.pdf" ;
123
+ String outFile = destinationFolder + "destIndirectRefInFilterAndNoTaggedPdf.pdf" ;
124
+
125
+ PdfDocument srcDoc = new PdfDocument (new PdfReader (inFile ));
126
+ PdfDocument outDoc = new PdfDocument (new PdfReader (inFile ), new PdfWriter (outFile ));
127
+ outDoc .close ();
128
+
129
+ PdfDocument doc = new PdfDocument (new PdfReader (outFile ));
130
+
131
+ PdfStream outStreamIm1 = doc .getFirstPage ().getResources ().getResource (PdfName .XObject )
132
+ .getAsStream (new PdfName ("Im1" ));
133
+ PdfStream outStreamIm2 = doc .getFirstPage ().getResources ().getResource (PdfName .XObject )
134
+ .getAsStream (new PdfName ("Im2" ));
135
+
136
+ PdfStream cmpStreamIm1 = srcDoc .getFirstPage ().getResources ().getResource (PdfName .XObject )
137
+ .getAsStream (new PdfName ("Im1" ));
138
+ PdfStream cmpStreamIm2 = srcDoc .getFirstPage ().getResources ().getResource (PdfName .XObject )
139
+ .getAsStream (new PdfName ("Im2" ));
140
+
141
+ Assert .assertNull (new CompareTool ().compareStreamsStructure (outStreamIm1 , cmpStreamIm1 ));
142
+ Assert .assertNull (new CompareTool ().compareStreamsStructure (outStreamIm2 , cmpStreamIm2 ));
143
+
144
+ srcDoc .close ();
145
+ outDoc .close ();
146
+ }
147
+
148
+ @ Test
149
+ // TODO DEVSIX-1193 remove junitExpectedException and expected NullPointerException after fix
150
+ public void indirectFilterInCatalogTest () throws IOException {
151
+ String inFile = sourceFolder + "indFilterInCatalog.pdf" ;
152
+
153
+ PdfDocument doc = new PdfDocument (new PdfReader (inFile ),
154
+ new PdfWriter (destinationFolder + "indFilterInCatalog.pdf" ));
155
+
156
+ junitExpectedException .expect (NullPointerException .class );
157
+ doc .close ();
158
+ }
159
+
160
+ @ Test
161
+ // TODO DEVSIX-1193 remove junitExpectedException after fix
162
+ public void indirectFilterFlushedBeforeStreamTest () throws IOException {
163
+ String inFile = sourceFolder + "indFilterInCatalog.pdf" ;
164
+ String out = destinationFolder + "indirectFilterFlushedBeforeStreamTest.pdf" ;
165
+
166
+ PdfDocument pdfDoc = new PdfDocument (new PdfReader (inFile ), new PdfWriter (out ));
167
+
168
+ // Simulate the case in which filter is somehow already flushed before stream.
169
+ // Either directly by user or because of any other reason.
170
+ PdfObject filterObject = pdfDoc .getPdfObject (6 );
171
+ filterObject .flush ();
172
+
173
+ junitExpectedException .expect (NullPointerException .class );
174
+ pdfDoc .close ();
175
+ }
176
+
177
+ @ Test
178
+ // TODO DEVSIX-1193 remove junitExpectedException after fix
179
+ public void indirectFilterMarkedToBeFlushedBeforeStreamTest () throws IOException {
180
+ String inFile = sourceFolder + "indFilterInCatalog.pdf" ;
181
+ String out = destinationFolder + "indirectFilterMarkedToBeFlushedBeforeStreamTest.pdf" ;
182
+
183
+ PdfWriter writer = new PdfWriter (out );
184
+ PdfDocument pdfDoc = new PdfDocument (new PdfReader (inFile ), writer );
185
+
186
+ // Simulate the case when indirect filter object is marked to be flushed before the stream itself.
187
+ PdfObject filterObject = pdfDoc .getPdfObject (6 );
188
+ filterObject .getIndirectReference ().setState (PdfObject .MUST_BE_FLUSHED );
189
+
190
+ // The image stream will be marked as MUST_BE_FLUSHED after page is flushed.
191
+ pdfDoc .getFirstPage ().getPdfObject ().getIndirectReference ().setState (PdfObject .MUST_BE_FLUSHED );
192
+
193
+ junitExpectedException .expect (NullPointerException .class );
194
+ writer .flushWaitingObjects (Collections .<PdfIndirectReference >emptySet ());
195
+
196
+ pdfDoc .close ();
197
+ }
111
198
}
0 commit comments