forked from mybatis/mybatis-3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReflectorTest.java
324 lines (274 loc) · 11.6 KB
/
ReflectorTest.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/**
* Copyright 2009-2020 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
*
* http://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 com.googlecode.catchexception.apis.BDDCatchException.*;
import static org.assertj.core.api.BDDAssertions.then;
import static org.junit.jupiter.api.Assertions.*;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import org.apache.ibatis.reflection.invoker.Invoker;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
class ReflectorTest {
@Test
void testGetSetterType() {
ReflectorFactory reflectorFactory = new DefaultReflectorFactory();
Reflector reflector = reflectorFactory.findForClass(Section.class);
Assertions.assertEquals(Long.class, reflector.getSetterType("id"));
}
@Test
void testGetGetterType() {
ReflectorFactory reflectorFactory = new DefaultReflectorFactory();
Reflector reflector = reflectorFactory.findForClass(Section.class);
Assertions.assertEquals(Long.class, reflector.getGetterType("id"));
}
@Test
void shouldNotGetClass() {
ReflectorFactory reflectorFactory = new DefaultReflectorFactory();
Reflector reflector = reflectorFactory.findForClass(Section.class);
Assertions.assertFalse(reflector.hasGetter("class"));
}
interface Entity<T> {
T getId();
void setId(T id);
}
static abstract class AbstractEntity implements Entity<Long> {
private Long id;
@Override
public Long getId() {
return id;
}
@Override
public void setId(Long id) {
this.id = id;
}
}
static class Section extends AbstractEntity implements Entity<Long> {
}
@Test
void shouldResolveSetterParam() {
ReflectorFactory reflectorFactory = new DefaultReflectorFactory();
Reflector reflector = reflectorFactory.findForClass(Child.class);
assertEquals(String.class, reflector.getSetterType("id"));
}
@Test
void shouldResolveParameterizedSetterParam() {
ReflectorFactory reflectorFactory = new DefaultReflectorFactory();
Reflector reflector = reflectorFactory.findForClass(Child.class);
assertEquals(List.class, reflector.getSetterType("list"));
}
@Test
void shouldResolveArraySetterParam() {
ReflectorFactory reflectorFactory = new DefaultReflectorFactory();
Reflector reflector = reflectorFactory.findForClass(Child.class);
Class<?> clazz = reflector.getSetterType("array");
assertTrue(clazz.isArray());
assertEquals(String.class, clazz.getComponentType());
}
@Test
void shouldResolveGetterType() {
ReflectorFactory reflectorFactory = new DefaultReflectorFactory();
Reflector reflector = reflectorFactory.findForClass(Child.class);
assertEquals(String.class, reflector.getGetterType("id"));
}
@Test
void shouldResolveSetterTypeFromPrivateField() {
ReflectorFactory reflectorFactory = new DefaultReflectorFactory();
Reflector reflector = reflectorFactory.findForClass(Child.class);
assertEquals(String.class, reflector.getSetterType("fld"));
}
@Test
void shouldResolveGetterTypeFromPublicField() {
ReflectorFactory reflectorFactory = new DefaultReflectorFactory();
Reflector reflector = reflectorFactory.findForClass(Child.class);
assertEquals(String.class, reflector.getGetterType("pubFld"));
}
@Test
void shouldResolveParameterizedGetterType() {
ReflectorFactory reflectorFactory = new DefaultReflectorFactory();
Reflector reflector = reflectorFactory.findForClass(Child.class);
assertEquals(List.class, reflector.getGetterType("list"));
}
@Test
void shouldResolveArrayGetterType() {
ReflectorFactory reflectorFactory = new DefaultReflectorFactory();
Reflector reflector = reflectorFactory.findForClass(Child.class);
Class<?> clazz = reflector.getGetterType("array");
assertTrue(clazz.isArray());
assertEquals(String.class, clazz.getComponentType());
}
static abstract class Parent<T extends Serializable> {
protected T id;
protected List<T> list;
protected T[] array;
private T fld;
public T pubFld;
public T getId() {
return id;
}
public void setId(T id) {
this.id = id;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
public T[] getArray() {
return array;
}
public void setArray(T[] array) {
this.array = array;
}
public T getFld() {
return fld;
}
}
static class Child extends Parent<String> {
}
@Test
void shouldResoleveReadonlySetterWithOverload() {
class BeanClass implements BeanInterface<String> {
@Override
public void setId(String id) {
// Do nothing
}
}
ReflectorFactory reflectorFactory = new DefaultReflectorFactory();
Reflector reflector = reflectorFactory.findForClass(BeanClass.class);
assertEquals(String.class, reflector.getSetterType("id"));
}
interface BeanInterface<T> {
void setId(T id);
}
@Test
void shouldSettersWithUnrelatedArgTypesThrowException() throws Exception {
@SuppressWarnings("unused")
class BeanClass {
public void setProp1(String arg) {}
public void setProp2(String arg) {}
public void setProp2(Integer arg) {}
public void setProp2(boolean arg) {}
}
ReflectorFactory reflectorFactory = new DefaultReflectorFactory();
Reflector reflector = reflectorFactory.findForClass(BeanClass.class);
List<String> setableProps = Arrays.asList(reflector.getSetablePropertyNames());
assertTrue(setableProps.contains("prop1"));
assertTrue(setableProps.contains("prop2"));
assertEquals("prop1", reflector.findPropertyName("PROP1"));
assertEquals("prop2", reflector.findPropertyName("PROP2"));
assertEquals(String.class, reflector.getSetterType("prop1"));
assertNotNull(reflector.getSetInvoker("prop1"));
Class<?> paramType = reflector.getSetterType("prop2");
assertTrue(String.class.equals(paramType) || Integer.class.equals(paramType) || boolean.class.equals(paramType));
Invoker ambiguousInvoker = reflector.getSetInvoker("prop2");
Object[] param = String.class.equals(paramType)? new String[]{"x"} : new Integer[]{1};
when(() -> ambiguousInvoker.invoke(new BeanClass(), param));
then(caughtException()).isInstanceOf(ReflectionException.class)
.hasMessageMatching(
"Ambiguous setters defined for property 'prop2' in class '" + BeanClass.class.getName().replace("$", "\\$")
+ "' with types '(java.lang.String|java.lang.Integer|boolean)' and '(java.lang.String|java.lang.Integer|boolean)'\\.");
}
@Test
void shouldTwoGettersForNonBooleanPropertyThrowException() throws Exception {
@SuppressWarnings("unused")
class BeanClass {
public Integer getProp1() {return 1;}
public int getProp2() {return 0;}
public int isProp2() {return 0;}
}
ReflectorFactory reflectorFactory = new DefaultReflectorFactory();
Reflector reflector = reflectorFactory.findForClass(BeanClass.class);
List<String> getableProps = Arrays.asList(reflector.getGetablePropertyNames());
assertTrue(getableProps.contains("prop1"));
assertTrue(getableProps.contains("prop2"));
assertEquals("prop1", reflector.findPropertyName("PROP1"));
assertEquals("prop2", reflector.findPropertyName("PROP2"));
assertEquals(Integer.class, reflector.getGetterType("prop1"));
Invoker getInvoker = reflector.getGetInvoker("prop1");
assertEquals(Integer.valueOf(1), getInvoker.invoke(new BeanClass(), null));
Class<?> paramType = reflector.getGetterType("prop2");
assertEquals(int.class, paramType);
Invoker ambiguousInvoker = reflector.getGetInvoker("prop2");
when(() -> ambiguousInvoker.invoke(new BeanClass(), new Integer[] {1}));
then(caughtException()).isInstanceOf(ReflectionException.class)
.hasMessageContaining("Illegal overloaded getter method with ambiguous type for property 'prop2' in class '"
+ BeanClass.class.getName()
+ "'. This breaks the JavaBeans specification and can cause unpredictable results.");
}
@Test
void shouldTwoGettersWithDifferentTypesThrowException() throws Exception {
@SuppressWarnings("unused")
class BeanClass {
public Integer getProp1() {return 1;}
public Integer getProp2() {return 1;}
public boolean isProp2() {return false;}
}
ReflectorFactory reflectorFactory = new DefaultReflectorFactory();
Reflector reflector = reflectorFactory.findForClass(BeanClass.class);
List<String> getableProps = Arrays.asList(reflector.getGetablePropertyNames());
assertTrue(getableProps.contains("prop1"));
assertTrue(getableProps.contains("prop2"));
assertEquals("prop1", reflector.findPropertyName("PROP1"));
assertEquals("prop2", reflector.findPropertyName("PROP2"));
assertEquals(Integer.class, reflector.getGetterType("prop1"));
Invoker getInvoker = reflector.getGetInvoker("prop1");
assertEquals(Integer.valueOf(1), getInvoker.invoke(new BeanClass(), null));
Class<?> returnType = reflector.getGetterType("prop2");
assertTrue(Integer.class.equals(returnType) || boolean.class.equals(returnType));
Invoker ambiguousInvoker = reflector.getGetInvoker("prop2");
when(() -> ambiguousInvoker.invoke(new BeanClass(), null));
then(caughtException()).isInstanceOf(ReflectionException.class)
.hasMessageContaining("Illegal overloaded getter method with ambiguous type for property 'prop2' in class '"
+ BeanClass.class.getName()
+ "'. This breaks the JavaBeans specification and can cause unpredictable results.");
}
@Test
void shouldAllowTwoBooleanGetters() throws Exception {
@SuppressWarnings("unused")
class Bean {
// JavaBean Spec allows this (see #906)
public boolean isBool() {return true;}
public boolean getBool() {return false;}
public void setBool(boolean bool) {}
}
ReflectorFactory reflectorFactory = new DefaultReflectorFactory();
Reflector reflector = reflectorFactory.findForClass(Bean.class);
assertTrue((Boolean)reflector.getGetInvoker("bool").invoke(new Bean(), new Byte[0]));
}
@Test
void shouldIgnoreBestMatchSetterIfGetterIsAmbiguous() throws Exception {
@SuppressWarnings("unused")
class Bean {
public Integer isBool() {return Integer.valueOf(1);}
public Integer getBool() {return Integer.valueOf(2);}
public void setBool(boolean bool) {}
public void setBool(Integer bool) {}
}
ReflectorFactory reflectorFactory = new DefaultReflectorFactory();
Reflector reflector = reflectorFactory.findForClass(Bean.class);
Class<?> paramType = reflector.getSetterType("bool");
Object[] param = boolean.class.equals(paramType) ? new Boolean[] { true } : new Integer[] { 1 };
Invoker ambiguousInvoker = reflector.getSetInvoker("bool");
when(() -> ambiguousInvoker.invoke(new Bean(), param));
then(caughtException()).isInstanceOf(ReflectionException.class)
.hasMessageMatching(
"Ambiguous setters defined for property 'bool' in class '" + Bean.class.getName().replace("$", "\\$")
+ "' with types '(java.lang.Integer|boolean)' and '(java.lang.Integer|boolean)'\\.");
}
}