|
| 1 | +package com.fasterxml.jackson.dataformat.csv.deser; |
| 2 | + |
| 3 | +import java.util.Collections; |
| 4 | +import java.util.EnumSet; |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 8 | +import com.fasterxml.jackson.databind.ObjectReader; |
| 9 | +import com.fasterxml.jackson.dataformat.csv.CsvMapper; |
| 10 | +import com.fasterxml.jackson.dataformat.csv.CsvSchema; |
| 11 | +import com.fasterxml.jackson.dataformat.csv.ModuleTestBase; |
| 12 | + |
| 13 | +public class ListField199Test extends ModuleTestBase |
| 14 | +{ |
| 15 | + // [dataformats-text#199] |
| 16 | + static class ModelString199 { |
| 17 | + @JsonProperty("STRINGS") |
| 18 | + List<String> strings; |
| 19 | + |
| 20 | + @JsonProperty("OTHER_FIELD") |
| 21 | + String otherField; |
| 22 | + } |
| 23 | + |
| 24 | + static class ModelLong199 { |
| 25 | + @JsonProperty("LONGS") |
| 26 | + List<Long> longs; |
| 27 | + |
| 28 | + @JsonProperty("OTHER_FIELD") |
| 29 | + String otherField; |
| 30 | + } |
| 31 | + |
| 32 | + enum ABC { A, B, C }; |
| 33 | + |
| 34 | + static class ModelEnums199 { |
| 35 | + public EnumSet<ABC> enums; |
| 36 | + public String extra; |
| 37 | + } |
| 38 | + |
| 39 | + /* |
| 40 | + /********************************************************************** |
| 41 | + /* Test methods |
| 42 | + /********************************************************************** |
| 43 | + */ |
| 44 | + |
| 45 | + private final CsvMapper MAPPER = mapperForCsv(); |
| 46 | + |
| 47 | + private final CsvSchema WITH_ARRAY_SCHEMA = MAPPER.schemaWithHeader() |
| 48 | + .withArrayElementSeparator(",") |
| 49 | + .withColumnSeparator(';') |
| 50 | + .withEscapeChar('"'); |
| 51 | + |
| 52 | + // [dataformats-text#199] |
| 53 | + public void testReadEmptyStringList() throws Exception |
| 54 | + { |
| 55 | + ObjectReader r = MAPPER.readerFor(ModelString199.class) |
| 56 | + .with(WITH_ARRAY_SCHEMA); |
| 57 | + ModelString199 result; |
| 58 | + |
| 59 | + // First, non-empty List |
| 60 | + result = r.readValue("STRINGS;OTHER_FIELD\n" + |
| 61 | + "stuff;other"); |
| 62 | + assertNotNull(result); |
| 63 | + assertEquals("other", result.otherField); |
| 64 | + assertEquals(Collections.singletonList("stuff"), result.strings); |
| 65 | + |
| 66 | + // then empty |
| 67 | + result = r.readValue("STRINGS;OTHER_FIELD\n" + |
| 68 | + ";Hello"); |
| 69 | + assertNotNull(result); |
| 70 | + assertEquals("Hello", result.otherField); |
| 71 | + assertEquals(Collections.emptyList(), result.strings); |
| 72 | + } |
| 73 | + |
| 74 | + // [dataformats-text#199] |
| 75 | + public void testReadEmptyLongList() throws Exception |
| 76 | + { |
| 77 | + ObjectReader r = MAPPER.readerFor(ModelLong199.class) |
| 78 | + .with(WITH_ARRAY_SCHEMA); |
| 79 | + ModelLong199 result; |
| 80 | + |
| 81 | + // First, non-empty List |
| 82 | + result = r.readValue("LONGS;OTHER_FIELD\n" + |
| 83 | + "123;other"); |
| 84 | + assertNotNull(result); |
| 85 | + assertEquals("other", result.otherField); |
| 86 | + assertNotNull(result.longs); |
| 87 | + assertEquals(Collections.singletonList(Long.valueOf(123)), result.longs); |
| 88 | + |
| 89 | + // then empty |
| 90 | + result = r.readValue("LONGS;OTHER_FIELD\n" + |
| 91 | + ";Hello"); |
| 92 | + assertNotNull(result); |
| 93 | + assertEquals("Hello", result.otherField); |
| 94 | + assertNotNull(result.longs); |
| 95 | + assertEquals(Collections.emptyList(), result.longs); |
| 96 | + } |
| 97 | + |
| 98 | + public void testReadEmptyEnumSet() throws Exception |
| 99 | + { |
| 100 | + ObjectReader r = MAPPER.readerFor(ModelEnums199.class) |
| 101 | + .with(WITH_ARRAY_SCHEMA); |
| 102 | + ModelEnums199 result; |
| 103 | + |
| 104 | + // First, non-empty List |
| 105 | + result = r.readValue("enums;extra\n" + |
| 106 | + "B;other"); |
| 107 | + assertNotNull(result); |
| 108 | + assertEquals("other", result.extra); |
| 109 | + assertNotNull(result.enums); |
| 110 | + assertEquals(EnumSet.of(ABC.B), result.enums); |
| 111 | + |
| 112 | + // then empty |
| 113 | + result = r.readValue("enums;extra\n" + |
| 114 | + ";stuff"); |
| 115 | + assertNotNull(result); |
| 116 | + assertEquals("stuff", result.extra); |
| 117 | + assertNotNull(result.enums); |
| 118 | + assertEquals(EnumSet.noneOf(ABC.class), result.enums); |
| 119 | + } |
| 120 | +} |
0 commit comments