This repository has been archived by the owner on Jan 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bee4187
commit 599e2e7
Showing
3 changed files
with
73 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
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
64 changes: 64 additions & 0 deletions
64
src/test/java/com/fasterxml/jackson/dataformat/csv/ser/NullWritingTest.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,64 @@ | ||
package com.fasterxml.jackson.dataformat.csv.ser; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.StringWriter; | ||
|
||
import com.fasterxml.jackson.databind.*; | ||
import com.fasterxml.jackson.dataformat.csv.*; | ||
|
||
// for [dataformat-csv#69] | ||
public class NullWritingTest extends ModuleTestBase | ||
{ | ||
private final CsvMapper csv = new CsvMapper(); | ||
|
||
public static class Nullable { | ||
public String a, b, c, d; | ||
} | ||
|
||
public void testObjectWithNullMembersToString() throws Exception { | ||
CsvSchema schema = csv.schemaFor(Nullable.class).withUseHeader(true); | ||
ObjectWriter writer = csv.writer(schema); | ||
String nullMembers = writer.writeValueAsString(new Nullable()); | ||
assertEquals("a,b,c,d\n,,,\n", nullMembers); | ||
} | ||
|
||
public void testNullToString() throws Exception { | ||
CsvSchema schema = csv.schemaFor(Nullable.class).withUseHeader(true); | ||
ObjectWriter writer = csv.writer(schema); | ||
String nullObject = writer.writeValueAsString(null); | ||
assertEquals("a,b,c,d\n", nullObject); | ||
} | ||
|
||
public void testObjectWithNullMembersToStream() throws Exception { | ||
CsvSchema schema = csv.schemaFor(Nullable.class).withUseHeader(true); | ||
ObjectWriter writer = csv.writer(schema); | ||
|
||
// Write an object with null members | ||
ByteArrayOutputStream stream = new ByteArrayOutputStream(); | ||
SequenceWriter writeValues = writer.writeValues(stream); | ||
writeValues.write(new Nullable()); | ||
writeValues.write(new Nullable()); | ||
writeValues.flush(); | ||
String nullMembers = stream.toString("UTF-8"); | ||
assertEquals("a,b,c,d\n,,,\n,,,\n", nullMembers); | ||
} | ||
|
||
public void testNullToStream() throws Exception { | ||
CsvSchema schema = csv.schemaFor(Nullable.class).withUseHeader(true); | ||
ObjectWriter writer = csv.writer(schema); | ||
|
||
// Write a null value | ||
StringWriter sw = new StringWriter(); | ||
SequenceWriter writeValues = writer.writeValues(sw); | ||
writeValues.write(null); | ||
writeValues.write(null); | ||
writeValues.flush(); | ||
String nullObject = sw.toString(); | ||
/* 11-Feb-2015, tatu: Two ways to go; either nulls get ignored, or they trigger serialization of | ||
* empty Object. For now, former occurs: | ||
*/ | ||
|
||
assertEquals("a,b,c,d\n", nullObject); | ||
// assertEquals("a,b,c,d\n\n\n", nullObject); | ||
} | ||
} |