From 939c7f949fa9d4e35f05c8c78e6cc09384ab1fd2 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Tue, 6 Jun 2017 18:19:25 -0700 Subject: [PATCH] Add failing test for #15, as well as tentative feature enum --- .../jackson/dataformat/csv/CsvParser.java | 13 ++++ .../csv/failing/SkipEmptyLines15Test.java | 67 +++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 csv/src/test/java/com/fasterxml/jackson/dataformat/csv/failing/SkipEmptyLines15Test.java diff --git a/csv/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvParser.java b/csv/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvParser.java index 7d08e46b..0ca67c90 100644 --- a/csv/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvParser.java +++ b/csv/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvParser.java @@ -62,11 +62,24 @@ public enum Feature * columns that appear after columns for which types are defined. When disabled, * an exception is thrown for such column values, but if enabled, they are * silently ignored. + *

+ * Feature is disabled by default. * * @since 2.7 */ IGNORE_TRAILING_UNMAPPABLE(false), + /** + * Feature that allows skipping input lines that are completely empty, instead + * of being decoded as lines of just a single column with empty String value (or, + * depending on binding, `null`). + *

+ * Feature is disabled by default. + * + * @since 2.9 + */ + SKIP_EMPTY_LINES(false), + /** * Feature that allows there to be a trailing single extraneous data * column that is empty. When this feature is disabled, any extraneous diff --git a/csv/src/test/java/com/fasterxml/jackson/dataformat/csv/failing/SkipEmptyLines15Test.java b/csv/src/test/java/com/fasterxml/jackson/dataformat/csv/failing/SkipEmptyLines15Test.java new file mode 100644 index 00000000..251ebb53 --- /dev/null +++ b/csv/src/test/java/com/fasterxml/jackson/dataformat/csv/failing/SkipEmptyLines15Test.java @@ -0,0 +1,67 @@ +package com.fasterxml.jackson.dataformat.csv.failing; + +import com.fasterxml.jackson.annotation.JsonPropertyOrder; +import com.fasterxml.jackson.dataformat.csv.*; + +public class SkipEmptyLines15Test extends ModuleTestBase +{ + @JsonPropertyOrder({ "age", "name", "cute" }) + protected static class Entry { + public int age; + public String name; + public boolean cute; + } + + /* + /********************************************************************** + /* Test methods, success + /********************************************************************** + */ + + // for [dataformats-text#15]: Allow skipping of empty lines + public void testSkipEmptyLinesFeature() throws Exception + { + final String CSV = "1,\"xyz\"\n\ntrue,\n"; + + CsvMapper mapper = mapperForCsv(); + mapper.enable(CsvParser.Feature.WRAP_AS_ARRAY); + + // First, verify default behavior: + + String[][] rows = mapper.readValue(CSV, String[][].class); + assertEquals(3, rows.length); + String[] row; + + row = rows[0]; + assertEquals(2, row.length); + assertEquals("1",row[0]); + assertEquals("xyz", row[1]); + + row = rows[1]; + assertEquals(1, row.length); + assertEquals("", row[0]); + + row = rows[2]; + assertEquals(2, row.length); + assertEquals("true", row[0]); + assertEquals("", row[1]); + + mapper.enable(CsvParser.Feature.SKIP_EMPTY_LINES); + + // when wrapped as an array, we'll get array of Lists: + rows = mapper.readerFor(String[][].class) + .with(CsvParser.Feature.WRAP_AS_ARRAY) + .readValue(CSV); + + assertEquals(2, rows.length); + row = rows[0]; + assertEquals(2, row.length); + assertEquals("1",row[0]); + assertEquals("xyz", row[1]); + + row = rows[1]; + assertEquals(2, row.length); + assertEquals("true", row[0]); + assertEquals("", row[1]); + } +}