Debian Codegraph @

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

raw
1 /*
2 * Created on 19 feb 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.component;
20
21 import static org.hamcrest.Matchers.*;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertNotNull;
25 import static org.hamcrest.MatcherAssert.assertThat;
26 import static org.junit.Assert.assertTrue;
27 import static org.mockito.Mockito.mock;
28 import static org.sejda.core.service.TestUtils.*;
29
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.List;
34
35 import org.junit.Test;
36 import org.sejda.core.service.TestUtils;
37 import org.sejda.io.SeekableSources;
38 import org.sejda.model.exception.TaskException;
39 import org.sejda.model.exception.UnsupportedTextException;
40 import org.sejda.model.input.PdfFileSource;
41 import org.sejda.model.input.PdfMergeInput;
42 import org.sejda.model.parameter.MergeParameters;
43 import org.sejda.model.toc.ToCPolicy;
44 import org.sejda.sambox.input.PDFParser;
45 import org.sejda.sambox.pdmodel.PDDocument;
46 import org.sejda.sambox.pdmodel.PDPage;
47 import org.sejda.sambox.pdmodel.common.PDRectangle;
48 import org.sejda.sambox.pdmodel.interactive.annotation.PDAnnotationLink;
49 import org.sejda.sambox.pdmodel.interactive.documentnavigation.destination.PDPageXYZDestination;
50
51 /**
52 * @author Andrea Vacondio
53 * @author Edi Weissmann
54 *
55 */
56 public class TableOfContentsCreatorTest {
57
58 @Test(expected = IllegalArgumentException.class)
59 public void nullDocument() {
60 new TableOfContentsCreator(new MergeParameters(), null);
61 }
62
63 @Test(expected = IllegalArgumentException.class)
64 public void nullParams() {
65 new TableOfContentsCreator(null, new PDDocument());
66 }
67
68 @Test
69 public void testShouldGenerateToC() {
70 MergeParameters params = new MergeParameters();
71 params.setTableOfContentsPolicy(ToCPolicy.DOC_TITLES);
72 assertTrue(new TableOfContentsCreator(params, new PDDocument()).shouldGenerateToC());
73 assertFalse(new TableOfContentsCreator(new MergeParameters(), new PDDocument()).shouldGenerateToC());
74 params.setTableOfContentsPolicy(null);
75 assertFalse(new TableOfContentsCreator(new MergeParameters(), new PDDocument()).shouldGenerateToC());
76 }
77
78 @Test
79 public void testHasToc() {
80 MergeParameters params = new MergeParameters();
81 params.setTableOfContentsPolicy(ToCPolicy.DOC_TITLES);
82 TableOfContentsCreator victim = new TableOfContentsCreator(params, new PDDocument());
83 assertFalse(victim.hasToc());
84 victim.appendItem("text", 10, new PDPage());
85 assertTrue(victim.hasToc());
86 }
87
88 @Test(expected = IllegalArgumentException.class)
89 public void testAppendItemInvalidString() {
90 new TableOfContentsCreator(new MergeParameters(), new PDDocument()).appendItem(" ", 10, new PDPage());
91 }
92
93 @Test(expected = IllegalArgumentException.class)
94 public void testAppendItemInvalidPage() {
95 new TableOfContentsCreator(new MergeParameters(), new PDDocument()).appendItem("Text", -10, new PDPage());
96 }
97
98 @Test(expected = IllegalArgumentException.class)
99 public void testAppendItemInvalidAnnotation() {
100 new TableOfContentsCreator(new MergeParameters(), new PDDocument()).appendItem("Text", 10, null);
101 }
102
103 @Test
104 public void testAppendItem() {
105 MergeParameters params = new MergeParameters();
106 params.setTableOfContentsPolicy(ToCPolicy.DOC_TITLES);
107 TableOfContentsCreator victim = new TableOfContentsCreator(params, new PDDocument());
108 assertFalse(victim.hasToc());
109 victim.appendItem("text", 10, new PDPage());
110 assertTrue(victim.hasToc());
111 }
112
113 @Test
114 public void testAddToCTop() throws IOException, TaskException {
115 PDDocument doc = PDFParser.parse(SeekableSources
116 .inMemorySeekableSourceFrom(getClass().getClassLoader().getResourceAsStream("pdf/test_outline.pdf")));
117 PDPage firstPage = doc.getPage(0);
118 assertEquals(3, doc.getNumberOfPages());
119 MergeParameters params = new MergeParameters();
120 params.setTableOfContentsPolicy(ToCPolicy.FILE_NAMES);
121 TableOfContentsCreator victim = new TableOfContentsCreator(params, doc);
122 victim.appendItem("text", 10, new PDPage());
123 victim.addToC();
124 assertEquals(4, doc.getNumberOfPages());
125 assertEquals(firstPage.getCOSObject(), doc.getPage(1).getCOSObject());
126 }
127
128 @Test
129 public void testAddToCWithBlankPageIfOdd() throws IOException, TaskException {
130 PDDocument doc = PDFParser.parse(SeekableSources
131 .inMemorySeekableSourceFrom(getClass().getClassLoader().getResourceAsStream("pdf/test_outline.pdf")));
132 PDPage firstPage = doc.getPage(0);
133 assertEquals(3, doc.getNumberOfPages());
134 MergeParameters params = new MergeParameters();
135 params.setTableOfContentsPolicy(ToCPolicy.FILE_NAMES);
136 params.setBlankPageIfOdd(true);
137 TableOfContentsCreator victim = new TableOfContentsCreator(params, doc);
138 victim.appendItem("text", 10, new PDPage());
139 victim.addToC();
140 assertEquals(5, doc.getNumberOfPages());
141 assertEquals(firstPage.getCOSObject(), doc.getPage(2).getCOSObject());
142 }
143
144 @Test
145 public void testAddTwoPagesToC() throws IOException, TaskException {
146 PDDocument doc = PDFParser.parse(SeekableSources
147 .inMemorySeekableSourceFrom(getClass().getClassLoader().getResourceAsStream("pdf/test_outline.pdf")));
148 PDPage firstPage = doc.getPage(0);
149 assertEquals(3, doc.getNumberOfPages());
150 MergeParameters params = new MergeParameters();
151 params.setTableOfContentsPolicy(ToCPolicy.FILE_NAMES);
152 TableOfContentsCreator victim = new TableOfContentsCreator(params, doc);
153 for (int i = 1; i < 40; i++) {
154 victim.appendItem("text", i, new PDPage());
155 }
156
157 victim.addToC();
158 assertEquals(5, doc.getNumberOfPages());
159 assertEquals(firstPage.getCOSObject(), doc.getPage(2).getCOSObject());
160 }
161
162 @Test
163 public void testAddToCSuperLongText() throws TaskException {
164 TableOfContentsCreator victim = newToCCreator("This is a very long file name and we expect that it's handled correctly and no Exception is thrown by the component. We are making this very very long so we can make sure that even the veeeery long ones are handled.");
165
166 victim.addToC();
167
168 assertEquals(1, victim.getDoc().getNumberOfPages());
169 }
170
171 @Test
172 public void testToCPageSize() throws TaskException {
173 TableOfContentsCreator victim = newToCCreator();
174 victim.pageSizeIfNotSet(PDRectangle.LETTER);
175 victim.addToC();
176 assertEquals(PDRectangle.LETTER, victim.getDoc().getPage(0).getMediaBox());
177 }
178
179 @Test
180 public void testToCForLargePageSize() throws TaskException {
181 TableOfContentsCreator victim = newToCCreator();
182 victim.pageSizeIfNotSet(PDRectangle.A1);
183 victim.addToC();
184 assertEquals(39.64, victim.getFontSize(), 0.1);
185 }
186
187 @Test
188 public void testStringsThatMixMultipleFontRequirements() throws TaskException {
189 TableOfContentsCreator victim = newToCCreator("1-abc-עברית");
190 victim.addToC();
191 assertEquals(1, victim.getDoc().getNumberOfPages());
192 }
193
194 @Test
195 public void indexPageIsConsideredInPageNumbers() throws IOException, TaskException {
196 TableOfContentsCreator victim = newToCCreator(ToCPolicy.DOC_TITLES,
197 Arrays.asList("This is a very long file name and we expect that it's handled correctly and no Exception is thrown by the component. We are making this very very long so we can make sure that even the veeeery long ones are handled."));
198
199 victim.addToC();
200 assertThat(getDocTextNormalized(victim.getDoc()), endsWith("veeeery long ones are handled. 101\n"));
201 }
202
203 @Test
204 public void testTocNumberOfPagesNoToc() throws TaskException {
205 TableOfContentsCreator victim = newToCCreator(ToCPolicy.NONE, Arrays.asList("sample title"));
206 int tocNumPages = victim.addToC();
207 assertEquals(0, tocNumPages);
208 assertEquals(0, victim.getDoc().getNumberOfPages());
209 }
210
211 @Test
212 public void testTocNumberOfPages() throws TaskException {
213 TableOfContentsCreator victim = newToCCreator("sample title");
214 int tocNumPages = victim.addToC();
215 assertEquals(1, tocNumPages);
216 assertEquals(1, victim.getDoc().getNumberOfPages());
217 }
218
219 @Test
220 public void testTocNumberOfPages_AddBlankOption() throws TaskException {
221 MergeParameters params = new MergeParameters();
222 params.setBlankPageIfOdd(true);
223 params.setTableOfContentsPolicy(ToCPolicy.DOC_TITLES);
224 params.addInput(new PdfMergeInput(mock(PdfFileSource.class)));
225 PDDocument doc = new PDDocument();
226 TableOfContentsCreator victim = new TableOfContentsCreator(params, doc);
227 victim.appendItem("sample title", 1, new PDPage());
228 int tocNumPages = victim.addToC();
229 assertEquals(2, tocNumPages);
230 assertEquals(2, doc.getNumberOfPages());
231 }
232
233 @Test
234 public void testTocNumberOfPages_Multiple() throws TaskException {
235 TableOfContentsCreator victim = newToCCreator();
236 victim.pageSizeIfNotSet(PDRectangle.A4);
237 for (int i = 1; i < 40; i++) {
238 victim.appendItem("entry " + i, i, new PDPage());
239 }
240
241 int tocNumPages = victim.addToC();
242 assertEquals(2, tocNumPages);
243 assertEquals(2, victim.getDoc().getNumberOfPages());
244 }
245
246 @Test
247 public void testTocNumberOfPagesMultipleInA2() throws TaskException {
248 TableOfContentsCreator victim = newToCCreator();
249 victim.pageSizeIfNotSet(PDRectangle.A2);
250 for (int i = 1; i < 40; i++) {
251 victim.appendItem("sample " + i + ".pdf", i + 1, new PDPage());
252 }
253
254 // ToC font is scaled so we get 2 pages even if a2 is twice the a4 height
255 int tocNumPages = victim.addToC();
256 assertEquals(2, tocNumPages);
257 assertEquals(2, victim.getDoc().getNumberOfPages());
258 }
259
260 @Test
261 public void test_Toc_Long_Item_That_Wraps_On_Two_Lines() throws TaskException, IOException {
262 MergeParameters params = new MergeParameters();
263 params.setTableOfContentsPolicy(ToCPolicy.FILE_NAMES);
264 PDDocument doc = new PDDocument();
265 PDPage pageDest1 = new PDPage(), pageDest2 = new PDPage(), pageDest3 = new PDPage();
266 TableOfContentsCreator victim = new TableOfContentsCreator(params, doc);
267 victim.appendItem("This is item 1", 1, pageDest1);
268 victim.appendItem(
269 "This is item 2 that has a very long name and should not be truncated so that the version is visible at the end v7.pdf",
270 10, pageDest2);
271 victim.appendItem("This is item 3", 14, pageDest3);
272 victim.pageSizeIfNotSet(PDRectangle.A4);
273 victim.addToC();
274
275 assertPageTextExactLines(doc.getPage(0),
276 "This is item 1 2\n" + "This is item 2 that has a very long name and should not be\n"
277 + "truncated so that the version is visible at the end v7.pdf 11\n"
278 + "This is item 3 15\n");
279
280 // verify size of the clickable annotations on top of the TOC items
281 List<PDAnnotationLink> annotations = TestUtils.getAnnotationsOf(doc.getPage(0), PDAnnotationLink.class);
282
283 // lower left y always changes, height is only different for the middle one that wraps
284 PDAnnotationLink link1 = annotations.get(0);
285 TestUtils.assertPDRectanglesEqual(link1.getRectangle(), new PDRectangle(72.0f, 764.98f, 451.27f, 14f));
286 TestUtils.assertPageDestination(link1, pageDest1);
287
288 PDAnnotationLink link2 = annotations.get(1);
289 TestUtils.assertPDRectanglesEqual(link2.getRectangle(), new PDRectangle(72.0f, 717.38f, 451.27f, 37.79f));
290 TestUtils.assertPageDestination(link2, pageDest2);
291
292 PDAnnotationLink link3 = annotations.get(2);
293 TestUtils.assertPDRectanglesEqual(link3.getRectangle(), new PDRectangle(72.0f, 693.58f, 451.27f, 14f));
294 TestUtils.assertPageDestination(link3, pageDest3);
295 }
296
297 @Test
298 public void test_Toc_Long_Item_That_Wraps_At_The_End_Of_The_Page() throws TaskException {
299 TableOfContentsCreator victim = newToCCreator();
300 for (int i = 0; i < 30; i++) {
301 victim.appendItem("This is an item", 1, new PDPage());
302 }
303
304 victim.appendItem(
305 "This is a long item that has a very long name and should not be truncated so that the version is visible at the end v7.pdf",
306 10, new PDPage());
307 victim.appendItem("This is an item", 14, new PDPage());
308 victim.pageSizeIfNotSet(PDRectangle.A4);
309 victim.addToC();
310
311 TestUtils.assertPageTextDoesNotContain(victim.getDoc().getPage(0), "This is a long item that");
312 TestUtils.assertPageTextContains(victim.getDoc().getPage(1), "This is a long item that");
313 }
314
315 @Test
316 public void test_Toc_Long_Item_That_Has_No_Word_Breaks() throws TaskException {
317 TableOfContentsCreator victim = newToCCreator("This_is_a_file_that_has_a_very_long_name_and_should_not_be_truncated_so_that_the_version_is_visible_at_the_end_v7.pdf");
318 victim.pageSizeIfNotSet(PDRectangle.A4);
319 victim.addToC();
320
321 assertPageTextExactLines(victim.getDoc().getPage(0),
322 "This_is_a_file_that_has_a_very_long_name_and_should_not_be-\n"
323 + "_truncated_so_that_the_version_is_visible_at_the_end_v7.pdf 101\n");
324 }
325
326 @Test
327 public void test_Toc_Item_Requiring_Multiple_Fonts() throws TaskException {
328 TableOfContentsCreator victim = newToCCreator("Item multiple fonts ทดสอบ");
329 victim.pageSizeIfNotSet(PDRectangle.A4);
330 victim.addToC();
331
332 assertPageTextExactLines(victim.getDoc().getPage(0), "Item multiple fonts ทดสอบ 101\n");
333 }
334
335 @Test(expected = UnsupportedTextException.class)
336 public void tocItemsMultipleFontsButNotFound() throws TaskException {
337 TableOfContentsCreator victim = newToCCreator();
338 victim.appendItem("Item multiple fonts հայերէն", 10, new PDPage());
339 victim.pageSizeIfNotSet(PDRectangle.A4);
340 victim.addToC();
341 }
342
343 @Test
344 public void test_Toc_Add_At_Specific_Page() throws TaskException {
345 MergeParameters params = new MergeParameters();
346 params.setTableOfContentsPolicy(ToCPolicy.FILE_NAMES);
347 params.setBlankPageIfOdd(true);
348
349 PDDocument doc = new PDDocument();
350 PDPage pageA = new PDPage(), pageB = new PDPage();
351 doc.addPage(pageA);
352 doc.addPage(pageB);
353
354 PageTextWriter.writeHeader(doc, pageA, "PageA");
355 PageTextWriter.writeHeader(doc, pageB, "PageB");
356
357 TableOfContentsCreator victim = new TableOfContentsCreator(params, doc);
358 victim.appendItem("This is an item", 2, pageB);
359 victim.pageSizeIfNotSet(PDRectangle.A4);
360
361 victim.addToC(1);
362
363 assertThat(doc.getNumberOfPages(), is(4)); // one extra for blank page if odd
364
365 // blank page pageA
366 assertPageTextExactLines(doc.getPage(0), "PageA\n");
367 // toc
368 assertPageTextExactLines(doc.getPage(1), "This is an item 4\n");
369 // extra blank page if odd
370 assertPageTextExactLines(doc.getPage(2), "");
371 // blank page pageB
372 assertPageTextExactLines(doc.getPage(3), "PageB\n");
373 }
374
375 @Test
376 public void tocItemsLinkWithRotatedPages() throws TaskException, IOException {
377 MergeParameters params = new MergeParameters();
378 params.setTableOfContentsPolicy(ToCPolicy.FILE_NAMES);
379 PDDocument doc = new PDDocument();
380 PDPage page = new PDPage(PDRectangle.A4);
381 PDPage page90 = new PDPage(PDRectangle.A4);
382 page90.setRotation(90);
383 PDPage page180 = new PDPage(PDRectangle.A4);
384 page180.setRotation(180);
385 PDPage page270 = new PDPage(PDRectangle.A4);
386 page270.setRotation(270);
387 TableOfContentsCreator victim = new TableOfContentsCreator(params, doc);
388 victim.appendItem("Item ", 1, page);
389 victim.appendItem("Item 90", 2, page90);
390 victim.appendItem("Item 180", 3, page180);
391 victim.appendItem("Item 270", 4, page270);
392 victim.addToC();
393
394 List<PDAnnotationLink> annotations = TestUtils.getAnnotationsOf(doc.getPage(0), PDAnnotationLink.class);
395 PDAnnotationLink link = annotations.get(0);
396 assertNotNull(link);
397 assertThat(link.getDestination(), instanceOf(PDPageXYZDestination.class));
398 assertEquals(0, ((PDPageXYZDestination) link.getDestination()).getLeft());
399 assertEquals((int) PDRectangle.A4.getHeight(), ((PDPageXYZDestination) link.getDestination()).getTop());
400
401 PDAnnotationLink link90 = annotations.get(1);
402 assertNotNull(link90);
403 assertThat(link90.getDestination(), instanceOf(PDPageXYZDestination.class));
404 assertEquals(0, ((PDPageXYZDestination) link90.getDestination()).getLeft());
405 assertEquals(0, ((PDPageXYZDestination) link90.getDestination()).getTop());
406
407 PDAnnotationLink link180 = annotations.get(2);
408 assertNotNull(link180);
409 assertThat(link180.getDestination(), instanceOf(PDPageXYZDestination.class));
410 assertEquals((int) PDRectangle.A4.getWidth(), ((PDPageXYZDestination) link180.getDestination()).getLeft());
411 assertEquals(0, ((PDPageXYZDestination) link180.getDestination()).getTop());
412
413 PDAnnotationLink link270 = annotations.get(3);
414 assertNotNull(link270);
415 assertThat(link270.getDestination(), instanceOf(PDPageXYZDestination.class));
416 assertEquals((int) PDRectangle.A4.getWidth(), ((PDPageXYZDestination) link270.getDestination()).getLeft());
417 assertEquals((int) PDRectangle.A4.getHeight(), ((PDPageXYZDestination) link270.getDestination()).getTop());
418
419 }
420
421 @Test
422 public void testLongTocItemsThatWrapAndGenerateMultiplePagesOfToC_HardToEstimateToCNumberOfPages() throws TaskException, IOException {
423 List<String> entries = new ArrayList<>();
424 for(int i = 1; i <= 26; i++) {
425 if(i % 3 == 0) {
426 entries.add("Attachment " + i + " - Sample file name - longer item that might wrap on the next line");
427 } else {
428 entries.add("Attachment " + i + " - Sample file name - shorter");
429 }
430 }
431
432 TableOfContentsCreator victim = newToCCreator(entries);
433 victim.pageSizeIfNotSet(PDRectangle.A4);
434
435 victim.addToC();
436
437 assertThat(victim.getDoc().getNumberOfPages(), is(2));
438
439 String expected = "Attachment 1 - Sample file name - shorter 102\n" +
440 "Attachment 2 - Sample file name - shorter 103\n" +
441 "Attachment 3 - Sample file name - longer item that might wrap on\n" +
442 "the next line 104\n" +
443 "Attachment 4 - Sample file name - shorter 105\n" +
444 "Attachment 5 - Sample file name - shorter 106\n" +
445 "Attachment 6 - Sample file name - longer item that might wrap on\n" +
446 "the next line 107\n" +
447 "Attachment 7 - Sample file name - shorter 108\n" +
448 "Attachment 8 - Sample file name - shorter 109\n" +
449 "Attachment 9 - Sample file name - longer item that might wrap on\n" +
450 "the next line 110\n" +
451 "Attachment 10 - Sample file name - shorter 111\n" +
452 "Attachment 11 - Sample file name - shorter 112\n" +
453 "Attachment 12 - Sample file name - longer item that might wrap\n" +
454 "on the next line 113\n" +
455 "Attachment 13 - Sample file name - shorter 114\n" +
456 "Attachment 14 - Sample file name - shorter 115\n" +
457 "Attachment 15 - Sample file name - longer item that might wrap\n" +
458 "on the next line 116\n" +
459 "Attachment 16 - Sample file name - shorter 117\n" +
460 "Attachment 17 - Sample file name - shorter 118\n" +
461 "Attachment 18 - Sample file name - longer item that might wrap\n" +
462 "on the next line 119\n" +
463 "Attachment 19 - Sample file name - shorter 120\n" +
464 "Attachment 20 - Sample file name - shorter 121\n" +
465 "Attachment 21 - Sample file name - longer item that might wrap\n" +
466 "on the next line 122\n" +
467 "Attachment 22 - Sample file name - shorter 123\n" +
468 "Attachment 23 - Sample file name - shorter 124\n" +
469 "Attachment 24 - Sample file name - longer item that might wrap\n" +
470 "on the next line 125\n" +
471 "Attachment 25 - Sample file name - shorter 126\n" +
472 "Attachment 26 - Sample file name - shorter 127\n";
473
474 assertDocTextExactLines(victim.getDoc(), expected);
475 }
476
477 @Test
478 public void lastItemDoesNotFitPage_WrapsToNextPage() throws TaskException, IOException {
479 List<String> entries = new ArrayList<>();
480 for(int i = 1; i <= 22; i++) {
481 if(i % 3 == 0) {
482 entries.add("Attachment " + i + " - Sample file name - longer item that might wrap on the next line");
483 } else {
484 entries.add("Attachment " + i + " - Sample file name - shorter");
485 }
486 }
487
488 // this item will not fit the 1st TOC page, will be added to the 2nd
489 entries.add("Attachment final - Sample file name - longer item that might wrap on the next line");
490
491 TableOfContentsCreator victim = newToCCreator(entries);
492 victim.pageSizeIfNotSet(PDRectangle.A4);
493
494 victim.addToC();
495
496 assertThat(victim.getDoc().getNumberOfPages(), is(2));
497 assertPageTextExactLines(victim.getDoc().getPage(1), "Attachment final - Sample file name - longer item that might wrap\non the next line 124\n");
498 }
499
500 @Test
501 public void lastItemBarelyFitsPage_NoWrapToNextPage() throws TaskException, IOException {
502 List<String> entries = new ArrayList<>();
503 for(int i = 1; i <= 22; i++) {
504 if(i % 3 == 0) {
505 entries.add("Attachment " + i + " - Sample file name - longer item that might wrap on the next line");
506 } else {
507 entries.add("Attachment " + i + " - Sample file name - shorter");
508 }
509 }
510
511 // this item will fit the 1st TOC page
512 entries.add("Attachment final - Sample file name - short and fits");
513
514 TableOfContentsCreator victim = newToCCreator(entries);
515 victim.pageSizeIfNotSet(PDRectangle.A4);
516
517 victim.addToC();
518
519 assertThat(victim.getDoc().getNumberOfPages(), is(1));
520 assertThat(getPageTextNormalized(victim.getDoc().getPage(0)), endsWith("\nAttachment final - Sample file name - short and fits 123\n"));
521 }
522
523 @Test(expected = IllegalStateException.class)
524 public void catchesWrongUsage_ItemAddedAfterTocGenerated() throws TaskException {
525 TableOfContentsCreator victim = newToCCreator();
526
527 victim.addToC();
528
529 victim.appendItem("cannot add more", 2, new PDPage());
530 }
531
532 private TableOfContentsCreator newToCCreator() {
533 return newToCCreator("test");
534 }
535
536 private TableOfContentsCreator newToCCreator(String item) {
537 return newToCCreator(Arrays.asList(item));
538 }
539
540 private TableOfContentsCreator newToCCreator(List<String> items) {
541 return newToCCreator(ToCPolicy.FILE_NAMES, items);
542 }
543
544 private TableOfContentsCreator newToCCreator(ToCPolicy toCPolicy, List<String> items) {
545 MergeParameters params = new MergeParameters();
546 params.setTableOfContentsPolicy(toCPolicy);
547
548 return newToCCreator(params, items);
549 }
550
551 private TableOfContentsCreator newToCCreator(MergeParameters params, List<String> items) {
552 PDDocument doc = new PDDocument();
553 TableOfContentsCreator victim = new TableOfContentsCreator(params, doc);
554
555 for (int i = 0; i < items.size(); i++) {
556 victim.appendItem(items.get(i), 100 + i, new PDPage());
557 }
558
559 return victim;
560 }
561 }