Skip to content

Commit

Permalink
Fix #174 (or more precisely, refactor test that shows earlier fix)
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Apr 17, 2020
1 parent 47ca693 commit 997858d
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,106 @@

import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.List;
import java.util.Map;
import java.util.Random;

import com.fasterxml.jackson.annotation.JsonPropertyOrder;

import com.fasterxml.jackson.databind.MappingIterator;
import com.fasterxml.jackson.databind.ObjectReader;

import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvParser;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
import com.fasterxml.jackson.dataformat.csv.ModuleTestBase;

// [dataformats-text#191]
// [dataformats-text#174]
public class SkipEmptyLines191Test extends ModuleTestBase {

private static String COL_1 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
private static String COL_2 = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
// [dataformats-text#174]
@JsonPropertyOrder({ "timestamp", "random" })
static class Row174 {
private int timestamp;
private String random;

public int getTimestamp() {
return timestamp;
}

public void setTimestamp(int timestamp) {
this.timestamp = timestamp;
}

public String getRandom() {
return random;
}

public void setRandom(String random) {
this.random = random;
}

@Override
public String toString() {
return "Row{timestamp=" + timestamp + ", random='" + random + "'}";
}
}

/*
/**********************************************************************
/* Test methods
/**********************************************************************
*/

private final static CsvMapper MAPPER = new CsvMapper();

// [dataformats-text#174]
public void testEmptyLines174() throws Exception
{
final StringWriter sw = new StringWriter(50000);
int lineCount = 0;
final Random rnd = new Random();

while (lineCount < 4000) {
sw.append("\"" + System.currentTimeMillis()/1000 + "\",\"" + randomString(rnd) + "\"\n");
++lineCount;
}
final String doc = sw.toString();

ObjectReader objectReader = MAPPER
.enable(CsvParser.Feature.SKIP_EMPTY_LINES)
.readerFor(Row174.class)
.with(MAPPER.schemaFor(Row174.class));

MappingIterator<Row174> iterator = objectReader.readValues(doc);
Row174 data = null;
lineCount = 0;
while (iterator.hasNext()) {
++lineCount;
try {
data = iterator.next();
} catch (Exception e) {
fail("Failed on row #"+lineCount+", previous row: "+data);
}
}
iterator.close();
}

private String randomString(Random rnd) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 10; ++i) {
sb.append((char) ('A' + (rnd.nextInt() & 0xF)));
}
return sb.toString();
}

// [dataformats-text#191]: IndexArrayOutOfBounds at 4000
public void testBigCsvFile() throws Exception
{
final String COL_1 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
final String COL_2 = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
CsvSchema schema = CsvSchema
.emptySchema()
.withHeader()
Expand All @@ -26,7 +110,7 @@ public void testBigCsvFile() throws Exception
.withComments();

try (Reader r = new StringReader(_generate4kDoc())) {
List<Map<String, String>> result = new CsvMapper()
List<Map<String, String>> result = MAPPER
.readerFor(Map.class)
.with(schema)
.<Map<String, String>>readValues(r)
Expand Down

This file was deleted.

8 changes: 6 additions & 2 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Fabrice Delhoste (spifd@github)

Thomas Hauk (thauk-copperleaf@github)

* Contibuted #84 (yaml): Add option to allow use of platform-linefeed
* Contributed #84 (yaml): Add option to allow use of platform-linefeed
(`YAMLGenerator.Feature.USE_PLATFORM_LINE_BREAKS`)
(2.9.6)

Expand Down Expand Up @@ -103,7 +103,6 @@ Jochen Schalanda (joschi@github)
* Reported #187: Update to SnakeYAML to 1.26 (from 1.24) to address CVE-2017-18640
(2.10.4)


Tyler Carpenter-Rivers (tyler2cr@github)
#7: Add `CsvParser.Feature.EMPTY_STRING_AS_NULL` to allow coercing empty Strings
into `null` values
Expand All @@ -112,3 +111,8 @@ Tyler Carpenter-Rivers (tyler2cr@github)
* Reported, constributed fix for #180: (yaml) YAMLGenerator serializes string with special
chars unquoted when using `MINIMIZE_QUOTES` mode
(2.11.0)

Yohann BONILLO (ybonillo@github)

* Reported #174: (csv) `CsvParser.Feature.SKIP_EMPTY_LINES` results in a mapping error
(2.11.0)
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Modules:
(contributed by Tyler C-R)
#115: (csv) JsonProperty index is not honored by CsvSchema builder
-- actually fixed by [databind#2555]
#174: (csv) `CsvParser.Feature.SKIP_EMPTY_LINES` results in a mapping error
(reported by Yohann B)
#180: (yaml) YAMLGenerator serializes string with special chars unquoted when
using `MINIMIZE_QUOTES` mode
(reported, fix contributed by Timo R)
Expand Down

0 comments on commit 997858d

Please sign in to comment.