Skip to content

Commit f6d90f6

Browse files
Fix tests, refactored PdfOutlinesTest
DEVSIX-608
1 parent ec7a775 commit f6d90f6

16 files changed

+68
-45
lines changed

kernel/src/test/java/com/itextpdf/kernel/pdf/FreeReferencesTest.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ public void freeARefInWrongWayTest01() throws IOException {
377377
}
378378

379379
@Test
380-
@LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.FLUSHED_OBJECT_CONTAINS_REFERENCE_WHICH_NOT_REFER_TO_ANY_OBJECT))
380+
@LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.FLUSHED_OBJECT_CONTAINS_REFERENCE_WHICH_NOT_REFER_TO_ANY_OBJECT, count = 1))
381381
public void freeARefInWrongWayTest02() throws IOException {
382382
String out = "freeARefInWrongWayTest02.pdf";
383383
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(destinationFolder + out));
@@ -399,6 +399,7 @@ public void freeARefInWrongWayTest02() throws IOException {
399399
catalogDict.put(new PdfName("TestArray"), a1);
400400

401401
a2.getIndirectReference().setFree();
402+
402403
List<PdfObject> objects = Arrays.asList(new PdfObject[]{new PdfString("The answer to life is "), new PdfNumber(42)});
403404
new PdfArray(objects)
404405
.makeIndirect(pdfDocument)
@@ -415,15 +416,15 @@ public void freeARefInWrongWayTest02() throws IOException {
415416
"xref\n" +
416417
"0 10\n" +
417418
"0000000007 65535 f \n" +
418-
"0000000429 00000 n \n" +
419-
"0000000656 00000 n \n" +
420-
"0000000490 00000 n \n" +
421-
"0000000314 00000 n \n" +
422-
"0000000241 00000 n \n" +
423-
"0000000057 00000 n \n" +
419+
"0000000433 00000 n \n" +
420+
"0000000660 00000 n \n" +
421+
"0000000494 00000 n \n" +
422+
"0000000318 00000 n \n" +
423+
"0000000245 00000 n \n" +
424+
"0000000060 00000 n \n" +
424425
"0000000000 00001 f \n" +
425426
"0000000015 00000 n \n" +
426-
"0000000707 00000 n \n"
427+
"0000000711 00000 n \n"
427428
};
428429
compareXrefTables(xrefString, expected);
429430
}
@@ -875,6 +876,7 @@ public void notUsedIndRef03() throws IOException {
875876
}
876877

877878
@Test
879+
@LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.INVALID_INDIRECT_REFERENCE))
878880
public void corruptedDocIndRefToFree01() throws IOException {
879881
String src = "corruptedDocIndRefToFree.pdf";
880882
String out = "corruptedDocIndRefToFree01.pdf";

kernel/src/test/java/com/itextpdf/kernel/pdf/PdfDocumentTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,16 @@ This file is part of the iText (R) project.
4242
*/
4343
package com.itextpdf.kernel.pdf;
4444

45+
import com.itextpdf.io.LogMessageConstant;
4546
import com.itextpdf.io.image.ImageDataFactory;
4647
import com.itextpdf.io.source.DeflaterOutputStream;
4748
import com.itextpdf.kernel.font.PdfFontFactory;
4849
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
4950
import com.itextpdf.kernel.pdf.navigation.PdfDestination;
5051
import com.itextpdf.kernel.utils.CompareTool;
5152
import com.itextpdf.test.ExtendedITextTest;
53+
import com.itextpdf.test.annotations.LogMessage;
54+
import com.itextpdf.test.annotations.LogMessages;
5255
import com.itextpdf.test.annotations.type.IntegrationTest;
5356
import org.junit.Assert;
5457
import org.junit.BeforeClass;
@@ -294,6 +297,7 @@ public void testImageCompressLevel() throws IOException {
294297
}
295298

296299
@Test
300+
@LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.FLUSHED_OBJECT_CONTAINS_REFERENCE_WHICH_NOT_REFER_TO_ANY_OBJECT))
297301
public void testFreeReference() throws IOException, InterruptedException {
298302
PdfWriter writer = new PdfWriter(destinationFolder + "freeReference.pdf", new WriterProperties().setFullCompressionMode(false));
299303
PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourceFolder + "baseFreeReference.pdf"), writer);

