-
Notifications
You must be signed in to change notification settings - Fork 429
/
TestObjectExtractor.java
150 lines (121 loc) · 5.06 KB
/
TestObjectExtractor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package technology.tabula;
import static org.junit.Assert.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.pdfbox.Loader;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.junit.Test;
public class TestObjectExtractor {
/*@Test(expected=IOException.class)
public void testWrongPasswordRaisesException() throws IOException {
PDDocument pdf_document = PDDocument.load(new File("src/test/resources/technology/tabula/encrypted.pdf"));
ObjectExtractor oe = new ObjectExtractor(pdf_document, "wrongpass");
oe.extract().next();
}*/
@Test(expected = IOException.class)
public void testEmptyOnEncryptedFileRaisesException() throws IOException {
PDDocument pdf_document = Loader.loadPDF(new File("src/test/resources/technology/tabula/encrypted.pdf"));
try (ObjectExtractor oe = new ObjectExtractor(pdf_document)) {
oe.extract().next();
}
}
@Test
public void testCanReadPDFWithOwnerEncryption() throws IOException {
PDDocument pdf_document = Loader.loadPDF(new File("src/test/resources/technology/tabula/S2MNCEbirdisland.pdf"));
try (ObjectExtractor oe = new ObjectExtractor(pdf_document)) {
PageIterator pi = oe.extract();
int i = 0;
while (pi.hasNext()) {
i++;
pi.next();
}
assertEquals(2, i);
}
}
@Test
public void testGoodPassword() throws IOException {
PDDocument pdf_document = Loader.loadPDF(new File("src/test/resources/technology/tabula/encrypted.pdf"), "userpassword");
try (ObjectExtractor oe = new ObjectExtractor(pdf_document)) {
List<Page> pages = new ArrayList<>();
PageIterator pi = oe.extract();
while (pi.hasNext()) {
pages.add(pi.next());
}
assertEquals(1, pages.size());
}
}
@Test
public void testTextExtractionDoesNotRaise() throws IOException {
PDDocument pdf_document = Loader.loadPDF(new File("src/test/resources/technology/tabula/rotated_page.pdf"));
try (ObjectExtractor oe = new ObjectExtractor(pdf_document)) {
PageIterator pi = oe.extract();
assertTrue(pi.hasNext());
assertNotNull(pi.next());
assertFalse(pi.hasNext());
}
}
@Test
public void testShouldDetectRulings() throws IOException {
PDDocument pdf_document = Loader.loadPDF(new File("src/test/resources/technology/tabula/should_detect_rulings.pdf"));
try (ObjectExtractor oe = new ObjectExtractor(pdf_document)) {
PageIterator pi = oe.extract();
Page page = pi.next();
List<Ruling> rulings = page.getRulings();
for (Ruling r: rulings) {
assertTrue(page.contains(r.getBounds()));
}
}
}
@Test
public void testDontThrowNPEInShfill() throws IOException {
PDDocument pdf_document = Loader.loadPDF(new File("src/test/resources/technology/tabula/labor.pdf"));
try (ObjectExtractor oe = new ObjectExtractor(pdf_document)) {
PageIterator pi = oe.extract();
assertTrue(pi.hasNext());
try {
Page p = pi.next();
assertNotNull(p);
} catch (NullPointerException e) {
fail("NPE in ObjectExtractor " + e.toString());
}
}
}
@Test
public void testExtractOnePage() throws IOException {
PDDocument pdf_document = Loader.loadPDF(new File("src/test/resources/technology/tabula/S2MNCEbirdisland.pdf"));
assertEquals(2, pdf_document.getNumberOfPages());
try (ObjectExtractor oe = new ObjectExtractor(pdf_document)) {
Page page = oe.extract(2);
assertNotNull(page);
}
}
@Test(expected = IndexOutOfBoundsException.class)
public void testExtractWrongPageNumber() throws IOException {
PDDocument pdf_document = Loader.loadPDF(new File("src/test/resources/technology/tabula/S2MNCEbirdisland.pdf"));
assertEquals(2, pdf_document.getNumberOfPages());
try (ObjectExtractor oe = new ObjectExtractor(pdf_document)) {
oe.extract(3);
}
}
@Test
public void testTextElementsContainedInPage() throws IOException {
PDDocument pdf_document = Loader.loadPDF(new File("src/test/resources/technology/tabula/cs-en-us-pbms.pdf"));
try (ObjectExtractor oe = new ObjectExtractor(pdf_document)) {
Page page = oe.extractPage(1);
for (TextElement te: page.getText()) {
assertTrue(page.contains(te));
}
}
}
@Test public void testDoNotNPEInPointComparator() throws IOException {
PDDocument pdf_document = Loader.loadPDF(new File("src/test/resources/technology/tabula/npe_issue_206.pdf"));
try (ObjectExtractor oe = new ObjectExtractor(pdf_document)) {
Page p = oe.extractPage(1);
assertNotNull(p);
} catch (NullPointerException e) {
fail("NPE in ObjectExtractor " + e.toString());
}
}
}