diff --git a/src/java/org/apache/commons/csv/CSVParser.java b/src/java/org/apache/commons/csv/CSVParser.java index 1813afeb..a7fc1936 100644 --- a/src/java/org/apache/commons/csv/CSVParser.java +++ b/src/java/org/apache/commons/csv/CSVParser.java @@ -343,7 +343,7 @@ public class CSVParser { } // important: make sure a new char gets consumed in each iteration - while (!tkn.isReady) { + while (!tkn.isReady && tkn.type != TT_EOF) { // ignore whitespaces at beginning of a token while (strategy.getIgnoreLeadingWhitespaces() && isWhitespace(c) && !eol) { wsBuf.append((char) c); diff --git a/src/test/org/apache/commons/csv/CSVParserTest.java b/src/test/org/apache/commons/csv/CSVParserTest.java index 08c0454d..a838be1b 100644 --- a/src/test/org/apache/commons/csv/CSVParserTest.java +++ b/src/test/org/apache/commons/csv/CSVParserTest.java @@ -497,7 +497,7 @@ public class CSVParserTest extends TestCase { String[][] res = { { "a", "b" }, { "\n", " " }, - { "", "#" }, // WARNING: TODO: this causes a hang if comments are enabled + { "", "#" }, }; CSVStrategy strategy = CSVStrategy.DEFAULT_STRATEGY; @@ -510,6 +510,20 @@ public class CSVParserTest extends TestCase { if (!CSVPrinterTest.equals(res, tmp)) { assertTrue(false); } + + String[][] res_comments = { + { "a", "b" }, + { "\n", " " }, + { ""}, + }; + + strategy = new CSVStrategy(',','"','#'); + parser = new CSVParser(new StringReader(code), strategy); + tmp = parser.getAllValues(); + + if (!CSVPrinterTest.equals(res_comments, tmp)) { + assertTrue(false); + } } diff --git a/src/test/org/apache/commons/csv/CSVPrinterTest.java b/src/test/org/apache/commons/csv/CSVPrinterTest.java index 6e9815d7..b4fd4e75 100644 --- a/src/test/org/apache/commons/csv/CSVPrinterTest.java +++ b/src/test/org/apache/commons/csv/CSVPrinterTest.java @@ -133,9 +133,15 @@ public class CSVPrinterTest extends TestCase { } public static boolean equals(String[][] a, String[][] b) { + if (a.length != b.length) { + return false; + } for (int i=0; i