[
https://issues.apache.org/jira/browse/CSV-195?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15473272#comment-15473272
]
Michael Vitz commented on CSV-195:
----------------------------------
I tried writing a unit test for it, but it passes on my machine. Maybe some
line endings/hidden characters?
See diff below:
{code}
diff --git a/src/test/java/org/apache/commons/csv/bugs/JiraCsv195Test.java
b/src/test/java/org/apache/commons/csv/bugs/JiraCsv195Test.java
new file mode 100644
index 0000000..aa66b32
--- /dev/null
+++ b/src/test/java/org/apache/commons/csv/bugs/JiraCsv195Test.java
@@ -0,0 +1,56 @@
+/*
+ * 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.bugs;
+
+import org.apache.commons.csv.CSVFormat;
+import org.apache.commons.csv.CSVParser;
+import org.apache.commons.csv.CSVRecord;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+
+public class JiraCsv195Test {
+
+ @Test
+ public void parse() throws IOException {
+ CSVFormat format = CSVFormat.RFC4180;
+ //
+ format = format.withFirstRecordAsHeader();
+ format = format.withIgnoreEmptyLines();
+ format = format.withTrim();
+ //
+ int count = 0;
+ try (CSVParser parser = format.parse(getTestInput())) {
+ for (CSVRecord record : parser) {
+ System.out.println("Processing " +
parser.getCurrentLineNumber());
+ count++;
+ }
+ System.out.println(count);
+ }
+ Assert.assertEquals(27, count);
+ }
+
+ private Reader getTestInput() {
+ final InputStream is =
ClassLoader.getSystemClassLoader().getResourceAsStream("csv-195/csv195.csv");
+ return new InputStreamReader(is);
+ }
+
+}
diff --git a/src/test/resources/csv-195/csv195.csv
b/src/test/resources/csv-195/csv195.csv
new file mode 100644
index 0000000..4d12d6e
--- /dev/null
+++ b/src/test/resources/csv-195/csv195.csv
@@ -0,0 +1,28 @@
+indicator,nominatedInfoType,nominatedInfo,mobileNumber,firstName,middleName,lastName,suffix,dateOfBirth,amount,id,branch,category
+a,SSS,1234567890,63**********,John,S,Devereaux,I,3/17/1986,80000,8510087650,Manila,A
+a,GSIS,1234567891,63**********,Jolie,A,Noname,II,8/24/1948,30000,8510087651,Mandaluyong,B
+a,TIN,1234567892,63**********,Jim,B,Lim,III,1/15/1952,40000,8510087652,Makati,C
+u,SSS,1234567890,63**********,Tammy,S,Devereaux,I,8/2/1990,180000,8510087650,Manila,A
+u,GSIS,1234567891,63**********,Earl,A,Noname,II,8/24/1948,190000,8510087651,Mandaluyong,B
+u,TIN,1234567892,63**********,Clive,B,Lim,III,1/15/1952,200000,8510087652,Makati,C
+u,SSS,1234567893,63**********,Mary,C,Quinto,IV,10/24/1947,210000,8510087653,Pasig,D
+u,GSIS,1234567894,63**********,Paul,D,Jordan,V,6/24/1948,220000,8510087654,Manila,A
+u,TIN,1234567895,63**********,Mike,E,Sanchez,VI,8/23/1949,230000,8510087655,Quezon,B
+u,SSS,1234567896,63**********,Karl
Len,A,Dimla,VII,7/3/1950,240000,8510087656,Mandaluyong,C
+u,GSIS,1234567897,63**********,Earl,B,James,VIII,6/7/1951,250000,8510087657,Makati,D
+u,TIN,1234567898,63**********,Janice,D,Mason,IX,9John/5/1949,260000,8510087658,Pasig,A
+u,SSS,1234567899,63**********,Owen,A,Manna,X,5/13/1950,270000,8510087659,Quezon,B
+u,GSIS,1234567900,63**********,Lawrence,C,McAdams,XI,11/9/1951,280000,8510087660,Mandaluyong,C
+u,TIN,1234567901,63**********,Rodney
Leo,E,Exupery,XII,10/18/1994,290000,8510087661,Pasig,A
+d,SSS,1234567890,63**********,Tammy,S,Devereaux,I,8/2/1990,180000,8510087650,Manila,A
+d,GSIS,1234567891,63**********,Earl,A,Noname,II,8/24/1948,190000,8510087651,Mandaluyong,B
+d,TIN,1234567892,63**********,Clive,B,Lima,III,1/15/1952,200000,8510087652,Makati,C
+d,SSS,1234567893,63**********,Mary,C,Quinto,IV,10/24/1947,210000,8510087653,Pasig,D
+d,GSIS,1234567894,63**********,Paul,D,Jones,V,6/24/1948,220000,8510087654,Manila,A
+d,TIN,1234567895,63**********,Mike,E,Sanchez,VI,8/23/1949,230000,8510087655,Quezon,B
+d,SSS,1234567896,63**********,Karl
Len,A,Dimla,VII,7/3/1950,240000,8510087656,Mandaluyong,C
+d,GSIS,1234567897,63**********,Earl,B,James,VIII,6/7/1951,250000,8510087657,Makati,D
+d,TIN,1234567898,63**********,Janice,D,Mason,IX,9/5/1949,260000,8510087658,Pasig,A
+d,SSS,1234567899,63**********,Bien,A,Mandac,X,5/13/1950,270000,8510087659,Quezon,B
+d,GSIS,1234567900,63**********,Lawrence,C,McAdams,XI,11/9/1951,280000,8510087660,Mandaluyong,C
+d,TIN,1234567901,63**********,Rodney
Leo,E,Exupery,XII,10/18/1994,290000,8510087661,Pasig,A
{code}
> Parser iterates over the last CSV Record twice.
> -----------------------------------------------
>
> Key: CSV-195
> URL: https://issues.apache.org/jira/browse/CSV-195
> Project: Commons CSV
> Issue Type: Bug
> Components: Parser
> Affects Versions: 1.4
> Environment: Mac OS X 10.10.5
> Reporter: Rodolfo Duldulao
> Fix For: Patch Needed
>
>
> {code:java}
> class CSVParserSpecification extends Specification {
> def "TEst CSVParser"() {
> setup:
> URL url = new URL("https://....../csv_with_28_lines_header_plus_
> 27_records");
> BufferedReader reader = new BufferedReader(new
> InputStreamReader(url.openStream()));
> def CSVParser parser =
> CSVFormat.RFC4180.withFirstRecordAsHeader().withIgnoreEmptyLines().withTrim().parse(reader);
> when:
> def count = 0
> for (CSVRecord record: parser)
> { println("Processing " + parser.getCurrentLineNumber()) count++ }
> println(count);
> parser.close()
> then:
> count == 27
> }
> }
> {code}
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)