-
Notifications
You must be signed in to change notification settings - Fork 12.9k
/
Copy pathMetaObjectTest.java
301 lines (257 loc) · 10.5 KB
/
MetaObjectTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
* Copyright 2009-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ibatis.reflection;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.domain.blog.Author;
import org.apache.ibatis.domain.blog.Section;
import org.apache.ibatis.domain.misc.CustomBeanWrapper;
import org.apache.ibatis.domain.misc.CustomBeanWrapperFactory;
import org.apache.ibatis.domain.misc.RichType;
import org.junit.jupiter.api.Test;
class MetaObjectTest {
@Test
void shouldGetAndSetField() {
RichType rich = new RichType();
MetaObject meta = SystemMetaObject.forObject(rich);
meta.setValue("richField", "foo");
assertEquals("foo", meta.getValue("richField"));
}
@Test
void shouldGetAndSetNestedField() {
RichType rich = new RichType();
MetaObject meta = SystemMetaObject.forObject(rich);
meta.setValue("richType.richField", "foo");
assertEquals("foo", meta.getValue("richType.richField"));
}
@Test
void shouldGetAndSetProperty() {
RichType rich = new RichType();
MetaObject meta = SystemMetaObject.forObject(rich);
meta.setValue("richProperty", "foo");
assertEquals("foo", meta.getValue("richProperty"));
}
@Test
void shouldGetAndSetNestedProperty() {
RichType rich = new RichType();
MetaObject meta = SystemMetaObject.forObject(rich);
meta.setValue("richType.richProperty", "foo");
assertEquals("foo", meta.getValue("richType.richProperty"));
}
@Test
void shouldGetAndSetMapPair() {
RichType rich = new RichType();
MetaObject meta = SystemMetaObject.forObject(rich);
meta.setValue("richMap.key", "foo");
assertEquals("foo", meta.getValue("richMap.key"));
}
@Test
void shouldGetAndSetMapPairUsingArraySyntax() {
RichType rich = new RichType();
MetaObject meta = SystemMetaObject.forObject(rich);
meta.setValue("richMap[key]", "foo");
assertEquals("foo", meta.getValue("richMap[key]"));
}
@Test
void shouldGetAndSetNestedMapPair() {
RichType rich = new RichType();
MetaObject meta = SystemMetaObject.forObject(rich);
meta.setValue("richType.richMap.key", "foo");
assertEquals("foo", meta.getValue("richType.richMap.key"));
}
@Test
void shouldGetAndSetNestedMapPairUsingArraySyntax() {
RichType rich = new RichType();
MetaObject meta = SystemMetaObject.forObject(rich);
meta.setValue("richType.richMap[key]", "foo");
assertEquals("foo", meta.getValue("richType.richMap[key]"));
}
@Test
void shouldGetAndSetListItem() {
RichType rich = new RichType();
MetaObject meta = SystemMetaObject.forObject(rich);
meta.setValue("richList[0]", "foo");
assertEquals("foo", meta.getValue("richList[0]"));
}
@Test
void shouldGetAndSetNestedListItem() {
RichType rich = new RichType();
MetaObject meta = SystemMetaObject.forObject(rich);
meta.setValue("richType.richList[0]", "foo");
assertEquals("foo", meta.getValue("richType.richList[0]"));
}
@Test
void shouldGetReadablePropertyNames() {
RichType rich = new RichType();
MetaObject meta = SystemMetaObject.forObject(rich);
String[] readables = meta.getGetterNames();
assertEquals(5, readables.length);
for (String readable : readables) {
assertTrue(meta.hasGetter(readable));
assertTrue(meta.hasGetter("richType." + readable));
}
assertTrue(meta.hasGetter("richType"));
}
@Test
void shouldGetWriteablePropertyNames() {
RichType rich = new RichType();
MetaObject meta = SystemMetaObject.forObject(rich);
String[] writeables = meta.getSetterNames();
assertEquals(5, writeables.length);
for (String writeable : writeables) {
assertTrue(meta.hasSetter(writeable));
assertTrue(meta.hasSetter("richType." + writeable));
}
assertTrue(meta.hasSetter("richType"));
}
@Test
void shouldSetPropertyOfNullNestedProperty() {
MetaObject richWithNull = SystemMetaObject.forObject(new RichType());
richWithNull.setValue("richType.richProperty", "foo");
assertEquals("foo", richWithNull.getValue("richType.richProperty"));
}
@Test
void shouldSetPropertyOfNullNestedPropertyWithNull() {
MetaObject richWithNull = SystemMetaObject.forObject(new RichType());
richWithNull.setValue("richType.richProperty", null);
assertNull(richWithNull.getValue("richType.richProperty"));
}
@Test
void shouldGetPropertyOfNullNestedProperty() {
MetaObject richWithNull = SystemMetaObject.forObject(new RichType());
assertNull(richWithNull.getValue("richType.richProperty"));
}
@Test
void shouldVerifyHasReadablePropertiesReturnedByGetReadablePropertyNames() {
MetaObject object = SystemMetaObject.forObject(new Author());
for (String readable : object.getGetterNames()) {
assertTrue(object.hasGetter(readable));
}
}
@Test
void shouldVerifyHasWriteablePropertiesReturnedByGetWriteablePropertyNames() {
MetaObject object = SystemMetaObject.forObject(new Author());
for (String writeable : object.getSetterNames()) {
assertTrue(object.hasSetter(writeable));
}
}
@Test
void shouldSetAndGetProperties() {
MetaObject object = SystemMetaObject.forObject(new Author());
object.setValue("email", "test");
assertEquals("test", object.getValue("email"));
}
@Test
void shouldVerifyPropertyTypes() {
MetaObject object = SystemMetaObject.forObject(new Author());
assertEquals(6, object.getSetterNames().length);
assertEquals(int.class, object.getGetterType("id"));
assertEquals(String.class, object.getGetterType("username"));
assertEquals(String.class, object.getGetterType("password"));
assertEquals(String.class, object.getGetterType("email"));
assertEquals(String.class, object.getGetterType("bio"));
assertEquals(Section.class, object.getGetterType("favouriteSection"));
}
@Test
void shouldDemonstrateDeeplyNestedMapProperties() {
HashMap<String, String> map = new HashMap<>();
MetaObject metaMap = SystemMetaObject.forObject(map);
assertTrue(metaMap.hasSetter("id"));
assertTrue(metaMap.hasSetter("name.first"));
assertTrue(metaMap.hasSetter("address.street"));
assertFalse(metaMap.hasGetter("id"));
assertFalse(metaMap.hasGetter("name.first"));
assertFalse(metaMap.hasGetter("address.street"));
metaMap.setValue("id", "100");
metaMap.setValue("name.first", "Clinton");
metaMap.setValue("name.last", "Begin");
metaMap.setValue("address.street", "1 Some Street");
metaMap.setValue("address.city", "This City");
metaMap.setValue("address.province", "A Province");
metaMap.setValue("address.postal_code", "1A3 4B6");
assertTrue(metaMap.hasGetter("id"));
assertTrue(metaMap.hasGetter("name.first"));
assertTrue(metaMap.hasGetter("address.street"));
assertEquals(3, metaMap.getGetterNames().length);
assertEquals(3, metaMap.getSetterNames().length);
@SuppressWarnings("unchecked")
Map<String, String> name = (Map<String, String>) metaMap.getValue("name");
@SuppressWarnings("unchecked")
Map<String, String> address = (Map<String, String>) metaMap.getValue("address");
assertEquals("Clinton", name.get("first"));
assertEquals("1 Some Street", address.get("street"));
}
@Test
void shouldDemonstrateNullValueInMap() {
HashMap<String, String> map = new HashMap<>();
MetaObject metaMap = SystemMetaObject.forObject(map);
assertFalse(metaMap.hasGetter("phone.home"));
metaMap.setValue("phone", null);
assertTrue(metaMap.hasGetter("phone"));
// hasGetter returns true if the parent exists and is null.
assertTrue(metaMap.hasGetter("phone.home"));
assertTrue(metaMap.hasGetter("phone.home.ext"));
assertNull(metaMap.getValue("phone"));
assertNull(metaMap.getValue("phone.home"));
assertNull(metaMap.getValue("phone.home.ext"));
metaMap.setValue("phone.office", "789");
assertFalse(metaMap.hasGetter("phone.home"));
assertFalse(metaMap.hasGetter("phone.home.ext"));
assertEquals("789", metaMap.getValue("phone.office"));
assertNotNull(metaMap.getValue("phone"));
assertNull(metaMap.getValue("phone.home"));
}
@Test
void shouldNotUseObjectWrapperFactoryByDefault() {
MetaObject meta = SystemMetaObject.forObject(new Author());
assertNotEquals(CustomBeanWrapper.class, meta.getObjectWrapper().getClass());
}
@Test
void shouldUseObjectWrapperFactoryWhenSet() {
MetaObject meta = MetaObject.forObject(new Author(), SystemMetaObject.DEFAULT_OBJECT_FACTORY,
new CustomBeanWrapperFactory(), new DefaultReflectorFactory());
assertEquals(CustomBeanWrapper.class, meta.getObjectWrapper().getClass());
// Make sure the old default factory is in place and still works
meta = SystemMetaObject.forObject(new Author());
assertNotEquals(CustomBeanWrapper.class, meta.getObjectWrapper().getClass());
}
@Test
void shouldMethodHasGetterReturnTrueWhenListElementSet() {
List<Object> param1 = new ArrayList<>();
param1.add("firstParam");
param1.add(222);
param1.add(new Date());
Map<String, Object> parametersEmulation = new HashMap<>();
parametersEmulation.put("param1", param1);
parametersEmulation.put("filterParams", param1);
MetaObject meta = SystemMetaObject.forObject(parametersEmulation);
assertEquals(param1.get(0), meta.getValue("filterParams[0]"));
assertEquals(param1.get(1), meta.getValue("filterParams[1]"));
assertEquals(param1.get(2), meta.getValue("filterParams[2]"));
assertTrue(meta.hasGetter("filterParams[0]"));
assertTrue(meta.hasGetter("filterParams[1]"));
assertTrue(meta.hasGetter("filterParams[2]"));
}
}