forked from FasterXML/jackson-dataformats-text
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(csv) Support coercion of empty strings to null objects
- Loading branch information
Tyler Carpenter-Rivers
committed
Oct 1, 2019
1 parent
770ef84
commit 1532adb
Showing
2 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
csv/src/test/java/com/fasterxml/jackson/dataformat/csv/deser/EmptyStringAsNullTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.fasterxml.jackson.dataformat.csv.deser; | ||
|
||
import com.fasterxml.jackson.annotation.JsonPropertyOrder; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectReader; | ||
import com.fasterxml.jackson.dataformat.csv.CsvMapper; | ||
import com.fasterxml.jackson.dataformat.csv.CsvParser; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* Test for {@link CsvParser.Feature#EMPTY_STRING_AS_NULL} | ||
*/ | ||
public class EmptyStringAsNullTest { | ||
|
||
|
||
@JsonPropertyOrder({"firstName", "middleName", "lastName"}) | ||
static class TestUser { | ||
public String firstName, middleName, lastName; | ||
} | ||
|
||
@Test | ||
public void givenFeatureDisabledByDefault_whenColumnIsEmptyString_thenParseAsEmptyString() throws IOException { | ||
// setup test data | ||
TestUser expectedTestUser = new TestUser(); | ||
expectedTestUser.firstName = "Grace"; | ||
expectedTestUser.middleName = ""; | ||
expectedTestUser.lastName = "Hopper"; | ||
CsvMapper csvMapper = CsvMapper.builder().build(); | ||
ObjectReader objectReader = csvMapper.readerFor(TestUser.class).with(csvMapper.schemaFor(TestUser.class)); | ||
String csv = "Grace,,Hopper"; | ||
|
||
// execute | ||
TestUser actualTestUser = objectReader.readValue(csv); | ||
|
||
// test | ||
assertNotNull(actualTestUser); | ||
assertEquals(expectedTestUser.firstName, actualTestUser.firstName); | ||
assertEquals(expectedTestUser.middleName, actualTestUser.middleName); | ||
assertEquals(expectedTestUser.lastName, actualTestUser.lastName); | ||
} | ||
|
||
@Test | ||
public void givenFeatureEnabled_whenColumnIsEmptyString_thenParseAsNull() throws IOException { | ||
// setup test data | ||
TestUser expectedTestUser = new TestUser(); | ||
expectedTestUser.firstName = "Grace"; | ||
expectedTestUser.lastName = "Hopper"; | ||
CsvMapper csvMapper = CsvMapper.builder().enable(CsvParser.Feature.EMPTY_STRING_AS_NULL).build(); | ||
ObjectReader objectReader = csvMapper.readerFor(TestUser.class).with(csvMapper.schemaFor(TestUser.class)); | ||
String csv = "Grace,,Hopper"; | ||
|
||
// execute | ||
TestUser actualTestUser = objectReader.readValue(csv); | ||
|
||
// test | ||
assertNotNull(actualTestUser); | ||
assertEquals(expectedTestUser.firstName, actualTestUser.firstName); | ||
assertNull("The column that contains an empty String should be deserialized as null ", actualTestUser.middleName); | ||
assertEquals(expectedTestUser.lastName, actualTestUser.lastName); | ||
} | ||
} |