Skip to content

Commit 8f43655

Browse files
committed
[CSV-96] CSVRecord does not verify that the length of the header mapping matches the number of values.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1462110 13f79535-47bb-0310-9956-ffa450edef68
1 parent 08f02a0 commit 8f43655

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

src/main/java/org/apache/commons/csv/CSVRecord.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@ public String get(final String name) {
8282
return index != null ? values[index.intValue()] : null;
8383
}
8484

85+
/**
86+
* Returns true if this record is consistent, false if not. Currently, the only check is matching the record size to
87+
* the header size. Some programs can export files that fails this test but still produce parsable files.
88+
*
89+
* @return true of this record is valid, false if not
90+
* @see CSVParserTest#org.apache.commons.csv.CSVParserTest.testMappedButNotSetAsOutlook2007ContactExport()
91+
*/
92+
public boolean isConsistent() {
93+
return mapping == null ? true : mapping.size() == values.length;
94+
}
95+
8596
/**
8697
* Checks whether a given column is mapped.
8798
*

src/test/java/org/apache/commons/csv/CSVParserTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,7 @@ public void testMappedButNotSetAsOutlook2007ContactExport() throws Exception {
550550
assertEquals("a", record.get("A"));
551551
assertEquals("b", record.get("B"));
552552
assertEquals("c", record.get("C"));
553+
assertTrue(record.isConsistent());
553554

554555
// 1st record
555556
record = records.next();
@@ -561,6 +562,7 @@ record = records.next();
561562
assertFalse(record.isSet("C"));
562563
assertEquals("1", record.get("A"));
563564
assertEquals("2", record.get("B"));
565+
assertFalse(record.isConsistent());
564566

565567
// 2nd record
566568
record = records.next();
@@ -573,6 +575,7 @@ record = records.next();
573575
assertEquals("x", record.get("A"));
574576
assertEquals("y", record.get("B"));
575577
assertEquals("z", record.get("C"));
578+
assertTrue(record.isConsistent());
576579

577580
assertFalse(records.hasNext());
578581
}

0 commit comments

Comments
 (0)