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
This commit is contained in:
Benedikt Ritter 2015-10-03 10:46:49 +00:00
parent 1d5dfcb8f6
commit 3b10c8f8ac
3 changed files with 36 additions and 2 deletions

View File

@ -39,7 +39,7 @@
</properties> </properties>
<body> <body>
<release version="1.3" date="2015-MM-DD" description="Feature and bug fix release"> <release version="1.3" date="2015-MM-DD" description="Feature and bug fix release">
<action issue="CSV-???" type="???" dev="???" due-to="???">???</action> <action issue="CSV-153" type="update" dev="britter" due-to="Wren">CSVPrinter doesn't skip creation of header record if skipHeaderRecord is set to true</action>
</release> </release>
<release version="1.2" date="2015-08-24" description="Feature and bug fix release"> <release version="1.2" date="2015-08-24" description="Feature and bug fix release">
<action issue="CSV-145" type="fix" dev="ggregory" due-to="Frank Ulbricht">CSVFormat.with* methods clear the header comments</action> <action issue="CSV-145" type="fix" dev="ggregory" due-to="Frank Ulbricht">CSVFormat.with* methods clear the header comments</action>

View File

@ -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()); this.printRecord((Object[]) format.getHeader());
} }
} }

View File

@ -557,6 +557,40 @@ public class CSVPrinterTest {
printer.close(); 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 @Test
public void testHeaderCommentExcel() throws IOException { public void testHeaderCommentExcel() throws IOException {
final StringWriter sw = new StringWriter(); final StringWriter sw = new StringWriter();