1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.reflection.wrapper;
17
18 import static org.assertj.core.api.Assertions.assertThat;
19 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
20 import static org.junit.jupiter.api.Assertions.*;
21 import static org.mockito.Mockito.verify;
22 import static org.mockito.Mockito.when;
23
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Set;
30
31 import org.apache.ibatis.domain.blog.Author;
32 import org.apache.ibatis.reflection.MetaObject;
33 import org.apache.ibatis.reflection.SystemMetaObject;
34 import org.apache.ibatis.reflection.property.PropertyTokenizer;
35 import org.junit.jupiter.api.BeforeEach;
36 import org.junit.jupiter.api.Test;
37 import org.mockito.Mock;
38
39
40
41
42
43
44 class MapWrapperUnitTest extends ObjectWrapperBaseTest {
45
46 @Mock
47 private Map<String, Object> map;
48
49 @Mock
50 private List<Integer> list;
51
52 private MetaObject metaObject;
53
54 private ObjectWrapper wrapper;
55
56 @BeforeEach
57 void setup() {
58 this.metaObject = SystemMetaObject.forObject(map);
59 this.wrapper = new MapWrapper(metaObject, map);
60 }
61
62 @Test
63 @Override
64 void shouldGet() {
65 when(map.get("key")).thenReturn("value");
66
67 Object value = wrapper.get(new PropertyTokenizer("key"));
68
69 assertEquals("value", value);
70 verify(map).get("key");
71 }
72
73 @Test
74 void shouldNotGetWhichContainsDelim() {
75 Author author = new Author(1);
76 when(map.get("author")).thenReturn(author);
77
78 Object value = wrapper.get(new PropertyTokenizer("author.id"));
79
80 assertEquals(1, value);
81 }
82
83 @Test
84 void shouldGetWhichContainsIndex() {
85 when(list.get(0)).thenReturn(1);
86 when(map.get("key")).thenReturn(list);
87
88 Object value = wrapper.get(new PropertyTokenizer("key[0]"));
89
90 assertEquals(1, value);
91 }
92
93 @Test
94 @Override
95 void shouldSet() {
96 wrapper.set(new PropertyTokenizer("key"), "value");
97
98 verify(map).put("key", "value");
99 }
100
101 @Test
102 void shouldSetWhichContainsDelim() {
103 wrapper.set(new PropertyTokenizer("author.id"), 1);
104
105 verify(map).put("author", new HashMap<>() {
106 {
107 put("id", 1);
108 }
109 });
110 }
111
112 @Test
113 void shouldSetWhichContainsIndex() {
114 when(map.get("key")).thenReturn(list);
115
116 wrapper.set(new PropertyTokenizer("key[0]"), 1);
117
118 verify(list).set(0, 1);
119 }
120
121 @Test
122 @Override
123 void shouldFindProperty() {
124 assertEquals("abc", wrapper.findProperty("abc", true));
125 assertEquals("abc", wrapper.findProperty("abc", false));
126 }
127
128 @Test
129 @Override
130 void shouldGetGetterNames() {
131 Set<String> sets = new HashSet<>() {
132 {
133 add("key1");
134 add("key2");
135 }
136 };
137 when(map.keySet()).thenReturn(sets);
138
139 String[] getterNames = wrapper.getGetterNames();
140
141 assertEquals(2, getterNames.length);
142 assertThat(getterNames).containsExactlyInAnyOrder("key1", "key2");
143 }
144
145 @Test
146 @Override
147 void shouldGetSetterNames() {
148 Set<String> sets = new HashSet<>() {
149 {
150 add("key1");
151 add("key2");
152 }
153 };
154 when(map.keySet()).thenReturn(sets);
155
156 String[] setterNames = wrapper.getSetterNames();
157
158 assertEquals(2, setterNames.length);
159 assertThat(setterNames).containsExactlyInAnyOrder("key1", "key2");
160 }
161
162 @Test
163 @Override
164 void shouldGetGetterType() {
165 when(map.get("key")).thenReturn("abc");
166
167 Class<?> type = wrapper.getGetterType("key");
168
169 assertEquals(String.class, type);
170 }
171
172 @Test
173 void shouldGetGetterTypeWhichContainsIndex() {
174 when(map.get("key")).thenReturn(list);
175
176 Class<?> collectionType = wrapper.getGetterType("key");
177 Class<?> type = wrapper.getGetterType("key[0]");
178
179 assertEquals(list.getClass(), collectionType);
180 assertEquals(Object.class, type);
181 }
182
183 @Test
184 @Override
185 void shouldGetSetterType() {
186 when(map.get("key")).thenReturn("abc");
187
188 Class<?> type = wrapper.getSetterType("key");
189
190 assertEquals(String.class, type);
191 }
192
193 @Test
194 void shouldGetSetterTypeWhichContainsIndex() {
195 when(map.get("key")).thenReturn(list);
196
197 Class<?> collectionType = wrapper.getSetterType("key");
198 Class<?> type = wrapper.getSetterType("key[0]");
199
200 assertEquals(list.getClass(), collectionType);
201 assertEquals(Object.class, type);
202 }
203
204 @Test
205 @Override
206 void shouldHasGetter() {
207 when(map.containsKey("key")).thenReturn(false);
208
209 assertFalse(wrapper.hasGetter("key"));
210
211 when(map.containsKey("key")).thenReturn(true);
212
213 assertTrue(wrapper.hasGetter("key"));
214 }
215
216 @Test
217 @Override
218 void shouldHasSetter() {
219 assertTrue(wrapper.hasSetter("abc"));
220 }
221
222 @Test
223 @Override
224 void shouldIsCollection() {
225 assertFalse(wrapper.isCollection());
226 }
227
228 @Test
229 @Override
230 void shouldInstantiatePropertyValue() {
231 MetaObject result = wrapper.instantiatePropertyValue("abc", new PropertyTokenizer("key"),
232 SystemMetaObject.DEFAULT_OBJECT_FACTORY);
233
234 assertFalse(result.hasGetter("key"));
235 }
236
237 @Test
238 @Override
239 void shouldAddElement() {
240 assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> wrapper.add("1"));
241 }
242
243 @Test
244 @Override
245 void shouldAddAll() {
246 assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> wrapper.addAll(new ArrayList<>()));
247 }
248 }