From feda1a2fde85b3e2a8669bd6027cff3f92f99166 Mon Sep 17 00:00:00 2001 From: Peter Ansell Date: Mon, 22 Aug 2016 11:25:46 +1000 Subject: [PATCH 1/3] issue-128 : Add regression test for headers writing with no data Signed-off-by: Peter Ansell --- .../dataformat/csv/ser/HeaderWriteTest.java | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/test/java/com/fasterxml/jackson/dataformat/csv/ser/HeaderWriteTest.java diff --git a/src/test/java/com/fasterxml/jackson/dataformat/csv/ser/HeaderWriteTest.java b/src/test/java/com/fasterxml/jackson/dataformat/csv/ser/HeaderWriteTest.java new file mode 100644 index 0000000..87862fc --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/dataformat/csv/ser/HeaderWriteTest.java @@ -0,0 +1,61 @@ +package com.fasterxml.jackson.dataformat.csv.ser; + +import java.io.IOException; +import java.io.StringWriter; +import java.util.Arrays; +import java.util.List; + +import com.fasterxml.jackson.databind.SequenceWriter; +import com.fasterxml.jackson.dataformat.csv.*; + +// Tests for verifying that headers are emitted +public class HeaderWriteTest extends ModuleTestBase +{ + /* + /********************************************************************** + /* Test methods + /********************************************************************** + */ + + private final CsvMapper MAPPER = mapperForCsv(); + + public void testNoLines() throws Exception + { + List headers = Arrays.asList("TestHeader1", "TestHeader2"); + List> dataSource = Arrays.asList(); + String result = runTest(headers, dataSource); + + assertEquals("Headers should have been written even with no other data", "TestHeader1,TestHeader2\n", result); + } + + public void testOneLine() throws Exception + { + List headers = Arrays.asList("TestHeader1", "TestHeader2"); + List> dataSource = Arrays.asList(Arrays.asList("TestValue1", "TestValue2")); + String result = runTest(headers, dataSource); + + assertEquals("Headers should have been written before line", "TestHeader1,TestHeader2\nTestValue1,TestValue2\n", result); + } + + private String runTest(List headers, List> dataSource) throws IOException + { + StringWriter writer = new StringWriter(); + + CsvSchema.Builder builder = CsvSchema.builder(); + for (String nextHeader : headers) { + builder = builder.addColumn(nextHeader); + } + + CsvSchema schema = builder.setUseHeader(true).build(); + try (SequenceWriter csvWriter = MAPPER.writerWithDefaultPrettyPrinter() + .with(schema) + .forType(List.class) + .writeValues(writer);) { + for(List nextRow : dataSource) { + csvWriter.write(nextRow); + } + } + + return writer.toString(); + } +} From 6637c033cd762e1a1cbc0ad8bfeb086f7fa4f4e8 Mon Sep 17 00:00:00 2001 From: Peter Ansell Date: Mon, 22 Aug 2016 11:30:10 +1000 Subject: [PATCH 2/3] issue-128 : Fix #128 by writing header during close if necessary Signed-off-by: Peter Ansell --- .../com/fasterxml/jackson/dataformat/csv/CsvGenerator.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvGenerator.java b/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvGenerator.java index e86deba..30e33a3 100644 --- a/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvGenerator.java +++ b/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvGenerator.java @@ -428,6 +428,12 @@ public void close() throws IOException // Let's mark row as closed, if we had any... finishRow(); + + // Write the header if necessary, occurs when no rows written + if (_handleFirstLine) { + _handleFirstLine(); + } + _writer.close(_ioContext.isResourceManaged() || isEnabled(JsonGenerator.Feature.AUTO_CLOSE_TARGET)); } From fa79ad4c5b41e328f3bfc9627a91087167991e65 Mon Sep 17 00:00:00 2001 From: Peter Ansell Date: Mon, 22 Aug 2016 11:35:50 +1000 Subject: [PATCH 3/3] issue-128 : Reformat tabs to spaces Signed-off-by: Peter Ansell --- .../dataformat/csv/ser/HeaderWriteTest.java | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/test/java/com/fasterxml/jackson/dataformat/csv/ser/HeaderWriteTest.java b/src/test/java/com/fasterxml/jackson/dataformat/csv/ser/HeaderWriteTest.java index 87862fc..845811c 100644 --- a/src/test/java/com/fasterxml/jackson/dataformat/csv/ser/HeaderWriteTest.java +++ b/src/test/java/com/fasterxml/jackson/dataformat/csv/ser/HeaderWriteTest.java @@ -22,19 +22,19 @@ public class HeaderWriteTest extends ModuleTestBase public void testNoLines() throws Exception { List headers = Arrays.asList("TestHeader1", "TestHeader2"); - List> dataSource = Arrays.asList(); - String result = runTest(headers, dataSource); - - assertEquals("Headers should have been written even with no other data", "TestHeader1,TestHeader2\n", result); + List> dataSource = Arrays.asList(); + String result = runTest(headers, dataSource); + + assertEquals("Headers should have been written even with no other data", "TestHeader1,TestHeader2\n", result); } public void testOneLine() throws Exception { List headers = Arrays.asList("TestHeader1", "TestHeader2"); - List> dataSource = Arrays.asList(Arrays.asList("TestValue1", "TestValue2")); - String result = runTest(headers, dataSource); - - assertEquals("Headers should have been written before line", "TestHeader1,TestHeader2\nTestValue1,TestValue2\n", result); + List> dataSource = Arrays.asList(Arrays.asList("TestValue1", "TestValue2")); + String result = runTest(headers, dataSource); + + assertEquals("Headers should have been written before line", "TestHeader1,TestHeader2\nTestValue1,TestValue2\n", result); } private String runTest(List headers, List> dataSource) throws IOException @@ -48,14 +48,14 @@ private String runTest(List headers, List> dataSource) thro CsvSchema schema = builder.setUseHeader(true).build(); try (SequenceWriter csvWriter = MAPPER.writerWithDefaultPrettyPrinter() - .with(schema) - .forType(List.class) - .writeValues(writer);) { + .with(schema) + .forType(List.class) + .writeValues(writer);) { for(List nextRow : dataSource) { - csvWriter.write(nextRow); + csvWriter.write(nextRow); } } - return writer.toString(); + return writer.toString(); } }