| 1 |
/* |
| 2 |
* Created on 07 mar 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.hamcrest.Matchers.isEmptyOrNullString; |
| 23 |
import static org.hamcrest.MatcherAssert.assertThat; |
| 24 |
|
| 25 |
import org.junit.Test; |
| 26 |
import org.sejda.model.exception.TaskException; |
| 27 |
import org.sejda.model.exception.TaskIOException; |
| 28 |
import org.sejda.sambox.pdmodel.PDDocument; |
| 29 |
import org.sejda.sambox.pdmodel.PDPage; |
| 30 |
|
| 31 |
/** |
| 32 |
* @author Andrea Vacondio |
| 33 |
* |
| 34 |
*/ |
| 35 |
public class FilenameFooterWriterTest { |
| 36 |
|
| 37 |
@Test |
| 38 |
public void write() throws TaskException { |
| 39 |
PDDocument doc = new PDDocument(); |
| 40 |
PDPage page = new PDPage(); |
| 41 |
doc.addPage(page); |
| 42 |
new FilenameFooterWriter(true, doc).addFooter(page, "My Footer", 20); |
| 43 |
assertThat(new PdfTextExtractorByArea().extractFooterText(page).trim(), is("My Footer 20")); |
| 44 |
} |
| 45 |
|
| 46 |
@Test |
| 47 |
public void write_long_filename_that_needs_truncation() throws TaskException { |
| 48 |
PDDocument doc = new PDDocument(); |
| 49 |
PDPage page = new PDPage(); |
| 50 |
doc.addPage(page); |
| 51 |
new FilenameFooterWriter(true, doc).addFooter(page, "My very long title that will not fit on the page and needs to be truncated so that it will not overflow and cover the page number and generally look not so nice", 20); |
| 52 |
assertPageFooterText(page,"My very long title that will not fit on the page and needs to be truncated so that it will not overflow and cover the page number a20"); |
| 53 |
} |
| 54 |
|
| 55 |
@Test |
| 56 |
public void dontWrite() throws TaskException { |
| 57 |
PDDocument doc = new PDDocument(); |
| 58 |
PDPage page = new PDPage(); |
| 59 |
doc.addPage(page); |
| 60 |
new FilenameFooterWriter(false, doc).addFooter(page, "My Footer", 20); |
| 61 |
assertThat(new PdfTextExtractorByArea().extractFooterText(page).trim(), isEmptyOrNullString()); |
| 62 |
} |
| 63 |
|
| 64 |
@Test |
| 65 |
public void write_filename_contains_bad_characters() throws TaskException { |
| 66 |
PDDocument doc = new PDDocument(); |
| 67 |
PDPage page = new PDPage(); |
| 68 |
doc.addPage(page); |
| 69 |
String withBadCharacter = "This is a bad \uF021character"; |
| 70 |
FilenameFooterWriter writer = new FilenameFooterWriter(true, doc); |
| 71 |
writer.addFooter(page, withBadCharacter, 1); |
| 72 |
|
| 73 |
assertPageFooterText(page, "This is a bad #character 1"); |
| 74 |
} |
| 75 |
|
| 76 |
private void assertPageFooterText(PDPage page, String expectedText) throws TaskIOException { |
| 77 |
assertThat(new PdfTextExtractorByArea().extractFooterText(page).trim(), is(expectedText)); |
| 78 |
} |
| 79 |
} |