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
This commit is contained in:
parent
f921e77605
commit
bfc40dede6
|
@ -0,0 +1,113 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.commons.csv;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.FilenameFilter;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
import org.junit.runners.Parameterized.Parameters;
|
||||
|
||||
/**
|
||||
* Parse tests using test files
|
||||
*
|
||||
*/
|
||||
@RunWith(Parameterized.class)
|
||||
public class CSVFileParserTest {
|
||||
|
||||
private static final File BASE = new File("src/test/resources/CSVFileParser");
|
||||
|
||||
private final BufferedReader testData;
|
||||
private final String testName;
|
||||
|
||||
public CSVFileParserTest(File file) throws FileNotFoundException
|
||||
{
|
||||
this.testName = file.getName();
|
||||
this.testData = new BufferedReader(new FileReader(file));
|
||||
}
|
||||
|
||||
private String readTestData() throws IOException {
|
||||
String line;
|
||||
do {
|
||||
line = testData.readLine();
|
||||
} while (line != null && line.startsWith("#"));
|
||||
return line;
|
||||
}
|
||||
|
||||
@Parameters
|
||||
public static Collection<Object[]> generateData()
|
||||
{
|
||||
List<Object[]> list = new ArrayList<Object[]>();
|
||||
|
||||
final FilenameFilter filenameFilter = new FilenameFilter(){
|
||||
public boolean accept(@SuppressWarnings("unused") File dir, String name) {
|
||||
return name.startsWith("test") && name.endsWith(".txt");
|
||||
}
|
||||
};
|
||||
File[] files = BASE.listFiles(filenameFilter);
|
||||
for(File f : files){
|
||||
list.add(new Object[]{f});
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCSVFile() throws Exception {
|
||||
String line = readTestData();
|
||||
assertNotNull("file must contain config line", line);
|
||||
String[] split = line.split(" ");
|
||||
assertTrue(testName+" require 1 param", split.length >= 1);
|
||||
// first line starts with csv data file name
|
||||
BufferedReader csvFile = new BufferedReader(new FileReader(new File(BASE, split[0])));
|
||||
CSVFormat fmt = CSVFormat.PRISTINE.withDelimiter(',').withEncapsulator('"');
|
||||
for(int i=1; i < split.length; i++) {
|
||||
final String option = split[i];
|
||||
String[] option_parts = option.split("=",2);
|
||||
if ("IgnoreEmpty".equalsIgnoreCase(option_parts[0])){
|
||||
fmt = fmt.withEmptyLinesIgnored(Boolean.parseBoolean(option_parts[1]));
|
||||
} else if ("IgnoreSpaces".equalsIgnoreCase(option_parts[0])) {
|
||||
fmt = fmt.withSurroundingSpacesIgnored(Boolean.parseBoolean(option_parts[1]));
|
||||
} else {
|
||||
fail(testName+" unexpected option: "+option);
|
||||
}
|
||||
}
|
||||
line = readTestData(); // get string version of format
|
||||
assertEquals(testName+" Expected format ", line, fmt.toString());
|
||||
|
||||
// Now parse the file and compare against the expected results
|
||||
for(CSVRecord rec : fmt.parse(csvFile)) {
|
||||
String parsed = rec.toString();
|
||||
int count = rec.size();
|
||||
assertEquals(testName, readTestData(), count+":"+parsed);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
This directory contains test files for the CSVFileParserTest class.
|
||||
|
||||
Files are of two types:
|
||||
- test*.txt: these are test control and expected results
|
||||
- test*.csv: these are the test input files, in various CSV formats
|
||||
|
||||
Test Control files (test*.txt)
|
||||
==============================
|
||||
The first line of this file consists of several space-separated fields:
|
||||
- name of CSV data file (in same directory)
|
||||
- (optional) settings to be applied to the default parsing format, which is delim=',' encap='"'
|
||||
The settings have the form (see test source file for full details):
|
||||
IgnoreEmpty=true|false
|
||||
|
||||
The second line is the expected output from invoking CSVFormat#toString() on the parsing format
|
||||
|
||||
Subsequent lines are of the form:
|
||||
n:[output]
|
||||
where n is the expected result of CSVRecord#size, and
|
||||
[output] is the expected output from invoking CSVRecord#toString() on the parsed records.
|
||||
|
||||
Lines beginning with # are ignored, and can be used for comments.
|
|
@ -0,0 +1,14 @@
|
|||
A,B,C,"D"
|
||||
# plain values
|
||||
a,b,c,d
|
||||
# spaces before and after
|
||||
e ,f , g,h
|
||||
# quoted: with spaces before and after
|
||||
" i ", " j " , " k "," l "
|
||||
# empty values
|
||||
,,,
|
||||
# empty quoted values
|
||||
"","","",""
|
||||
# empty line
|
||||
|
||||
# EOF on next line
|
Can't render this file because it has a wrong number of fields in line 2.
|
|
@ -0,0 +1,15 @@
|
|||
test.csv IgnoreEmpty=true
|
||||
Delimiter=<,> Encapsulator=<"> EmptyLines:ignored
|
||||
4:[A, B, C, D]
|
||||
1:[# plain values]
|
||||
4:[a, b, c, d]
|
||||
1:[# spaces before and after]
|
||||
4:[ e , f , g, h ]
|
||||
1:[# quoted: with spaces before and after]
|
||||
4:[ i , " j " , " k ", l ]
|
||||
1:[# empty values]
|
||||
4:[, , , ]
|
||||
1:[# empty quoted values]
|
||||
4:[, , , ]
|
||||
1:[# empty line]
|
||||
1:[# EOF on next line]
|
|
@ -0,0 +1,16 @@
|
|||
test.csv
|
||||
Delimiter=<,> Encapsulator=<">
|
||||
4:[A, B, C, D]
|
||||
1:[# plain values]
|
||||
4:[a, b, c, d]
|
||||
1:[# spaces before and after]
|
||||
4:[ e , f , g, h ]
|
||||
1:[# quoted: with spaces before and after]
|
||||
4:[ i , " j " , " k ", l ]
|
||||
1:[# empty values]
|
||||
4:[, , , ]
|
||||
1:[# empty quoted values]
|
||||
4:[, , , ]
|
||||
1:[# empty line]
|
||||
1:[]
|
||||
1:[# EOF on next line]
|
|
@ -0,0 +1,16 @@
|
|||
test.csv IgnoreSpaces=true
|
||||
Delimiter=<,> Encapsulator=<"> SurroundingSpaces:ignored
|
||||
4:[A, B, C, D]
|
||||
1:[# plain values]
|
||||
4:[a, b, c, d]
|
||||
1:[# spaces before and after]
|
||||
4:[e, f, g, h]
|
||||
1:[# quoted: with spaces before and after]
|
||||
4:[ i , j , k , l ]
|
||||
1:[# empty values]
|
||||
4:[, , , ]
|
||||
1:[# empty quoted values]
|
||||
4:[, , , ]
|
||||
1:[# empty line]
|
||||
1:[]
|
||||
1:[# EOF on next line]
|
Loading…
Reference in New Issue