You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by se...@apache.org on 2012/03/27 19:32:15 UTC

svn commit: r1305919 - in /commons/proper/csv/trunk/src/test: java/org/apache/commons/csv/ resources/ resources/CSVFileParser/

Author: sebb
Date: Tue Mar 27 17:32:15 2012
New Revision: 1305919

URL: http://svn.apache.org/viewvc?rev=1305919&view=rev
Log:
Add CSV file parser test case runner

Added:
    commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFileParserTest.java   (with props)
    commons/proper/csv/trunk/src/test/resources/
    commons/proper/csv/trunk/src/test/resources/CSVFileParser/
    commons/proper/csv/trunk/src/test/resources/CSVFileParser/README.txt   (with props)
    commons/proper/csv/trunk/src/test/resources/CSVFileParser/test.csv
    commons/proper/csv/trunk/src/test/resources/CSVFileParser/test_default.txt   (with props)
    commons/proper/csv/trunk/src/test/resources/CSVFileParser/test_rfc4180.txt   (with props)
    commons/proper/csv/trunk/src/test/resources/CSVFileParser/test_rfc4180_trim.txt   (with props)

Added: commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFileParserTest.java
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFileParserTest.java?rev=1305919&view=auto
==============================================================================
--- commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFileParserTest.java (added)
+++ commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFileParserTest.java Tue Mar 27 17:32:15 2012
@@ -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);
+        }
+    }
+}

Propchange: commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFileParserTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFileParserTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: commons/proper/csv/trunk/src/test/resources/CSVFileParser/README.txt
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/resources/CSVFileParser/README.txt?rev=1305919&view=auto
==============================================================================
--- commons/proper/csv/trunk/src/test/resources/CSVFileParser/README.txt (added)
+++ commons/proper/csv/trunk/src/test/resources/CSVFileParser/README.txt Tue Mar 27 17:32:15 2012
@@ -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

Propchange: commons/proper/csv/trunk/src/test/resources/CSVFileParser/README.txt
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/csv/trunk/src/test/resources/CSVFileParser/test.csv
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/resources/CSVFileParser/test.csv?rev=1305919&view=auto
==============================================================================
--- commons/proper/csv/trunk/src/test/resources/CSVFileParser/test.csv (added)
+++ commons/proper/csv/trunk/src/test/resources/CSVFileParser/test.csv Tue Mar 27 17:32:15 2012
@@ -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

Added: commons/proper/csv/trunk/src/test/resources/CSVFileParser/test_default.txt
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/resources/CSVFileParser/test_default.txt?rev=1305919&view=auto
==============================================================================
--- commons/proper/csv/trunk/src/test/resources/CSVFileParser/test_default.txt (added)
+++ commons/proper/csv/trunk/src/test/resources/CSVFileParser/test_default.txt Tue Mar 27 17:32:15 2012
@@ -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

Propchange: commons/proper/csv/trunk/src/test/resources/CSVFileParser/test_default.txt
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/csv/trunk/src/test/resources/CSVFileParser/test_rfc4180.txt
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/resources/CSVFileParser/test_rfc4180.txt?rev=1305919&view=auto
==============================================================================
--- commons/proper/csv/trunk/src/test/resources/CSVFileParser/test_rfc4180.txt (added)
+++ commons/proper/csv/trunk/src/test/resources/CSVFileParser/test_rfc4180.txt Tue Mar 27 17:32:15 2012
@@ -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

Propchange: commons/proper/csv/trunk/src/test/resources/CSVFileParser/test_rfc4180.txt
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/csv/trunk/src/test/resources/CSVFileParser/test_rfc4180_trim.txt
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/resources/CSVFileParser/test_rfc4180_trim.txt?rev=1305919&view=auto
==============================================================================
--- commons/proper/csv/trunk/src/test/resources/CSVFileParser/test_rfc4180_trim.txt (added)
+++ commons/proper/csv/trunk/src/test/resources/CSVFileParser/test_rfc4180_trim.txt Tue Mar 27 17:32:15 2012
@@ -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

Propchange: commons/proper/csv/trunk/src/test/resources/CSVFileParser/test_rfc4180_trim.txt
------------------------------------------------------------------------------
    svn:eol-style = native