1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.beanutils.bugs;
18
19 import java.util.HashMap;
20 import java.util.Map;
21
22 import junit.framework.Test;
23 import junit.framework.TestCase;
24 import junit.framework.TestSuite;
25
26 import org.apache.commons.beanutils.BeanUtils;
27 import org.apache.commons.beanutils.PropertyUtils;
28 import org.apache.commons.beanutils.WrapDynaBean;
29 import org.apache.commons.beanutils.bugs.other.Jira61BeanFactory;
30 import org.apache.commons.beanutils.bugs.other.Jira61BeanFactory.TestBean;
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33
34 /***
35 * Test case for Jira issue# BEANUTILS-61.
36 * <p />
37 * See https://issues.apache.org/jira/browse/BEANUTILS-61
38 * <p />
39 *
40 * {@link WrapDynaBean} is a secial case for the PropertyUtils's
41 * isReadable() and isWriteable() methods - since the bean being
42 * wrapped may have read-only or write-only properties (unlike
43 * regular DynaBeans.
44 *
45 * @version $Revision: 553357 $ $Date: 2007-07-05 02:10:25 +0100 (Thu, 05 Jul 2007) $
46 */
47 public class Jira61TestCase extends TestCase {
48
49 private Log log = LogFactory.getLog(Jira61TestCase.class);
50 private Jira61BeanFactory.TestBean testBean;
51 private WrapDynaBean wrapDynaBean;
52
53 /***
54 * Create a test case with the specified name.
55 *
56 * @param name The name of the test
57 */
58 public Jira61TestCase(String name) {
59 super(name);
60 }
61
62 /***
63 * Run the Test.
64 *
65 * @param args Arguments
66 */
67 public static void main(String[] args) {
68 junit.textui.TestRunner.run(suite());
69 }
70
71 /***
72 * Create a test suite for this test.
73 *
74 * @return a test suite
75 */
76 public static Test suite() {
77 return (new TestSuite(Jira61TestCase.class));
78 }
79
80 /***
81 * Set up.
82 *
83 * @throws java.lang.Exception
84 */
85 protected void setUp() throws Exception {
86 super.setUp();
87 testBean = Jira61BeanFactory.createBean();
88 PropertyUtils.getPropertyDescriptor(testBean, "mappedReadOnly");
89 PropertyUtils.getPropertyDescriptor(testBean, "mappedWriteOnly");
90 wrapDynaBean = new WrapDynaBean(testBean);
91 }
92
93 /***
94 * Tear Down.
95 *
96 * @throws java.lang.Exception
97 */
98 protected void tearDown() throws Exception {
99 super.tearDown();
100 }
101
102 /***
103 * Test {@link PropertyUtils#isReadable(Object, String)}
104 * for simple properties.
105 */
106 public void testIssue_BEANUTILS_61_PropertyUtils_isReadable() {
107 boolean result = false;
108
109 try {
110 result = PropertyUtils.isReadable(wrapDynaBean, "simpleReadOnly");
111 } catch (Throwable t) {
112 log.error("ERROR " + t, t);
113 fail("simpleReadOnly Threw exception: " + t);
114 }
115 assertTrue("PropertyUtils.isReadable(bean, \"simpleReadOnly\") returned " + result, result);
116
117 try {
118 result = PropertyUtils.isReadable(wrapDynaBean, "simpleWriteOnly");
119 } catch (Throwable t) {
120 log.error("ERROR " + t, t);
121 fail("simpleWriteOnly Threw exception: " + t);
122 }
123 assertFalse("PropertyUtils.isReadable(bean, \"simpleWriteOnly\") returned " + result, result);
124 }
125
126 /***
127 * Test {@link PropertyUtils#isWriteable(Object, String)}
128 * for simple properties.
129 */
130 public void testIssue_BEANUTILS_61_PropertyUtils_isWriteable() {
131 boolean result = false;
132
133 try {
134 result = PropertyUtils.isWriteable(wrapDynaBean, "simpleReadOnly");
135 } catch (Throwable t) {
136 log.error("ERROR " + t, t);
137 fail("simpleReadOnly Threw exception: " + t);
138 }
139 assertFalse("PropertyUtils.isWriteable(bean, \"simpleReadOnly\") returned " + result, result);
140
141 try {
142 result = PropertyUtils.isWriteable(wrapDynaBean, "simpleWriteOnly");
143 } catch (Throwable t) {
144 log.error("ERROR " + t, t);
145 fail("simpleWriteOnly Threw exception: " + t);
146 }
147 assertTrue("PropertyUtils.isWriteable(bean, \"simpleWriteOnly\") returned " + result, result);
148 }
149
150 /***
151 * Test {@link PropertyUtils#isReadable(Object, String)}
152 * for indexed properties.
153 */
154 public void testIssue_BEANUTILS_61_PropertyUtils_isReadable_Indexed() {
155 boolean result = false;
156
157 try {
158 result = PropertyUtils.isReadable(wrapDynaBean, "indexedReadOnly");
159 } catch (Throwable t) {
160 log.error("ERROR " + t, t);
161 fail("indexedReadOnly Threw exception: " + t);
162 }
163 assertTrue("PropertyUtils.isReadable(bean, \"indexedReadOnly\") returned " + result, result);
164
165 try {
166 result = PropertyUtils.isReadable(wrapDynaBean, "indexedWriteOnly");
167 } catch (Throwable t) {
168 log.error("ERROR " + t, t);
169 fail("indexedWriteOnly Threw exception: " + t);
170 }
171 assertFalse("PropertyUtils.isReadable(bean, \"indexedWriteOnly\") returned " + result, result);
172 }
173
174 /***
175 * Test {@link PropertyUtils#isReadable(Object, String)}
176 * for mapped properties.
177 */
178 public void testIssue_BEANUTILS_61_PropertyUtils_isReadable_Mapped() {
179 boolean result = false;
180
181 try {
182 result = PropertyUtils.isReadable(wrapDynaBean, "mappedReadOnly");
183 } catch (Throwable t) {
184 log.error("ERROR " + t, t);
185 fail("mappedReadOnly Threw exception: " + t);
186 }
187 assertTrue("PropertyUtils.isReadable(bean, \"mappedReadOnly\") returned " + result, result);
188
189 try {
190 result = PropertyUtils.isReadable(wrapDynaBean, "mappedWriteOnly");
191 } catch (Throwable t) {
192 log.error("ERROR " + t, t);
193 fail("mappedWriteOnly Threw exception: " + t);
194 }
195 assertFalse("PropertyUtils.isReadable(bean, \"mappedWriteOnly\") returned " + result, result);
196 }
197
198 /***
199 * Test {@link PropertyUtils#isWriteable(Object, String)}
200 * for indexed properties.
201 */
202 public void testIssue_BEANUTILS_61_PropertyUtils_isWriteable_Indexed() {
203 boolean result = false;
204
205 try {
206 result = PropertyUtils.isWriteable(wrapDynaBean, "indexedReadOnly");
207 } catch (Throwable t) {
208 log.error("ERROR " + t, t);
209 fail("indexedReadOnly Threw exception: " + t);
210 }
211 assertFalse("PropertyUtils.isWriteable(bean, \"indexedReadOnly\") returned " + result, result);
212
213 try {
214 result = PropertyUtils.isWriteable(wrapDynaBean, "indexedWriteOnly");
215 } catch (Throwable t) {
216 log.error("ERROR " + t, t);
217 fail("indexedWriteOnly Threw exception: " + t);
218 }
219 assertTrue("PropertyUtils.isWriteable(bean, \"indexedWriteOnly\") returned " + result, result);
220 }
221
222 /***
223 * Test {@link PropertyUtils#isWriteable(Object, String)}
224 * for mapped properties.
225 */
226 public void testIssue_BEANUTILS_61_PropertyUtils_isWriteable_Mapped() {
227 boolean result = false;
228
229 try {
230 result = PropertyUtils.isWriteable(wrapDynaBean, "mappedReadOnly");
231 } catch (Throwable t) {
232 log.error("ERROR " + t, t);
233 fail("mappedReadOnly Threw exception: " + t);
234 }
235 assertFalse("PropertyUtils.isWriteable(bean, \"mappedReadOnly\") returned " + result, result);
236
237 try {
238 result = PropertyUtils.isWriteable(wrapDynaBean, "mappedWriteOnly");
239 } catch (Throwable t) {
240 log.error("ERROR " + t, t);
241 fail("mappedWriteOnly Threw exception: " + t);
242 }
243 assertTrue("PropertyUtils.isWriteable(bean, \"mappedWriteOnly\") returned " + result, result);
244 }
245
246 /***
247 * Test {@link PropertyUtils#getProperty(Object, String)}
248 * for simple properties.
249 */
250 public void testIssue_BEANUTILS_61_PropertyUtils_getProperty() {
251 boolean threwIllegalArgumentException = false;
252 Object result = null;
253 try {
254 result = PropertyUtils.getProperty(wrapDynaBean, "simpleReadOnly");
255 } catch (Throwable t) {
256 log.error("ERROR " + t, t);
257 fail("simpleWriteOnly Threw exception: " + t);
258 }
259 assertEquals("simpleReadOnly", testBean.getSimpleReadOnly(), result);
260
261 try {
262 result = PropertyUtils.getProperty(wrapDynaBean, "simpleWriteOnly");
263 } catch (IllegalArgumentException ex) {
264 threwIllegalArgumentException = true;
265 } catch (Throwable t) {
266 log.error("ERROR " + t, t);
267 fail("simpleWriteOnly Threw exception: " + t);
268 }
269 assertTrue("Expected IllegalArgumentException but returned '" + result + "'", threwIllegalArgumentException);
270 }
271
272 /***
273 * Test {@link PropertyUtils#setProperty(Object, String, Object)}
274 * for simple properties.
275 */
276 public void testIssue_BEANUTILS_61_PropertyUtils_setProperty() {
277 boolean threwIllegalArgumentException = false;
278 try {
279 PropertyUtils.setProperty(wrapDynaBean, "simpleReadOnly", "READONLY-SIMPLE-BAR");
280 } catch (IllegalArgumentException ex) {
281 threwIllegalArgumentException = true;
282 } catch (Throwable t) {
283 log.error("ERROR " + t, t);
284 fail("simpleReadOnly Threw exception: " + t);
285 }
286 assertTrue("Expected IllegalArgumentException", threwIllegalArgumentException);
287
288 try {
289 PropertyUtils.setProperty(wrapDynaBean, "simpleWriteOnly", "SIMPLE-BAR");
290 } catch (Throwable t) {
291 log.error("ERROR " + t, t);
292 fail("simpleWriteOnly Threw exception: " + t);
293 }
294 assertEquals("simpleWriteOnly", testBean.getSimpleReadOnly(), "SIMPLE-BAR");
295 }
296
297 /***
298 * Test {@link PropertyUtils#getProperty(Object, String)}
299 * for indexed properties.
300 */
301 public void testIssue_BEANUTILS_61_PropertyUtils_getProperty_Indexed() {
302 boolean threwIllegalArgumentException = false;
303 Object result = null;
304 try {
305 result = PropertyUtils.getProperty(wrapDynaBean, "indexedReadOnly[0]");
306 } catch (Throwable t) {
307 log.error("ERROR " + t, t);
308 fail("indexedReadOnly Threw exception: " + t);
309 }
310 assertEquals("indexedReadOnly", testBean.getIndexedReadOnly(0), result);
311
312 try {
313 result = PropertyUtils.getProperty(wrapDynaBean, "indexedWriteOnly[0]");
314 } catch (IllegalArgumentException ex) {
315 threwIllegalArgumentException = true;
316 } catch (Throwable t) {
317 log.error("ERROR " + t, t);
318 fail("indexedWriteOnly Threw exception: " + t);
319 }
320 assertTrue("Expected IllegalArgumentException but returned '" + result + "'", threwIllegalArgumentException);
321 }
322
323 /***
324 * Test {@link PropertyUtils#setProperty(Object, String, Object)}
325 * for indexed properties.
326 */
327 public void testIssue_BEANUTILS_61_PropertyUtils_setProperty_Indexed() {
328 boolean threwIllegalArgumentException = false;
329 try {
330 PropertyUtils.setProperty(wrapDynaBean, "indexedReadOnly[0]", "READONLY-INDEXED-BAR");
331 } catch (IllegalArgumentException ex) {
332 threwIllegalArgumentException = true;
333 } catch (Throwable t) {
334 log.error("ERROR " + t, t);
335 fail("indexedReadOnly Threw exception: " + t);
336 }
337 assertTrue("Expected IllegalArgumentException", threwIllegalArgumentException);
338
339 try {
340 PropertyUtils.setProperty(wrapDynaBean, "indexedWriteOnly[0]", "INDEXED-BAR");
341 } catch (Throwable t) {
342 log.error("ERROR " + t, t);
343 fail("indexedWriteOnly Threw exception: " + t);
344 }
345 assertEquals("indexedWriteOnly", testBean.getIndexedReadOnly(0), "INDEXED-BAR");
346 }
347
348 /***
349 * Test {@link PropertyUtils#getProperty(Object, String)}
350 * for mapped properties.
351 */
352 public void testIssue_BEANUTILS_61_PropertyUtils_getProperty_Mapped() {
353 boolean threwIllegalArgumentException = false;
354 Object result = null;
355 try {
356 result = PropertyUtils.getProperty(wrapDynaBean, "mappedReadOnly(foo-key)");
357 } catch (Throwable t) {
358 log.error("ERROR " + t, t);
359 fail("mappedReadOnly Threw exception: " + t);
360 }
361 assertEquals("mappedReadOnly", testBean.getMappedReadOnly("foo-key"), result);
362
363 try {
364 result = PropertyUtils.getProperty(wrapDynaBean, "mappedWriteOnly(foo-key)");
365 } catch (IllegalArgumentException ex) {
366 threwIllegalArgumentException = true;
367 } catch (Throwable t) {
368 log.error("ERROR " + t, t);
369 fail("mappedWriteOnly Threw exception: " + t);
370 }
371 assertTrue("Expected IllegalArgumentException but returned '" + result + "'", threwIllegalArgumentException);
372 }
373
374 /***
375 * Test {@link PropertyUtils#setProperty(Object, String, Object)}
376 * for mapped properties.
377 */
378 public void testIssue_BEANUTILS_61_PropertyUtils_setProperty_Mapped() {
379 boolean threwIllegalArgumentException = false;
380 try {
381 PropertyUtils.setProperty(wrapDynaBean, "mappedReadOnly(foo-key)", "READONLY-MAPPED-BAR");
382 } catch (IllegalArgumentException ex) {
383 threwIllegalArgumentException = true;
384 } catch (Throwable t) {
385 log.error("ERROR " + t, t);
386 fail("mappedReadOnly Threw exception: " + t);
387 }
388 assertTrue("Expected IllegalArgumentException", threwIllegalArgumentException);
389
390 try {
391 PropertyUtils.setProperty(wrapDynaBean, "mappedWriteOnly(foo-key)", "MAPPED-BAR");
392 } catch (Throwable t) {
393 log.error("ERROR " + t, t);
394 fail("mappedWriteOnly Threw exception: " + t);
395 }
396 assertEquals("mappedWriteOnly", testBean.getMappedReadOnly("foo-key"), "MAPPED-BAR");
397 }
398
399 /***
400 * Test {@link PropertyUtils#copyProperties(Object, Object)}
401 * to a read-only WrapDynaBean property.
402 */
403 public void testIssue_BEANUTILS_61_PropertyUtils_copyProperties_to_WrapDynaBean() {
404 String value = "copied simpleReadOnly";
405 Map source = new HashMap();
406 source.put("simpleReadOnly", value);
407 try {
408 PropertyUtils.copyProperties(wrapDynaBean, source);
409 } catch (Throwable t) {
410 log.error("ERROR " + t, t);
411 fail("copyProperties Threw exception: " + t);
412 }
413 assertFalse("Target value='" + value + "'", value.equals(testBean.getSimpleReadOnly()));
414 }
415
416 /***
417 * Test {@link PropertyUtils#copyProperties(Object, Object)}
418 * to a read-only WrapDynaBean property.
419 */
420 public void testIssue_BEANUTILS_61_PropertyUtils_copyProperties_from_WrapDynaBean() {
421 String value = "ORIG TARGET VALUE";
422 TestBean targetBean = Jira61BeanFactory.createBean();
423 targetBean.setSimpleWriteOnly(value);
424 try {
425 PropertyUtils.copyProperties(targetBean, wrapDynaBean);
426 } catch (Throwable t) {
427 log.error("ERROR " + t, t);
428 fail("copyProperties Threw exception: " + t);
429 }
430 assertTrue("Target value='" + targetBean.getSimpleReadOnly() + "'", value.equals(targetBean.getSimpleReadOnly()));
431 }
432
433 /***
434 * Test {@link BeanUtils#copyProperties(Object, Object)}
435 * to a read-only WrapDynaBean property.
436 */
437 public void testIssue_BEANUTILS_61_BeanUtils_copyProperties_to_WrapDynaBean() {
438 String value = "copied simpleReadOnly";
439 Map source = new HashMap();
440 source.put("simpleReadOnly", value);
441 try {
442 BeanUtils.copyProperties(wrapDynaBean, source);
443 } catch (Throwable t) {
444 log.error("ERROR " + t, t);
445 fail("copyProperties Threw exception: " + t);
446 }
447 assertFalse("Target value='" + value + "'", value.equals(testBean.getSimpleReadOnly()));
448 }
449
450 /***
451 * Test {@link BeanUtils#copyProperties(Object, Object)}
452 * to a read-only WrapDynaBean property.
453 */
454 public void testIssue_BEANUTILS_61_BeanUtils_copyProperties_from_WrapDynaBean() {
455 String value = "ORIG TARGET VALUE";
456 TestBean targetBean = Jira61BeanFactory.createBean();
457 targetBean.setSimpleWriteOnly(value);
458 try {
459 BeanUtils.copyProperties(targetBean, wrapDynaBean);
460 } catch (Throwable t) {
461 log.error("ERROR " + t, t);
462 fail("copyProperties Threw exception: " + t);
463 }
464 assertTrue("Target value='" + targetBean.getSimpleReadOnly() + "'", value.equals(targetBean.getSimpleReadOnly()));
465 }
466 }