Debian Codegraph @

sejda-conversion/src/test/java/org/sejda/conversion/PdfFileSourceAdapterTest.java

raw
1 /*
2 * Created on Oct 13, 2011
3 * Copyright 2010 by Eduard Weissmann ([email protected]).
4 *
5 * This file is part of the Sejda source code
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20 package org.sejda.conversion;
21
22 import static org.hamcrest.CoreMatchers.containsString;
23 import static org.hamcrest.CoreMatchers.is;
24 import static org.junit.Assert.assertNull;
25 import static org.hamcrest.MatcherAssert.assertThat;
26
27 import java.io.File;
28 import java.io.IOException;
29 import java.io.InputStream;
30
31 import org.apache.commons.io.FileUtils;
32 import org.hamcrest.core.CombinableMatcher;
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.sejda.model.exception.SejdaRuntimeException;
37 import org.sejda.model.input.PdfFileSource;
38
39 /**
40 * Tests for {@link PdfFileSourceAdapter}
41 *
42 * @author Eduard Weissmann
43 *
44 */
45 public class PdfFileSourceAdapterTest {
46
47 private File file;
48
49 @Before
50 public void setUp() {
51 InputStream contents = getClass().getResourceAsStream("/pdf/test_outline.pdf");
52 file = new File("/tmp/inputFile1.pdf");
53 file.deleteOnExit();
54 try {
55 FileUtils.copyInputStreamToFile(contents, file);
56 } catch (IOException e) {
57 throw new SejdaRuntimeException("Can't create test file. Reason: " + e.getMessage(), e);
58 }
59 }
60
61 @After
62 public void tearDown() {
63 if (file != null) {
64 file.delete();
65 }
66 }
67
68 @Test
69 public void windowsFileWithPassword() {
70 PdfFileSource result = new PdfFileSourceAdapter("/tmp/inputFile1.pdf:secret123").getPdfFileSource();
71 assertThat(result.getPassword(), is("secret123"));
72 assertThat(result.getSource(),
73 CombinableMatcher.<File> either(is(new File("/tmp/inputFile1.pdf"))).or(
74 is(new File("c:\\tmp\\inputFile1.pdf"))));
75 }
76
77 @Test
78 public void windowsFileNoPassword() {
79 PdfFileSource result = new PdfFileSourceAdapter("/tmp/inputFile1.pdf").getPdfFileSource();
80 assertNull(result.getPassword());
81 assertThat(result.getSource(),
82 CombinableMatcher.<File> either(is(new File("/tmp/inputFile1.pdf"))).or(
83 is(new File("c:\\tmp\\inputFile1.pdf"))));
84 }
85
86 @Test
87 public void protectedFileWithPasswordContainingSeparator() {
88 PdfFileSource result = new PdfFileSourceAdapter("/tmp/inputFile1.pdf:secret.pdf:password").getPdfFileSource();
89 assertThat(result.getPassword(), is("secret.pdf:password"));
90 assertThat(result.getSource(),
91 CombinableMatcher.<File> either(is(new File("/tmp/inputFile1.pdf"))).or(
92 is(new File("c:\\tmp\\inputFile1.pdf"))));
93 }
94
95 @Test
96 public void fileDoestExist() {
97 try {
98 new PdfFileSourceAdapter("/tmp/doesntexist.pdf:secret").getPdfFileSource();
99 } catch (SejdaRuntimeException e) {
100 assertThat(e.getMessage(), containsString("does not exist"));
101 }
102 }
103
104 }