1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.beanutils;
18
19 import junit.framework.Test;
20 import junit.framework.TestCase;
21 import junit.framework.TestSuite;
22
23
24
25
26
27
28 public class MappedPropertyTestCase extends TestCase {
29
30
31
32
33
34
35
36
37 public MappedPropertyTestCase(final String name) {
38 super(name);
39 }
40
41
42
43
44
45
46 public static void main(final String[] args) {
47 junit.textui.TestRunner.run(suite());
48 }
49
50
51
52
53 @Override
54 public void setUp() throws Exception {
55 }
56
57
58
59
60 public static Test suite() {
61 return (new TestSuite(MappedPropertyTestCase.class));
62 }
63
64
65
66
67 @Override
68 public void tearDown() {
69 }
70
71
72
73
74
75
76 public void testFound() {
77 final String property = "mapproperty";
78 final Class<?> clazz = MappedPropertyTestBean.class;
79 try {
80 final MappedPropertyDescriptor desc
81 = new MappedPropertyDescriptor(property, clazz);
82 assertNotNull("Getter is missing", desc.getMappedReadMethod());
83 assertNotNull("Setter is missing", desc.getMappedWriteMethod());
84 } catch (final Exception ex) {
85 fail("Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex);
86 }
87 }
88
89
90
91
92 public void testBooleanMapped() {
93 final String property = "mappedBoolean";
94 final Class<?> clazz = MappedPropertyTestBean.class;
95 try {
96 final MappedPropertyDescriptor desc
97 = new MappedPropertyDescriptor(property, clazz);
98 assertNotNull("Getter is missing", desc.getMappedReadMethod());
99 assertNotNull("Setter is missing", desc.getMappedWriteMethod());
100 } catch (final Exception ex) {
101 fail("Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex);
102 }
103 }
104
105
106
107
108 public void testNotFound() {
109 final String property = "xxxxxxx";
110 final Class<?> clazz = MappedPropertyTestBean.class;
111 try {
112 new MappedPropertyDescriptor(property, clazz);
113 fail("Property '" + property + "' found in " + clazz.getName());
114 } catch (final Exception ex) {
115
116 }
117 }
118
119
120
121
122 public void testMappedGetterOnly() {
123 final String property = "mappedGetterOnly";
124 final Class<?> clazz = MappedPropertyTestBean.class;
125 try {
126 final MappedPropertyDescriptor desc
127 = new MappedPropertyDescriptor(property, clazz);
128 assertNotNull("Getter is missing", desc.getMappedReadMethod());
129 assertNull("Setter is found", desc.getMappedWriteMethod());
130 } catch (final Exception ex) {
131 fail("Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex);
132 }
133 }
134
135
136
137
138 public void testMappedSetterOnly() {
139 final String property = "mappedSetterOnly";
140 final Class<?> clazz = MappedPropertyTestBean.class;
141 try {
142 final MappedPropertyDescriptor desc
143 = new MappedPropertyDescriptor(property, clazz);
144 assertNull("Getter is found", desc.getMappedReadMethod());
145 assertNotNull("Setter is missing", desc.getMappedWriteMethod());
146 } catch (final Exception ex) {
147 fail("Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex);
148 }
149 }
150
151
152
153
154 public void testInvalidSetter() {
155 final String property = "invalidSetter";
156 final Class<?> clazz = MappedPropertyTestBean.class;
157 try {
158 final MappedPropertyDescriptor desc
159 = new MappedPropertyDescriptor(property, clazz);
160 assertNotNull("Getter is missing", desc.getMappedReadMethod());
161 assertNull("Setter is found", desc.getMappedWriteMethod());
162 } catch (final Exception ex) {
163 fail("Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex);
164 }
165 }
166
167
168
169
170 public void testInvalidGetter() {
171 final String property = "invalidGetter";
172 final Class<?> clazz = MappedPropertyTestBean.class;
173 try {
174 final MappedPropertyDescriptor desc
175 = new MappedPropertyDescriptor(property, clazz);
176 assertNull("Getter is found", desc.getMappedReadMethod());
177 assertNotNull("Setter is missing", desc.getMappedWriteMethod());
178 } catch (final Exception ex) {
179 fail("Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex);
180 }
181 }
182
183
184
185
186
187
188
189
190 public void testDifferentTypes() {
191 final String property = "differentTypes";
192 final Class<?> clazz = MappedPropertyTestBean.class;
193 try {
194 final MappedPropertyDescriptor desc
195 = new MappedPropertyDescriptor(property, clazz);
196 assertNotNull("Getter is missing", desc.getMappedReadMethod());
197 assertNull("Setter is found", desc.getMappedWriteMethod());
198 } catch (final Exception ex) {
199 fail("Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex);
200 }
201 }
202
203
204
205
206 public void testMapGetter() {
207 final MappedPropertyTestBean bean = new MappedPropertyTestBean();
208 try {
209 final String testValue = "test value";
210 final String testKey = "testKey";
211 BeanUtils.setProperty(bean, "myMap("+testKey+")", "test value");
212 assertEquals("Map getter", testValue, bean.getMyMap().get(testKey));
213 } catch (final Exception ex) {
214 fail("Test set mapped property failed: " + ex);
215 }
216 }
217
218
219
220
221
222 public void testAnyArgsProperty() {
223 final String property = "anyMapped";
224 final Class<?> clazz = MappedPropertyTestBean.class;
225 try {
226 final MappedPropertyDescriptor desc
227 = new MappedPropertyDescriptor(property, clazz);
228 assertNull("Getter is found", desc.getMappedReadMethod());
229 assertNotNull("Setter is missing", desc.getMappedWriteMethod());
230 } catch (final Exception ex) {
231 fail("Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex);
232 }
233 }
234
235
236
237
238 public void testPrimitiveArgsProperty() {
239 final String property = "mappedPrimitive";
240 final Class<?> clazz = MappedPropertyTestBean.class;
241 try {
242 final MappedPropertyDescriptor desc
243 = new MappedPropertyDescriptor(property, clazz);
244 assertNull("Getter is found", desc.getMappedReadMethod());
245 assertNotNull("Setter is missing", desc.getMappedWriteMethod());
246 } catch (final Exception ex) {
247 fail("Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex);
248 }
249 }
250
251
252
253
254 public void testProtected() {
255 final String property = "protectedProperty";
256 final Class<?> clazz = MappedPropertyTestBean.class;
257 try {
258 new MappedPropertyDescriptor(property, clazz);
259 fail("Property '" + property + "' found in " + clazz.getName());
260 } catch (final Exception ex) {
261
262 }
263 }
264
265
266
267
268
269 public void testPublicParentMethod() {
270 final String property = "mapproperty";
271 final Class<?> clazz = MappedPropertyChildBean.class;
272 try {
273 final MappedPropertyDescriptor desc
274 = new MappedPropertyDescriptor(property, clazz);
275 assertNotNull("Getter is missing", desc.getMappedReadMethod());
276 assertNotNull("Setter is missing", desc.getMappedWriteMethod());
277 } catch (final Exception ex) {
278 fail("Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex);
279 }
280 }
281
282
283
284
285 public void testProtectedParentMethod() {
286 final String property = "protectedMapped";
287 final Class<?> clazz = MappedPropertyChildBean.class;
288 try {
289 new MappedPropertyDescriptor(property, clazz);
290 fail("Property '" + property + "' found in " + clazz.getName());
291 } catch (final Exception ex) {
292 }
293 }
294
295
296
297
298
299 public void testInterfaceMapped() {
300 final String property = "mapproperty";
301 final Class<?> clazz = MappedPropertyTestInterface.class;
302 try {
303 final MappedPropertyDescriptor desc
304 = new MappedPropertyDescriptor(property, clazz);
305 assertNotNull("Getter is missing", desc.getMappedReadMethod());
306 assertNotNull("Setter is missing", desc.getMappedWriteMethod());
307 } catch (final Exception ex) {
308 fail("Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex);
309 }
310 }
311
312
313
314
315 public void testInterfaceNotFound() {
316 final String property = "XXXXXX";
317 final Class<?> clazz = MappedPropertyTestInterface.class;
318 try {
319 new MappedPropertyDescriptor(property, clazz);
320 fail("Property '" + property + "' found in " + clazz.getName());
321 } catch (final Exception ex) {
322 }
323 }
324
325
326
327
328 public void testChildInterfaceMapped() {
329 final String property = "mapproperty";
330 final Class<?> clazz = MappedPropertyChildInterface.class;
331 try {
332 final MappedPropertyDescriptor desc
333 = new MappedPropertyDescriptor(property, clazz);
334 assertNotNull("Getter is missing", desc.getMappedReadMethod());
335 assertNotNull("Setter is missing", desc.getMappedWriteMethod());
336 } catch (final Exception ex) {
337 fail("Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex);
338 }
339 }
340 }