|
| 1 | +package tools.jackson.dataformat.csv.deser; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.annotation.JsonPropertyOrder; |
| 6 | + |
| 7 | +import tools.jackson.databind.MappingIterator; |
| 8 | +import tools.jackson.databind.ObjectReader; |
| 9 | +import tools.jackson.dataformat.csv.*; |
| 10 | + |
| 11 | +import static org.junit.jupiter.api.Assertions.*; |
| 12 | + |
| 13 | +/** |
| 14 | + * Tests for {@code CsvReadFeature.NULL_VALUE_UNQUOTED_AS_NULL} |
| 15 | + * (see [dataformats-text#601]) |
| 16 | + */ |
| 17 | +public class NullValueUnquotedAsNullTest extends ModuleTestBase |
| 18 | +{ |
| 19 | + @JsonPropertyOrder({"firstName", "middleName", "lastName"}) |
| 20 | + static class TestUser { |
| 21 | + public String firstName, middleName, lastName; |
| 22 | + } |
| 23 | + |
| 24 | + private final CsvMapper MAPPER = mapperForCsv(); |
| 25 | + |
| 26 | + // Default behavior: both quoted and unquoted null values become null |
| 27 | + @Test |
| 28 | + public void testDefaultBothQuotedAndUnquotedAsNull() throws Exception { |
| 29 | + CsvSchema schema = MAPPER.schemaFor(TestUser.class).withNullValue("N/A"); |
| 30 | + ObjectReader reader = MAPPER.readerFor(TestUser.class).with(schema); |
| 31 | + |
| 32 | + // Unquoted N/A -> null |
| 33 | + TestUser user1 = reader.readValue("Grace,N/A,Hopper"); |
| 34 | + assertEquals("Grace", user1.firstName); |
| 35 | + assertNull(user1.middleName); |
| 36 | + assertEquals("Hopper", user1.lastName); |
| 37 | + |
| 38 | + // Quoted "N/A" -> also null (default behavior) |
| 39 | + TestUser user2 = reader.readValue("Grace,\"N/A\",Hopper"); |
| 40 | + assertEquals("Grace", user2.firstName); |
| 41 | + assertNull(user2.middleName); |
| 42 | + assertEquals("Hopper", user2.lastName); |
| 43 | + } |
| 44 | + |
| 45 | + // With feature enabled: only unquoted null values become null |
| 46 | + @Test |
| 47 | + public void testUnquotedNullValueAsNull() throws Exception { |
| 48 | + CsvSchema schema = MAPPER.schemaFor(TestUser.class).withNullValue("N/A"); |
| 49 | + ObjectReader reader = MAPPER.readerFor(TestUser.class) |
| 50 | + .with(schema) |
| 51 | + .with(CsvReadFeature.ONLY_UNQUOTED_NULL_VALUES_AS_NULL); |
| 52 | + |
| 53 | + // Unquoted N/A -> null |
| 54 | + TestUser user1 = reader.readValue("Grace,N/A,Hopper"); |
| 55 | + assertEquals("Grace", user1.firstName); |
| 56 | + assertNull(user1.middleName); |
| 57 | + assertEquals("Hopper", user1.lastName); |
| 58 | + |
| 59 | + // Quoted "N/A" -> remains as string "N/A" |
| 60 | + TestUser user2 = reader.readValue("Grace,\"N/A\",Hopper"); |
| 61 | + assertEquals("Grace", user2.firstName); |
| 62 | + assertEquals("N/A", user2.middleName); |
| 63 | + assertEquals("Hopper", user2.lastName); |
| 64 | + } |
| 65 | + |
| 66 | + // Test with array binding (non-POJO) |
| 67 | + @Test |
| 68 | + public void testUnquotedNullValueAsNullWithArrays() throws Exception { |
| 69 | + CsvSchema schema = CsvSchema.emptySchema().withNullValue("null"); |
| 70 | + ObjectReader reader = MAPPER.reader() |
| 71 | + .with(schema) |
| 72 | + .with(CsvReadFeature.ONLY_UNQUOTED_NULL_VALUES_AS_NULL) |
| 73 | + .with(CsvReadFeature.WRAP_AS_ARRAY); |
| 74 | + |
| 75 | + // Unquoted null -> null |
| 76 | + try (MappingIterator<String[]> it = reader.forType(String[].class) |
| 77 | + .readValues("a,null,b")) { |
| 78 | + String[] arr = it.next(); |
| 79 | + assertEquals(3, arr.length); |
| 80 | + assertEquals("a", arr[0]); |
| 81 | + assertNull(arr[1]); |
| 82 | + assertEquals("b", arr[2]); |
| 83 | + } |
| 84 | + |
| 85 | + // Quoted "null" -> string "null" |
| 86 | + try (MappingIterator<String[]> it = reader.forType(String[].class) |
| 87 | + .readValues("a,\"null\",b")) { |
| 88 | + String[] arr = it.next(); |
| 89 | + assertEquals(3, arr.length); |
| 90 | + assertEquals("a", arr[0]); |
| 91 | + assertEquals("null", arr[1]); |
| 92 | + assertEquals("b", arr[2]); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + // Test with Object[] binding |
| 97 | + @Test |
| 98 | + public void testUnquotedNullValueAsNullWithObjectArrays() throws Exception { |
| 99 | + CsvSchema schema = CsvSchema.emptySchema().withNullValue("NULL"); |
| 100 | + ObjectReader reader = MAPPER.reader() |
| 101 | + .with(schema) |
| 102 | + .with(CsvReadFeature.ONLY_UNQUOTED_NULL_VALUES_AS_NULL) |
| 103 | + .with(CsvReadFeature.WRAP_AS_ARRAY); |
| 104 | + |
| 105 | + // Unquoted NULL -> null |
| 106 | + try (MappingIterator<Object[]> it = reader.forType(Object[].class) |
| 107 | + .readValues("first,NULL,last")) { |
| 108 | + Object[] arr = it.next(); |
| 109 | + assertEquals(3, arr.length); |
| 110 | + assertEquals("first", arr[0]); |
| 111 | + assertNull(arr[1]); |
| 112 | + assertEquals("last", arr[2]); |
| 113 | + } |
| 114 | + |
| 115 | + // Quoted "NULL" -> string "NULL" |
| 116 | + try (MappingIterator<Object[]> it = reader.forType(Object[].class) |
| 117 | + .readValues("first,\"NULL\",last")) { |
| 118 | + Object[] arr = it.next(); |
| 119 | + assertEquals(3, arr.length); |
| 120 | + assertEquals("first", arr[0]); |
| 121 | + assertEquals("NULL", arr[1]); |
| 122 | + assertEquals("last", arr[2]); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + // Test feature disabled has no effect (same as default) |
| 127 | + @Test |
| 128 | + public void testFeatureDisabledSameAsDefault() throws Exception { |
| 129 | + CsvSchema schema = MAPPER.schemaFor(TestUser.class).withNullValue("N/A"); |
| 130 | + ObjectReader reader = MAPPER.readerFor(TestUser.class) |
| 131 | + .with(schema) |
| 132 | + .without(CsvReadFeature.ONLY_UNQUOTED_NULL_VALUES_AS_NULL); |
| 133 | + |
| 134 | + // Both quoted and unquoted should become null |
| 135 | + TestUser user1 = reader.readValue("Grace,N/A,Hopper"); |
| 136 | + assertNull(user1.middleName); |
| 137 | + |
| 138 | + TestUser user2 = reader.readValue("Grace,\"N/A\",Hopper"); |
| 139 | + assertNull(user2.middleName); |
| 140 | + } |
| 141 | +} |
0 commit comments