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