Skip to content
This repository has been archived by the owner on Jan 22, 2019. It is now read-only.

Commit

Permalink
Add more tests for #72; fix an accidental exclusion of empty String f…
Browse files Browse the repository at this point in the history
…or deserialization
  • Loading branch information
cowtowncoder committed Jun 29, 2015
1 parent a4c1046 commit b249d53
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ public void setSchema(FormatSchema schema)
if (schema instanceof CsvSchema) {
_schema = (CsvSchema) schema;
String str = _schema.getNullValueString();
_nullValue = str.isEmpty() ? null : str;
_nullValue = str;
} else if (schema == null) {
schema = EMPTY_SCHEMA;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
* </li>
* <li>nullValue (String) [default: "" (empty String)]: When asked to write Java `null`,
* this String value will be used instead.<br />
* NOTE: NOT used for reading at this point (this may change in future)
* With 2.6, value will also be recognized during value reads.
* </li>
* </ul>
*<p>
Expand Down Expand Up @@ -545,7 +545,7 @@ public Builder setLineSeparator(char lf) {
}

public Builder setNullValue(String nvl) {
return setNullValue(nvl.toCharArray());
return setNullValue((nvl == null) ? null : nvl.toCharArray());
}

public Builder setNullValue(char[] nvl) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.fasterxml.jackson.dataformat.csv.deser;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.dataformat.csv.*;

public class NullReadTest extends ModuleTestBase
{
final CsvMapper MAPPER = mapperForCsv();

// For [dataformat-csv#72]: recognize "null value" for reading too
public void testReadNullValue72() throws Exception
{
CsvSchema schema = CsvSchema.builder()
.setNullValue("n/a")
.addColumn("id")
.addColumn("desc")
.build();

// start by writing, first
String csv = MAPPER.writer(schema).writeValueAsString(new IdDesc("id", null));
// MUST use doubling for quotes!
assertEquals("id,n/a\n", csv);

// but read back

ObjectReader r = MAPPER.readerFor(IdDesc.class)
.with(schema);

IdDesc result = r.readValue(csv);
assertNotNull(result);
assertEquals("id", result.id);
assertNull(result.desc);

// also try the other combination
result = r.readValue("n/a,Whatevs\n");
assertNotNull(result);
assertNull(result.id);
assertEquals("Whatevs", result.desc);
}

public void testReadNullValueFromEmptyString() throws Exception
{
// first: empty String should work as default
CsvSchema schemaWithDefault = CsvSchema.builder()
.addColumn("id")
.addColumn("desc")
.build();

// start by writing, first
String csv = MAPPER.writer(schemaWithDefault).writeValueAsString(new IdDesc("id", null));
assertEquals("id,\n", csv);

// but read back

ObjectReader r = MAPPER.readerFor(IdDesc.class).with(schemaWithDefault);

IdDesc result = r.readValue(csv);
assertNotNull(result);
assertEquals("id", result.id);
assertNull(result.desc);

// also try the other combination
result = r.readValue(",Whatevs\n");
assertNotNull(result);
assertNull(result.id);
assertEquals("Whatevs", result.desc);

// And then with explicit Empty String
CsvSchema schemaWithExplicitEmpty = CsvSchema.builder()
.setNullValue("")
.addColumn("id")
.addColumn("desc")
.build();

csv = MAPPER.writer(schemaWithExplicitEmpty).writeValueAsString(new IdDesc("id", null));
assertEquals("id,\n", csv);
r = MAPPER.readerFor(IdDesc.class).with(schemaWithExplicitEmpty);
result = r.readValue(csv);
assertNotNull(result);
assertEquals("id", result.id);
assertNull(result.desc);

// and finally with explicit `null`
CsvSchema schemaWithExplicitNull = CsvSchema.builder()
.setNullValue((String) null)
.addColumn("id")
.addColumn("desc")
.build();

csv = MAPPER.writer(schemaWithExplicitNull).writeValueAsString(new IdDesc("id", null));
assertEquals("id,\n", csv);
r = MAPPER.readerFor(IdDesc.class).with(schemaWithExplicitNull);
result = r.readValue(csv);
assertNotNull(result);
assertEquals("id", result.id);
assertNull(result.desc);
}
}

2 comments on commit b249d53

@dharaburda
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cowtowncoder Thanks for fixing this so quickly -- there are two tests that are failing, easy fix just change empty strings to null:

Failed tests: 
  ParserTrimSpacesTest.testTrimming:78 expected:<> but was:<null>
  BasicParserTest.testSimpleAsMaps:115 expected:<> but was:<null>

Also, any idea of we can expect a 2.6.0 release? And rc4 too -- is there a date associated with it?

@cowtowncoder
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dharaburda d'oh. I thought I had run the test suite. Shows me to check in things in a hurry. :)

As to 2.6.0 -- was hoping by end of June, but not happening now. Not 100% sure if there will be rc4 before 2.6.0 final, but regardless next release will happen at earlier on week of july 6th (I'll be on vacation until then). But at the same time, planning to get the release out by mid-July now.

Please sign in to comment.