-
Notifications
You must be signed in to change notification settings - Fork 432
/
TestCommandLineApp.java
219 lines (174 loc) · 8.37 KB
/
TestCommandLineApp.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package technology.tabula;
import static org.junit.Assert.*;
import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.ParseException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
public class TestCommandLineApp {
@Rule
public TemporaryFolder folder = new TemporaryFolder();
private String csvFromCommandLineArgs(String[] args) throws ParseException {
CommandLineParser parser = new DefaultParser();
CommandLine cmd = parser.parse(CommandLineApp.buildOptions(), args);
StringBuilder stringBuilder = new StringBuilder();
new CommandLineApp(stringBuilder, cmd).extractTables(cmd);
return stringBuilder.toString();
}
@Test
public void testExtractSpreadsheetWithArea() throws ParseException, IOException {
String expectedCsv = UtilsForTesting.loadCsv("src/test/resources/technology/tabula/csv/spreadsheet_no_bounding_frame.csv");
assertEquals(expectedCsv, this.csvFromCommandLineArgs(new String[]{
"src/test/resources/technology/tabula/spreadsheet_no_bounding_frame.pdf",
"-p", "1", "-a",
"150.56,58.9,654.7,536.12", "-f",
"CSV"
}));
}
@Test
public void testExtractBatchSpreadsheetWithArea() throws ParseException, IOException {
FileSystem fs = FileSystems.getDefault();
String expectedCsv = UtilsForTesting.loadCsv("src/test/resources/technology/tabula/csv/spreadsheet_no_bounding_frame.csv");
Path tmpFolder = Files.createTempDirectory("tabula-java-batch-test");
tmpFolder.toFile().deleteOnExit();
Path copiedPDF = tmpFolder.resolve(fs.getPath("spreadsheet.pdf"));
Path sourcePDF = fs.getPath("src/test/resources/technology/tabula/spreadsheet_no_bounding_frame.pdf");
Files.copy(sourcePDF, copiedPDF);
copiedPDF.toFile().deleteOnExit();
this.csvFromCommandLineArgs(new String[]{
"-b", tmpFolder.toString(),
"-p", "1", "-a",
"150.56,58.9,654.7,536.12", "-f",
"CSV"
});
Path csvPath = tmpFolder.resolve(fs.getPath("spreadsheet.csv"));
assertTrue(csvPath.toFile().exists());
assertArrayEquals(expectedCsv.getBytes(), Files.readAllBytes(csvPath));
}
@Test
public void testExtractSpreadsheetWithAreaAndNewFile() throws ParseException, IOException {
String expectedCsv = UtilsForTesting.loadCsv("src/test/resources/technology/tabula/csv/spreadsheet_no_bounding_frame.csv");
File newFile = folder.newFile();
this.csvFromCommandLineArgs(new String[]{
"src/test/resources/technology/tabula/spreadsheet_no_bounding_frame.pdf",
"-p", "1", "-a",
"150.56,58.9,654.7,536.12", "-f",
"CSV", "-o", newFile.getAbsolutePath()
});
assertArrayEquals(expectedCsv.getBytes(), Files.readAllBytes(Paths.get(newFile.getAbsolutePath())));
}
@Test
public void testExtractJSONWithArea() throws ParseException, IOException {
String expectedJson = UtilsForTesting.loadJson("src/test/resources/technology/tabula/json/spanning_cells_basic.json");
assertEquals(expectedJson, this.csvFromCommandLineArgs(new String[]{
"src/test/resources/technology/tabula/spanning_cells.pdf",
"-p", "1", "-a",
"150.56,58.9,654.7,536.12", "-f",
"JSON"
}));
}
@Test
public void testExtractCSVWithArea() throws ParseException, IOException {
String expectedCsv = UtilsForTesting.loadCsv("src/test/resources/technology/tabula/csv/spanning_cells.csv");
assertEquals(expectedCsv, this.csvFromCommandLineArgs(new String[]{
"src/test/resources/technology/tabula/spanning_cells.pdf",
"-p", "1", "-a",
"150.56,58.9,654.7,536.12", "-f",
"CSV"
}));
}
@Test
public void testGuessOption() throws ParseException, IOException {
String expectedCsvNoGuessing = UtilsForTesting.loadCsv("src/test/resources/technology/tabula/csv/TestCommandLineApp_testGuessOption_no_guessing.csv");
assertEquals(expectedCsvNoGuessing, this.csvFromCommandLineArgs(new String[]{
"src/test/resources/technology/tabula/icdar2013-dataset/competition-dataset-eu/eu-001.pdf",
"-p", "1",
"-f", "CSV"
}));
String expectedCsvWithGuessing = UtilsForTesting.loadCsv("src/test/resources/technology/tabula/csv/TestCommandLineApp_testGuessOption_with_guessing.csv");
assertEquals(expectedCsvWithGuessing, this.csvFromCommandLineArgs(new String[]{
"src/test/resources/technology/tabula/icdar2013-dataset/competition-dataset-eu/eu-001.pdf",
"-p", "1",
"-f", "CSV",
"-g"
}));
}
@Test
public void testEncryptedPasswordSupplied() throws ParseException {
String s = this.csvFromCommandLineArgs(new String[]{
"src/test/resources/technology/tabula/encrypted.pdf",
"-s", "userpassword",
"-p", "1",
"-f", "CSV"
});
assertEquals("FLA Audit Profile,,,,,,,,,", s.split("\\r?\\n")[0]);
}
@Test(expected=org.apache.commons.cli.ParseException.class)
public void testEncryptedWrongPassword() throws ParseException {
String s = this.csvFromCommandLineArgs(new String[]{
"src/test/resources/technology/tabula/encrypted.pdf",
"-s", "wrongpassword",
"-p", "1",
"-f", "CSV"
});
}
@Test
public void testExtractWithMultiplePercentArea() throws ParseException, IOException {
String expectedCsv = UtilsForTesting.loadCsv("src/test/resources/technology/tabula/csv/MultiColumn.csv");
assertEquals(expectedCsv, this.csvFromCommandLineArgs(new String[]{
"src/test/resources/technology/tabula/MultiColumn.pdf",
"-p", "1", "-a",
"%0,0,100,50", "-a",
"%0,50,100,100", "-f",
"CSV"
}));
}
@Test
public void testExtractWithMultipleAbsoluteArea() throws ParseException, IOException {
String expectedCsv = UtilsForTesting.loadCsv("src/test/resources/technology/tabula/csv/MultiColumn.csv");
assertEquals(expectedCsv, this.csvFromCommandLineArgs(new String[]{
"src/test/resources/technology/tabula/MultiColumn.pdf",
"-p", "1", "-a",
"0,0,451,212", "-a",
"0,212,451,425", "-f",
"CSV"
}));
}
@Test
public void testExtractWithPercentAndAbsoluteArea() throws ParseException, IOException {
String expectedCsv = UtilsForTesting.loadCsv("src/test/resources/technology/tabula/csv/MultiColumn.csv");
assertEquals(expectedCsv, this.csvFromCommandLineArgs(new String[]{
"src/test/resources/technology/tabula/MultiColumn.pdf",
"-p", "1", "-a",
"%0,0,100,50", "-a",
"0,212,451,425", "-f",
"CSV"
}));
}
@Test
public void testLatticeModeWithColumnOption() throws ParseException, IOException {
String expectedCsv = UtilsForTesting.loadCsv("src/test/resources/technology/tabula/csv/AnimalSounds.csv");
assertEquals(expectedCsv, this.csvFromCommandLineArgs(new String[]{
"src/test/resources/technology/tabula/AnimalSounds.pdf",
"-p", "1", "-c",
"59,218,331,551",
"-r", "-f", "CSV"
}));
}
@Test
public void testLatticeModeWithColumnAndMultipleAreasOption() throws ParseException, IOException {
String expectedJson = UtilsForTesting.loadJson("src/test/resources/technology/tabula/json/AnimalSounds1.json");
String resultJson = this.csvFromCommandLineArgs(new String[]{
"src/test/resources/technology/tabula/AnimalSounds1.pdf",
"-p", "1", "-c", "57,136,197,296,314,391,457,553",
"-a", "%0,0,100,50", "-a", "%0,50,100,100",
"-r", "-f", "JSON"
});
assertEquals(expectedJson, resultJson);
}
}