Debian Codegraph @

sejda-sambox/src/test/java/org/sejda/impl/sambox/component/AnnotationsDistillerTest.java

raw
1 /*
2 * Created on 04/set/2015
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.component;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertFalse;
23 import static org.junit.Assert.assertNull;
24 import static org.hamcrest.MatcherAssert.assertThat;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
29 import static org.sejda.sambox.pdmodel.interactive.annotation.PDAnnotationSquareCircle.SUB_TYPE_SQUARE;
30
31 import java.io.IOException;
32 import java.util.Arrays;
33 import java.util.Collections;
34 import java.util.List;
35 import java.util.stream.Collectors;
36
37 import org.hamcrest.Matchers;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.sejda.commons.LookupTable;
41 import org.sejda.io.SeekableSources;
42 import org.sejda.sambox.cos.COSName;
43 import org.sejda.sambox.input.PDFParser;
44 import org.sejda.sambox.pdmodel.PDDocument;
45 import org.sejda.sambox.pdmodel.PDDocumentCatalog;
46 import org.sejda.sambox.pdmodel.PDPage;
47 import org.sejda.sambox.pdmodel.interactive.action.PDActionGoTo;
48 import org.sejda.sambox.pdmodel.interactive.action.PDActionJavaScript;
49 import org.sejda.sambox.pdmodel.interactive.annotation.PDAnnotation;
50 import org.sejda.sambox.pdmodel.interactive.annotation.PDAnnotationLine;
51 import org.sejda.sambox.pdmodel.interactive.annotation.PDAnnotationLink;
52 import org.sejda.sambox.pdmodel.interactive.annotation.PDAnnotationMarkup;
53 import org.sejda.sambox.pdmodel.interactive.annotation.PDAnnotationPopup;
54 import org.sejda.sambox.pdmodel.interactive.annotation.PDAnnotationSquareCircle;
55 import org.sejda.sambox.pdmodel.interactive.annotation.PDAnnotationText;
56 import org.sejda.sambox.pdmodel.interactive.documentnavigation.destination.PDNamedDestination;
57 import org.sejda.sambox.pdmodel.interactive.documentnavigation.destination.PDPageDestination;
58 import org.sejda.sambox.pdmodel.interactive.documentnavigation.destination.PDPageFitDestination;
59
60 /**
61 * @author Andrea Vacondio
62 *
63 */
64 public class AnnotationsDistillerTest {
65
66 private PDPage oldPage;
67 private PDPage newPage;
68 private LookupTable<PDPage> lookup;
69
70 @Before
71 public void setUp() {
72 oldPage = new PDPage();
73 newPage = new PDPage();
74 lookup = new LookupTable<>();
75 lookup.addLookupEntry(oldPage, newPage);
76 }
77
78 @Test(expected = IllegalArgumentException.class)
79 public void fiterNullDocument() {
80 new AnnotationsDistiller(null);
81 }
82
83 @Test
84 public void orderIsKept() {
85 PDAnnotationSquareCircle one = new PDAnnotationSquareCircle(SUB_TYPE_SQUARE);
86 one.setAnnotationName("one");
87 PDAnnotationText two = new PDAnnotationText();
88 two.setAnnotationName("two");
89 PDAnnotationLine three = new PDAnnotationLine();
90 three.setAnnotationName("three");
91 PDAnnotationSquareCircle four = new PDAnnotationSquareCircle(SUB_TYPE_SQUARE);
92 four.setAnnotationName("four");
93 PDAnnotationLine five = new PDAnnotationLine();
94 five.setAnnotationName("five");
95 PDAnnotationSquareCircle six = new PDAnnotationSquareCircle(SUB_TYPE_SQUARE);
96 six.setAnnotationName("six");
97
98 List<PDAnnotation> annotations = Arrays.asList(one, two, three, four, five, six);
99 oldPage.setAnnotations(annotations);
100 PDDocument doc = new PDDocument();
101 doc.addPage(oldPage);
102 new AnnotationsDistiller(doc).retainRelevantAnnotations(lookup);
103 List<PDAnnotation> annots = newPage.getAnnotations();
104 assertEquals(6, annots.size());
105 assertThat(annots.stream().map(PDAnnotation::getAnnotationName).collect(Collectors.toList()),
106 Matchers.contains(annotations.stream().map(PDAnnotation::getAnnotationName)
107 .map(d -> Matchers.equalTo(d)).collect(Collectors.toList())));
108 }
109
110 @Test
111 public void noLinks() {
112 List<PDAnnotation> annotations = Arrays.asList(new PDAnnotationText());
113 oldPage.setAnnotations(annotations);
114 PDDocument doc = new PDDocument();
115 doc.addPage(oldPage);
116 new AnnotationsDistiller(doc).retainRelevantAnnotations(lookup);
117 assertEquals(1, newPage.getAnnotations().size());
118 }
119
120 public void noLinks_PageRelevant() {
121 PDPage destPage = new PDPage();
122 PDAnnotationText annotation = new PDAnnotationText();
123 annotation.setPage(destPage);
124 List<PDAnnotation> annotations = Arrays.asList(annotation);
125 oldPage.setAnnotations(annotations);
126 PDDocument doc = new PDDocument();
127 doc.addPage(oldPage);
128 LookupTable<PDAnnotation> annotationsLookup = new AnnotationsDistiller(doc).retainRelevantAnnotations(lookup);
129 assertEquals(annotationsLookup.lookup(annotation), newPage.getAnnotations().get(0));
130 }
131
132 @Test
133 public void noLinks_PageNotRelevant() {
134 PDAnnotationText annotation = new PDAnnotationText();
135 annotation.setPage(new PDPage());
136 List<PDAnnotation> annotations = Arrays.asList(annotation);
137 oldPage.setAnnotations(annotations);
138 PDDocument doc = new PDDocument();
139 doc.addPage(oldPage);
140 LookupTable<PDAnnotation> annotationsLookup = new AnnotationsDistiller(doc).retainRelevantAnnotations(lookup);
141 assertEquals(0, newPage.getAnnotations().size());
142 assertTrue(annotationsLookup.isEmpty());
143 }
144
145 @Test
146 public void noLinks_OnePageNotRelevantOneRelevant() {
147 PDPage destPage = new PDPage();
148 PDAnnotationText annotation = new PDAnnotationText();
149 annotation.setPage(destPage);
150 PDAnnotationText annotation2 = new PDAnnotationText();
151 annotation2.setPage(oldPage);
152 List<PDAnnotation> annotations = Arrays.asList(annotation, annotation2);
153 oldPage.setAnnotations(annotations);
154 PDDocument doc = new PDDocument();
155 doc.addPage(oldPage);
156 LookupTable<PDAnnotation> annotationsLookup = new AnnotationsDistiller(doc).retainRelevantAnnotations(lookup);
157 assertEquals(annotationsLookup.lookup(annotation2), newPage.getAnnotations().get(0));
158 }
159
160 @Test
161 public void linksNoGoTo() {
162 PDAnnotationLink annotation = new PDAnnotationLink();
163 annotation.setAction(new PDActionJavaScript());
164 List<PDAnnotation> annotations = Arrays.asList(annotation);
165 oldPage.setAnnotations(annotations);
166 PDDocument doc = new PDDocument();
167 doc.addPage(oldPage);
168 LookupTable<PDAnnotation> annotationsLookup = new AnnotationsDistiller(doc).retainRelevantAnnotations(lookup);
169 assertEquals(annotationsLookup.lookup(annotation), newPage.getAnnotations().get(0));
170 }
171
172 @Test
173 public void links_PageNotRelevant() {
174 PDPage destPage = new PDPage();
175 PDAnnotationLink annotation = new PDAnnotationLink();
176 PDPageDestination dest = new PDPageFitDestination();
177 dest.setPage(destPage);
178 annotation.setDestination(dest);
179 List<PDAnnotation> annotations = Arrays.asList(annotation);
180 oldPage.setAnnotations(annotations);
181 PDDocument doc = new PDDocument();
182 doc.addPage(oldPage);
183 LookupTable<PDAnnotation> annotationsLookup = new AnnotationsDistiller(doc).retainRelevantAnnotations(lookup);
184 assertEquals(0, newPage.getAnnotations().size());
185 assertTrue(annotationsLookup.isEmpty());
186 }
187
188 @Test
189 public void links_PageRelevant() {
190 PDAnnotationLink annotation = new PDAnnotationLink();
191 PDPageDestination dest = new PDPageFitDestination();
192 dest.setPage(oldPage);
193 annotation.setDestination(dest);
194 List<PDAnnotation> annotations = Arrays.asList(annotation);
195 oldPage.setAnnotations(annotations);
196 PDDocument doc = new PDDocument();
197 doc.addPage(oldPage);
198 LookupTable<PDAnnotation> annotationsLookup = new AnnotationsDistiller(doc).retainRelevantAnnotations(lookup);
199 assertEquals(annotationsLookup.lookup(annotation), newPage.getAnnotations().get(0));
200 }
201
202 @Test
203 public void links_OnePageNotRelevantOneRelevant() {
204
205 PDPage destPage = new PDPage();
206 PDAnnotationLink annotation = new PDAnnotationLink();
207 PDPageDestination dest = new PDPageFitDestination();
208 dest.setPage(destPage);
209 annotation.setDestination(dest);
210
211 PDAnnotationLink annotation2 = new PDAnnotationLink();
212 PDPageDestination dest2 = new PDPageFitDestination();
213 dest2.setPage(oldPage);
214 annotation2.setDestination(dest2);
215
216 List<PDAnnotation> annotations = Arrays.asList(annotation, annotation2);
217 oldPage.setAnnotations(annotations);
218 PDDocument doc = new PDDocument();
219 doc.addPage(oldPage);
220 LookupTable<PDAnnotation> annotationsLookup = new AnnotationsDistiller(doc).retainRelevantAnnotations(lookup);
221 assertEquals(annotationsLookup.lookup(annotation2), newPage.getAnnotations().get(0));
222 }
223
224 @Test
225 public void linksGoTo_PageNotRelevant() {
226
227 PDPage destPage = new PDPage();
228 PDAnnotationLink annotation = new PDAnnotationLink();
229 PDPageDestination dest = new PDPageFitDestination();
230 dest.setPage(destPage);
231 PDActionGoTo action = new PDActionGoTo();
232 action.setDestination(dest);
233 annotation.setAction(action);
234 List<PDAnnotation> annotations = Arrays.asList(annotation);
235 oldPage.setAnnotations(annotations);
236 PDDocument doc = new PDDocument();
237 doc.addPage(oldPage);
238 LookupTable<PDAnnotation> annotationsLookup = new AnnotationsDistiller(doc).retainRelevantAnnotations(lookup);
239 assertEquals(0, newPage.getAnnotations().size());
240 assertTrue(annotationsLookup.isEmpty());
241 }
242
243 @Test
244 public void linksGoTo_PageRelevant() {
245 PDAnnotationLink annotation = new PDAnnotationLink();
246 PDPageDestination dest = new PDPageFitDestination();
247 dest.setPage(oldPage);
248 PDActionGoTo action = new PDActionGoTo();
249 action.setDestination(dest);
250 annotation.setAction(action);
251 List<PDAnnotation> annotations = Arrays.asList(annotation);
252 oldPage.setAnnotations(annotations);
253 PDDocument doc = new PDDocument();
254 doc.addPage(oldPage);
255 LookupTable<PDAnnotation> annotationsLookup = new AnnotationsDistiller(doc).retainRelevantAnnotations(lookup);
256 assertEquals(annotationsLookup.lookup(annotation), newPage.getAnnotations().get(0));
257 }
258
259 @Test
260 public void linksGoTo_OnePageNotRelevantOneRelevant() {
261 PDPage destPage = new PDPage();
262 PDAnnotationLink annotation = new PDAnnotationLink();
263 PDPageDestination dest = new PDPageFitDestination();
264 dest.setPage(destPage);
265 PDActionGoTo action = new PDActionGoTo();
266 action.setDestination(dest);
267 annotation.setAction(action);
268
269 PDAnnotationLink annotation2 = new PDAnnotationLink();
270 PDPageDestination dest2 = new PDPageFitDestination();
271 dest2.setPage(oldPage);
272 PDActionGoTo action2 = new PDActionGoTo();
273 action2.setDestination(dest2);
274 annotation2.setAction(action2);
275
276 List<PDAnnotation> annotations = Arrays.asList(annotation, annotation2);
277 oldPage.setAnnotations(annotations);
278 PDDocument doc = new PDDocument();
279 doc.addPage(oldPage);
280 LookupTable<PDAnnotation> annotationsLookup = new AnnotationsDistiller(doc).retainRelevantAnnotations(lookup);
281 assertEquals(annotationsLookup.lookup(annotation2), newPage.getAnnotations().get(0));
282 }
283
284 @Test
285 public void links_NamedPageNotRelevant() throws IOException {
286 PDPage destPage = new PDPage();
287 PDAnnotationLink annotation = new PDAnnotationLink();
288 PDNamedDestination namedDest = new PDNamedDestination(COSName.AESV3);
289 PDPageDestination dest = new PDPageFitDestination();
290 dest.setPage(destPage);
291 annotation.setDestination(namedDest);
292 List<PDAnnotation> annotations = Arrays.asList(annotation);
293 oldPage.setAnnotations(annotations);
294 PDDocument doc = mock(PDDocument.class);
295 PDDocumentCatalog catalog = mock(PDDocumentCatalog.class);
296 when(doc.getDocumentCatalog()).thenReturn(catalog);
297 when(catalog.findNamedDestinationPage(any(PDNamedDestination.class))).thenReturn(dest);
298 doc.addPage(oldPage);
299 LookupTable<PDAnnotation> annotationsLookup = new AnnotationsDistiller(doc).retainRelevantAnnotations(lookup);
300 assertEquals(0, newPage.getAnnotations().size());
301 assertTrue(annotationsLookup.isEmpty());
302 }
303
304 @Test
305 public void links_NamedPageRelevant() throws IOException {
306 PDAnnotationLink annotation = new PDAnnotationLink();
307 PDNamedDestination namedDest = new PDNamedDestination(COSName.AESV3);
308
309 PDPageDestination dest = new PDPageFitDestination();
310 dest.setPage(oldPage);
311 annotation.setDestination(namedDest);
312 List<PDAnnotation> annotations = Arrays.asList(annotation);
313 oldPage.setAnnotations(annotations);
314 PDDocument doc = mock(PDDocument.class);
315 PDDocumentCatalog catalog = mock(PDDocumentCatalog.class);
316 when(doc.getDocumentCatalog()).thenReturn(catalog);
317 when(catalog.findNamedDestinationPage(namedDest)).thenReturn(dest);
318 doc.addPage(oldPage);
319 LookupTable<PDAnnotation> annotationsLookup = new AnnotationsDistiller(doc).retainRelevantAnnotations(lookup);
320 assertEquals(annotationsLookup.lookup(annotation), newPage.getAnnotations().get(0));
321 }
322
323 @Test
324 public void popupRelevant() throws IOException {
325 try (PDDocument doc = PDFParser.parse(SeekableSources.inMemorySeekableSourceFrom(
326 getClass().getClassLoader().getResourceAsStream("pdf/popup_annotation.pdf")))) {
327 PDPage firstOrigin = doc.getPage(0);
328 PDPage firstNew = new PDPage();
329 lookup.addLookupEntry(firstOrigin, firstNew);
330 new AnnotationsDistiller(doc).retainRelevantAnnotations(lookup);
331 List<PDAnnotation> annotations = firstNew.getAnnotations();
332 assertFalse(annotations.isEmpty());
333 List<PDAnnotationMarkup> parent = annotations.stream().filter(a -> a instanceof PDAnnotationPopup)
334 .map(a -> ((PDAnnotationPopup) a).getParent()).collect(Collectors.toList());
335 assertEquals(1, parent.size());
336 assertTrue(annotations.contains(parent.get(0)));
337 }
338 }
339
340 @Test
341 public void removePopupIfGarbage() throws IOException {
342 try (PDDocument doc = PDFParser.parse(SeekableSources.inMemorySeekableSourceFrom(
343 getClass().getClassLoader().getResourceAsStream("pdf/popup_annotation.pdf")))) {
344 PDPage firstOrigin = doc.getPage(0);
345 // let's put some garbage in place of the popup
346 firstOrigin.getAnnotations().stream().filter(a -> !(a instanceof PDAnnotationPopup))
347 .forEach(a -> a.getCOSObject().setItem(COSName.POPUP, new PDPage()));
348 PDPage firstNew = new PDPage();
349 lookup.addLookupEntry(firstOrigin, firstNew);
350 new AnnotationsDistiller(doc).retainRelevantAnnotations(lookup);
351 List<PDAnnotation> annotations = firstNew.getAnnotations();
352 assertFalse(annotations.isEmpty());
353
354 PDAnnotationMarkup parent = annotations.stream().filter(a -> a instanceof PDAnnotationMarkup)
355 .map(a -> ((PDAnnotationMarkup) a)).findFirst().get();
356
357 assertNull(parent.getPopup());
358 }
359 }
360
361 @Test
362 public void popupRelevantRevertedOrder() throws IOException {
363 try (PDDocument doc = PDFParser.parse(SeekableSources.inMemorySeekableSourceFrom(
364 getClass().getClassLoader().getResourceAsStream("pdf/popup_annotation.pdf")))) {
365 PDPage firstOrigin = doc.getPage(0);
366 List<PDAnnotation> annots = firstOrigin.getAnnotations();
367 Collections.reverse(annots);
368 firstOrigin.setAnnotations(annots);
369 PDPage firstNew = new PDPage();
370 lookup.addLookupEntry(firstOrigin, firstNew);
371 new AnnotationsDistiller(doc).retainRelevantAnnotations(lookup);
372 List<PDAnnotation> annotations = firstNew.getAnnotations();
373 assertFalse(annotations.isEmpty());
374 List<PDAnnotationMarkup> parent = annotations.stream().filter(a -> a instanceof PDAnnotationPopup)
375 .map(a -> ((PDAnnotationPopup) a).getParent()).collect(Collectors.toList());
376 assertEquals(1, parent.size());
377 assertTrue(annotations.contains(parent.get(0)));
378 }
379 }
380
381 @Test
382 public void pagesHaveTheSameAnnotations() {
383 PDAnnotationLink annotation = new PDAnnotationLink();
384 PDPageDestination dest = new PDPageFitDestination();
385 dest.setPage(oldPage);
386 annotation.setDestination(dest);
387
388 PDPage secondOld = new PDPage();
389 PDPage secondNew = new PDPage();
390 lookup.addLookupEntry(secondOld, secondNew);
391 List<PDAnnotation> annotations = Arrays.asList(annotation, new PDAnnotationText());
392 oldPage.setAnnotations(annotations);
393 secondOld.setAnnotations(annotations);
394 PDDocument doc = new PDDocument();
395 doc.addPage(oldPage);
396 doc.addPage(secondOld);
397 new AnnotationsDistiller(doc).retainRelevantAnnotations(lookup);
398 assertEquals(oldPage.getAnnotations().size(), newPage.getAnnotations().size());
399 assertEquals(secondOld.getAnnotations().size(), secondNew.getAnnotations().size());
400 }
401 }