| 1 |
/* |
| 2 |
* Created on 26 ago 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.CoreMatchers.is; |
| 22 |
import static org.junit.Assert.assertEquals; |
| 23 |
import static org.junit.Assert.assertFalse; |
| 24 |
import static org.hamcrest.MatcherAssert.assertThat; |
| 25 |
import static org.junit.Assert.assertTrue; |
| 26 |
|
| 27 |
import java.awt.Rectangle; |
| 28 |
import java.io.IOException; |
| 29 |
|
| 30 |
import org.junit.Test; |
| 31 |
import org.sejda.model.exception.TaskIOException; |
| 32 |
import org.sejda.model.exception.TaskPermissionsException; |
| 33 |
import org.sejda.model.input.PdfMixInput; |
| 34 |
import org.sejda.model.input.PdfStreamSource; |
| 35 |
|
| 36 |
/** |
| 37 |
* @author Andrea Vacondio |
| 38 |
* |
| 39 |
*/ |
| 40 |
public class PdfMixFragmentTest { |
| 41 |
|
| 42 |
@Test |
| 43 |
public void nextPage() throws TaskIOException, TaskPermissionsException, IOException { |
| 44 |
try (PdfMixFragment victim = PdfMixFragment.newInstance(new PdfMixInput(PdfStreamSource.newInstanceNoPassword( |
| 45 |
getClass().getClassLoader().getResourceAsStream("pdf/2_pages.pdf"), "test.pdf")))) { |
| 46 |
assertTrue(victim.hasNextPage()); |
| 47 |
assertThat(new PdfTextExtractorByArea() |
| 48 |
.extractTextFromArea(victim.nextPage(), new Rectangle(54, 56, 60, 21)).trim(), is("First page")); |
| 49 |
assertTrue(victim.hasNextPage()); |
| 50 |
assertThat(new PdfTextExtractorByArea() |
| 51 |
.extractTextFromArea(victim.nextPage(), new Rectangle(54, 56, 60, 21)).trim(), is("Second page")); |
| 52 |
assertFalse(victim.hasNextPage()); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
@Test |
| 57 |
public void numberOfPages() throws TaskIOException, TaskPermissionsException, IOException { |
| 58 |
try (PdfMixFragment victim = PdfMixFragment.newInstance(new PdfMixInput(PdfStreamSource.newInstanceNoPassword( |
| 59 |
getClass().getClassLoader().getResourceAsStream("pdf/2_pages.pdf"), "test.pdf")))) { |
| 60 |
assertEquals(2, victim.getNumberOfPages()); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
@Test |
| 65 |
public void step() throws TaskIOException, TaskPermissionsException, IOException { |
| 66 |
try (PdfMixFragment victim = PdfMixFragment |
| 67 |
.newInstance(new PdfMixInput( |
| 68 |
PdfStreamSource.newInstanceNoPassword( |
| 69 |
getClass().getClassLoader().getResourceAsStream("pdf/2_pages.pdf"), "test.pdf"), |
| 70 |
true, 2))) { |
| 71 |
assertEquals(2, victim.getStep()); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
@Test |
| 76 |
public void reverse() throws TaskIOException, TaskPermissionsException, IOException { |
| 77 |
try (PdfMixFragment victim = PdfMixFragment |
| 78 |
.newInstance(new PdfMixInput( |
| 79 |
PdfStreamSource.newInstanceNoPassword( |
| 80 |
getClass().getClassLoader().getResourceAsStream("pdf/2_pages.pdf"), "test.pdf"), |
| 81 |
true, 1))) { |
| 82 |
assertTrue(victim.hasNextPage()); |
| 83 |
assertThat(new PdfTextExtractorByArea() |
| 84 |
.extractTextFromArea(victim.nextPage(), new Rectangle(54, 56, 60, 21)).trim(), is("Second page")); |
| 85 |
assertTrue(victim.hasNextPage()); |
| 86 |
assertThat(new PdfTextExtractorByArea() |
| 87 |
.extractTextFromArea(victim.nextPage(), new Rectangle(54, 56, 60, 21)).trim(), is("First page")); |
| 88 |
assertFalse(victim.hasNextPage()); |
| 89 |
} |
| 90 |
} |
| 91 |
} |