diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 80522404..30ccec10 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -39,7 +39,7 @@
- ???
+ CSVPrinter doesn't skip creation of header record if skipHeaderRecord is set to true
CSVFormat.with* methods clear the header comments
diff --git a/src/main/java/org/apache/commons/csv/CSVPrinter.java b/src/main/java/org/apache/commons/csv/CSVPrinter.java
index 99952975..ac4234bb 100644
--- a/src/main/java/org/apache/commons/csv/CSVPrinter.java
+++ b/src/main/java/org/apache/commons/csv/CSVPrinter.java
@@ -73,7 +73,7 @@ public final class CSVPrinter implements Flushable, Closeable {
}
}
}
- if (format.getHeader() != null) {
+ if (format.getHeader() != null && !format.getSkipHeaderRecord()) {
this.printRecord((Object[]) format.getHeader());
}
}
diff --git a/src/test/java/org/apache/commons/csv/CSVPrinterTest.java b/src/test/java/org/apache/commons/csv/CSVPrinterTest.java
index 3265b848..cf49a6d6 100644
--- a/src/test/java/org/apache/commons/csv/CSVPrinterTest.java
+++ b/src/test/java/org/apache/commons/csv/CSVPrinterTest.java
@@ -557,6 +557,40 @@ public class CSVPrinterTest {
printer.close();
}
+ @Test
+ public void testHeaderNotSet() throws IOException {
+ final StringWriter sw = new StringWriter();
+ final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuote(null));
+ printer.printRecord("a", "b", "c");
+ printer.printRecord("x", "y", "z");
+ assertEquals("a,b,c\r\nx,y,z\r\n", sw.toString());
+ printer.close();
+ }
+
+ @Test
+ public void testSkipHeaderRecordTrue() throws IOException {
+ // functionally identical to testHeaderNotSet, used to test CSV-153
+ final StringWriter sw = new StringWriter();
+ final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuote(null)
+ .withHeader("C1", "C2", "C3").withSkipHeaderRecord(true));
+ printer.printRecord("a", "b", "c");
+ printer.printRecord("x", "y", "z");
+ assertEquals("a,b,c\r\nx,y,z\r\n", sw.toString());
+ printer.close();
+ }
+
+ @Test
+ public void testSkipHeaderRecordFalse() throws IOException {
+ // functionally identical to testHeader, used to test CSV-153
+ final StringWriter sw = new StringWriter();
+ final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuote(null)
+ .withHeader("C1", "C2", "C3").withSkipHeaderRecord(false));
+ printer.printRecord("a", "b", "c");
+ printer.printRecord("x", "y", "z");
+ assertEquals("C1,C2,C3\r\na,b,c\r\nx,y,z\r\n", sw.toString());
+ printer.close();
+ }
+
@Test
public void testHeaderCommentExcel() throws IOException {
final StringWriter sw = new StringWriter();