| 1 |
/* |
| 2 |
* Copyright 2018 by Eduard Weissmann ([email protected]). |
| 3 |
* |
| 4 |
* This file is part of the Sejda source code |
| 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 |
|
| 20 |
package org.sejda.impl.sambox.util; |
| 21 |
|
| 22 |
import static java.util.Arrays.asList; |
| 23 |
import static org.sejda.core.service.TestUtils.assertPageLabelIndexesAre; |
| 24 |
import static org.sejda.core.service.TestUtils.assertPageLabelRangeIs; |
| 25 |
|
| 26 |
import java.io.IOException; |
| 27 |
import java.util.List; |
| 28 |
import java.util.function.Consumer; |
| 29 |
|
| 30 |
import org.junit.Test; |
| 31 |
import org.sejda.impl.sambox.component.DocBuilder; |
| 32 |
import org.sejda.sambox.pdmodel.PDDocument; |
| 33 |
import org.sejda.sambox.pdmodel.common.PDPageLabels; |
| 34 |
|
| 35 |
public class PageLabelUtilsTest { |
| 36 |
|
| 37 |
@Test |
| 38 |
public void removeFirstPage() throws IOException { |
| 39 |
PDDocument doc = new DocBuilder().withPages(10) |
| 40 |
.withPageLabelRange(0, "r") |
| 41 |
.withPageLabelRange(2, "D") |
| 42 |
.get(); |
| 43 |
|
| 44 |
removePages(doc, asList(1), result -> { |
| 45 |
assertPageLabelIndexesAre(result, 0, 1); |
| 46 |
assertPageLabelRangeIs(result, 0, "r"); |
| 47 |
assertPageLabelRangeIs(result, 1, "D"); |
| 48 |
}); |
| 49 |
} |
| 50 |
|
| 51 |
@Test |
| 52 |
public void removeFirstPages() throws IOException { |
| 53 |
PDDocument doc = new DocBuilder().withPages(10) |
| 54 |
.withPageLabelRange(0, "r") |
| 55 |
.withPageLabelRange(2, "D") |
| 56 |
.get(); |
| 57 |
|
| 58 |
removePages(doc, asList(1, 2), result -> { |
| 59 |
assertPageLabelIndexesAre(result, 0); |
| 60 |
assertPageLabelRangeIs(result, 0, "D"); |
| 61 |
}); |
| 62 |
} |
| 63 |
|
| 64 |
@Test |
| 65 |
public void removeMiddlePage() throws IOException { |
| 66 |
PDDocument doc = new DocBuilder().withPages(10) |
| 67 |
.withPageLabelRange(0, "r") |
| 68 |
.withPageLabelRange(2, "D") |
| 69 |
.get(); |
| 70 |
|
| 71 |
removePages(doc, asList(2), result -> { |
| 72 |
assertPageLabelIndexesAre(result, 0, 1); |
| 73 |
assertPageLabelRangeIs(result, 0, "r"); |
| 74 |
assertPageLabelRangeIs(result, 1, "D"); |
| 75 |
}); |
| 76 |
} |
| 77 |
|
| 78 |
@Test |
| 79 |
public void removeMiddlePages() throws IOException { |
| 80 |
PDDocument doc = new DocBuilder().withPages(10) |
| 81 |
.withPageLabelRange(0, "r") |
| 82 |
.withPageLabelRange(2, "D") |
| 83 |
.withPageLabelRange(4, "A") |
| 84 |
.get(); |
| 85 |
|
| 86 |
removePages(doc, asList(3, 4, 5), result -> { |
| 87 |
assertPageLabelIndexesAre(result, 0, 2); |
| 88 |
assertPageLabelRangeIs(result, 0, "r"); |
| 89 |
assertPageLabelRangeIs(result, 2, "A"); |
| 90 |
}); |
| 91 |
} |
| 92 |
|
| 93 |
@Test |
| 94 |
public void removeLastPage() throws IOException { |
| 95 |
PDDocument doc = new DocBuilder().withPages(10) |
| 96 |
.withPageLabelRange(0, "r") |
| 97 |
.withPageLabelRange(2, "D") |
| 98 |
.get(); |
| 99 |
|
| 100 |
removePages(doc, asList(10), result -> { |
| 101 |
assertPageLabelIndexesAre(result, 0, 2); |
| 102 |
assertPageLabelRangeIs(result, 0, "r"); |
| 103 |
assertPageLabelRangeIs(result, 2, "D"); |
| 104 |
}); |
| 105 |
} |
| 106 |
|
| 107 |
@Test |
| 108 |
public void removeLastPages() throws IOException { |
| 109 |
PDDocument doc = new DocBuilder().withPages(5) |
| 110 |
.withPageLabelRange(0, "r") |
| 111 |
.withPageLabelRange(2, "D") |
| 112 |
.get(); |
| 113 |
|
| 114 |
removePages(doc, asList(3, 4, 5), result -> { |
| 115 |
assertPageLabelIndexesAre(result, 0); |
| 116 |
assertPageLabelRangeIs(result, 0, "r"); |
| 117 |
}); |
| 118 |
} |
| 119 |
|
| 120 |
private void removePages(PDDocument doc, List<Integer> pages, Consumer<PDPageLabels> consumer) throws IOException { |
| 121 |
PDPageLabels result = PageLabelUtils.removePages(doc.getDocumentCatalog().getPageLabels(), pages, doc.getNumberOfPages()); |
| 122 |
consumer.accept(result); |
| 123 |
} |
| 124 |
} |