[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
This commit is contained in:
Gary D. Gregory 2013-03-28 14:23:56 +00:00
parent 08f02a086c
commit 8f436552ae
2 changed files with 14 additions and 0 deletions

View File

@ -82,6 +82,17 @@ public class CSVRecord implements Serializable, Iterable<String> {
return index != null ? values[index.intValue()] : null;
}
/**
* Returns true if this record is consistent, false if not. Currently, the only check is matching the record size to
* the header size. Some programs can export files that fails this test but still produce parsable files.
*
* @return true of this record is valid, false if not
* @see CSVParserTest#org.apache.commons.csv.CSVParserTest.testMappedButNotSetAsOutlook2007ContactExport()
*/
public boolean isConsistent() {
return mapping == null ? true : mapping.size() == values.length;
}
/**
* Checks whether a given column is mapped.
*

View File

@ -550,6 +550,7 @@ public class CSVParserTest {
assertEquals("a", record.get("A"));
assertEquals("b", record.get("B"));
assertEquals("c", record.get("C"));
assertTrue(record.isConsistent());
// 1st record
record = records.next();
@ -561,6 +562,7 @@ public class CSVParserTest {
assertFalse(record.isSet("C"));
assertEquals("1", record.get("A"));
assertEquals("2", record.get("B"));
assertFalse(record.isConsistent());
// 2nd record
record = records.next();
@ -573,6 +575,7 @@ public class CSVParserTest {
assertEquals("x", record.get("A"));
assertEquals("y", record.get("B"));
assertEquals("z", record.get("C"));
assertTrue(record.isConsistent());
assertFalse(records.hasNext());
}