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