| 1 |
/* |
| 2 |
* Created on 05/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.assertTrue; |
| 24 |
|
| 25 |
import java.io.IOException; |
| 26 |
import java.util.stream.StreamSupport; |
| 27 |
|
| 28 |
import org.apache.commons.lang3.tuple.ImmutablePair; |
| 29 |
import org.junit.After; |
| 30 |
import org.junit.Before; |
| 31 |
import org.junit.Test; |
| 32 |
import org.sejda.commons.LookupTable; |
| 33 |
import org.sejda.commons.util.IOUtils; |
| 34 |
import org.sejda.io.SeekableSources; |
| 35 |
import org.sejda.model.outline.OutlinePolicy; |
| 36 |
import org.sejda.sambox.input.PDFParser; |
| 37 |
import org.sejda.sambox.pdmodel.PDDocument; |
| 38 |
import org.sejda.sambox.pdmodel.PDPage; |
| 39 |
import org.sejda.sambox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline; |
| 40 |
import org.sejda.sambox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem; |
| 41 |
|
| 42 |
/** |
| 43 |
* @author Andrea Vacondio |
| 44 |
* |
| 45 |
*/ |
| 46 |
public class OutlineMergerTest { |
| 47 |
|
| 48 |
private ImmutablePair<PDDocument, LookupTable<PDPage>> testData; |
| 49 |
private ImmutablePair<PDDocument, LookupTable<PDPage>> testData2; |
| 50 |
|
| 51 |
@Before |
| 52 |
public void setUp() throws IOException { |
| 53 |
testData = getTestData("pdf/large_outline.pdf"); |
| 54 |
testData2 = getTestData("pdf/test_outline.pdf"); |
| 55 |
} |
| 56 |
|
| 57 |
@After |
| 58 |
public void tearDown() { |
| 59 |
IOUtils.closeQuietly(testData.getKey()); |
| 60 |
IOUtils.closeQuietly(testData2.getKey()); |
| 61 |
} |
| 62 |
|
| 63 |
@Test |
| 64 |
public void empty() { |
| 65 |
OutlineMerger victim = new OutlineMerger(OutlinePolicy.DISCARD); |
| 66 |
victim.updateOutline(testData.getKey(), "large_outline.pdf", testData.getValue()); |
| 67 |
assertFalse(victim.hasOutline()); |
| 68 |
assertFalse(victim.getOutline().hasChildren()); |
| 69 |
} |
| 70 |
|
| 71 |
@Test |
| 72 |
public void retainAll() { |
| 73 |
OutlineMerger victim = new OutlineMerger(OutlinePolicy.RETAIN); |
| 74 |
victim.updateOutline(testData.getKey(), "large_outline.pdf", testData.getValue()); |
| 75 |
assertTrue(victim.hasOutline()); |
| 76 |
assertEquals(count(testData.getKey().getDocumentCatalog().getDocumentOutline()), count(victim.getOutline())); |
| 77 |
} |
| 78 |
|
| 79 |
@Test |
| 80 |
public void onePerDoc() { |
| 81 |
OutlineMerger victim = new OutlineMerger(OutlinePolicy.ONE_ENTRY_EACH_DOC); |
| 82 |
victim.updateOutline(testData.getKey(), "large_outline.pdf", testData.getValue()); |
| 83 |
victim.updateOutline(testData2.getKey(), "test_outline.pdf", testData2.getValue()); |
| 84 |
assertTrue(victim.hasOutline()); |
| 85 |
assertEquals(2, count(victim.getOutline())); |
| 86 |
for (PDOutlineItem current : victim.getOutline().children()) { |
| 87 |
assertFalse(current.hasChildren()); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
@Test |
| 92 |
public void retainAsOneEntry() { |
| 93 |
OutlineMerger victim = new OutlineMerger(OutlinePolicy.RETAIN_AS_ONE_ENTRY); |
| 94 |
victim.updateOutline(testData.getKey(), "large_outline.pdf", testData.getValue()); |
| 95 |
victim.updateOutline(testData2.getKey(), "test_outline.pdf", testData2.getValue()); |
| 96 |
assertTrue(victim.hasOutline()); |
| 97 |
assertEquals(2, count(victim.getOutline())); |
| 98 |
for (PDOutlineItem current : victim.getOutline().children()) { |
| 99 |
assertTrue(current.hasChildren()); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
@Test |
| 104 |
public void retainSome() { |
| 105 |
OutlineMerger victim = new OutlineMerger(OutlinePolicy.RETAIN); |
| 106 |
testData.getValue().clear(); |
| 107 |
testData.getValue().addLookupEntry(testData.getKey().getPage(2), new PDPage()); |
| 108 |
testData.getValue().addLookupEntry(testData.getKey().getPage(3), new PDPage()); |
| 109 |
testData.getValue().addLookupEntry(testData.getKey().getPage(4), new PDPage()); |
| 110 |
victim.updateOutline(testData.getKey(), "large_outline.pdf", testData.getValue()); |
| 111 |
assertTrue(victim.hasOutline()); |
| 112 |
assertEquals(28, count(victim.getOutline())); |
| 113 |
} |
| 114 |
|
| 115 |
@Test |
| 116 |
public void onePerDocNoName() { |
| 117 |
OutlineMerger victim = new OutlineMerger(OutlinePolicy.ONE_ENTRY_EACH_DOC); |
| 118 |
victim.updateOutline(testData.getKey(), "", testData.getValue()); |
| 119 |
assertFalse(victim.hasOutline()); |
| 120 |
} |
| 121 |
|
| 122 |
@Test |
| 123 |
public void retainAllNoRelevantPage() { |
| 124 |
OutlineMerger victim = new OutlineMerger(OutlinePolicy.RETAIN); |
| 125 |
victim.updateOutline(testData.getKey(), "large_outline.pdf", new LookupTable<>()); |
| 126 |
assertFalse(victim.hasOutline()); |
| 127 |
} |
| 128 |
|
| 129 |
@Test |
| 130 |
public void handlePageNumsInsteadOfRefsInDestinations() throws IOException { |
| 131 |
ImmutablePair<PDDocument, LookupTable<PDPage>> data = getTestData( |
| 132 |
"pdf/page_dests_with_number_insteadof_refs.pdf"); |
| 133 |
OutlineMerger victim = new OutlineMerger(OutlinePolicy.RETAIN); |
| 134 |
victim.updateOutline(data.getKey(), "page_dests_with_number_insteadof_refs.pdf", data.getValue()); |
| 135 |
assertTrue(victim.hasOutline()); |
| 136 |
assertEquals(1, count(victim.getOutline())); |
| 137 |
assertEquals(2, |
| 138 |
StreamSupport.stream(victim.getOutline().getFirstChild().children().spliterator(), false).count()); |
| 139 |
} |
| 140 |
|
| 141 |
private ImmutablePair<PDDocument, LookupTable<PDPage>> getTestData(String path) throws IOException { |
| 142 |
PDDocument document = PDFParser.parse( |
| 143 |
SeekableSources.inMemorySeekableSourceFrom(getClass().getClassLoader().getResourceAsStream(path))); |
| 144 |
LookupTable<PDPage> mapping = new LookupTable<>(); |
| 145 |
for (PDPage current : document.getPages()) { |
| 146 |
mapping.addLookupEntry(current, new PDPage()); |
| 147 |
} |
| 148 |
return ImmutablePair.of(document, mapping); |
| 149 |
} |
| 150 |
|
| 151 |
private static long count(PDDocumentOutline outline) { |
| 152 |
return StreamSupport.stream(outline.children().spliterator(), false).count(); |
| 153 |
} |
| 154 |
} |