kernel/src/test/java/com/itextpdf/kernel/pdf/PdfOutlineTest.java

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,14 @@ public class PdfOutlineTest extends ExtendedITextTest {
7171
public static final String destinationFolder = "./target/test/com/itextpdf/kernel/pdf/PdfOutlineTest/";
7272

7373
@BeforeClass
74-
public static void beforeClass() throws FileNotFoundException {
75-
createDestinationFolder(destinationFolder);
76-
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(destinationFolder + "documentWithOutlines.pdf"));
74+
public static void before() {
75+
createOrClearDestinationFolder(destinationFolder);
76+
}
77+
78+
@Test
79+
public void createSimpleDocWithOutlines() throws IOException, InterruptedException {
80+
String filename = "simpleDocWithOutlines.pdf";
81+
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(destinationFolder + filename));
7782
pdfDoc.getCatalog().setPageMode(PdfName.UseOutlines);
7883

7984
PdfPage firstPage = pdfDoc.addNewPage();
@@ -86,6 +91,8 @@ public static void beforeClass() throws FileNotFoundException {
8691
secondOutline.addDestination(PdfExplicitDestination.createFit(secondPage));
8792

8893
pdfDoc.close();
94+
95+
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + filename, sourceFolder + "cmp_" + filename, destinationFolder, "diff_"));
8996
}
9097

9198
@Test
@@ -113,12 +120,11 @@ public void outlinesWithPagesTest() throws IOException {
113120
}
114121
}
115122

116-
@Before
117-
public void setupAddOutlinesToDocumentTest() throws IOException {
118-
String filename = sourceFolder + "iphone_user_guide.pdf";
119-
120-
PdfReader reader = new PdfReader(filename);
121-
PdfWriter writer = new PdfWriter(destinationFolder + "addOutlinesResult.pdf");
123+
@Test
124+
public void addOutlinesToDocumentTest() throws IOException, InterruptedException {
125+
PdfReader reader = new PdfReader(sourceFolder + "iphone_user_guide.pdf");
126+
String filename = "addOutlinesToDocumentTest.pdf";
127+
PdfWriter writer = new PdfWriter(destinationFolder + filename);
122128
PdfDocument pdfDoc = new PdfDocument(reader, writer);
123129
pdfDoc.setTagged();
124130

@@ -135,11 +141,13 @@ public void setupAddOutlinesToDocumentTest() throws IOException {
135141
outlines.getAllChildren().get(0).getAllChildren().get(1).addOutline("testOutline", 1).addDestination(PdfExplicitDestination.createFit(pdfDoc.getPage(102)));
136142

137143
pdfDoc.close();
144+
145+
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + filename, sourceFolder + "cmp_" + filename, destinationFolder, "diff_"));
138146
}
139147

140148
@Test
141-
public void addOutlinesToDocumentTest() throws IOException, InterruptedException {
142-
String filename = destinationFolder + "addOutlinesResult.pdf";
149+
public void readOutlinesFromDocumentTest() throws IOException, InterruptedException {
150+
String filename = sourceFolder + "addOutlinesResult.pdf";
143151

144152
PdfDocument pdfDoc = new PdfDocument(new PdfReader(filename));
145153
PdfOutline outlines = pdfDoc.getOutlines(false);
@@ -151,19 +159,21 @@ public void addOutlinesToDocumentTest() throws IOException, InterruptedException
151159
}
152160
}
153161

