1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package au.csiro.netcdf;
21
22 import java.io.BufferedWriter;
23 import java.io.ByteArrayOutputStream;
24 import java.io.File;
25 import java.io.FileWriter;
26 import java.io.IOException;
27 import java.io.PrintStream;
28
29 import junit.framework.TestCase;
30
31 import org.apache.commons.cli.Options;
32 import org.apache.commons.cli.ParseException;
33
34 import au.csiro.netcdf.cli.Command;
35
36
37
38
39
40
41
42
43
44 public class TestExtractCommand extends TestCase
45 {
46
47
48
49 private Command extractCommand = new NcCSVExtract();
50
51
52
53
54 private final String dummyCSVFilename = System.getProperty("user.dir") + "\\test.csv";
55
56
57
58
59 private final String[] dummyCSVFileContents = new String[] { "A1, B1, C1" + System.getProperty("line.separator"),
60 "A2, B2, C2" + System.getProperty("line.separator"),
61 "A3, B3, B3" + System.getProperty("line.separator") };
62
63
64
65
66 private String invalidColumnIndex = "999";
67
68
69
70
71 private String invalidEndRowIndex = "999";
72
73
74
75
76 private String validColumnIndex = "0";
77
78
79
80
81 private String validStartRowIndex = "1";
82
83
84
85
86 private String validEndRowIndex = "2";
87
88
89
90
91 private String validValuesCol0Row1Row2 = "A2" + System.getProperty("line.separator") +
92 "A3"+ System.getProperty("line.separator");
93
94
95
96
97 private String validValuesCol0Row0Row1Row2 = "A1" + System.getProperty("line.separator") +
98 "A2" + System.getProperty("line.separator") +
99 "A3" + System.getProperty("line.separator");
100
101
102
103
104 private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
105
106
107
108
109
110
111 @Override
112 protected void setUp() throws Exception
113 {
114 super.setUp();
115
116
117 this.createDummyCSVFile();
118
119
120 System.setOut(new PrintStream(outContent));
121 }
122
123
124
125
126
127
128 @Override
129 protected void tearDown() throws Exception
130 {
131 super.tearDown();
132
133 System.setOut(null);
134
135
136 File file = new File(this.dummyCSVFilename);
137 if(file.exists())
138 {
139 file.delete();
140 }
141 }
142
143
144
145
146 public final void testCreateOptions()
147 {
148 Options options = extractCommand.createOptions();
149
150 assertTrue("The following option is not recognised by the command: " + NcCSVExtract.INPUT_FILE, options
151 .hasOption(NcCSVExtract.INPUT_FILE));
152 assertTrue("The following option is not recognised by the command: " + NcCSVExtract.COLUMN_INDEX, options
153 .hasOption(NcCSVExtract.COLUMN_INDEX));
154 assertTrue("The following option is not recognised by the command: " + NcCSVExtract.START_ROW, options
155 .hasOption(NcCSVExtract.START_ROW));
156 assertTrue("The following option is not recognised by the command: " + NcCSVExtract.END_ROW, options
157 .hasOption(NcCSVExtract.END_ROW));
158 }
159
160
161
162
163 public final void testGetUsage()
164 {
165 assertTrue("The command does not display any usage information.", !extractCommand.getUsageString().isEmpty());
166 }
167
168
169
170
171 public final void testNonExistentInputFile()
172 {
173 String inputFileName = "C:\\abcdefghijklmnopqrstuvwxyz.csv";
174
175
176 File file = new File(inputFileName);
177 assertFalse("This unit test can not be run if the following file exists: " + inputFileName, file.exists());
178
179 String[] args = new String[] {extractCommand.getCommandName(),
180 "-"+NcCSVExtract.INPUT_FILE, inputFileName
181 "-"+NcCSVExtract.COLUMN_INDEX, "0"
182
183 String errors = extractCommand.validCommand(args);
184 assertTrue("A non-existent file should return an error", !errors.isEmpty());
185 }
186
187
188
189
190 public final void testInvalidInputFileExtension()
191 {
192 File file = new File(".");
193 assertTrue("This unit test can not be run if the following file does not exist: .", file.exists());
194
195 String[] args = new String[] {extractCommand.getCommandName(),
196 "-"+NcCSVExtract.INPUT_FILE, file.getPath()
197 "-"+NcCSVExtract.COLUMN_INDEX, "0"
198
199 String errors = extractCommand.validCommand(args);
200 assertTrue("A file without a *.csv file extension should return an error", !errors.isEmpty());
201 }
202
203
204
205
206 public final void testInvalidColumnIndex()
207 {
208 String[] args = new String[] {extractCommand.getCommandName(),
209 "-"+NcCSVExtract.INPUT_FILE, this.dummyCSVFilename
210 "-"+NcCSVExtract.COLUMN_INDEX, this.invalidColumnIndex
211
212 String errors = extractCommand.validCommand(args);
213 assertTrue("Valid arguments should not return an error", errors.isEmpty());
214
215 try
216 {
217 extractCommand.execute(args);
218 fail("Referencing an invalid column index in a file should throw an exception.");
219 }
220 catch(ParseException pe)
221 {
222
223 pe.printStackTrace();
224 }
225 catch(IOException ioe)
226 {
227 ioe.printStackTrace();
228 fail(ioe.getMessage());
229 }
230 }
231
232
233
234
235 public final void testInvalidColumnIndexArgValue()
236 {
237 String[] args = new String[] {extractCommand.getCommandName(),
238 "-"+NcCSVExtract.INPUT_FILE, this.dummyCSVFilename
239 "-"+NcCSVExtract.COLUMN_INDEX, "ABCDEFG"
240
241 String errors = extractCommand.validCommand(args);
242 assertTrue("Non-numeric columnIndex values should return an error", !errors.isEmpty());
243 }
244
245
246
247
248 public final void testValidColumnIndex()
249 {
250 String[] args = new String[] {extractCommand.getCommandName(),
251 "-"+NcCSVExtract.INPUT_FILE, this.dummyCSVFilename
252 "-"+NcCSVExtract.COLUMN_INDEX, this.validColumnIndex
253
254 String errors = extractCommand.validCommand(args);
255 assertTrue("Valid arguments should not return an error", errors.isEmpty());
256
257 try
258 {
259 extractCommand.execute(args);
260 assertEquals(this.validValuesCol0Row0Row1Row2, outContent.toString());
261 }
262 catch(ParseException pe)
263 {
264 pe.printStackTrace();
265 fail(pe.getMessage());
266 }
267 catch(IOException ioe)
268 {
269 ioe.printStackTrace();
270 fail(ioe.getMessage());
271 }
272 }
273
274
275
276
277 public final void testValidRowIndexRange()
278 {
279 String[] args = new String[] {extractCommand.getCommandName(),
280 "-"+NcCSVExtract.INPUT_FILE, this.dummyCSVFilename
281 "-"+NcCSVExtract.COLUMN_INDEX, this.validColumnIndex
282 "-"+NcCSVExtract.START_ROW, this.validStartRowIndex
283 "-"+NcCSVExtract.END_ROW, this.validEndRowIndex
284
285 String errors = extractCommand.validCommand(args);
286 assertTrue("Valid arguments should not return an error", errors.isEmpty());
287
288 try
289 {
290 extractCommand.execute(args);
291 assertEquals(this.validValuesCol0Row1Row2, outContent.toString());
292 }
293 catch(ParseException pe)
294 {
295 pe.printStackTrace();
296 fail(pe.getMessage());
297 }
298 catch(IOException ioe)
299 {
300 ioe.printStackTrace();
301 fail(ioe.getMessage());
302 }
303 }
304
305
306
307
308 public final void testInvalidRowIndexRange()
309 {
310 String[] args = new String[] {extractCommand.getCommandName(),
311 "-"+NcCSVExtract.INPUT_FILE, this.dummyCSVFilename
312 "-"+NcCSVExtract.COLUMN_INDEX, this.validColumnIndex
313 "-"+NcCSVExtract.START_ROW, this.validStartRowIndex
314 "-"+NcCSVExtract.END_ROW, this.invalidEndRowIndex
315
316 String errors = extractCommand.validCommand(args);
317 assertTrue("Valid arguments should not return an error", errors.isEmpty());
318
319 try
320 {
321 extractCommand.execute(args);
322 assertEquals(this.validValuesCol0Row1Row2, outContent.toString());
323 }
324 catch(ParseException pe)
325 {
326 pe.printStackTrace();
327 fail(pe.getMessage());
328 }
329 catch(IOException ioe)
330 {
331 ioe.printStackTrace();
332 fail(ioe.getMessage());
333 }
334 }
335
336
337
338
339 public final void testInvalidStartRowIndexArgValue()
340 {
341 String[] args = new String[] {extractCommand.getCommandName(),
342 "-"+NcCSVExtract.INPUT_FILE, this.dummyCSVFilename
343 "-"+NcCSVExtract.COLUMN_INDEX, this.validColumnIndex
344 "-"+NcCSVExtract.START_ROW, "ABCDEFG"
345
346 String errors = extractCommand.validCommand(args);
347 assertTrue("Non-numeric startRow index value should return an error", !errors.isEmpty());
348 }
349
350
351
352
353 public final void testInvalidEndRowIndexArgValue()
354 {
355 String[] args = new String[] {extractCommand.getCommandName(),
356 "-"+NcCSVExtract.INPUT_FILE, this.dummyCSVFilename
357 "-"+NcCSVExtract.COLUMN_INDEX, this.validColumnIndex
358 "-"+NcCSVExtract.START_ROW, "0"
359 "-"+NcCSVExtract.END_ROW, "ABCDEFG"
360
361 String errors = extractCommand.validCommand(args);
362 assertTrue("Non-numeric startRow index value should return an error", !errors.isEmpty());
363 }
364
365
366
367
368 private void createDummyCSVFile()
369 {
370 try
371 {
372
373 FileWriter fstream = new FileWriter(this.dummyCSVFilename);
374 BufferedWriter out = new BufferedWriter(fstream);
375 for(int i=0; i<this.dummyCSVFileContents.length; i++)
376 {
377 out.write(this.dummyCSVFileContents[i]);
378 }
379
380 out.close();
381 }
382 catch (Exception e)
383 {
384
385 e.printStackTrace();
386 fail("Failed trying to create a dummy csv file for testing the extract command.");
387 }
388 }
389 }