aboutsummaryrefslogtreecommitdiff
path: root/pdf-as-pdfbox-2/src
diff options
context:
space:
mode:
authorAlexander Marsalek <amarsalek@iaik.tugraz.at>2021-07-22 17:15:06 +0200
committerAlexander Marsalek <amarsalek@iaik.tugraz.at>2021-07-22 17:15:06 +0200
commitf56db3e87ce5cf7f8b345796cc95660a872b3a18 (patch)
tree3571032028cf8d52c64bd63b5946c82a1d89957f /pdf-as-pdfbox-2/src
parent7617dbe08583cc8b7fa63670a7dbb860d3f25023 (diff)
downloadpdf-as-4-f56db3e87ce5cf7f8b345796cc95660a872b3a18.tar.gz
pdf-as-4-f56db3e87ce5cf7f8b345796cc95660a872b3a18.tar.bz2
pdf-as-4-f56db3e87ce5cf7f8b345796cc95660a872b3a18.zip
added tests
Diffstat (limited to 'pdf-as-pdfbox-2/src')
-rw-r--r--pdf-as-pdfbox-2/src/test/java/at/gv/egiz/pdfas/lib/testpdfbox/SignatureFieldsAndPlaceHolderExtractorTest.java153
1 files changed, 139 insertions, 14 deletions
diff --git a/pdf-as-pdfbox-2/src/test/java/at/gv/egiz/pdfas/lib/testpdfbox/SignatureFieldsAndPlaceHolderExtractorTest.java b/pdf-as-pdfbox-2/src/test/java/at/gv/egiz/pdfas/lib/testpdfbox/SignatureFieldsAndPlaceHolderExtractorTest.java
index e137c16e..0d85c82b 100644
--- a/pdf-as-pdfbox-2/src/test/java/at/gv/egiz/pdfas/lib/testpdfbox/SignatureFieldsAndPlaceHolderExtractorTest.java
+++ b/pdf-as-pdfbox-2/src/test/java/at/gv/egiz/pdfas/lib/testpdfbox/SignatureFieldsAndPlaceHolderExtractorTest.java
@@ -3,43 +3,168 @@ package at.gv.egiz.pdfas.lib.testpdfbox;
import at.gv.egiz.pdfas.lib.impl.pdfbox2.placeholder.SignatureFieldsAndPlaceHolderExtractor;
import at.gv.egiz.pdfas.lib.impl.placeholder.SignaturePlaceholderData;
import org.apache.pdfbox.pdmodel.PDDocument;
+import org.junit.Assert;
+import org.junit.Test;
import java.io.File;
+import java.util.ArrayList;
+import java.util.Arrays;
import java.util.List;
public class SignatureFieldsAndPlaceHolderExtractorTest {
- public static void main(String[] args) {
- String dir = "/Users/amarsalek/Documents/pdf-as-4/pdf-as-pdfbox-2/src/test/resources/";
+ public String getPath(String resourceName) {
+ ClassLoader classLoader = this.getClass().getClassLoader();
+ File file = new File(classLoader.getResource(resourceName).getFile());
+ String absolutePath = file.getAbsolutePath();
- getPlacHolders(dir+ "manySignFields.pdf");
- getPlacHolders(dir+ "manySignFields_signed4.pdf");
- getNextSignaturePlaceHolder(dir + "new_qr_2-2.pdf");
- getNextSignaturePlaceHolder(dir + "new_qr_2_signed.pdf");
- getNextSignaturePlaceHolder(dir + "new_qr_2_signed_signed.pdf");
- getNextSignaturePlaceHolder(dir + "new_qr_2_signed_signed_signed.pdf");
- //TODO convert into junit test
+ System.out.println(absolutePath);
+ return absolutePath;
}
- private static void getPlacHolders(String filePath) {
+ @Test
+ public void notSigned(){
+ SignaturePlaceholderData result = getNextSignaturePlaceHolder(getPath("new_qr_2-2.pdf"));
+ Assert.assertEquals("Image5",result.getPlaceholderName());
+ }
+ @Test
+ public void signedOnce(){
+ SignaturePlaceholderData result = getNextSignaturePlaceHolder(getPath("new_qr_2_signed.pdf"));
+ Assert.assertEquals("Image8",result.getPlaceholderName());
+ }
+ @Test
+ public void signedTwice(){
+ SignaturePlaceholderData result = getNextSignaturePlaceHolder(getPath("new_qr_2_signed_signed.pdf"));
+ Assert.assertEquals(null,result);
+ }
+ @Test
+ public void signedThrice(){
+ SignaturePlaceholderData result = getNextSignaturePlaceHolder(getPath("new_qr_2_signed_signed_signed.pdf"));
+ Assert.assertEquals(null,result);
+ }
+
+ @Test
+ public void noPlaceHolder(){
+ SignaturePlaceholderData result = getNextSignaturePlaceHolder(getPath("manySignFields.pdf"));
+ Assert.assertEquals(null,result);
+ }
+ @Test
+ public void subsequentCalls(){
+ SignaturePlaceholderData result = getNextSignaturePlaceHolder(getPath("new_qr_2_signed_signed_signed.pdf"));
+ Assert.assertEquals(null,result);
+
+ result = getNextSignaturePlaceHolder(getPath("new_qr_2_signed.pdf"));
+ Assert.assertEquals("Image8",result.getPlaceholderName());
+
+ result = getNextSignaturePlaceHolder(getPath("new_qr_2-2.pdf"));
+ Assert.assertEquals("Image5",result.getPlaceholderName());
+
+ result = getNextSignaturePlaceHolder(getPath("new_qr_2-2.pdf"));
+ Assert.assertEquals("Image5",result.getPlaceholderName());
+
+ result = getNextSignaturePlaceHolder(getPath("new_qr_2-2.pdf"));
+ Assert.assertEquals("Image5",result.getPlaceholderName());
+
+ result = getNextSignaturePlaceHolder(getPath("new_qr_2_signed.pdf"));
+ Assert.assertEquals("Image8",result.getPlaceholderName());
+
+ result = getNextSignaturePlaceHolder(getPath("new_qr_2_signed_signed_signed.pdf"));
+ Assert.assertEquals(null,result);
+
+ result = getNextSignaturePlaceHolder(getPath("new_qr_2-2.pdf"));
+ Assert.assertEquals("Image5",result.getPlaceholderName());
+
+ result = getNextSignaturePlaceHolder(getPath("new_qr_2_signed.pdf"));
+ Assert.assertEquals("Image8",result.getPlaceholderName());
+ }
+ @Test
+ public void notSignedAndNoFields(){
+ List<String> result = getPlaceHolders(getPath("new_qr_2-2.pdf"));
+
+ List<String> expectedResult = new ArrayList<>();
+ Assert.assertEquals(expectedResult,result);
+ }
+
+ @Test
+ public void notSignedFields(){
+ List<String> result = getPlaceHolders(getPath("manySignFields.pdf"));
+
+ List<String> expectedResult = Arrays.asList("Signature_0", "Signature_1", "Signature_2", "Signature_3",
+ "Signature_4", "Signature_5", "Signature_6", "Signature_7");
+ Assert.assertEquals(expectedResult,result);
+ }
+
+ @Test
+ public void signedOncePosition4FieldTest(){
+ List<String> result = getPlaceHolders(getPath("manySignFields_signed4.pdf"));
+
+ List<String> expectedResult = Arrays.asList("Signature_0", "Signature_1", "Signature_2", "Signature_3",
+ "Signature_5", "Signature_6", "Signature_7");
+ Assert.assertEquals(expectedResult,result);
+ }
+
+ @Test
+ public void multipleCallsFieldTest(){
+ List<String> result = getPlaceHolders(getPath("manySignFields_signed4.pdf"));
+ List<String> expectedResult = Arrays.asList("Signature_0", "Signature_1", "Signature_2", "Signature_3",
+ "Signature_5", "Signature_6", "Signature_7");
+ Assert.assertEquals(expectedResult,result);
+
+ result = getPlaceHolders(getPath("manySignFields_signed4.pdf"));
+ expectedResult = Arrays.asList("Signature_0", "Signature_1", "Signature_2", "Signature_3",
+ "Signature_5", "Signature_6", "Signature_7");
+ Assert.assertEquals(expectedResult,result);
+
+ result = getPlaceHolders(getPath("manySignFields.pdf"));
+ expectedResult = Arrays.asList("Signature_0", "Signature_1", "Signature_2", "Signature_3",
+ "Signature_4", "Signature_5", "Signature_6", "Signature_7");
+ Assert.assertEquals(expectedResult,result);
+
+ result = getPlaceHolders(getPath("manySignFields.pdf"));
+ expectedResult = Arrays.asList("Signature_0", "Signature_1", "Signature_2", "Signature_3",
+ "Signature_4", "Signature_5", "Signature_6", "Signature_7");
+ Assert.assertEquals(expectedResult,result);
+
+ result = getPlaceHolders(getPath("manySignFields_signed4.pdf"));
+ expectedResult = Arrays.asList("Signature_0", "Signature_1", "Signature_2", "Signature_3",
+ "Signature_5", "Signature_6", "Signature_7");
+ Assert.assertEquals(expectedResult,result);
+
+ result = getPlaceHolders(getPath("manySignFields_signed4.pdf"));
+ expectedResult = Arrays.asList("Signature_0", "Signature_1", "Signature_2", "Signature_3",
+ "Signature_5", "Signature_6", "Signature_7");
+ Assert.assertEquals(expectedResult,result);
+
+ result = getPlaceHolders(getPath("manySignFields.pdf"));
+ expectedResult = Arrays.asList("Signature_0", "Signature_1", "Signature_2", "Signature_3",
+ "Signature_4", "Signature_5", "Signature_6", "Signature_7");
+ Assert.assertEquals(expectedResult,result);
+
+ }
+
+ private static List<String> getPlaceHolders(String filePath) {
try {
PDDocument doc = PDDocument.load(new File(filePath));
- List<String> result = SignatureFieldsAndPlaceHolderExtractor.findEmptySignatureFields(doc);
- System.out.println(filePath + ": " + result);
+ List<String> results = SignatureFieldsAndPlaceHolderExtractor.findEmptySignatureFields(doc);
+// System.out.println(filePath + ": " + result);
+ return results;
} catch (Throwable e) {
e.printStackTrace();
}
+ return null;
}
- public static void getNextSignaturePlaceHolder(String filePath) {
+ public static SignaturePlaceholderData getNextSignaturePlaceHolder(String filePath) {
try {
PDDocument doc = PDDocument.load(new File(filePath));
SignaturePlaceholderData result =
SignatureFieldsAndPlaceHolderExtractor.getNextUnusedSignaturePlaceHolder(doc);
- System.out.println(filePath + ": " + result);
+// System.out.println(filePath + ": " + result);
+ return result;
} catch (Throwable e) {
e.printStackTrace();
}
+ return null;
}
}