154-
@Before
155-
public void setupRemovePageWithOutlinesTest() throws IOException {
156-
String filename = sourceFolder + "iphone_user_guide.pdf";
157-
158-
PdfDocument pdfDoc = new PdfDocument(new PdfReader(filename), new PdfWriter(destinationFolder + "removePagesWithOutlinesResult.pdf"));
162+
@Test
163+
@LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.FLUSHED_OBJECT_CONTAINS_REFERENCE_WHICH_NOT_REFER_TO_ANY_OBJECT, count = 36)) // TODO DEVSIX-1583: destinations are not removed along with page
164+
public void removePageWithOutlinesTest() throws IOException, InterruptedException {
165+
String filename = "removePageWithOutlinesTest.pdf";
166+
PdfDocument pdfDoc = new PdfDocument(new PdfReader(sourceFolder + "iphone_user_guide.pdf"), new PdfWriter(destinationFolder + filename));
159167
pdfDoc.removePage(102);
160168

161169
pdfDoc.close();
170+
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + filename, sourceFolder + "cmp_" + filename, destinationFolder, "diff_"));
162171
}
163172

164173
@Test
165-
public void removePageWithOutlinesTest() throws IOException {
166-
String filename = destinationFolder + "removePagesWithOutlinesResult.pdf";
174+
public void readRemovedPageWithOutlinesTest() throws IOException {
175+
// TODO DEVSIX-1583: src document is taken from the previous removePageWithOutlinesTest test, however it contains numerous destination objects which contain PdfNull instead of page reference
176+
String filename = sourceFolder + "removePagesWithOutlinesResult.pdf";
167177

168178
PdfDocument pdfDoc = new PdfDocument(new PdfReader(filename));
169179

@@ -176,22 +186,24 @@ public void removePageWithOutlinesTest() throws IOException {
176186
}
177187
}
178188

179-
@Before
180-
public void setupUpdateOutlineTitle() throws IOException {
181-
String filename = sourceFolder + "iphone_user_guide.pdf";
182-
PdfReader reader = new PdfReader(filename);
183-
PdfWriter writer = new PdfWriter(destinationFolder + "updateOutlineTitleResult.pdf");
189+
@Test
190+
public void updateOutlineTitle() throws IOException, InterruptedException {
191+
PdfReader reader = new PdfReader(sourceFolder + "iphone_user_guide.pdf");
192+
String filename = "updateOutlineTitle.pdf";
193+
PdfWriter writer = new PdfWriter(destinationFolder + filename);
184194
PdfDocument pdfDoc = new PdfDocument(reader, writer);
185195

186196
PdfOutline outlines = pdfDoc.getOutlines(false);
187197
outlines.getAllChildren().get(0).getAllChildren().get(1).setTitle("New Title");
188198

189199
pdfDoc.close();
200+
201+
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + filename, sourceFolder + "cmp_" + filename, destinationFolder, "diff_"));
190202
}
191203

