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 2020/08/27 17:08:02 UTC

svn commit: r1881250 - /pdfbox/branches/issue45/examples/src/test/java/org/apache/pdfbox/examples/pdmodel/TestRubberStampWithImage.java

Author: tilman
Date: Thu Aug 27 17:08:02 2020
New Revision: 1881250

URL: http://svn.apache.org/viewvc?rev=1881250&view=rev
Log:
PDFBOX-4892: increase test coverage

Modified:
    pdfbox/branches/issue45/examples/src/test/java/org/apache/pdfbox/examples/pdmodel/TestRubberStampWithImage.java

Modified: pdfbox/branches/issue45/examples/src/test/java/org/apache/pdfbox/examples/pdmodel/TestRubberStampWithImage.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/issue45/examples/src/test/java/org/apache/pdfbox/examples/pdmodel/TestRubberStampWithImage.java?rev=1881250&r1=1881249&r2=1881250&view=diff
==============================================================================
--- pdfbox/branches/issue45/examples/src/test/java/org/apache/pdfbox/examples/pdmodel/TestRubberStampWithImage.java (original)
+++ pdfbox/branches/issue45/examples/src/test/java/org/apache/pdfbox/examples/pdmodel/TestRubberStampWithImage.java Thu Aug 27 17:08:02 2020
@@ -16,16 +16,31 @@
  */
 package org.apache.pdfbox.examples.pdmodel;
 
-import junit.framework.TestCase;
+import java.awt.image.BufferedImage;
 
 import java.io.File;
 import java.io.IOException;
 
+import javax.imageio.ImageIO;
+
+import org.apache.pdfbox.cos.COSName;
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationRubberStamp;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceEntry;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream;
+import org.apache.pdfbox.rendering.PDFRenderer;
+
+import org.junit.Assert;
+import org.junit.Test;
+
 /**
  * Test for RubberStampWithImage
  */
-public class TestRubberStampWithImage extends TestCase
+public class TestRubberStampWithImage
 {
+    @Test
     public void test() throws IOException
     {
         String documentFile = "src/test/resources/org/apache/pdfbox/examples/pdmodel/document.pdf";
@@ -34,8 +49,48 @@ public class TestRubberStampWithImage ex
 
         new File("target/test-output").mkdirs();
 
+        PDDocument doc1 = PDDocument.load(new File(documentFile));
+        BufferedImage bim1 = new PDFRenderer(doc1).renderImage(0);
+        doc1.close();
+
         String[] args = new String[] { documentFile, outFile, stampFile };
         RubberStampWithImage rubberStamp = new RubberStampWithImage();
         rubberStamp.doIt(args);
+
+        PDDocument doc2 = PDDocument.load(new File(documentFile));
+        BufferedImage bim2 = new PDFRenderer(doc2).renderImage(0);
+        Assert.assertFalse(compareImages(bim1, bim2));
+        PDAnnotationRubberStamp rubberStampAnnotation = (PDAnnotationRubberStamp) doc2.getPage(0).getAnnotations().get(0);
+        PDAppearanceDictionary appearance = rubberStampAnnotation.getAppearance();
+        PDAppearanceEntry normalAppearance = appearance.getNormalAppearance();
+        PDAppearanceStream appearanceStream = normalAppearance.getAppearanceStream();
+        PDImageXObject ximage = (PDImageXObject) appearanceStream.getResources().getXObject(COSName.getPDFName("Im1"));
+        BufferedImage actualStampImage = ximage.getImage();
+        BufferedImage expectedStampImage = ImageIO.read(new File(stampFile));
+        Assert.assertTrue(compareImages(expectedStampImage, actualStampImage));
+        doc2.close();
+    }
+
+    private boolean compareImages(BufferedImage bim1, BufferedImage bim2)
+    {
+        if (bim1.getWidth() != bim2.getWidth())
+        {
+            return false;
+        }
+        if (bim1.getHeight() != bim2.getHeight())
+        {
+            return false;
+        }
+        for (int x = 0; x < bim1.getWidth(); ++x)
+        {
+            for (int y = 0; y < bim1.getHeight(); ++y)
+            {
+                if (bim1.getRGB(x, y) != bim2.getRGB(x, y))
+                {
+                    return false;
+                }
+            }
+        }
+        return true;
     }
-}
+}
\ No newline at end of file