Skip to content

Commit bfc40de

Browse files
committed
Add CSV file parser test case runner
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1305919 13f79535-47bb-0310-9956-ffa450edef68
1 parent f921e77 commit bfc40de

6 files changed

Lines changed: 196 additions & 0 deletions

File tree

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.commons.csv;
19+
20+
import static org.junit.Assert.assertEquals;
21+
import static org.junit.Assert.assertNotNull;
22+
import static org.junit.Assert.assertTrue;
23+
import static org.junit.Assert.fail;
24+
25+
import java.io.BufferedReader;
26+
import java.io.File;
27+
import java.io.FileNotFoundException;
28+
import java.io.FileReader;
29+
import java.io.FilenameFilter;
30+
import java.io.IOException;
31+
import java.util.ArrayList;
32+
import java.util.Collection;
33+
import java.util.List;
34+
35+
import org.junit.Test;
36+
import org.junit.runner.RunWith;
37+
import org.junit.runners.Parameterized;
38+
import org.junit.runners.Parameterized.Parameters;
39+
40+
/**
41+
* Parse tests using test files
42+
*
43+
*/
44+
@RunWith(Parameterized.class)
45+
public class CSVFileParserTest {
46+
47+
private static final File BASE = new File("src/test/resources/CSVFileParser");
48+
49+
private final BufferedReader testData;
50+
private final String testName;
51+
52+
public CSVFileParserTest(File file) throws FileNotFoundException
53+
{
54+
this.testName = file.getName();
55+
this.testData = new BufferedReader(new FileReader(file));
56+
}
57+
58+
private String readTestData() throws IOException {
59+
String line;
60+
do {
61+
line = testData.readLine();
62+
} while (line != null && line.startsWith("#"));
63+
return line;
64+
}
65+
66+
@Parameters
67+
public static Collection<Object[]> generateData()
68+
{
69+
List<Object[]> list = new ArrayList<Object[]>();
70+
71+
final FilenameFilter filenameFilter = new FilenameFilter(){
72+
public boolean accept(@SuppressWarnings("unused") File dir, String name) {
73+
return name.startsWith("test") && name.endsWith(".txt");
74+
}
75+
};
76+
File[] files = BASE.listFiles(filenameFilter);
77+
for(File f : files){
78+
list.add(new Object[]{f});
79+
}
80+
return list;
81+
}
82+
83+
@Test
84+
public void testCSVFile() throws Exception {
85+
String line = readTestData();
86+
assertNotNull("file must contain config line", line);
87+
String[] split = line.split(" ");
88+
assertTrue(testName+" require 1 param", split.length >= 1);
89+
// first line starts with csv data file name
90+
BufferedReader csvFile = new BufferedReader(new FileReader(new File(BASE, split[0])));
91+
CSVFormat fmt = CSVFormat.PRISTINE.withDelimiter(',').withEncapsulator('"');
92+
for(int i=1; i < split.length; i++) {
93+
final String option = split[i];
94+
String[] option_parts = option.split("=",2);
95+
if ("IgnoreEmpty".equalsIgnoreCase(option_parts[0])){
96+
fmt = fmt.withEmptyLinesIgnored(Boolean.parseBoolean(option_parts[1]));
97+
} else if ("IgnoreSpaces".equalsIgnoreCase(option_parts[0])) {
98+
fmt = fmt.withSurroundingSpacesIgnored(Boolean.parseBoolean(option_parts[1]));
99+
} else {
100+
fail(testName+" unexpected option: "+option);
101+
}
102+
}
103+
line = readTestData(); // get string version of format
104+
assertEquals(testName+" Expected format ", line, fmt.toString());
105+
106+
// Now parse the file and compare against the expected results
107+
for(CSVRecord rec : fmt.parse(csvFile)) {
108+
String parsed = rec.toString();
109+
int count = rec.size();
110+
assertEquals(testName, readTestData(), count+":"+parsed);
111+
}
112+
}
113+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
This directory contains test files for the CSVFileParserTest class.
2+
3+
Files are of two types:
4+
- test*.txt: these are test control and expected results
5+
- test*.csv: these are the test input files, in various CSV formats
6+
7+
Test Control files (test*.txt)
8+
==============================
9+
The first line of this file consists of several space-separated fields:
10+
- name of CSV data file (in same directory)
11+
- (optional) settings to be applied to the default parsing format, which is delim=',' encap='"'
12+
The settings have the form (see test source file for full details):
13+
IgnoreEmpty=true|false
14+
15+
The second line is the expected output from invoking CSVFormat#toString() on the parsing format
16+
17+
Subsequent lines are of the form:
18+
n:[output]
19+
where n is the expected result of CSVRecord#size, and
20+
[output] is the expected output from invoking CSVRecord#toString() on the parsed records.
21+
22+
Lines beginning with # are ignored, and can be used for comments.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
A,B,C,"D"
2+
# plain values
3+
a,b,c,d
4+
# spaces before and after
5+
e ,f , g,h
6+
# quoted: with spaces before and after
7+
" i ", " j " , " k "," l "
8+
# empty values
9+
,,,
10+
# empty quoted values
11+
"","","",""
12+
# empty line
13+
14+
# EOF on next line
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
test.csv IgnoreEmpty=true
2+
Delimiter=<,> Encapsulator=<"> EmptyLines:ignored
3+
4:[A, B, C, D]
4+
1:[# plain values]
5+
4:[a, b, c, d]
6+
1:[# spaces before and after]
7+
4:[ e , f , g, h ]
8+
1:[# quoted: with spaces before and after]
9+
4:[ i , " j " , " k ", l ]
10+
1:[# empty values]
11+
4:[, , , ]
12+
1:[# empty quoted values]
13+
4:[, , , ]
14+
1:[# empty line]
15+
1:[# EOF on next line]
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
test.csv
2+
Delimiter=<,> Encapsulator=<">
3+
4:[A, B, C, D]
4+
1:[# plain values]
5+
4:[a, b, c, d]
6+
1:[# spaces before and after]
7+
4:[ e , f , g, h ]
8+
1:[# quoted: with spaces before and after]
9+
4:[ i , " j " , " k ", l ]
10+
1:[# empty values]
11+
4:[, , , ]
12+
1:[# empty quoted values]
13+
4:[, , , ]
14+
1:[# empty line]
15+
1:[]
16+
1:[# EOF on next line]
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
test.csv IgnoreSpaces=true
2+
Delimiter=<,> Encapsulator=<"> SurroundingSpaces:ignored
3+
4:[A, B, C, D]
4+
1:[# plain values]
5+
4:[a, b, c, d]
6+
1:[# spaces before and after]
7+
4:[e, f, g, h]
8+
1:[# quoted: with spaces before and after]
9+
4:[ i , j , k , l ]
10+
1:[# empty values]
11+
4:[, , , ]
12+
1:[# empty quoted values]
13+
4:[, , , ]
14+
1:[# empty line]
15+
1:[]
16+
1:[# EOF on next line]

0 commit comments

Comments
 (0)