commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r1721778 - in /commons/proper/csv/trunk/src: main/java/org/apache/commons/csv/CSVRecord.java test/java/org/apache/commons/csv/JiraCsv167Test.java test/resources/csv-167/ test/resources/csv-167/sample1.csv
Date Sun, 27 Dec 2015 00:02:30 GMT
Author: ggregory
Date: Sun Dec 27 00:02:30 2015
New Revision: 1721778

URL: http://svn.apache.org/viewvc?rev=1721778&view=rev
Log:
Unit test for [CSV-167] Comment line hides next record.

Added:
    commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/JiraCsv167Test.java
    commons/proper/csv/trunk/src/test/resources/csv-167/
    commons/proper/csv/trunk/src/test/resources/csv-167/sample1.csv
Modified:
    commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVRecord.java

Modified: commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVRecord.java
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVRecord.java?rev=1721778&r1=1721777&r2=1721778&view=diff
==============================================================================
--- commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVRecord.java (original)
+++ commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVRecord.java Sun Dec 27
00:02:30 2015
@@ -162,6 +162,16 @@ public final class CSVRecord implements
     }
 
     /**
+     * Checks whether this record is a comment, false otherwise.
+     *
+     * @return true if this record is a comment, false otherwise
+     * @since 1.3
+     */
+    public boolean isComment() {
+        return comment != null;
+    }
+
+    /**
      * Checks whether a given column is mapped, i.e. its name has been defined to the parser.
      *
      * @param name

Added: commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/JiraCsv167Test.java
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/JiraCsv167Test.java?rev=1721778&view=auto
==============================================================================
--- commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/JiraCsv167Test.java (added)
+++ commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/JiraCsv167Test.java Sun
Dec 27 00:02:30 2015
@@ -0,0 +1,64 @@
+/*
+ * 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 java.io.File;
+import java.io.IOException;
+import java.nio.charset.Charset;
+
+import org.junit.Assert;
+import org.junit.Ignore;
+import org.junit.Test;
+
+public class JiraCsv167Test {
+
+    @Test
+    @Ignore("Fails")
+    public void parse() throws IOException {
+        File csvData = new File("src/test/resources/csv-167/sample1.csv");
+        CSVFormat format = CSVFormat.DEFAULT;
+        //
+        format = format.withAllowMissingColumnNames(false);
+        format = format.withCommentMarker('#');
+        format = format.withDelimiter(',');
+        format = format.withEscape('\\');
+        format = format.withHeader("author", "title", "publishDate");
+        format = format.withHeaderComments("headerComment");
+        format = format.withNullString("NULL");
+        format = format.withIgnoreEmptyLines(true);
+        format = format.withIgnoreSurroundingSpaces(true);
+        format = format.withQuote('"');
+        format = format.withQuoteMode(QuoteMode.ALL);
+        format = format.withRecordSeparator('\n');
+        format = format.withSkipHeaderRecord(false);
+        //
+        CSVParser parser = CSVParser.parse(csvData, Charset.defaultCharset(), format);
+        int comments = 0;
+        int records = 0;
+        for (CSVRecord csvRecord : parser) {
+            if (csvRecord.isComment()) {
+                comments++;
+            } else {
+                records++;
+                // System.out.println("[" + csvRecord.toString() + "]");
+            }
+        }
+        // Comment lines are concatenated, in this example 4 lines become 2 comments.
+        Assert.assertEquals(2, comments);
+        Assert.assertEquals(3, records);
+    }
+}

Added: commons/proper/csv/trunk/src/test/resources/csv-167/sample1.csv
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/resources/csv-167/sample1.csv?rev=1721778&view=auto
==============================================================================
--- commons/proper/csv/trunk/src/test/resources/csv-167/sample1.csv (added)
+++ commons/proper/csv/trunk/src/test/resources/csv-167/sample1.csv Sun Dec 27 00:02:30 2015
@@ -0,0 +1,8 @@
+# Comment before header
+author,title,publishDate
+Dan Simmons,Hyperion,"1989"
+# Comment Line 1
+# Comment Line 2
+# Comment Line 3
+Douglas Adams,The Hitchhiker's \"Guide\" to the Galaxy,1979
+Douglas John,The Hitchhiker's \"Guide\" to the Mars,1979



Mime
View raw message