pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1829674 - /pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/multipdf/PDFMergerUtilityTest.java
Date Fri, 20 Apr 2018 19:19:50 GMT
Author: tilman
Date: Fri Apr 20 19:19:50 2018
New Revision: 1829674

URL: http://svn.apache.org/viewvc?rev=1829674&view=rev
Log:
PDFBOX-3999: check count of structure elements in /K tree

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=1829674&r1=1829673&r2=1829674&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
Fri Apr 20 19:19:50 2018
@@ -18,6 +18,8 @@ package org.apache.pdfbox.multipdf;
 import java.awt.image.BufferedImage;
 import java.io.File;
 import java.io.IOException;
+import java.util.HashSet;
+import java.util.Set;
 
 import junit.framework.TestCase;
 import org.apache.pdfbox.cos.COSArray;
@@ -202,6 +204,12 @@ public class PDFMergerUtilityTest extend
         PDDocument doc = PDDocument.load(new File(TARGETPDFDIR, "PDFBOX-3999-GeneralForbearance.pdf"));
         doc.getDocumentCatalog().getAcroForm().flatten();
         doc.save(new File(TARGETTESTDIR, "PDFBOX-3999-GeneralForbearance-flattened.pdf"));
+
+        ElementCounter elementCounter = new ElementCounter();
+        elementCounter.walk(doc.getDocumentCatalog().getStructureTreeRoot().getK());
+        int singleCnt = elementCounter.cnt;
+        int singleSetSize = elementCounter.set.size();
+
         doc.close();
 
         PDDocument src = PDDocument.load(new File(TARGETTESTDIR, "PDFBOX-3999-GeneralForbearance-flattened.pdf"));
@@ -220,6 +228,48 @@ public class PDFMergerUtilityTest extend
         PDStructureTreeRoot structureTreeRoot = doc.getDocumentCatalog().getStructureTreeRoot();
         checkElement(pageTree, structureTreeRoot.getParentTree().getCOSObject());
         checkElement(pageTree, structureTreeRoot.getK());
+
+        // Assume that the merged tree has double element count
+        elementCounter = new ElementCounter();
+        elementCounter.walk(structureTreeRoot.getK());
+        assertEquals(singleCnt * 2, elementCounter.cnt);
+        assertEquals(singleSetSize * 2, elementCounter.set.size());
+
+        doc.close();
+    }
+
+    private class ElementCounter
+    {
+        int cnt = 0;
+        Set<COSBase> set = new HashSet<>();
+
+        void walk(COSBase base)
+        {
+            if (base instanceof COSArray)
+            {
+                for (COSBase base2 : (COSArray) base)
+                {
+                    if (base2 instanceof COSObject)
+                    {
+                        base2 = ((COSObject) base2).getObject();
+                    }
+                    walk(base2);
+                }
+            }
+            else if (base instanceof COSDictionary)
+            {
+                COSDictionary kdict = (COSDictionary) base;
+                if (kdict.containsKey(COSName.PG))
+                {
+                    ++cnt;
+                    set.add(kdict);
+                }
+                if (kdict.containsKey(COSName.K))
+                {
+                    walk(kdict.getDictionaryObject(COSName.K));
+                }
+            }
+        }
     }
 
     // Each element can be an array, a dictionary or a number.



Mime
View raw message