Debian Codegraph @

sejda-sambox/src/test/java/org/sejda/impl/sambox/util/AcroFormUtilsTest.java

raw
1 /*
2 * Created on 16 set 2016
3 * Copyright 2015 by Andrea Vacondio ([email protected]).
4 * This file is part of Sejda.
5 *
6 * Sejda is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Sejda is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with Sejda. If not, see <http://www.gnu.org/licenses/>.
18 */
19 package org.sejda.impl.sambox.util;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertNotNull;
23 import static org.junit.Assert.assertNull;
24 import static org.junit.Assert.assertTrue;
25
26 import java.io.IOException;
27
28 import org.junit.Test;
29 import org.sejda.io.SeekableSources;
30 import org.sejda.sambox.cos.COSArray;
31 import org.sejda.sambox.cos.COSBase;
32 import org.sejda.sambox.cos.COSName;
33 import org.sejda.sambox.input.PDFParser;
34 import org.sejda.sambox.pdmodel.PDDocument;
35 import org.sejda.sambox.pdmodel.PDResources;
36 import org.sejda.sambox.pdmodel.interactive.form.PDAcroForm;
37
38 /**
39 * @author Andrea Vacondio
40 *
41 */
42 public class AcroFormUtilsTest {
43
44 @Test
45 public void megeDetaults() throws IOException {
46 try (PDDocument anotherDoc = PDFParser.parse(SeekableSources.inMemorySeekableSourceFrom(
47 getClass().getClassLoader().getResourceAsStream("pdf/forms/simple_form_with_full_dic.pdf")))) {
48 PDAcroForm destination = new PDAcroForm(new PDDocument());
49 AcroFormUtils.mergeDefaults(anotherDoc.getDocumentCatalog().getAcroForm(), destination);
50 assertEquals(2, destination.getQuadding());
51 assertEquals("/ArialMT 0 Tf 0 g ", destination.getDefaultAppearance());
52 assertTrue(destination.isNeedAppearances());
53 assertTrue(destination.getDefaultResources().getCOSObject().size() > 0);
54 }
55 }
56
57 @Test
58 public void falseNeedAppearance() {
59 PDAcroForm mergeThis = new PDAcroForm(new PDDocument());
60 mergeThis.setNeedAppearances(false);
61 PDAcroForm intoThis = new PDAcroForm(new PDDocument());
62 intoThis.setNeedAppearances(true);
63 AcroFormUtils.mergeDefaults(mergeThis, intoThis);
64 assertTrue(intoThis.isNeedAppearances());
65 }
66
67 @Test
68 public void blankNeedAppearance() {
69 PDAcroForm mergeThis = new PDAcroForm(new PDDocument());
70 mergeThis.setDefaultAppearance("this");
71 PDAcroForm intoThis = new PDAcroForm(new PDDocument());
72 intoThis.setDefaultAppearance("that");
73 AcroFormUtils.mergeDefaults(mergeThis, intoThis);
74 assertEquals("that", intoThis.getDefaultAppearance());
75 }
76
77 @Test
78 public void negativeQuadding() {
79 PDAcroForm mergeThis = new PDAcroForm(new PDDocument());
80 mergeThis.setQuadding(-2);
81 PDAcroForm intoThis = new PDAcroForm(new PDDocument());
82 AcroFormUtils.mergeDefaults(mergeThis, intoThis);
83 assertEquals(0, intoThis.getQuadding());
84 }
85
86 @Test
87 public void invalidQuadding() {
88 PDAcroForm mergeThis = new PDAcroForm(new PDDocument());
89 mergeThis.setQuadding(20);
90 PDAcroForm intoThis = new PDAcroForm(new PDDocument());
91 AcroFormUtils.mergeDefaults(mergeThis, intoThis);
92 assertEquals(0, intoThis.getQuadding());
93 }
94
95 @Test
96 public void alreadySetQuadding() {
97 PDAcroForm mergeThis = new PDAcroForm(new PDDocument());
98 mergeThis.setQuadding(2);
99 PDAcroForm intoThis = new PDAcroForm(new PDDocument());
100 intoThis.setQuadding(1);
101 AcroFormUtils.mergeDefaults(mergeThis, intoThis);
102 assertEquals(1, intoThis.getQuadding());
103 }
104
105 @Test
106 public void invalidResourceType() {
107 PDAcroForm mergeThis = new PDAcroForm(new PDDocument());
108 PDResources dr = new PDResources();
109 dr.getCOSObject().setInt(COSName.FONT, 10);
110 mergeThis.setDefaultResources(dr);
111 PDAcroForm intoThis = new PDAcroForm(new PDDocument());
112 AcroFormUtils.mergeDefaults(mergeThis, intoThis);
113 assertNull(intoThis.getDefaultResources().getCOSObject().getItem(COSName.FONT));
114 }
115
116 @Test
117 public void mergeFormsWithProcSet() throws IOException {
118
119 try (PDDocument anotherDoc = PDFParser.parse(SeekableSources.inMemorySeekableSourceFrom(
120 getClass().getClassLoader().getResourceAsStream("pdf/forms/simple_form_proc_set.pdf")))) {
121 PDAcroForm destination = new PDAcroForm(new PDDocument());
122 AcroFormUtils.mergeDefaults(anotherDoc.getDocumentCatalog().getAcroForm(), destination);
123 COSBase procSet = destination.getDefaultResources().getCOSObject().getItem(COSName.PROC_SET);
124 assertNotNull(procSet);
125 assertEquals(2, ((COSArray) procSet).size());
126 }
127
128 }
129 }