Skip to content

Commit 7bf2114

Browse files
committed
Increase test coverage
1 parent df0dd4b commit 7bf2114

2 files changed

Lines changed: 37 additions & 16 deletions

File tree

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

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ private void put(final String name, final Integer index) {
304304
}
305305

306306
public boolean isEmpty() {
307-
return this.headerMap == null || this.headerMap.isEmpty();
307+
return this.headerMap.isEmpty();
308308
}
309309
}
310310

@@ -619,7 +619,7 @@ private Headers createHeaders() throws IOException {
619619
final Headers headers = new Headers(format);
620620
final String[] formatHeader = format.getHeader();
621621
if (formatHeader != null) {
622-
final String[] headerRecord = getHeaderRecord();
622+
final String[] headerRecord = getHeaderRecord(formatHeader);
623623
// build the name to index mappings
624624
if (headerRecord != null) {
625625
// Track an occurrence of a null, empty or blank header.
@@ -657,26 +657,23 @@ private Headers createHeaders() throws IOException {
657657
* If the format does not contain header record, the header record from the first line of the file
658658
* @throws IOException if there is a problem reading the header or skipping the first record
659659
*/
660-
private String[] getHeaderRecord() throws IOException {
660+
private String[] getHeaderRecord(final String[] formatHeader) throws IOException {
661661
String[] headerRecord = null;
662-
final String[] formatHeader = format.getHeader();
663-
if (formatHeader != null) {
664-
if (formatHeader.length == 0) {
665-
// read the header from the first line of the file
662+
if (formatHeader.length == 0) {
663+
// read the header from the first line of the file
664+
final CSVRecord nextRecord = nextRecord();
665+
if (nextRecord != null) {
666+
headerRecord = nextRecord.values();
667+
headerComment = nextRecord.getComment();
668+
}
669+
} else {
670+
if (format.getSkipHeaderRecord()) {
666671
final CSVRecord nextRecord = nextRecord();
667672
if (nextRecord != null) {
668-
headerRecord = nextRecord.values();
669673
headerComment = nextRecord.getComment();
670674
}
671-
} else {
672-
if (format.getSkipHeaderRecord()) {
673-
final CSVRecord nextRecord = nextRecord();
674-
if (nextRecord != null) {
675-
headerComment = nextRecord.getComment();
676-
}
677-
}
678-
headerRecord = formatHeader;
679675
}
676+
headerRecord = formatHeader;
680677
}
681678
return headerRecord;
682679
}

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,30 @@ void testGetHeaderMap() throws Exception {
729729
}
730730
}
731731

732+
@Test
733+
void testGetHeaderMapWithIgnoreHeaderCase() throws Exception {
734+
try (CSVParser parser = CSVParser.parse("a,b,c,A,B,C\n1,2,3,1,2,3\nx,y,z,x,y,z", CSVFormat.DEFAULT.withHeader("a", "b", "c").withIgnoreHeaderCase())) {
735+
final Map<String, Integer> headerMap = parser.getHeaderMap();
736+
final Iterator<String> columnNames = headerMap.keySet().iterator();
737+
// Headers are iterated in column order.
738+
assertEquals("a", columnNames.next());
739+
assertEquals("b", columnNames.next());
740+
assertEquals("c", columnNames.next());
741+
final Iterator<CSVRecord> records = parser.iterator();
742+
743+
// Parse to make sure getHeaderMap did not have a side-effect.
744+
for (int i = 0; i < 3; i++) {
745+
assertTrue(records.hasNext());
746+
final CSVRecord record = records.next();
747+
assertEquals(record.get(0), record.get("a"));
748+
assertEquals(record.get(1), record.get("b"));
749+
assertEquals(record.get(2), record.get("c"));
750+
}
751+
752+
assertFalse(records.hasNext());
753+
}
754+
}
755+
732756
@Test
733757
void testGetHeaderNames() throws IOException {
734758
try (CSVParser parser = CSVParser.parse("a,b,c\n1,2,3\nx,y,z", CSVFormat.DEFAULT.withHeader("A", "B", "C"))) {

0 commit comments

Comments
 (0)