From bfc40dede6ddf4988d966b1d4b8aa711518fa7b5 Mon Sep 17 00:00:00 2001 From: Sebastian Bazley Date: Tue, 27 Mar 2012 17:32:15 +0000 Subject: [PATCH] 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 --- .../apache/commons/csv/CSVFileParserTest.java | 113 ++++++++++++++++++ src/test/resources/CSVFileParser/README.txt | 22 ++++ src/test/resources/CSVFileParser/test.csv | 14 +++ .../resources/CSVFileParser/test_default.txt | 15 +++ .../resources/CSVFileParser/test_rfc4180.txt | 16 +++ .../CSVFileParser/test_rfc4180_trim.txt | 16 +++ 6 files changed, 196 insertions(+) create mode 100644 src/test/java/org/apache/commons/csv/CSVFileParserTest.java create mode 100644 src/test/resources/CSVFileParser/README.txt create mode 100644 src/test/resources/CSVFileParser/test.csv create mode 100644 src/test/resources/CSVFileParser/test_default.txt create mode 100644 src/test/resources/CSVFileParser/test_rfc4180.txt create mode 100644 src/test/resources/CSVFileParser/test_rfc4180_trim.txt diff --git a/src/test/java/org/apache/commons/csv/CSVFileParserTest.java b/src/test/java/org/apache/commons/csv/CSVFileParserTest.java new file mode 100644 index 00000000..f7fb6bfc --- /dev/null +++ b/src/test/java/org/apache/commons/csv/CSVFileParserTest.java @@ -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 generateData() + { + List list = new ArrayList(); + + 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); + } + } +} diff --git a/src/test/resources/CSVFileParser/README.txt b/src/test/resources/CSVFileParser/README.txt new file mode 100644 index 00000000..3540a80d --- /dev/null +++ b/src/test/resources/CSVFileParser/README.txt @@ -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. \ No newline at end of file diff --git a/src/test/resources/CSVFileParser/test.csv b/src/test/resources/CSVFileParser/test.csv new file mode 100644 index 00000000..d394ba57 --- /dev/null +++ b/src/test/resources/CSVFileParser/test.csv @@ -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 diff --git a/src/test/resources/CSVFileParser/test_default.txt b/src/test/resources/CSVFileParser/test_default.txt new file mode 100644 index 00000000..cca4de26 --- /dev/null +++ b/src/test/resources/CSVFileParser/test_default.txt @@ -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] \ No newline at end of file diff --git a/src/test/resources/CSVFileParser/test_rfc4180.txt b/src/test/resources/CSVFileParser/test_rfc4180.txt new file mode 100644 index 00000000..28028545 --- /dev/null +++ b/src/test/resources/CSVFileParser/test_rfc4180.txt @@ -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] \ No newline at end of file diff --git a/src/test/resources/CSVFileParser/test_rfc4180_trim.txt b/src/test/resources/CSVFileParser/test_rfc4180_trim.txt new file mode 100644 index 00000000..6ed401e4 --- /dev/null +++ b/src/test/resources/CSVFileParser/test_rfc4180_trim.txt @@ -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] \ No newline at end of file