| 1 |
/* |
| 2 |
* This file is part of the Sejda source code |
| 3 |
* Created on 11/mar/2015 |
| 4 |
* Copyright 2013-2014 by Andrea Vacondio ([email protected]). |
| 5 |
* |
| 6 |
* This program is free software: you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU Affero General Public License as |
| 8 |
* published by the Free Software Foundation, either version 3 of the |
| 9 |
* License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This program 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 this program. 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.assertTrue; |
| 23 |
|
| 24 |
import java.io.IOException; |
| 25 |
|
| 26 |
import org.junit.Test; |
| 27 |
import org.sejda.io.SeekableSources; |
| 28 |
import org.sejda.sambox.input.PDFParser; |
| 29 |
import org.sejda.sambox.pdmodel.PDDocument; |
| 30 |
import org.sejda.sambox.pdmodel.PDPage; |
| 31 |
import org.sejda.sambox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline; |
| 32 |
import org.sejda.sambox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem; |
| 33 |
|
| 34 |
/** |
| 35 |
* @author Andrea Vacondio |
| 36 |
* |
| 37 |
*/ |
| 38 |
public class SamboxOutlineLevelsHandlerTest { |
| 39 |
|
| 40 |
private PDDocument fromResource(String name) throws IOException { |
| 41 |
return PDFParser.parse(SeekableSources.inMemorySeekableSourceFrom(getClass().getClassLoader().getResourceAsStream(name))); |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
@Test |
| 46 |
public void getPageNumbersAtOutlineLevel() throws IOException { |
| 47 |
try (PDDocument document = fromResource("pdf/test_outline.pdf")) { |
| 48 |
org.sejda.model.outline.OutlineLevelsHandler victim = new SamboxOutlineLevelsHandler(document, null); |
| 49 |
assertTrue(victim.getPageDestinationsForLevel(4).getPages().isEmpty()); |
| 50 |
assertEquals(2, victim.getPageDestinationsForLevel(2).getPages().size()); |
| 51 |
assertEquals(1, victim.getPageDestinationsForLevel(3).getPages().size()); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
@Test |
| 56 |
public void getPageNumbersAtOutlineLevelNoOutline() throws IOException { |
| 57 |
try (PDDocument document = fromResource("pdf/test_no_outline.pdf")) { |
| 58 |
org.sejda.model.outline.OutlineLevelsHandler victim = new SamboxOutlineLevelsHandler(document, null); |
| 59 |
assertEquals(0, victim.getPageDestinationsForLevel(2).getPages().size()); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
@Test |
| 64 |
public void getPageNumbersAtOutlineLevelMatching() throws IOException { |
| 65 |
try (PDDocument document = fromResource("pdf/test_outline.pdf")) { |
| 66 |
org.sejda.model.outline.OutlineLevelsHandler victim = new SamboxOutlineLevelsHandler(document, "(.+)page(.*)"); |
| 67 |
assertEquals(1, victim.getPageDestinationsForLevel(2).getPages().size()); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
@Test |
| 72 |
public void parentsNoPageDests() { |
| 73 |
PDPage page1 = new PDPage(); |
| 74 |
PDDocument document = new PDDocument(); |
| 75 |
document.addPage(page1); |
| 76 |
PDDocumentOutline outlines = new PDDocumentOutline(); |
| 77 |
PDOutlineItem root = new PDOutlineItem(); |
| 78 |
root.setTitle("title"); |
| 79 |
PDOutlineItem child = new PDOutlineItem(); |
| 80 |
child.setTitle("child"); |
| 81 |
PDOutlineItem child2 = new PDOutlineItem(); |
| 82 |
child2.setTitle("child2"); |
| 83 |
child2.setDestination(page1); |
| 84 |
child.addFirst(child2); |
| 85 |
root.addLast(child); |
| 86 |
outlines.addFirst(root); |
| 87 |
document.getDocumentCatalog().setDocumentOutline(outlines); |
| 88 |
org.sejda.model.outline.OutlineLevelsHandler victim = new SamboxOutlineLevelsHandler(document, null); |
| 89 |
assertEquals(0, victim.getPageDestinationsForLevel(2).getPages().size()); |
| 90 |
assertEquals(1, victim.getPageDestinationsForLevel(3).getPages().size()); |
| 91 |
} |
| 92 |
|
| 93 |
@Test |
| 94 |
public void newLinesInBookmarks() throws IOException { |
| 95 |
try (PDDocument document = fromResource("pdf/new_line_in_bookmarks.pdf")) { |
| 96 |
org.sejda.model.outline.OutlineLevelsHandler victim = new SamboxOutlineLevelsHandler(document, null); |
| 97 |
assertEquals(3, victim.getPageDestinationsForLevel(1).getPages().size()); |
| 98 |
} |
| 99 |
} |
| 100 |
} |