1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.beanutils2;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertNotNull;
21 import static org.junit.jupiter.api.Assertions.assertNull;
22 import static org.junit.jupiter.api.Assertions.assertThrows;
23
24 import java.beans.IntrospectionException;
25
26 import org.junit.jupiter.api.AfterEach;
27 import org.junit.jupiter.api.BeforeEach;
28 import org.junit.jupiter.api.Test;
29
30
31
32
33
34
35 public class MappedPropertyTest {
36
37
38
39
40 @BeforeEach
41 public void setUp() throws Exception {
42 }
43
44
45
46
47 @AfterEach
48 public void tearDown() {
49 }
50
51
52
53
54 @Test
55 public void testAnyArgsProperty() throws Exception {
56 final String property = "anyMapped";
57 final Class<?> clazz = MappedPropertyTestBean.class;
58 final MappedPropertyDescriptor desc = new MappedPropertyDescriptor(property, clazz);
59 assertNull(desc.getMappedReadMethod(), "Getter is found");
60 assertNotNull(desc.getMappedWriteMethod(), "Setter is missing");
61 }
62
63
64
65
66 @Test
67 public void testBooleanMapped() throws Exception {
68 final String property = "mappedBoolean";
69 final Class<?> clazz = MappedPropertyTestBean.class;
70 final MappedPropertyDescriptor desc = new MappedPropertyDescriptor(property, clazz);
71 assertNotNull(desc.getMappedReadMethod(), "Getter is missing");
72 assertNotNull(desc.getMappedWriteMethod(), "Setter is missing");
73 }
74
75
76
77
78 @Test
79 public void testChildInterfaceMapped() throws Exception {
80 final String property = "mapproperty";
81 final Class<?> clazz = MappedPropertyChildInterface.class;
82 final MappedPropertyDescriptor desc = new MappedPropertyDescriptor(property, clazz);
83 assertNotNull(desc.getMappedReadMethod(), "Getter is missing");
84 assertNotNull(desc.getMappedWriteMethod(), "Setter is missing");
85 }
86
87
88
89
90
91
92
93 @Test
94 public void testDifferentTypes() throws Exception {
95 final String property = "differentTypes";
96 final Class<?> clazz = MappedPropertyTestBean.class;
97 final MappedPropertyDescriptor desc = new MappedPropertyDescriptor(property, clazz);
98 assertNotNull(desc.getMappedReadMethod(), "Getter is missing");
99 assertNull(desc.getMappedWriteMethod(), "Setter is found");
100 }
101
102
103
104
105 @Test
106 public void testFound() throws Exception {
107 final String property = "mapproperty";
108 final Class<?> clazz = MappedPropertyTestBean.class;
109 final MappedPropertyDescriptor desc = new MappedPropertyDescriptor(property, clazz);
110 assertNotNull(desc.getMappedReadMethod(), "Getter is missing");
111 assertNotNull(desc.getMappedWriteMethod(), "Setter is missing");
112 }
113
114
115
116
117 @Test
118 public void testInterfaceMapped() throws Exception {
119 final String property = "mapproperty";
120 final Class<?> clazz = MappedPropertyTestInterface.class;
121 final MappedPropertyDescriptor desc = new MappedPropertyDescriptor(property, clazz);
122 assertNotNull(desc.getMappedReadMethod(), "Getter is missing");
123 assertNotNull(desc.getMappedWriteMethod(), "Setter is missing");
124 }
125
126
127
128
129 @Test
130 public void testInterfaceNotFound() {
131 final String property = "XXXXXX";
132 final Class<?> clazz = MappedPropertyTestInterface.class;
133 assertThrows(IntrospectionException.class, () -> new MappedPropertyDescriptor(property, clazz));
134 }
135
136
137
138
139 @Test
140 public void testInvalidGetter() throws Exception {
141 final String property = "invalidGetter";
142 final Class<?> clazz = MappedPropertyTestBean.class;
143 final MappedPropertyDescriptor desc = new MappedPropertyDescriptor(property, clazz);
144 assertNull(desc.getMappedReadMethod(), "Getter is found");
145 assertNotNull(desc.getMappedWriteMethod(), "Setter is missing");
146 }
147
148
149
150
151 @Test
152 public void testInvalidSetter() throws Exception {
153 final String property = "invalidSetter";
154 final Class<?> clazz = MappedPropertyTestBean.class;
155 final MappedPropertyDescriptor desc = new MappedPropertyDescriptor(property, clazz);
156 assertNotNull(desc.getMappedReadMethod(), "Getter is missing");
157 assertNull(desc.getMappedWriteMethod(), "Setter is found");
158 }
159
160
161
162
163 @Test
164 public void testMapGetter() throws Exception {
165 final MappedPropertyTestBean bean = new MappedPropertyTestBean();
166 final String testValue = "test value";
167 final String testKey = "testKey";
168 BeanUtils.setProperty(bean, "myMap(" + testKey + ")", "test value");
169 assertEquals(testValue, bean.getMyMap().get(testKey), "Map getter");
170 }
171
172
173
174
175 @Test
176 public void testMappedGetterOnly() throws Exception {
177 final String property = "mappedGetterOnly";
178 final Class<?> clazz = MappedPropertyTestBean.class;
179 final MappedPropertyDescriptor desc = new MappedPropertyDescriptor(property, clazz);
180 assertNotNull(desc.getMappedReadMethod(), "Getter is missing");
181 assertNull(desc.getMappedWriteMethod(), "Setter is found");
182 }
183
184
185
186
187 @Test
188 public void testMappedSetterOnly() throws Exception {
189 final String property = "mappedSetterOnly";
190 final Class<?> clazz = MappedPropertyTestBean.class;
191 final MappedPropertyDescriptor desc = new MappedPropertyDescriptor(property, clazz);
192 assertNull(desc.getMappedReadMethod(), "Getter is found");
193 assertNotNull(desc.getMappedWriteMethod(), "Setter is missing");
194 }
195
196
197
198
199 @Test
200 public void testNotFound() {
201 final String property = "xxxxxxx";
202 final Class<?> clazz = MappedPropertyTestBean.class;
203 assertThrows(IntrospectionException.class, () -> new MappedPropertyDescriptor(property, clazz));
204 }
205
206
207
208
209 @Test
210 public void testPrimitiveArgsProperty() throws Exception {
211 final String property = "mappedPrimitive";
212 final Class<?> clazz = MappedPropertyTestBean.class;
213 final MappedPropertyDescriptor desc = new MappedPropertyDescriptor(property, clazz);
214 assertNull(desc.getMappedReadMethod(), "Getter is found");
215 assertNotNull(desc.getMappedWriteMethod(), "Setter is missing");
216 }
217
218
219
220
221 @Test
222 public void testProtected() {
223 final String property = "protectedProperty";
224 final Class<?> clazz = MappedPropertyTestBean.class;
225 assertThrows(IntrospectionException.class, () -> new MappedPropertyDescriptor(property, clazz));
226 }
227
228
229
230
231 @Test
232 public void testProtectedParentMethod() {
233 final String property = "protectedMapped";
234 final Class<?> clazz = MappedPropertyChildBean.class;
235 assertThrows(IntrospectionException.class, () -> new MappedPropertyDescriptor(property, clazz));
236 }
237
238
239
240
241 @Test
242 public void testPublicParentMethod() throws Exception {
243 final String property = "mapproperty";
244 final Class<?> clazz = MappedPropertyChildBean.class;
245 final MappedPropertyDescriptor desc = new MappedPropertyDescriptor(property, clazz);
246 assertNotNull(desc.getMappedReadMethod(), "Getter is missing");
247 assertNotNull(desc.getMappedWriteMethod(), "Setter is missing");
248 }
249 }