1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.ibatis.reflection.wrapper;
18
19 import static org.junit.jupiter.api.Assertions.*;
20
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.List;
24
25 import org.apache.ibatis.reflection.DefaultReflectorFactory;
26 import org.apache.ibatis.reflection.MetaObject;
27 import org.apache.ibatis.reflection.ReflectionException;
28 import org.apache.ibatis.reflection.factory.DefaultObjectFactory;
29 import org.junit.jupiter.api.Test;
30
31 class BeanWrapperTest {
32
33 @Test
34 void assertBasicOperations() {
35 Bean1 bean = new Bean1();
36 MetaObject metaObj = MetaObject.forObject(bean, new DefaultObjectFactory(), new DefaultObjectWrapperFactory(),
37 new DefaultReflectorFactory());
38 assertFalse(metaObj.isCollection());
39 assertTrue(metaObj.hasGetter("id"));
40 assertTrue(metaObj.hasSetter("id"));
41 assertTrue(metaObj.hasGetter("bean2.id"));
42 assertTrue(metaObj.hasSetter("bean2.id"));
43 assertEquals("id", metaObj.findProperty("id", false));
44 assertNull(metaObj.findProperty("attr_val", false));
45 assertEquals("attrVal", metaObj.findProperty("attr_val", true));
46 String[] getterNames = metaObj.getGetterNames();
47 Arrays.sort(getterNames);
48 assertArrayEquals(new String[] { "attrVal", "bean2", "bean2List", "id", "nums" }, getterNames);
49 String[] setterNames = metaObj.getSetterNames();
50 Arrays.sort(setterNames);
51 assertArrayEquals(new String[] { "attrVal", "bean2", "bean2List", "id", "nums" }, setterNames);
52 assertEquals(String.class, metaObj.getGetterType("attrVal"));
53 assertEquals(String.class, metaObj.getSetterType("attrVal"));
54 assertEquals(String.class, metaObj.getGetterType("bean2.name"));
55 assertEquals(String.class, metaObj.getSetterType("bean2.name"));
56
57 assertTrue(metaObj.hasGetter("bean2List[0]"));
58 try {
59 metaObj.getValue("bean2List[0]");
60 fail();
61 } catch (ReflectionException e) {
62 assertEquals("Cannot get the value 'bean2List[0]' because the property 'bean2List' is null.", e.getMessage());
63 }
64 try {
65 metaObj.setValue("bean2List[0]", new Bean2());
66 fail();
67 } catch (ReflectionException e) {
68 assertEquals("Cannot set the value 'bean2List[0]' because the property 'bean2List' is null.", e.getMessage());
69 }
70 assertTrue(metaObj.hasSetter("bean2List[0]"));
71
72 List<Bean2> bean2List = new ArrayList<>();
73 for (int i = 0; i < 3; i++) {
74 Bean2 bean2 = new Bean2();
75 bean2.setId(i);
76 bean2.setName("name_" + i);
77 bean2List.add(bean2);
78 }
79 bean.setBean2List(bean2List);
80
81 assertEquals(0, metaObj.getValue("bean2List[0].id"));
82
83 metaObj.setValue("attrVal", "value");
84 assertEquals("value", bean.getAttrVal());
85 try {
86 metaObj.getValue("attrVal[0]");
87 fail();
88 } catch (ReflectionException e) {
89 assertEquals("Cannot get the value 'attrVal[0]' because the property 'attrVal' is not Map, List or Array.",
90 e.getMessage());
91 }
92 try {
93 metaObj.setValue("attrVal[0]", "blur");
94 fail();
95 } catch (ReflectionException e) {
96 assertEquals("Cannot set the value 'attrVal[0]' because the property 'attrVal' is not Map, List or Array.",
97 e.getMessage());
98 }
99
100 metaObj.setValue("bean2List[1].name", "new name 1");
101 assertEquals("new name 1", bean.getBean2List().get(1).getName());
102
103 try {
104 metaObj.getValue("nums[0]");
105 fail();
106 } catch (ReflectionException e) {
107
108 }
109 metaObj.setValue("nums", new Integer[] { 5, 6, 7 });
110 assertTrue(metaObj.hasGetter("bean2List[0].child.id"));
111 assertTrue(metaObj.hasSetter("bean2List[0].child.id"));
112
113 {
114 Bean2 bean2 = new Bean2();
115 bean2.setId(100);
116 bean2.setName("name_100");
117 metaObj.setValue("bean2", bean2);
118 assertEquals(String.class, metaObj.getGetterType("bean2.name"));
119 assertEquals(String.class, metaObj.getSetterType("bean2.name"));
120 }
121
122 try {
123 metaObj.setValue("bean2.child.bean2", "bogus");
124 fail();
125 } catch (ReflectionException e) {
126
127 }
128
129 {
130 Bean2 bean2 = new Bean2();
131 bean2.setId(101);
132 bean2.setName("name_101");
133 metaObj.setValue("bean2.child.bean2", bean2);
134 assertEquals(101, bean.getBean2().getChild().getBean2().getId());
135 }
136
137 metaObj.setValue("bean2.child.nums", new Integer[] { 8, 9 });
138 metaObj.setValue("bean2.child.nums[0]", 88);
139 assertEquals(88, bean.getBean2().getChild().getNums()[0]);
140
141 assertFalse(metaObj.hasSetter("x[0].y"));
142 assertFalse(metaObj.hasGetter("x[0].y"));
143
144 try {
145 metaObj.getValue("x");
146 fail();
147 } catch (ReflectionException e) {
148
149 }
150
151
152
153
154 try {
155 metaObj.add("x");
156 fail();
157 } catch (UnsupportedOperationException e) {
158
159 }
160 try {
161 metaObj.addAll(Arrays.asList("x", "y"));
162 fail();
163 } catch (UnsupportedOperationException e) {
164
165 }
166 }
167
168 static class Bean1 {
169 private Integer id;
170 private String attrVal;
171 private Integer[] nums;
172 private Bean2 bean2;
173 private List<Bean2> bean2List;
174
175 public Integer getId() {
176 return id;
177 }
178
179 public void setId(Integer id) {
180 this.id = id;
181 }
182
183 public String getAttrVal() {
184 return attrVal;
185 }
186
187 public void setAttrVal(String attrVal) {
188 this.attrVal = attrVal;
189 }
190
191 public Integer[] getNums() {
192 return nums;
193 }
194
195 public void setNums(Integer[] nums) {
196 this.nums = nums;
197 }
198
199 public Bean2 getBean2() {
200 return bean2;
201 }
202
203 public void setBean2(Bean2 bean2) {
204 this.bean2 = bean2;
205 }
206
207 public List<Bean2> getBean2List() {
208 return bean2List;
209 }
210
211 public void setBean2List(List<Bean2> bean2List) {
212 this.bean2List = bean2List;
213 }
214 }
215
216 static class Bean2 {
217 private Integer id;
218 private String name;
219 private Bean1 child;
220
221 public Integer getId() {
222 return id;
223 }
224
225 public void setId(Integer id) {
226 this.id = id;
227 }
228
229 public String getName() {
230 return name;
231 }
232
233 public void setName(String name) {
234 this.name = name;
235 }
236
237 public Bean1 getChild() {
238 return child;
239 }
240
241 public void setChild(Bean1 child) {
242 this.child = child;
243 }
244 }
245 }