|
11 | 11 | // Unit tests for [databind#1868], [databind#1880], [databind#2265]
|
12 | 12 | public class UtilCollectionsTypesTest extends BaseMapTest
|
13 | 13 | {
|
14 |
| - private final ObjectMapper DEFAULT_MAPPER = JsonMapper.builder() |
15 |
| - .activateDefaultTyping(NoCheckSubTypeValidator.instance, |
16 |
| - ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY) |
17 |
| - .build(); |
18 |
| - |
19 |
| - /* |
20 |
| - /********************************************************** |
21 |
| - /* Unit tests, "empty" types |
22 |
| - /********************************************************** |
23 |
| - */ |
24 |
| - |
25 |
| - public void testEmptyList() throws Exception { |
26 |
| - _verifyCollection(Collections.emptyList()); |
27 |
| - } |
28 |
| - |
29 |
| - public void testEmptySet() throws Exception { |
30 |
| - _verifyCollection(Collections.emptySet()); |
31 |
| - } |
32 |
| - |
33 |
| - public void testEmptyMap() throws Exception { |
34 |
| - _verifyMap(Collections.emptyMap()); |
35 |
| - } |
36 |
| - |
37 |
| - /* |
38 |
| - /********************************************************** |
39 |
| - /* Unit tests, "singleton" types |
40 |
| - /********************************************************** |
41 |
| - */ |
42 |
| - |
43 |
| - public void testSingletonList() throws Exception { |
44 |
| - _verifyCollection(Collections.singletonList(Arrays.asList("TheOne"))); |
45 |
| - } |
46 |
| - |
47 |
| - public void testSingletonSet() throws Exception { |
48 |
| - _verifyCollection(Collections.singleton(Arrays.asList("TheOne"))); |
49 |
| - } |
50 |
| - |
51 |
| - public void testSingletonMap() throws Exception { |
52 |
| - _verifyMap(Collections.singletonMap("foo", "bar")); |
53 |
| - } |
54 |
| - |
55 |
| - /* |
56 |
| - /********************************************************** |
57 |
| - /* Unit tests, "unmodifiable" types |
58 |
| - /********************************************************** |
59 |
| - */ |
60 |
| - |
61 |
| - public void testUnmodifiableList() throws Exception { |
62 |
| - _verifyCollection(Collections.unmodifiableList(Arrays.asList("first", "second"))); |
63 |
| - } |
64 |
| - |
65 |
| - // [databind#2265] |
66 |
| - public void testUnmodifiableListFromLinkedList() throws Exception { |
67 |
| - final List<String> input = new LinkedList<>(); |
68 |
| - input.add("first"); |
69 |
| - input.add("second"); |
70 |
| - |
71 |
| - // Can't use simple "_verifyCollection" as type may change; instead use |
72 |
| - // bit more flexible check: |
73 |
| - Collection<?> act = _writeReadCollection(Collections.unmodifiableList(input)); |
74 |
| - assertEquals(input, act); |
75 |
| - |
76 |
| - // and this check may be bit fragile (may need to revisit), but is good enough for now: |
77 |
| - assertEquals(Collections.unmodifiableList(new ArrayList<>(input)).getClass(), act.getClass()); |
78 |
| - } |
79 |
| - |
80 |
| - public void testUnmodifiableSet() throws Exception |
81 |
| - { |
82 |
| - Set<String> input = new LinkedHashSet<>(Arrays.asList("first", "second")); |
83 |
| - _verifyCollection(Collections.unmodifiableSet(input)); |
84 |
| - } |
85 |
| - |
86 |
| - public void testUnmodifiableMap() throws Exception |
87 |
| - { |
88 |
| - Map<String,String> input = new LinkedHashMap<>(); |
89 |
| - input.put("a", "b"); |
90 |
| - input.put("c", "d"); |
91 |
| - _verifyMap(Collections.unmodifiableMap(input)); |
92 |
| - } |
93 |
| - |
94 |
| - /* |
95 |
| - /********************************************************** |
96 |
| - /* Unit tests, other |
97 |
| - /********************************************************** |
98 |
| - */ |
99 |
| - |
100 |
| - public void testArraysAsList() throws Exception |
101 |
| - { |
102 |
| - // Here there are no semantics to preserve, so simply check that |
103 |
| - // contents remain the same |
104 |
| - List<String> input = Arrays.asList("a", "bc", "def"); |
105 |
| - String json = DEFAULT_MAPPER.writeValueAsString(input); |
106 |
| - List<?> result = DEFAULT_MAPPER.readValue(json, List.class); |
107 |
| - assertEquals(input, result); |
108 |
| - } |
109 |
| - |
110 |
| - /* |
111 |
| - /********************************************************** |
112 |
| - /* Helper methods |
113 |
| - /********************************************************** |
114 |
| - */ |
115 |
| - |
116 |
| - protected void _verifyCollection(Collection<?> exp) throws Exception { |
117 |
| - Collection<?> act = _writeReadCollection(exp); |
118 |
| - assertEquals(exp, act); |
119 |
| - assertEquals(exp.getClass(), act.getClass()); |
120 |
| - } |
121 |
| - |
122 |
| - protected Collection<?> _writeReadCollection(Collection<?> input) throws Exception { |
123 |
| - final String json = DEFAULT_MAPPER.writeValueAsString(input); |
124 |
| - return DEFAULT_MAPPER.readValue(json, Collection.class); |
125 |
| - } |
| 14 | + private final ObjectMapper DEFAULT_MAPPER = JsonMapper.builder() |
| 15 | + .activateDefaultTyping(NoCheckSubTypeValidator.instance, |
| 16 | + ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY) |
| 17 | + .build(); |
| 18 | + |
| 19 | + /* |
| 20 | + /********************************************************** |
| 21 | + /* Unit tests, "empty" types |
| 22 | + /********************************************************** |
| 23 | + */ |
| 24 | + |
| 25 | + public void testEmptyList() throws Exception { |
| 26 | + _verifyCollection(Collections.emptyList()); |
| 27 | + } |
| 28 | + |
| 29 | + public void testEmptySet() throws Exception { |
| 30 | + _verifyCollection(Collections.emptySet()); |
| 31 | + } |
| 32 | + |
| 33 | + public void testEmptyMap() throws Exception { |
| 34 | + _verifyMap(Collections.emptyMap()); |
| 35 | + } |
| 36 | + |
| 37 | + /* |
| 38 | + /********************************************************** |
| 39 | + /* Unit tests, "singleton" types |
| 40 | + /********************************************************** |
| 41 | + */ |
| 42 | + |
| 43 | + public void testSingletonList() throws Exception { |
| 44 | + _verifyCollection(Collections.singletonList(Arrays.asList("TheOne"))); |
| 45 | + } |
| 46 | + |
| 47 | + public void testSingletonSet() throws Exception { |
| 48 | + _verifyCollection(Collections.singleton(Arrays.asList("TheOne"))); |
| 49 | + } |
| 50 | + |
| 51 | + public void testSingletonMap() throws Exception { |
| 52 | + _verifyMap(Collections.singletonMap("foo", "bar")); |
| 53 | + } |
| 54 | + |
| 55 | + /* |
| 56 | + /********************************************************** |
| 57 | + /* Unit tests, "unmodifiable" types |
| 58 | + /********************************************************** |
| 59 | + */ |
| 60 | + |
| 61 | + public void testUnmodifiableList() throws Exception { |
| 62 | + _verifyCollection(Collections.unmodifiableList(Arrays.asList("first", "second"))); |
| 63 | + } |
| 64 | + |
| 65 | + // [databind#2265] |
| 66 | + public void testUnmodifiableListFromLinkedList() throws Exception { |
| 67 | + final List<String> input = new LinkedList<>(); |
| 68 | + input.add("first"); |
| 69 | + input.add("second"); |
| 70 | + |
| 71 | + // Can't use simple "_verifyCollection" as type may change; instead use |
| 72 | + // bit more flexible check: |
| 73 | + Collection<?> act = _writeReadCollection(Collections.unmodifiableList(input)); |
| 74 | + assertEquals(input, act); |
| 75 | + |
| 76 | + // and this check may be bit fragile (may need to revisit), but is good enough for now: |
| 77 | + assertEquals(Collections.unmodifiableList(new ArrayList<>(input)).getClass(), act.getClass()); |
| 78 | + } |
| 79 | + |
| 80 | + public void testUnmodifiableSet() throws Exception |
| 81 | + { |
| 82 | + Set<String> input = new LinkedHashSet<>(Arrays.asList("first", "second")); |
| 83 | + _verifyCollection(Collections.unmodifiableSet(input)); |
| 84 | + } |
| 85 | + |
| 86 | + public void testUnmodifiableMap() throws Exception |
| 87 | + { |
| 88 | + Map<String,String> input = new LinkedHashMap<>(); |
| 89 | + input.put("a", "b"); |
| 90 | + input.put("c", "d"); |
| 91 | + _verifyMap(Collections.unmodifiableMap(input)); |
| 92 | + } |
| 93 | + |
| 94 | + /* |
| 95 | + /********************************************************** |
| 96 | + /* Unit tests, other |
| 97 | + /********************************************************** |
| 98 | + */ |
| 99 | + |
| 100 | + public void testArraysAsList() throws Exception |
| 101 | + { |
| 102 | + // Here there are no semantics to preserve, so simply check that |
| 103 | + // contents remain the same |
| 104 | + List<String> input = Arrays.asList("a", "bc", "def"); |
| 105 | + String json = DEFAULT_MAPPER.writeValueAsString(input); |
| 106 | + List<?> result = DEFAULT_MAPPER.readValue(json, List.class); |
| 107 | + assertEquals(input, result); |
| 108 | + } |
| 109 | + |
| 110 | + /* |
| 111 | + /********************************************************** |
| 112 | + /* Helper methods |
| 113 | + /********************************************************** |
| 114 | + */ |
| 115 | + |
| 116 | + protected void _verifyCollection(Collection<?> exp) throws Exception { |
| 117 | + Collection<?> act = _writeReadCollection(exp); |
| 118 | + assertEquals(exp, act); |
| 119 | + assertEquals(exp.getClass(), act.getClass()); |
| 120 | + } |
| 121 | + |
| 122 | + protected Collection<?> _writeReadCollection(Collection<?> input) throws Exception { |
| 123 | + final String json = DEFAULT_MAPPER.writeValueAsString(input); |
| 124 | + return DEFAULT_MAPPER.readValue(json, Collection.class); |
| 125 | + } |
126 | 126 |
|
127 |
| - protected void _verifyMap(Map<?,?> exp) throws Exception |
128 |
| - { |
129 |
| - String json = DEFAULT_MAPPER.writeValueAsString(exp); |
130 |
| - Map<?,?> act = DEFAULT_MAPPER.readValue(json, Map.class); |
131 |
| - |
132 |
| - assertEquals(exp, act); |
133 |
| - assertEquals(exp.getClass(), act.getClass()); |
134 |
| - } |
| 127 | + protected void _verifyMap(Map<?,?> exp) throws Exception |
| 128 | + { |
| 129 | + String json = DEFAULT_MAPPER.writeValueAsString(exp); |
| 130 | + Map<?,?> act = DEFAULT_MAPPER.readValue(json, Map.class); |
| 131 | + |
| 132 | + assertEquals(exp, act); |
| 133 | + assertEquals(exp.getClass(), act.getClass()); |
| 134 | + } |
135 | 135 | }
|
0 commit comments