192204
@Test
193-
public void updateOutlineTitle() throws IOException {
194-
String filename = destinationFolder + "updateOutlineTitleResult.pdf";
205+
public void readOutlineTitle() throws IOException {
206+
String filename = sourceFolder + "updateOutlineTitleResult.pdf";
195207
PdfDocument pdfDoc = new PdfDocument(new PdfReader(filename));
196208

197209
PdfOutline outlines = pdfDoc.getOutlines(false);
@@ -203,12 +215,11 @@ public void updateOutlineTitle() throws IOException {
203215
}
204216
}
205217

206-
@Before
207-
public void setupAddOutlineInNotOutlineMode() throws IOException {
208-
String filename = sourceFolder + "iphone_user_guide.pdf";
209-
210-
PdfReader reader = new PdfReader(filename);
211-
PdfWriter writer = new PdfWriter(destinationFolder + "addOutlinesWithoutOutlineModeResult.pdf");
218+
@Test
219+
public void addOutlineInNotOutlineMode() throws IOException, InterruptedException {
220+
String filename = "addOutlineInNotOutlineMode.pdf";
221+
PdfReader reader = new PdfReader(sourceFolder + "iphone_user_guide.pdf");
222+
PdfWriter writer = new PdfWriter(destinationFolder + filename);
212223
PdfDocument pdfDoc = new PdfDocument(reader, writer);
213224

214225
PdfOutline outlines = new PdfOutline(pdfDoc);
@@ -223,11 +234,13 @@ public void setupAddOutlineInNotOutlineMode() throws IOException {
223234
secondPageChild.addDestination(PdfExplicitDestination.createFit(pdfDoc.getPage(2)));
224235

225236
pdfDoc.close();
237+
238+
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + filename, sourceFolder + "cmp_" + filename, destinationFolder, "diff_"));
226239
}
227240

228241
@Test
229-
public void addOutlineInNotOutlineMode() throws IOException {
230-
String filename = destinationFolder + "addOutlinesWithoutOutlineModeResult.pdf";
242+
public void readOutlineAddedInNotOutlineMode() throws IOException {
243+
String filename = sourceFolder + "addOutlinesWithoutOutlineModeResult.pdf";
231244
PdfDocument pdfDoc = new PdfDocument(new PdfReader(filename));
232245

233246
List<PdfOutline> pageOutlines = pdfDoc.getPage(102).getOutlines(true);
@@ -240,7 +253,7 @@ public void addOutlineInNotOutlineMode() throws IOException {
240253

241254
@Test
242255
public void createDocWithOutlines() throws IOException, InterruptedException {
243-
String filename = destinationFolder + "documentWithOutlines.pdf";
256+
String filename = sourceFolder + "documentWithOutlines.pdf";
244257

245258
PdfDocument pdfDoc = new PdfDocument(new PdfReader(filename));
246259

kernel/src/test/java/com/itextpdf/kernel/pdf/PdfPagesTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,8 @@ public void removeFlushedPage() throws IOException {
289289
page.flush();
290290
}
291291

292-
Assert.assertEquals("Remove last page", true, pdfDoc.removePage(pdfDoc.getPage(pageCount)));
293-
Assert.assertEquals("Free reference", true, pdfDoc.getXref().get(removedPageObjectNumber).checkState(PdfObject.FREE));
292+
Assert.assertTrue("Remove last page", pdfDoc.removePage(pdfDoc.getPage(pageCount)));
293+
Assert.assertFalse("Free reference", pdfDoc.getXref().get(removedPageObjectNumber).checkState(PdfObject.FREE));
294294

295295
pdfDoc.close();
296296
verifyPagesOrder(destinationFolder + filename, pageCount - 1);

kernel/src/test/java/com/itextpdf/kernel/pdf/PdfReaderTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ public void indirectsChain3() throws IOException {
330330
}
331331

332332
@Test
333+
@LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.INVALID_INDIRECT_REFERENCE))
333334
public void invalidIndirect() throws IOException {
334335
String filename = sourceFolder + "invalidIndirect.pdf";
335336

@@ -747,7 +748,7 @@ public void correctSimpleDoc3() throws IOException {
747748
}
748749

749750
@Test
750-
@Ignore("ignore") //test with abnormal object declaration
751+
@Ignore("test with abnormal object declaration")
751752
public void correctSimpleDoc4() throws IOException {
752753
String filename = sourceFolder + "correctSimpleDoc4.pdf";
753754

@@ -882,6 +883,7 @@ public void fixPdfTest06() throws IOException {
882883
}
883884

884885
@Test
886+
@LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.INVALID_INDIRECT_REFERENCE, count = 2))
885887
public void fixPdfTest07() throws IOException {
886888
String filename = sourceFolder + "XRefSectionWithFreeReferences1.pdf";
887889

@@ -948,6 +950,7 @@ public void fixPdfTest09() throws IOException {
948950
}
949951

950952
@Test
953+
@LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.INVALID_INDIRECT_REFERENCE, count = 1))
951954
public void fixPdfTest10() throws IOException {
952955
String filename = sourceFolder + "XRefSectionWithFreeReferences4.pdf";
953956

@@ -1466,6 +1469,7 @@ public void StreamLengthCorrection9() throws IOException {
14661469
}
14671470

14681471
@Test
1472+
@LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.INVALID_INDIRECT_REFERENCE))
14691473
public void freeReferencesTest() throws IOException {
14701474
String filename = sourceFolder + "freeReferences.pdf";
14711475

0 commit comments

Comments
 (0)