Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/main/java/com/j256/simplecsv/processor/CsvProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,8 @@ private String[] processHeader(String line, ParseError parseError, int lineNumbe
+ localParseError + "): " + lineInfo.line, lineInfo.linePos);
}
}
if (sb.length() > 0) {
if (!atEnd || (lineInfo.line.length() > 0
&& lineInfo.line.charAt(lineInfo.line.length() - 1) == columnSeparator)) {
headerColumns.add(sb.toString());
}
if (atEnd) {
Expand Down
54 changes: 54 additions & 0 deletions src/test/java/com/j256/simplecsv/processor/EmptyHeaderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.j256.simplecsv.processor;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;

import java.io.BufferedReader;
import java.io.StringReader;

import org.junit.Test;
import com.j256.simplecsv.common.CsvColumn;

public class EmptyHeaderTest {
@Test
public void emptyHeaderPreservesColumnPositions() throws Exception {
CsvProcessor<Row> processor = new CsvProcessor<Row>(Row.class).withIgnoreUnknownColumns(true);
BufferedReader input = new BufferedReader(new StringReader("first,,second\nA,ignored,B\n"));
assertArrayEquals(new String[] {"first", "", "second"}, processor.readHeader(input, null));
Row row = processor.readRow(input, null);
assertEquals("A", row.first);
assertEquals("B", row.second);
}

@Test
public void quotedEmptyHeaderPreservesColumnPositions() throws Exception {
CsvProcessor<Row> processor = new CsvProcessor<Row>(Row.class).withIgnoreUnknownColumns(true);
BufferedReader input = new BufferedReader(new StringReader("first,\"\",second\nA,ignored,B\n"));
processor.readHeader(input, null);
assertEquals("B", processor.readRow(input, null).second);
}

@Test
public void leadingAndTrailingEmptyHeadersAreRetained() throws Exception {
CsvProcessor<Row> processor = new CsvProcessor<Row>(Row.class).withIgnoreUnknownColumns(true);
BufferedReader input = new BufferedReader(new StringReader(",first,second,\nignored,A,B,ignored\n"));
assertArrayEquals(new String[] {"", "first", "second", ""}, processor.readHeader(input, null));
Row row = processor.readRow(input, null);
assertEquals("A", row.first);
assertEquals("B", row.second);
}

@Test
public void ordinaryHeaderDoesNotGainAnExtraColumn() throws Exception {
CsvProcessor<Row> processor = new CsvProcessor<Row>(Row.class);
assertArrayEquals(new String[] {"first", "second"}, processor.readHeader(
new BufferedReader(new StringReader("first,second\n")), null));
}

public static class Row {
@CsvColumn
public String first;
@CsvColumn
public String second;
}
}