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