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

Commit 2e2c44f

Browse files
committed
Add unit tests for #106
1 parent e0e8bd7 commit 2e2c44f

File tree

1 file changed

+45
-3
lines changed

1 file changed

+45
-3
lines changed

src/test/java/com/fasterxml/jackson/dataformat/csv/ser/NullWritingTest.java

+45-3
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@
22

33
import java.io.ByteArrayOutputStream;
44
import java.io.StringWriter;
5+
import java.util.Arrays;
6+
import java.util.List;
57

68
import com.fasterxml.jackson.databind.*;
79
import com.fasterxml.jackson.dataformat.csv.*;
810

911
// for [dataformat-csv#69], other null value serialization
1012
public class NullWritingTest extends ModuleTestBase
1113
{
12-
private final CsvMapper csv = new CsvMapper();
13-
1414
public static class Nullable {
1515
public String a, b, c, d;
1616
}
17-
17+
18+
private final CsvMapper csv = new CsvMapper();
19+
1820
public void testObjectWithNullMembersToString() throws Exception {
1921
CsvSchema schema = csv.schemaFor(Nullable.class).withUseHeader(true);
2022
ObjectWriter writer = csv.writer(schema);
@@ -78,4 +80,44 @@ public void testCustomNullValue() throws Exception
7880
// MUST use doubling for quotes!
7981
assertEquals("id,n/a\n", result);
8082
}
83+
84+
public void testNullFieldsOfListsContainedByMainLevelListIssue106() throws Exception
85+
{
86+
CsvMapper mapper = mapperForCsv();
87+
CsvSchema schema = CsvSchema.builder().build();
88+
89+
List<String> row1 = Arrays.asList("d0", null, "d2");
90+
List<String> row2 = Arrays.asList(null, "d1", "d2");
91+
List<String> row3 = Arrays.asList("d0", "d1", null);
92+
93+
List<List<String>> dataList = Arrays.asList(row1, row2, row3);
94+
95+
String result = mapper.writer(schema).writeValueAsString(dataList);
96+
assertEquals("d0,,d2\n,d1,d2\nd0,d1,\n", result);
97+
98+
schema = schema.withNullValue("n/a");
99+
result = mapper.writer(schema).writeValueAsString(dataList);
100+
assertEquals("d0,n/a,d2\nn/a,d1,d2\nd0,d1,n/a\n", result);
101+
}
102+
103+
public void testNullElementsOfMainLevelListIssue106() throws Exception
104+
{
105+
CsvMapper mapper = mapperForCsv();
106+
CsvSchema schema = CsvSchema.builder().build();
107+
108+
List<String> row1 = Arrays.asList("d0", null, "d2");
109+
List<String> row2 = Arrays.asList(null, "d1", "d2");
110+
List<String> row3 = Arrays.asList("d0", "d1", null);
111+
112+
// when serialized, the added root level nulls at index 1 and 3
113+
// should be absent from the output
114+
List<List<String>> dataList = Arrays.asList(row1, null, row2, null, row3);
115+
116+
String result = mapper.writer(schema).writeValueAsString(dataList);
117+
assertEquals("d0,,d2\n,d1,d2\nd0,d1,\n", result);
118+
119+
schema = schema.withNullValue("n/a");
120+
result = mapper.writer(schema).writeValueAsString(dataList);
121+
assertEquals("d0,n/a,d2\nn/a,d1,d2\nd0,d1,n/a\n", result);
122+
}
81123
}

0 commit comments

Comments
 (0)