commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r1589281 - in /commons/proper/csv/trunk/src: main/java/org/apache/commons/csv/CSVRecord.java test/java/org/apache/commons/csv/CSVRecordTest.java
Date Tue, 22 Apr 2014 20:20:18 GMT
Author: ggregory
Date: Tue Apr 22 20:20:17 2014
New Revision: 1589281

URL: http://svn.apache.org/r1589281
Log:
[CSV-111] CSVRecord.toMap() fails if row length shorter than header length.

Modified:
    commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVRecord.java
    commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVRecordTest.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=1589281&r1=1589280&r2=1589281&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 Tue Apr 22
20:20:17 2014
@@ -178,7 +178,10 @@ public final class CSVRecord implements 
      */
     <M extends Map<String, String>> M putIn(final M map) {
         for (final Entry<String, Integer> entry : mapping.entrySet()) {
-            map.put(entry.getKey(), values[entry.getValue().intValue()]);
+            final int col = entry.getValue().intValue();
+            if (col < values.length) {
+                map.put(entry.getKey(), values[col]);
+            }
         }
         return map;
     }

Modified: commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVRecordTest.java
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVRecordTest.java?rev=1589281&r1=1589280&r2=1589281&view=diff
==============================================================================
--- commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVRecordTest.java (original)
+++ commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVRecordTest.java Tue Apr
22 20:20:17 2014
@@ -160,6 +160,13 @@ public class CSVRecordTest {
         this.validateMap(map, true);
     }
 
+    @Test
+    public void testToMapWithShortRecord() throws Exception {
+       final CSVParser parser =  CSVParser.parse("a,b", CSVFormat.DEFAULT.withHeader("A",
"B", "C"));
+       final CSVRecord shortRec = parser.iterator().next();
+       shortRec.toMap();
+    }
+    
     private void validateMap(final Map<String, String> map, final boolean allowsNulls)
{
         assertTrue(map.containsKey("first"));
         assertTrue(map.containsKey("second"));



Mime
View raw message