Skip to content

Commit eeb9e68

Browse files
Add test for copying of tagged documents to partially flushed output
DEVSIX-7006 DEVSIX-7005
1 parent cec4618 commit eeb9e68

File tree

1 file changed

+48
-20
lines changed

1 file changed

+48
-20
lines changed

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

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ This file is part of the iText (R) project.
4444

4545
import com.itextpdf.io.logs.IoLogMessageConstant;
4646
import com.itextpdf.io.font.constants.StandardFonts;
47+
import com.itextpdf.kernel.exceptions.KernelExceptionMessageConstant;
48+
import com.itextpdf.kernel.exceptions.PdfException;
4749
import com.itextpdf.kernel.font.PdfFontFactory;
4850
import com.itextpdf.kernel.geom.Rectangle;
4951
import com.itextpdf.kernel.pdf.annot.PdfLinkAnnotation;
@@ -135,7 +137,7 @@ public void structElemTest01() throws Exception {
135137

136138
document.close();
137139

138-
compareResult("structElemTest01.pdf", "cmp_structElemTest01.pdf", "diff_structElem_01_");
140+
compareResult("structElemTest01.pdf", "cmp_structElemTest01.pdf");
139141
}
140142

141143
@Test
@@ -167,7 +169,7 @@ public void structElemTest02() throws Exception {
167169

168170
document.close();
169171

170-
compareResult("structElemTest02.pdf", "cmp_structElemTest02.pdf", "diff_structElem_02_");
172+
compareResult("structElemTest02.pdf", "cmp_structElemTest02.pdf");
171173
}
172174

173175
@Test
@@ -289,7 +291,7 @@ public void structElemTest04() throws Exception {
289291

290292
document.close();
291293

292-
compareResult("structElemTest04.pdf", "cmp_structElemTest04.pdf", "diff_structElem_04_");
294+
compareResult("structElemTest04.pdf", "cmp_structElemTest04.pdf");
293295
}
294296

295297
@Test
@@ -330,7 +332,7 @@ public void structElemTest05() throws Exception {
330332

331333
document.close();
332334

333-
compareResult("structElemTest05.pdf", "cmp_structElemTest05.pdf", "diff_structElem_05_");
335+
compareResult("structElemTest05.pdf", "cmp_structElemTest05.pdf");
334336
}
335337

336338
@Test
@@ -359,7 +361,7 @@ public void structElemTest06() throws Exception {
359361

360362
document.close();
361363

362-
compareResult("structElemTest06.pdf", "cmp_structElemTest06.pdf", "diff_structElem_06_");
364+
compareResult("structElemTest06.pdf", "cmp_structElemTest06.pdf");
363365
}
364366

365367
@Test
@@ -401,7 +403,7 @@ public void structElemTest07() throws Exception {
401403

402404
document.close();
403405

404-
compareResult("structElemTest07.pdf", "cmp_structElemTest07.pdf", "diff_structElem_07_");
406+
compareResult("structElemTest07.pdf", "cmp_structElemTest07.pdf");
405407
}
406408

407409
@Test
@@ -437,7 +439,7 @@ public void structElemTest08() throws Exception {
437439

438440
document.close();
439441

440-
compareResult("structElemTest08.pdf", "cmp_structElemTest08.pdf", "diff_structElem_08_");
442+
compareResult("structElemTest08.pdf", "cmp_structElemTest08.pdf");
441443
}
442444

443445
@Test
@@ -450,7 +452,7 @@ public void structElemTest09() throws Exception {
450452
document.removePage(1);
451453
document.close();
452454

453-
compareResult("structElemTest09.pdf", "cmp_structElemTest09.pdf", "diff_structElem_09_");
455+
compareResult("structElemTest09.pdf", "cmp_structElemTest09.pdf");
454456
}
455457

456458
@Test
@@ -475,7 +477,7 @@ public void structTreeCopyingTest01() throws Exception {
475477
destination.close();
476478
source.close();
477479

478-
compareResult("structTreeCopyingTest01.pdf", "cmp_structTreeCopyingTest01.pdf", "diff_copying_01_");
480+
compareResult("structTreeCopyingTest01.pdf", "cmp_structTreeCopyingTest01.pdf");
479481
}
480482

481483
@Test
@@ -495,7 +497,7 @@ public void structTreeCopyingTest02() throws Exception {
495497
destination.close();
496498
source.close();
497499

498-
compareResult("structTreeCopyingTest02.pdf", "cmp_structTreeCopyingTest02.pdf", "diff_copying_02_");
500+
compareResult("structTreeCopyingTest02.pdf", "cmp_structTreeCopyingTest02.pdf");
499501
}
500502

501503
@Test
@@ -539,7 +541,7 @@ public void structTreeCopyingTest04() throws Exception {
539541
destination.close();
540542
source.close();
541543

542-
compareResult("structTreeCopyingTest04.pdf", "cmp_structTreeCopyingTest04.pdf", "diff_copying_04_");
544+
compareResult("structTreeCopyingTest04.pdf", "cmp_structTreeCopyingTest04.pdf");
543545
}
544546

