From ad11cd14d0e407e351d7de71018cbc25ccb4352f Mon Sep 17 00:00:00 2001 From: Chen Date: Tue, 2 Jun 2020 03:53:07 +0800 Subject: [PATCH] [CSV-149]/[CSV-195] fix line number is not proper at EOF (#60) --- .../apache/commons/csv/ExtendedBufferedReader.java | 3 ++- .../java/org/apache/commons/csv/CSVParserTest.java | 12 ++++++------ .../apache/commons/csv/issues/JiraCsv149Test.java | 2 -- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/apache/commons/csv/ExtendedBufferedReader.java b/src/main/java/org/apache/commons/csv/ExtendedBufferedReader.java index b9ca79df..9731f818 100644 --- a/src/main/java/org/apache/commons/csv/ExtendedBufferedReader.java +++ b/src/main/java/org/apache/commons/csv/ExtendedBufferedReader.java @@ -125,7 +125,8 @@ final class ExtendedBufferedReader extends BufferedReader { @Override public int read() throws IOException { final int current = super.read(); - if (current == CR || current == LF && lastChar != CR) { + if ((current == CR || current == LF && lastChar != CR) + || (current == END_OF_STREAM && lastChar != CR && lastChar != LF && lastChar != END_OF_STREAM)) { eolCounter++; } lastChar = current; diff --git a/src/test/java/org/apache/commons/csv/CSVParserTest.java b/src/test/java/org/apache/commons/csv/CSVParserTest.java index d058cc42..8c410945 100644 --- a/src/test/java/org/apache/commons/csv/CSVParserTest.java +++ b/src/test/java/org/apache/commons/csv/CSVParserTest.java @@ -644,11 +644,11 @@ public class CSVParserTest { assertEquals(2, record.getRecordNumber()); assertEquals(2, parser.getRecordNumber()); assertNotNull(record = parser.nextRecord()); - assertEquals(8, parser.getCurrentLineNumber()); + assertEquals(9, parser.getCurrentLineNumber()); assertEquals(3, record.getRecordNumber()); assertEquals(3, parser.getRecordNumber()); assertNull(record = parser.nextRecord()); - assertEquals(8, parser.getCurrentLineNumber()); + assertEquals(9, parser.getCurrentLineNumber()); assertEquals(3, parser.getRecordNumber()); } } @@ -1192,11 +1192,11 @@ public class CSVParserTest { assertNotNull(parser.nextRecord()); assertEquals(2, parser.getCurrentLineNumber()); assertNotNull(parser.nextRecord()); - // Still 2 because the last line is does not have EOL chars - assertEquals(2, parser.getCurrentLineNumber()); + // Read EOF without EOL should 3 + assertEquals(3, parser.getCurrentLineNumber()); assertNull(parser.nextRecord()); - // Still 2 because the last line is does not have EOL chars - assertEquals(2, parser.getCurrentLineNumber()); + // Read EOF without EOL should 3 + assertEquals(3, parser.getCurrentLineNumber()); } } diff --git a/src/test/java/org/apache/commons/csv/issues/JiraCsv149Test.java b/src/test/java/org/apache/commons/csv/issues/JiraCsv149Test.java index d44093a7..83fb36fd 100644 --- a/src/test/java/org/apache/commons/csv/issues/JiraCsv149Test.java +++ b/src/test/java/org/apache/commons/csv/issues/JiraCsv149Test.java @@ -24,10 +24,8 @@ import java.io.StringReader; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVParser; import org.apache.commons.csv.CSVRecord; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; -@Disabled public class JiraCsv149Test { private static final String CR_LF = "\r\n";