[CSV-111] CSVRecord.toMap() fails if row length shorter than header length.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1589281 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gary D. Gregory 2014-04-22 20:20:17 +00:00
parent a84668e0ea
commit 9f03b06a1e
2 changed files with 11 additions and 1 deletions

View File

@ -178,7 +178,10 @@ public final class CSVRecord implements Serializable, Iterable<String> {
*/ */
<M extends Map<String, String>> M putIn(final M map) { <M extends Map<String, String>> M putIn(final M map) {
for (final Entry<String, Integer> entry : mapping.entrySet()) { 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; return map;
} }

View File

@ -160,6 +160,13 @@ public class CSVRecordTest {
this.validateMap(map, true); 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) { private void validateMap(final Map<String, String> map, final boolean allowsNulls) {
assertTrue(map.containsKey("first")); assertTrue(map.containsKey("first"));
assertTrue(map.containsKey("second")); assertTrue(map.containsKey("second"));