Skip to content

Commit 1533fac

Browse files
committed
When withHeader is set to any non-null value, the first record is the first <em>data</em> record, not the header record.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1508612 13f79535-47bb-0310-9956-ffa450edef68
1 parent 390800f commit 1533fac

3 files changed

Lines changed: 54 additions & 25 deletions

File tree

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -614,22 +614,24 @@ public CSVFormat withEscape(final Character escape) {
614614
}
615615

616616
/**
617-
* Sets the header of the format. The header can either be parsed automatically from the
618-
* input file with:
619-
*
617+
* Sets the header of the format. The header can either be parsed automatically from the input file with:
618+
*
620619
* <pre>
621620
* CSVFormat format = aformat.withHeader();
622621
* </pre>
623-
*
622+
*
624623
* or specified manually with:
625-
*
624+
*
626625
* <pre>
627626
* CSVFormat format = aformat.withHeader(&quot;name&quot;, &quot;email&quot;, &quot;phone&quot;);
628627
* </pre>
629-
*
628+
*
629+
* When this option is is set to any non-null value, the first record is the first <em>data</em> record, not the
630+
* header record.
631+
*
630632
* @param header
631633
* the header, <tt>null</tt> if disabled, empty if parsed automatically, user specified otherwise.
632-
*
634+
*
633635
* @return A new CSVFormat that is equal to this but with the specified header
634636
*/
635637
public CSVFormat withHeader(final String... header) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,12 +323,12 @@ private Map<String, Integer> initializeHeader() throws IOException {
323323
Map<String, Integer> hdrMap = null;
324324
String[] formatHeader = this.format.getHeader();
325325
if (formatHeader != null) {
326+
final CSVRecord record = this.nextRecord();
326327
hdrMap = new LinkedHashMap<String, Integer>();
327328

328329
String[] header = null;
329330
if (formatHeader.length == 0) {
330331
// read the header from the first line of the file
331-
final CSVRecord record = this.nextRecord();
332332
if (record != null) {
333333
header = record.values();
334334
}

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

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,26 @@ public void testHeader() throws Exception {
506506
assertFalse(records.hasNext());
507507
}
508508

509+
@Test
510+
public void testSkipSetHeader() throws Exception {
511+
final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
512+
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("a", "b", "c").parse(in).iterator();
513+
final CSVRecord record = records.next();
514+
assertEquals("1", record.get("a"));
515+
assertEquals("2", record.get("b"));
516+
assertEquals("3", record.get("c"));
517+
}
518+
519+
@Test
520+
public void testSkipAutoHeader() throws Exception {
521+
final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
522+
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader().parse(in).iterator();
523+
final CSVRecord record = records.next();
524+
assertEquals("1", record.get("a"));
525+
assertEquals("2", record.get("b"));
526+
assertEquals("3", record.get("c"));
527+
}
528+
509529
@Test
510530
public void testHeaderComment() throws Exception {
511531
final Reader in = new StringReader("# comment\na,b,c\n1,2,3\nx,y,z");
@@ -529,7 +549,7 @@ public void testProvidedHeader() throws Exception {
529549

530550
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("A", "B", "C").parse(in).iterator();
531551

532-
for (int i = 0; i < 3; i++) {
552+
for (int i = 0; i < 2; i++) {
533553
assertTrue(records.hasNext());
534554
final CSVRecord record = records.next();
535555
assertTrue(record.isMapped("A"));
@@ -544,25 +564,32 @@ public void testProvidedHeader() throws Exception {
544564
assertFalse(records.hasNext());
545565
}
546566

567+
@Test
568+
public void testProvidedHeaderAuto() throws Exception {
569+
final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
570+
571+
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader().parse(in).iterator();
572+
573+
for (int i = 0; i < 2; i++) {
574+
assertTrue(records.hasNext());
575+
final CSVRecord record = records.next();
576+
assertTrue(record.isMapped("a"));
577+
assertTrue(record.isMapped("b"));
578+
assertTrue(record.isMapped("c"));
579+
assertFalse(record.isMapped("NOT MAPPED"));
580+
assertEquals(record.get(0), record.get("a"));
581+
assertEquals(record.get(1), record.get("b"));
582+
assertEquals(record.get(2), record.get("c"));
583+
}
584+
585+
assertFalse(records.hasNext());
586+
}
587+
547588
@Test
548589
public void testMappedButNotSetAsOutlook2007ContactExport() throws Exception {
549590
final Reader in = new StringReader("a,b,c\n1,2\nx,y,z");
550-
551591
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("A", "B", "C").parse(in).iterator();
552-
553-
// header record
554-
assertTrue(records.hasNext());
555-
CSVRecord record = records.next();
556-
assertTrue(record.isMapped("A"));
557-
assertTrue(record.isMapped("B"));
558-
assertTrue(record.isMapped("C"));
559-
assertTrue(record.isSet("A"));
560-
assertTrue(record.isSet("B"));
561-
assertTrue(record.isSet("C"));
562-
assertEquals("a", record.get("A"));
563-
assertEquals("b", record.get("B"));
564-
assertEquals("c", record.get("C"));
565-
assertTrue(record.isConsistent());
592+
CSVRecord record;
566593

567594
// 1st record
568595
record = records.next();
@@ -604,7 +631,7 @@ public void testGetHeaderMap() throws Exception {
604631
final Iterator<CSVRecord> records = parser.iterator();
605632

606633
// Parse to make sure getHeaderMap did not have a side-effect.
607-
for (int i = 0; i < 3; i++) {
634+
for (int i = 0; i < 2; i++) {
608635
assertTrue(records.hasNext());
609636
final CSVRecord record = records.next();
610637
assertEquals(record.get(0), record.get("A"));

0 commit comments

Comments
 (0)