545547
@Test
@@ -557,7 +559,7 @@ public void structTreeCopyingTest05() throws Exception {
557559
document1.close();
558560
document2.close();
559561

560-
compareResult("structTreeCopyingTest05.pdf", "cmp_structTreeCopyingTest05.pdf", "diff_copying_05_");
562+
compareResult("structTreeCopyingTest05.pdf", "cmp_structTreeCopyingTest05.pdf");
561563
}
562564

563565
@Test
@@ -576,7 +578,7 @@ public void structTreeCopyingTest06() throws Exception {
576578
destination.close();
577579
source.close();
578580

579-
compareResult("structTreeCopyingTest06.pdf", "cmp_structTreeCopyingTest06.pdf", "diff_copying_06_");
581+
compareResult("structTreeCopyingTest06.pdf", "cmp_structTreeCopyingTest06.pdf");
580582
}
581583

582584
@Test
@@ -611,7 +613,7 @@ public void structTreeCopyingTest07() throws Exception {
611613
document.close();
612614
document1.close();
613615

614-
compareResult("structTreeCopyingTest07.pdf", "cmp_structTreeCopyingTest07.pdf", "diff_copying_07_");
616+
compareResult("structTreeCopyingTest07.pdf", "cmp_structTreeCopyingTest07.pdf");
615617
}
616618

617619
@Test
@@ -626,7 +628,7 @@ public void structTreeCopyingTest08() throws Exception {
626628
document.close();
627629
document1.close();
628630

629-
compareResult("structTreeCopyingTest08.pdf", "cmp_structTreeCopyingTest08.pdf", "diff_copying_08_");
631+
compareResult("structTreeCopyingTest08.pdf", "cmp_structTreeCopyingTest08.pdf");
630632
}
631633

632634
@Test
@@ -642,7 +644,7 @@ public void structTreeCopyingTest09() throws Exception {
642644
document.close();
643645
document1.close();
644646

645-
compareResult("structTreeCopyingTest09.pdf", "cmp_structTreeCopyingTest09.pdf", "diff_copying_09_");
647+
compareResult("structTreeCopyingTest09.pdf", "cmp_structTreeCopyingTest09.pdf");
646648
}
647649

648650
@Test
@@ -662,7 +664,7 @@ public void structTreeCopyingTest10() throws Exception {
662664
document1.close();
663665
document2.close();
664666

665-
compareResult("structTreeCopyingTest10.pdf", "cmp_structTreeCopyingTest10.pdf", "diff_copying_10_");
667+
compareResult("structTreeCopyingTest10.pdf", "cmp_structTreeCopyingTest10.pdf");
666668
}
667669

668670
@Test
@@ -683,7 +685,33 @@ public void structTreeCopyingTest11() throws Exception {
683685
document1.close();
684686
document2.close();
685687

686-
compareResult("structTreeCopyingTest11.pdf", "cmp_structTreeCopyingTest11.pdf", "diff_copying_11_");
688+
compareResult("structTreeCopyingTest11.pdf", "cmp_structTreeCopyingTest11.pdf");
689+
}
690+
691+
@Test
692+
public void structTreeCopyingToPartiallyFlushedDocumentTest() throws Exception {
693+
String outFile = "structTreeCopyingToPartiallyFlushedDocumentTest.pdf";
694+
695+
PdfDocument resultDoc = new PdfDocument(new PdfWriter(destinationFolder + outFile));
696+
resultDoc.setTagged();
697+
698+
PdfDocument document1 = new PdfDocument(new PdfReader(sourceFolder + "quick-brown-fox.pdf"));
699+
document1.copyPagesTo(1, 1, resultDoc);
700+
resultDoc.flushCopiedObjects(document1);
701+
document1.close();
702+
703+
PdfDocument document2 = new PdfDocument(new PdfReader(sourceFolder + "quick-brown-fox.pdf"));
704+
Exception e = Assert.assertThrows(PdfException.class, () -> {
705+
document2.copyPagesTo(1, 1, resultDoc);
706+
});
707+
// TODO DEVSIX-7005 after exception is gone add assertion for the resulting document
708+
Assert.assertEquals(
709+
KernelExceptionMessageConstant.TAG_STRUCTURE_COPYING_FAILED_IT_MIGHT_BE_CORRUPTED_IN_ONE_OF_THE_DOCUMENTS,
710+
e.getMessage()
711+
);
712+
document2.close();
713+
714+
resultDoc.close();
687715
}
688716

689717
@Test
@@ -734,14 +762,14 @@ public void corruptedTagStructureTest04() throws IOException {
734762
document.close();
735763
}
736764

737-
private void compareResult(String outFileName, String cmpFileName, String diffNamePrefix)
765+
private void compareResult(String outFileName, String cmpFileName)
738766
throws IOException, InterruptedException, ParserConfigurationException, SAXException {
739767
CompareTool compareTool = new CompareTool();
740768
String outPdf = destinationFolder + outFileName;
741769
String cmpPdf = sourceFolder + cmpFileName;
742770

743771
String contentDifferences = compareTool.compareByContent(outPdf,
744-
cmpPdf, destinationFolder, diffNamePrefix);
772+
cmpPdf, destinationFolder);
745773
String taggedStructureDifferences = compareTool.compareTagStructures(outPdf, cmpPdf);
746774

747775
String errorMessage = "";

0 commit comments

Comments
 (0)