From 3b10c8f8ac64b75c573e0352dbb3d7ff9279d121 Mon Sep 17 00:00:00 2001 From: Benedikt Ritter Date: Sat, 3 Oct 2015 10:46:49 +0000 Subject: [PATCH] CSV-153: CSVPrinter doesn't skip creation of header record if skipHeaderRecord is set to true. Thanks to Wren. This also fixes #8 from github. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1706542 13f79535-47bb-0310-9956-ffa450edef68 --- src/changes/changes.xml | 2 +- .../org/apache/commons/csv/CSVPrinter.java | 2 +- .../apache/commons/csv/CSVPrinterTest.java | 34 +++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) 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();