You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2021/05/29 03:00:59 UTC

svn commit: r1890302 - /pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/multipdf/PDFMergerUtilityTest.java

Author: tilman
Date: Sat May 29 03:00:59 2021
New Revision: 1890302

URL: http://svn.apache.org/viewvc?rev=1890302&view=rev
Log:
PDFBOX-5198: add test

Modified:
    pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/multipdf/PDFMergerUtilityTest.java

Modified: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/multipdf/PDFMergerUtilityTest.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/multipdf/PDFMergerUtilityTest.java?rev=1890302&r1=1890301&r2=1890302&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/multipdf/PDFMergerUtilityTest.java (original)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/multipdf/PDFMergerUtilityTest.java Sat May 29 03:00:59 2021
@@ -56,6 +56,7 @@ import org.apache.pdfbox.pdmodel.interac
 import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
 import org.apache.pdfbox.pdmodel.interactive.form.PDField;
 import org.apache.pdfbox.rendering.PDFRenderer;
+import static org.junit.jupiter.api.Assertions.assertEquals;
 import org.junit.jupiter.api.BeforeAll;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.parallel.Execution;
@@ -709,6 +710,67 @@ class PDFMergerUtilityTest
         Files.delete(outFile.toPath());
     }
 
+    /**
+     * Check that there is a top level Document and Parts below in a merge of 2 documents.
+     *
+     * @param file
+     * @throws IOException 
+     */
+    @Test
+    void testPDFBox5198_2() throws IOException
+    {
+        PDFMergerUtility pdfMergerUtility = new PDFMergerUtility();
+        pdfMergerUtility.addSource(new File(SRCDIR, "PDFA3A.pdf"));
+        pdfMergerUtility.addSource(new File(SRCDIR, "PDFA3A.pdf"));
+        pdfMergerUtility.setDestinationFileName(TARGETTESTDIR + "PDFA3A-merged2.pdf");
+        pdfMergerUtility.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());
+
+        checkParts(new File(TARGETTESTDIR + "PDFA3A-merged2.pdf"));
+    }
+    
+    /**
+     * Check that there is a top level Document and Parts below in a merge of 3 documents.
+     *
+     * @param file
+     * @throws IOException 
+     */
+    @Test
+    void testPDFBox5198_3() throws IOException
+    {
+        PDFMergerUtility pdfMergerUtility = new PDFMergerUtility();
+        pdfMergerUtility.addSource(new File(SRCDIR, "PDFA3A.pdf"));
+        pdfMergerUtility.addSource(new File(SRCDIR, "PDFA3A.pdf"));
+        pdfMergerUtility.addSource(new File(SRCDIR, "PDFA3A.pdf"));
+        pdfMergerUtility.setDestinationFileName(TARGETTESTDIR + "PDFA3A-merged3.pdf");
+        pdfMergerUtility.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());
+
+        checkParts(new File(TARGETTESTDIR + "PDFA3A-merged3.pdf"));
+    }
+
+    /**
+     * Check that there is a top level Document and Parts below.
+     * @param file
+     * @throws IOException 
+     */
+    private void checkParts(File file) throws IOException
+    {
+        try (PDDocument doc = Loader.loadPDF(file))
+        {
+            PDStructureTreeRoot structureTreeRoot = doc.getDocumentCatalog().getStructureTreeRoot();
+            COSDictionary topDict = (COSDictionary) structureTreeRoot.getK();
+            assertEquals(COSName.DOCUMENT, topDict.getItem(COSName.S));
+            assertEquals(structureTreeRoot.getCOSObject(), topDict.getCOSDictionary(COSName.P));
+            COSArray kArray = topDict.getCOSArray(COSName.K);
+            assertEquals(doc.getNumberOfPages(), kArray.size());
+            for (int i = 0; i < kArray.size(); ++i)
+            {
+                COSDictionary dict = (COSDictionary) kArray.getObject(i);
+                assertEquals(COSName.PART, dict.getItem(COSName.S));
+                assertEquals(topDict, dict.getCOSDictionary(COSName.P));
+            }
+        }
+    }
+
     private void checkForPageOrphans(PDDocument doc) throws IOException
     {
         // check for orphan pages in the StructTreeRoot/K, StructTreeRoot/ParentTree and