Author: yonik
Date: Thu Jul 15 01:32:55 2010
New Revision: 964273

URL: http://svn.apache.org/viewvc?rev=964273&view=rev
Log:
SANDBOX-313: Endless loops in CSV parser when last line is comment

Modified:
    commons/sandbox/csv/trunk/src/java/org/apache/commons/csv/CSVParser.java
    commons/sandbox/csv/trunk/src/test/org/apache/commons/csv/CSVParserTest.java
    
commons/sandbox/csv/trunk/src/test/org/apache/commons/csv/CSVPrinterTest.java

Modified: 
commons/sandbox/csv/trunk/src/java/org/apache/commons/csv/CSVParser.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/csv/trunk/src/java/org/apache/commons/csv/CSVParser.java?rev=964273&r1=964272&r2=964273&view=diff
==============================================================================
--- commons/sandbox/csv/trunk/src/java/org/apache/commons/csv/CSVParser.java 
(original)
+++ commons/sandbox/csv/trunk/src/java/org/apache/commons/csv/CSVParser.java 
Thu Jul 15 01:32:55 2010
@@ -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);

Modified: 
commons/sandbox/csv/trunk/src/test/org/apache/commons/csv/CSVParserTest.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/csv/trunk/src/test/org/apache/commons/csv/CSVParserTest.java?rev=964273&r1=964272&r2=964273&view=diff
==============================================================================
--- 
commons/sandbox/csv/trunk/src/test/org/apache/commons/csv/CSVParserTest.java 
(original)
+++ 
commons/sandbox/csv/trunk/src/test/org/apache/commons/csv/CSVParserTest.java 
Thu Jul 15 01:32:55 2010
@@ -497,7 +497,7 @@ public class CSVParserTest extends TestC
     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 TestC
     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);
+    }
   }
 
 

Modified: 
commons/sandbox/csv/trunk/src/test/org/apache/commons/csv/CSVPrinterTest.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/csv/trunk/src/test/org/apache/commons/csv/CSVPrinterTest.java?rev=964273&r1=964272&r2=964273&view=diff
==============================================================================
--- 
commons/sandbox/csv/trunk/src/test/org/apache/commons/csv/CSVPrinterTest.java 
(original)
+++ 
commons/sandbox/csv/trunk/src/test/org/apache/commons/csv/CSVPrinterTest.java 
Thu Jul 15 01:32:55 2010
@@ -133,9 +133,15 @@ public class CSVPrinterTest extends Test
   }
 
   public static boolean equals(String[][] a, String[][] b) {
+    if (a.length != b.length) {
+      return false;
+    }
     for (int i=0; i<a.length; i++) {
       String[] linea = a[i];
       String[] lineb = b[i];
+      if (linea.length != lineb.length) {
+        return false;
+      }
       for (int j=0; j<linea.length; j++) {
         String aval = linea[j];
         String bval = lineb[j];


Reply via email to