1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.beanutils;
20
21
22 import java.lang.reflect.InvocationTargetException;
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27
28 import junit.framework.TestCase;
29 import junit.framework.Test;
30 import junit.framework.TestSuite;
31
32
33 /***
34 * Test accessing DynaBeans transparently via PropertyUtils.
35 *
36 * @author Craig R. McClanahan
37 * @version $Revision: 471138 $ $Date: 2006-11-04 08:06:03 +0000 (Sat, 04 Nov 2006) $
38 */
39
40 public class DynaPropertyUtilsTestCase extends TestCase {
41
42
43
44
45
46 /***
47 * The basic test bean for each test.
48 */
49 protected DynaBean bean = null;
50
51
52 /***
53 * The set of properties that should be described.
54 */
55 protected String describes[] =
56 { "booleanProperty",
57 "booleanSecond",
58 "doubleProperty",
59 "floatProperty",
60 "intArray",
61 "intIndexed",
62 "intProperty",
63 "listIndexed",
64 "longProperty",
65 "mappedObjects",
66 "mappedProperty",
67 "mappedIntProperty",
68 "nested",
69 "nullProperty",
70
71 "shortProperty",
72 "stringArray",
73 "stringIndexed",
74 "stringProperty"
75 };
76
77
78 /***
79 * The nested bean pointed at by the "nested" property.
80 */
81 protected TestBean nested = null;
82
83
84
85
86
87 /***
88 * Construct a new instance of this test case.
89 *
90 * @param name Name of the test case
91 */
92 public DynaPropertyUtilsTestCase(String name) {
93
94 super(name);
95
96 }
97
98
99
100
101
102 /***
103 * Set up instance variables required by this test case.
104 */
105 public void setUp() throws Exception {
106
107
108 DynaClass dynaClass = createDynaClass();
109 bean = dynaClass.newInstance();
110
111
112 bean.set("booleanProperty", new Boolean(true));
113 bean.set("booleanSecond", new Boolean(true));
114 bean.set("doubleProperty", new Double(321.0));
115 bean.set("floatProperty", new Float((float) 123.0));
116 int intArray[] = { 0, 10, 20, 30, 40 };
117 bean.set("intArray", intArray);
118 int intIndexed[] = { 0, 10, 20, 30, 40 };
119 bean.set("intIndexed", intIndexed);
120 bean.set("intProperty", new Integer(123));
121 List listIndexed = new ArrayList();
122 listIndexed.add("String 0");
123 listIndexed.add("String 1");
124 listIndexed.add("String 2");
125 listIndexed.add("String 3");
126 listIndexed.add("String 4");
127 bean.set("listIndexed", listIndexed);
128 bean.set("longProperty", new Long(321));
129 HashMap mapProperty = new HashMap();
130 mapProperty.put("First Key", "First Value");
131 mapProperty.put("Second Key", "Second Value");
132 bean.set("mapProperty", mapProperty);
133 HashMap mappedObjects = new HashMap();
134 mappedObjects.put("First Key", "First Value");
135 mappedObjects.put("Second Key", "Second Value");
136 bean.set("mappedObjects", mappedObjects);
137 HashMap mappedProperty = new HashMap();
138 mappedProperty.put("First Key", "First Value");
139 mappedProperty.put("Second Key", "Second Value");
140 bean.set("mappedProperty", mappedProperty);
141 HashMap mappedIntProperty = new HashMap();
142 mappedIntProperty.put("One", new Integer(1));
143 mappedIntProperty.put("Two", new Integer(2));
144 bean.set("mappedIntProperty", mappedIntProperty);
145 nested = new TestBean();
146 bean.set("nested", nested);
147
148 bean.set("shortProperty", new Short((short) 987));
149 String stringArray[] =
150 { "String 0", "String 1", "String 2", "String 3", "String 4" };
151 bean.set("stringArray", stringArray);
152 String stringIndexed[] =
153 { "String 0", "String 1", "String 2", "String 3", "String 4" };
154 bean.set("stringIndexed", stringIndexed);
155 bean.set("stringProperty", "This is a string");
156
157 }
158
159
160 /***
161 * Return the tests included in this test suite.
162 */
163 public static Test suite() {
164
165 return (new TestSuite(DynaPropertyUtilsTestCase.class));
166
167 }
168
169
170 /***
171 * Tear down instance variables required by this test case.
172 */
173 public void tearDown() {
174
175 bean = null;
176 nested = null;
177
178 }
179
180
181
182
183
184
185 /***
186 * Test copyProperties() when the origin is a a <code>Map</code>.
187 */
188 public void testCopyPropertiesMap() {
189
190 Map map = new HashMap();
191 map.put("booleanProperty", Boolean.FALSE);
192 map.put("doubleProperty", new Double(333.0));
193 map.put("dupProperty", new String[] { "New 0", "New 1", "New 2" });
194 map.put("floatProperty", new Float((float) 222.0));
195 map.put("intArray", new int[] { 0, 100, 200 });
196 map.put("intProperty", new Integer(111));
197 map.put("longProperty", new Long(444));
198 map.put("shortProperty", new Short((short) 555));
199 map.put("stringProperty", "New String Property");
200
201 try {
202 PropertyUtils.copyProperties(bean, map);
203 } catch (Throwable t) {
204 fail("Threw " + t.toString());
205 }
206
207
208 assertEquals("booleanProperty", false,
209 ((Boolean) bean.get("booleanProperty")).booleanValue());
210 assertEquals("doubleProperty", 333.0,
211 ((Double) bean.get("doubleProperty")).doubleValue(),
212 0.005);
213 assertEquals("floatProperty", (float) 222.0,
214 ((Float) bean.get("floatProperty")).floatValue(),
215 (float) 0.005);
216 assertEquals("intProperty", 111,
217 ((Integer) bean.get("intProperty")).intValue());
218 assertEquals("longProperty", 444,
219 ((Long) bean.get("longProperty")).longValue());
220 assertEquals("shortProperty", (short) 555,
221 ((Short) bean.get("shortProperty")).shortValue());
222 assertEquals("stringProperty", "New String Property",
223 (String) bean.get("stringProperty"));
224
225
226 String dupProperty[] = (String[]) bean.get("dupProperty");
227 assertNotNull("dupProperty present", dupProperty);
228 assertEquals("dupProperty length", 3, dupProperty.length);
229 assertEquals("dupProperty[0]", "New 0", dupProperty[0]);
230 assertEquals("dupProperty[1]", "New 1", dupProperty[1]);
231 assertEquals("dupProperty[2]", "New 2", dupProperty[2]);
232 int intArray[] = (int[]) bean.get("intArray");
233 assertNotNull("intArray present", intArray);
234 assertEquals("intArray length", 3, intArray.length);
235 assertEquals("intArray[0]", 0, intArray[0]);
236 assertEquals("intArray[1]", 100, intArray[1]);
237 assertEquals("intArray[2]", 200, intArray[2]);
238
239 }
240
241
242 /***
243 * Test the describe() method.
244 */
245 public void testDescribe() {
246
247 Map map = null;
248 try {
249 map = PropertyUtils.describe(bean);
250 } catch (Exception e) {
251 fail("Threw exception " + e);
252 }
253
254
255 for (int i = 0; i < describes.length; i++) {
256 assertTrue("Property '" + describes[i] + "' is present",
257 map.containsKey(describes[i]));
258 }
259 assertTrue("Property 'writeOnlyProperty' is not present",
260 !map.containsKey("writeOnlyProperty"));
261
262
263 assertEquals("Value of 'booleanProperty'",
264 Boolean.TRUE, map.get("booleanProperty"));
265 assertEquals("Value of 'doubleProperty'",
266 new Double(321.0), map.get("doubleProperty"));
267 assertEquals("Value of 'floatProperty'",
268 new Float((float) 123.0), map.get("floatProperty"));
269 assertEquals("Value of 'intProperty'",
270 new Integer(123), map.get("intProperty"));
271 assertEquals("Value of 'longProperty'",
272 new Long(321), map.get("longProperty"));
273 assertEquals("Value of 'shortProperty'",
274 new Short((short) 987), map.get("shortProperty"));
275 assertEquals("Value of 'stringProperty'",
276 "This is a string",
277 (String) map.get("stringProperty"));
278
279 }
280
281
282 /***
283 * Corner cases on getIndexedProperty invalid arguments.
284 */
285 public void testGetIndexedArguments() {
286
287
288
289 try {
290 PropertyUtils.getIndexedProperty(null, "intArray", 0);
291 fail("Should throw IllegalArgumentException 1");
292 } catch (IllegalArgumentException e) {
293
294 } catch (Throwable t) {
295 fail("Threw " + t + " instead of IllegalArgumentException 1");
296 }
297
298 try {
299 PropertyUtils.getIndexedProperty(bean, null, 0);
300 fail("Should throw IllegalArgumentException 2");
301 } catch (IllegalArgumentException e) {
302
303 } catch (Throwable t) {
304 fail("Threw " + t + " instead of IllegalArgumentException 2");
305 }
306
307
308
309 try {
310 PropertyUtils.getIndexedProperty(null,
311 "intArray[0]");
312 fail("Should throw IllegalArgumentException 3");
313 } catch (IllegalArgumentException e) {
314
315 } catch (Throwable t) {
316 fail("Threw " + t + " instead of IllegalArgumentException 3");
317 }
318
319 try {
320 PropertyUtils.getIndexedProperty(bean, "[0]");
321 fail("Should throw NoSuchMethodException 4");
322 } catch (NoSuchMethodException e) {
323
324 } catch (Throwable t) {
325 fail("Threw " + t + " instead of NoSuchMethodException 4");
326 }
327
328 try {
329 PropertyUtils.getIndexedProperty(bean, "intArray");
330 fail("Should throw IllegalArgumentException 5");
331 } catch (IllegalArgumentException e) {
332
333 } catch (Throwable t) {
334 fail("Threw " + t + " instead of IllegalArgumentException 5");
335 }
336
337
338
339 try {
340 PropertyUtils.getIndexedProperty(null, "intIndexed", 0);
341 fail("Should throw IllegalArgumentException 1");
342 } catch (IllegalArgumentException e) {
343
344 } catch (Throwable t) {
345 fail("Threw " + t + " instead of IllegalArgumentException 1");
346 }
347
348 try {
349 PropertyUtils.getIndexedProperty(bean, null, 0);
350 fail("Should throw IllegalArgumentException 2");
351 } catch (IllegalArgumentException e) {
352
353 } catch (Throwable t) {
354 fail("Threw " + t + " instead of IllegalArgumentException 2");
355 }
356
357
358
359 try {
360 PropertyUtils.getIndexedProperty(null,
361 "intIndexed[0]");
362 fail("Should throw IllegalArgumentException 3");
363 } catch (IllegalArgumentException e) {
364
365 } catch (Throwable t) {
366 fail("Threw " + t + " instead of IllegalArgumentException 3");
367 }
368
369 try {
370 PropertyUtils.getIndexedProperty(bean, "[0]");
371 fail("Should throw NoSuchMethodException 4");
372 } catch (NoSuchMethodException e) {
373
374 } catch (Throwable t) {
375 fail("Threw " + t + " instead of NoSuchMethodException 4");
376 }
377
378 try {
379 PropertyUtils.getIndexedProperty(bean, "intIndexed");
380 fail("Should throw IllegalArgumentException 5");
381 } catch (IllegalArgumentException e) {
382
383 } catch (Throwable t) {
384 fail("Threw " + t + " instead of IllegalArgumentException 5");
385 }
386
387 }
388
389
390 /***
391 * Positive and negative tests on getIndexedProperty valid arguments.
392 */
393 public void testGetIndexedValues() {
394
395 Object value = null;
396
397
398
399 for (int i = 0; i < 5; i++) {
400
401 try {
402 value =
403 PropertyUtils.getIndexedProperty(bean, "intArray", i);
404 assertNotNull("intArray returned value " + i, value);
405 assertTrue("intArray returned Integer " + i,
406 value instanceof Integer);
407 assertEquals("intArray returned correct " + i, i * 10,
408 ((Integer) value).intValue());
409 } catch (Throwable t) {
410 fail("intArray " + i + " threw " + t);
411 }
412
413 try {
414 value =
415 PropertyUtils.getIndexedProperty(bean, "intIndexed", i);
416 assertNotNull("intIndexed returned value " + i, value);
417 assertTrue("intIndexed returned Integer " + i,
418 value instanceof Integer);
419 assertEquals("intIndexed returned correct " + i, i * 10,
420 ((Integer) value).intValue());
421 } catch (Throwable t) {
422 fail("intIndexed " + i + " threw " + t);
423 }
424
425 try {
426 value =
427 PropertyUtils.getIndexedProperty(bean, "listIndexed", i);
428 assertNotNull("listIndexed returned value " + i, value);
429 assertTrue("list returned String " + i,
430 value instanceof String);
431 assertEquals("listIndexed returned correct " + i,
432 "String " + i, (String) value);
433 } catch (Throwable t) {
434 fail("listIndexed " + i + " threw " + t);
435 }
436
437 try {
438 value =
439 PropertyUtils.getIndexedProperty(bean, "stringArray", i);
440 assertNotNull("stringArray returned value " + i, value);
441 assertTrue("stringArray returned String " + i,
442 value instanceof String);
443 assertEquals("stringArray returned correct " + i,
444 "String " + i, (String) value);
445 } catch (Throwable t) {
446 fail("stringArray " + i + " threw " + t);
447 }
448
449 try {
450 value =
451 PropertyUtils.getIndexedProperty(bean, "stringIndexed", i);
452 assertNotNull("stringIndexed returned value " + i, value);
453 assertTrue("stringIndexed returned String " + i,
454 value instanceof String);
455 assertEquals("stringIndexed returned correct " + i,
456 "String " + i, (String) value);
457 } catch (Throwable t) {
458 fail("stringIndexed " + i + " threw " + t);
459 }
460
461 }
462
463
464
465 for (int i = 0; i < 5; i++) {
466
467 try {
468 value =
469 PropertyUtils.getIndexedProperty(bean,
470 "intArray[" + i + "]");
471 assertNotNull("intArray returned value " + i, value);
472 assertTrue("intArray returned Integer " + i,
473 value instanceof Integer);
474 assertEquals("intArray returned correct " + i, i * 10,
475 ((Integer) value).intValue());
476 } catch (Throwable t) {
477 fail("intArray " + i + " threw " + t);
478 }
479
480 try {
481 value =
482 PropertyUtils.getIndexedProperty(bean,
483 "intIndexed[" + i + "]");
484 assertNotNull("intIndexed returned value " + i, value);
485 assertTrue("intIndexed returned Integer " + i,
486 value instanceof Integer);
487 assertEquals("intIndexed returned correct " + i, i * 10,
488 ((Integer) value).intValue());
489 } catch (Throwable t) {
490 fail("intIndexed " + i + " threw " + t);
491 }
492
493 try {
494 value =
495 PropertyUtils.getIndexedProperty(bean,
496 "listIndexed[" + i + "]");
497 assertNotNull("listIndexed returned value " + i, value);
498 assertTrue("listIndexed returned String " + i,
499 value instanceof String);
500 assertEquals("listIndexed returned correct " + i,
501 "String " + i, (String) value);
502 } catch (Throwable t) {
503 fail("listIndexed " + i + " threw " + t);
504 }
505
506 try {
507 value =
508 PropertyUtils.getIndexedProperty(bean,
509 "stringArray[" + i + "]");
510 assertNotNull("stringArray returned value " + i, value);
511 assertTrue("stringArray returned String " + i,
512 value instanceof String);
513 assertEquals("stringArray returned correct " + i,
514 "String " + i, (String) value);
515 } catch (Throwable t) {
516 fail("stringArray " + i + " threw " + t);
517 }
518
519 try {
520 value =
521 PropertyUtils.getIndexedProperty(bean,
522 "stringIndexed[" + i + "]");
523 assertNotNull("stringIndexed returned value " + i, value);
524 assertTrue("stringIndexed returned String " + i,
525 value instanceof String);
526 assertEquals("stringIndexed returned correct " + i,
527 "String " + i, (String) value);
528 } catch (Throwable t) {
529 fail("stringIndexed " + i + " threw " + t);
530 }
531
532 }
533
534
535
536 try {
537 value =
538 PropertyUtils.getIndexedProperty(bean,
539 "intArray", -1);
540 fail("Should have thrown ArrayIndexOutOfBoundsException");
541 } catch (ArrayIndexOutOfBoundsException t) {
542
543 } catch (Throwable t) {
544 fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException");
545 }
546
547 try {
548 value =
549 PropertyUtils.getIndexedProperty(bean,
550 "intArray", 5);
551 fail("Should have thrown ArrayIndexOutOfBoundsException");
552 } catch (ArrayIndexOutOfBoundsException t) {
553
554 } catch (Throwable t) {
555 fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException");
556 }
557
558 try {
559 value =
560 PropertyUtils.getIndexedProperty(bean,
561 "intIndexed", -1);
562 fail("Should have thrown ArrayIndexOutOfBoundsException");
563 } catch (ArrayIndexOutOfBoundsException t) {
564
565 } catch (Throwable t) {
566 fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException");
567 }
568
569 try {
570 value =
571 PropertyUtils.getIndexedProperty(bean,
572 "intIndexed", 5);
573 fail("Should have thrown ArrayIndexOutOfBoundsException");
574 } catch (ArrayIndexOutOfBoundsException t) {
575
576 } catch (Throwable t) {
577 fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException");
578 }
579
580 try {
581 value =
582 PropertyUtils.getIndexedProperty(bean,
583 "listIndexed", -1);
584 fail("Should have thrown IndexOutOfBoundsException");
585 } catch (IndexOutOfBoundsException t) {
586
587 } catch (Throwable t) {
588 fail("Threw " + t + " instead of IndexOutOfBoundsException");
589 }
590
591 try {
592 value =
593 PropertyUtils.getIndexedProperty(bean,
594 "listIndexed", 5);
595 fail("Should have thrown IndexOutOfBoundsException");
596 } catch (IndexOutOfBoundsException t) {
597
598 } catch (Throwable t) {
599 fail("Threw " + t + " instead of IndexOutOfBoundsException");
600 }
601
602 try {
603 value =
604 PropertyUtils.getIndexedProperty(bean,
605 "stringArray", -1);
606 fail("Should have thrown ArrayIndexOutOfBoundsException");
607 } catch (ArrayIndexOutOfBoundsException t) {
608
609 } catch (Throwable t) {
610 fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException");
611 }
612
613 try {
614 value =
615 PropertyUtils.getIndexedProperty(bean,
616 "stringArray", 5);
617 fail("Should have thrown ArrayIndexOutOfBoundsException");
618 } catch (ArrayIndexOutOfBoundsException t) {
619
620 } catch (Throwable t) {
621 fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException");
622 }
623
624 try {
625 value =
626 PropertyUtils.getIndexedProperty(bean,
627 "stringIndexed", -1);
628 fail("Should have thrown ArrayIndexOutOfBoundsException");
629 } catch (ArrayIndexOutOfBoundsException t) {
630
631 } catch (Throwable t) {
632 fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException");
633 }
634
635 try {
636 value =
637 PropertyUtils.getIndexedProperty(bean,
638 "stringIndexed", 5);
639 fail("Should have thrown ArrayIndexOutOfBoundsException");
640 } catch (ArrayIndexOutOfBoundsException t) {
641
642 } catch (Throwable t) {
643 fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException");
644 }
645
646 }
647
648
649 /***
650 * Corner cases on getMappedProperty invalid arguments.
651 */
652 public void testGetMappedArguments() {
653
654
655
656 try {
657 PropertyUtils.getMappedProperty(null, "mappedProperty",
658 "First Key");
659 fail("Should throw IllegalArgumentException 1");
660 } catch (IllegalArgumentException e) {
661
662 } catch (Throwable t) {
663 fail("Threw " + t + " instead of IllegalArgumentException 1");
664 }
665
666 try {
667 PropertyUtils.getMappedProperty(bean, null, "First Key");
668 fail("Should throw IllegalArgumentException 2");
669 } catch (IllegalArgumentException e) {
670
671 } catch (Throwable t) {
672 fail("Threw " + t + " instead of IllegalArgumentException 2");
673 }
674
675 try {
676 PropertyUtils.getMappedProperty(bean, "mappedProperty", null);
677 fail("Should throw IllegalArgumentException 3");
678 } catch (IllegalArgumentException e) {
679
680 } catch (Throwable t) {
681 fail("Threw " + t + " instead of IllegalArgumentException 3");
682 }
683
684
685
686 try {
687 PropertyUtils.getMappedProperty(null,
688 "mappedProperty(First Key)");
689 fail("Should throw IllegalArgumentException 4");
690 } catch (IllegalArgumentException e) {
691
692 } catch (Throwable t) {
693 fail("Threw " + t + " instead of IllegalArgumentException 4");
694 }
695
696 try {
697 PropertyUtils.getMappedProperty(bean, "(Second Key)");
698 fail("Should throw IllegalArgumentException 5");
699 } catch (NoSuchMethodException e) {
700
701 } catch (Throwable t) {
702 fail("Threw " + t + " instead of NoSuchMethodException 5");
703 }
704
705 try {
706 PropertyUtils.getMappedProperty(bean, "mappedProperty");
707 fail("Should throw IllegalArgumentException 6");
708 } catch (IllegalArgumentException e) {
709
710 } catch (Throwable t) {
711 fail("Threw " + t + " instead of IllegalArgumentException 6");
712 }
713
714 }
715
716
717 /***
718 * Test getting mapped values with periods in the key.
719 */
720 public void testGetMappedPeriods() {
721
722 bean.set("mappedProperty", "key.with.a.dot", "Special Value");
723 assertEquals("Can retrieve directly",
724 "Special Value",
725 (String) bean.get("mappedProperty", "key.with.a.dot"));
726 try {
727 assertEquals("Can retrieve via getMappedProperty",
728 "Special Value",
729 PropertyUtils.getMappedProperty
730 (bean, "mappedProperty", "key.with.a.dot"));
731 } catch (Exception e) {
732 fail("Thew exception: " + e);
733 }
734 try {
735 assertEquals("Can retrieve via getNestedProperty",
736 "Special Value",
737 PropertyUtils.getNestedProperty
738 (bean, "mappedProperty(key.with.a.dot)"));
739 } catch (Exception e) {
740 fail("Thew exception: " + e);
741 }
742
743 bean.set("mappedObjects", "nested.property", new TestBean());
744 assertNotNull("Can retrieve directly",
745 bean.get("mappedObjects", "nested.property"));
746 try {
747 assertEquals("Can retrieve nested",
748 "This is a string",
749 PropertyUtils.getNestedProperty
750 (bean,
751 "mappedObjects(nested.property).stringProperty"));
752 } catch (Exception e) {
753 fail("Thew exception: " + e);
754 }
755
756 }
757
758
759 /***
760 * Test getting mapped values with slashes in the key. This is different
761 * from periods because slashes are not syntactically significant.
762 */
763 public void testGetMappedSlashes() {
764
765 bean.set("mappedProperty", "key/with/a/slash", "Special Value");
766 assertEquals("Can retrieve directly",
767 "Special Value",
768 bean.get("mappedProperty", "key/with/a/slash"));
769 try {
770 assertEquals("Can retrieve via getMappedProperty",
771 "Special Value",
772 PropertyUtils.getMappedProperty
773 (bean, "mappedProperty", "key/with/a/slash"));
774 } catch (Exception e) {
775 fail("Thew exception: " + e);
776 }
777 try {
778 assertEquals("Can retrieve via getNestedProperty",
779 "Special Value",
780 PropertyUtils.getNestedProperty
781 (bean, "mappedProperty(key/with/a/slash)"));
782 } catch (Exception e) {
783 fail("Thew exception: " + e);
784 }
785
786 bean.set("mappedObjects", "nested/property", new TestBean());
787 assertNotNull("Can retrieve directly",
788 bean.get("mappedObjects", "nested/property"));
789 try {
790 assertEquals("Can retrieve nested",
791 "This is a string",
792 PropertyUtils.getNestedProperty
793 (bean,
794 "mappedObjects(nested/property).stringProperty"));
795 } catch (Exception e) {
796 fail("Thew exception: " + e);
797 }
798
799 }
800
801
802 /***
803 * Positive and negative tests on getMappedProperty valid arguments.
804 */
805 public void testGetMappedValues() {
806
807 Object value = null;
808
809
810
811 try {
812 value = PropertyUtils.getMappedProperty(bean, "mappedProperty",
813 "First Key");
814 assertEquals("Can find first value", "First Value", value);
815 } catch (Throwable t) {
816 fail("Finding first value threw " + t);
817 }
818
819 try {
820 value = PropertyUtils.getMappedProperty(bean, "mappedProperty",
821 "Second Key");
822 assertEquals("Can find second value", "Second Value", value);
823 } catch (Throwable t) {
824 fail("Finding second value threw " + t);
825 }
826
827 try {
828 value = PropertyUtils.getMappedProperty(bean, "mappedProperty",
829 "Third Key");
830 assertNull("Can not find third value", value);
831 } catch (Throwable t) {
832 fail("Finding third value threw " + t);
833 }
834
835
836
837 try {
838 value =
839 PropertyUtils.getMappedProperty(bean,
840 "mappedProperty(First Key)");
841 assertEquals("Can find first value", "First Value", value);
842 } catch (Throwable t) {
843 fail("Finding first value threw " + t);
844 }
845
846 try {
847 value =
848 PropertyUtils.getMappedProperty(bean,
849 "mappedProperty(Second Key)");
850 assertEquals("Can find second value", "Second Value", value);
851 } catch (Throwable t) {
852 fail("Finding second value threw " + t);
853 }
854
855 try {
856 value =
857 PropertyUtils.getMappedProperty(bean,
858 "mappedProperty(Third Key)");
859 assertNull("Can not find third value", value);
860 } catch (Throwable t) {
861 fail("Finding third value threw " + t);
862 }
863
864
865
866 try {
867 value =
868 PropertyUtils.getNestedProperty(bean,
869 "mapProperty.First Key");
870 assertEquals("Can find first value", "First Value", value);
871 } catch (Throwable t) {
872 fail("Finding first value threw " + t);
873 }
874
875 try {
876 value =
877 PropertyUtils.getNestedProperty(bean,
878 "mapProperty.Second Key");
879 assertEquals("Can find second value", "Second Value", value);
880 } catch (Throwable t) {
881 fail("Finding second value threw " + t);
882 }
883
884 try {
885 value =
886 PropertyUtils.getNestedProperty(bean,
887 "mapProperty.Third Key");
888 assertNull("Can not find third value", value);
889 } catch (Throwable t) {
890 fail("Finding third value threw " + t);
891 }
892
893 }
894
895
896 /***
897 * Corner cases on getNestedProperty invalid arguments.
898 */
899 public void testGetNestedArguments() {
900
901 try {
902 PropertyUtils.getNestedProperty(null, "stringProperty");
903 fail("Should throw IllegalArgumentException 1");
904 } catch (IllegalArgumentException e) {
905
906 } catch (Throwable t) {
907 fail("Threw " + t + " instead of IllegalArgumentException 1");
908 }
909
910 try {
911 PropertyUtils.getNestedProperty(bean, null);
912 fail("Should throw IllegalArgumentException 2");
913 } catch (IllegalArgumentException e) {
914
915 } catch (Throwable t) {
916 fail("Threw " + t + " instead of IllegalArgumentException 2");
917 }
918
919 }
920
921
922 /***
923 * Test getNestedProperty on a boolean property.
924 */
925 public void testGetNestedBoolean() {
926
927 try {
928 Object value =
929 PropertyUtils.getNestedProperty
930 (bean, "nested.booleanProperty");
931 assertNotNull("Got a value", value);
932 assertTrue("Got correct type", (value instanceof Boolean));
933 TestBean nested = (TestBean) bean.get("nested");
934 assertTrue("Got correct value",
935 ((Boolean) value).booleanValue() ==
936 nested.getBooleanProperty());
937 } catch (IllegalAccessException e) {
938 fail("IllegalAccessException");
939 } catch (IllegalArgumentException e) {
940 fail("IllegalArgumentException");
941 } catch (InvocationTargetException e) {
942 fail("InvocationTargetException");
943 } catch (NoSuchMethodException e) {
944 fail("NoSuchMethodException");
945 }
946
947 }
948
949
950 /***
951 * Test getNestedProperty on a double property.
952 */
953 public void testGetNestedDouble() {
954
955 try {
956 Object value =
957 PropertyUtils.getNestedProperty
958 (bean, "nested.doubleProperty");
959 assertNotNull("Got a value", value);
960 assertTrue("Got correct type", (value instanceof Double));
961 TestBean nested = (TestBean) bean.get("nested");
962 assertEquals("Got correct value",
963 ((Double) value).doubleValue(),
964 nested.getDoubleProperty(),
965 0.005);
966 } catch (IllegalAccessException e) {
967 fail("IllegalAccessException");
968 } catch (IllegalArgumentException e) {
969 fail("IllegalArgumentException");
970 } catch (InvocationTargetException e) {
971 fail("InvocationTargetException");
972 } catch (NoSuchMethodException e) {
973 fail("NoSuchMethodException");
974 }
975
976 }
977
978
979 /***
980 * Test getNestedProperty on a float property.
981 */
982 public void testGetNestedFloat() {
983
984 try {
985 Object value =
986 PropertyUtils.getNestedProperty
987 (bean, "nested.floatProperty");
988 assertNotNull("Got a value", value);
989 assertTrue("Got correct type", (value instanceof Float));
990 TestBean nested = (TestBean) bean.get("nested");
991 assertEquals("Got correct value",
992 ((Float) value).floatValue(),
993 nested.getFloatProperty(),
994 (float) 0.005);
995 } catch (IllegalAccessException e) {
996 fail("IllegalAccessException");
997 } catch (IllegalArgumentException e) {
998 fail("IllegalArgumentException");
999 } catch (InvocationTargetException e) {
1000 fail("InvocationTargetException");
1001 } catch (NoSuchMethodException e) {
1002 fail("NoSuchMethodException");
1003 }
1004
1005 }
1006
1007
1008 /***
1009 * Test getNestedProperty on an int property.
1010 */
1011 public void testGetNestedInt() {
1012
1013 try {
1014 Object value =
1015 PropertyUtils.getNestedProperty
1016 (bean, "nested.intProperty");
1017 assertNotNull("Got a value", value);
1018 assertTrue("Got correct type", (value instanceof Integer));
1019 TestBean nested = (TestBean) bean.get("nested");
1020 assertEquals("Got correct value",
1021 ((Integer) value).intValue(),
1022 nested.getIntProperty());
1023 } catch (IllegalAccessException e) {
1024 fail("IllegalAccessException");
1025 } catch (IllegalArgumentException e) {
1026 fail("IllegalArgumentException");
1027 } catch (InvocationTargetException e) {
1028 fail("InvocationTargetException");
1029 } catch (NoSuchMethodException e) {
1030 fail("NoSuchMethodException");
1031 }
1032
1033 }
1034
1035
1036 /***
1037 * Test getNestedProperty on a long property.
1038 */
1039 public void testGetNestedLong() {
1040
1041 try {
1042 Object value =
1043 PropertyUtils.getNestedProperty
1044 (bean, "nested.longProperty");
1045 assertNotNull("Got a value", value);
1046 assertTrue("Got correct type", (value instanceof Long));
1047 TestBean nested = (TestBean) bean.get("nested");
1048 assertEquals("Got correct value",
1049 ((Long) value).longValue(),
1050 nested.getLongProperty());
1051 } catch (IllegalAccessException e) {
1052 fail("IllegalAccessException");
1053 } catch (IllegalArgumentException e) {
1054 fail("IllegalArgumentException");
1055 } catch (InvocationTargetException e) {
1056 fail("InvocationTargetException");
1057 } catch (NoSuchMethodException e) {
1058 fail("NoSuchMethodException");
1059 }
1060
1061 }
1062
1063
1064 /***
1065 * Test getNestedProperty on a read-only String property.
1066 */
1067 public void testGetNestedReadOnly() {
1068
1069 try {
1070 Object value =
1071 PropertyUtils.getNestedProperty
1072 (bean, "nested.readOnlyProperty");
1073 assertNotNull("Got a value", value);
1074 assertTrue("Got correct type", (value instanceof String));
1075 TestBean nested = (TestBean) bean.get("nested");
1076 assertEquals("Got correct value",
1077 (String) value,
1078 nested.getReadOnlyProperty());
1079 } catch (IllegalAccessException e) {
1080 fail("IllegalAccessException");
1081 } catch (IllegalArgumentException e) {
1082 fail("IllegalArgumentException");
1083 } catch (InvocationTargetException e) {
1084 fail("InvocationTargetException");
1085 } catch (NoSuchMethodException e) {
1086 fail("NoSuchMethodException");
1087 }
1088
1089 }
1090
1091
1092 /***
1093 * Test getNestedProperty on a short property.
1094 */
1095 public void testGetNestedShort() {
1096
1097 try {
1098 Object value =
1099 PropertyUtils.getNestedProperty
1100 (bean, "nested.shortProperty");
1101 assertNotNull("Got a value", value);
1102 assertTrue("Got correct type", (value instanceof Short));
1103 TestBean nested = (TestBean) bean.get("nested");
1104 assertEquals("Got correct value",
1105 ((Short) value).shortValue(),
1106 nested.getShortProperty());
1107 } catch (IllegalAccessException e) {
1108 fail("IllegalAccessException");
1109 } catch (IllegalArgumentException e) {
1110 fail("IllegalArgumentException");
1111 } catch (InvocationTargetException e) {
1112 fail("InvocationTargetException");
1113 } catch (NoSuchMethodException e) {
1114 fail("NoSuchMethodException");
1115 }
1116
1117 }
1118
1119
1120 /***
1121 * Test getNestedProperty on a String property.
1122 */
1123 public void testGetNestedString() {
1124
1125 try {
1126 Object value =
1127 PropertyUtils.getNestedProperty
1128 (bean, "nested.stringProperty");
1129 assertNotNull("Got a value", value);
1130 assertTrue("Got correct type", (value instanceof String));
1131 TestBean nested = (TestBean) bean.get("nested");
1132 assertEquals("Got correct value",
1133 ((String) value),
1134 nested.getStringProperty());
1135 } catch (IllegalAccessException e) {
1136 fail("IllegalAccessException");
1137 } catch (IllegalArgumentException e) {
1138 fail("IllegalArgumentException");
1139 } catch (InvocationTargetException e) {
1140 fail("InvocationTargetException");
1141 } catch (NoSuchMethodException e) {
1142 fail("NoSuchMethodException");
1143 }
1144
1145 }
1146
1147
1148 /***
1149 * Negative test getNestedProperty on an unknown property.
1150 */
1151 public void testGetNestedUnknown() {
1152
1153 try {
1154 PropertyUtils.getNestedProperty(bean, "nested.unknown");
1155 fail("Should have thrown NoSuchMethodException");
1156 } catch (IllegalAccessException e) {
1157 fail("IllegalAccessException");
1158 } catch (IllegalArgumentException e) {
1159 fail("IllegalArgumentException");
1160 } catch (InvocationTargetException e) {
1161 fail("InvocationTargetException");
1162 } catch (NoSuchMethodException e) {
1163
1164 }
1165
1166 }
1167
1168
1169 /***
1170 * Corner cases on getSimpleProperty invalid arguments.
1171 */
1172 public void testGetSimpleArguments() {
1173
1174 try {
1175 PropertyUtils.getSimpleProperty(null, "stringProperty");
1176 fail("Should throw IllegalArgumentException 1");
1177 } catch (IllegalArgumentException e) {
1178
1179 } catch (Throwable t) {
1180 fail("Threw " + t + " instead of IllegalArgumentException 1");
1181 }
1182
1183 try {
1184 PropertyUtils.getSimpleProperty(bean, null);
1185 fail("Should throw IllegalArgumentException 2");
1186 } catch (IllegalArgumentException e) {
1187
1188 } catch (Throwable t) {
1189 fail("Threw " + t + " instead of IllegalArgumentException 2");
1190 }
1191
1192 }
1193
1194
1195 /***
1196 * Test getSimpleProperty on a boolean property.
1197 */
1198 public void testGetSimpleBoolean() {
1199
1200 try {
1201 Object value =
1202 PropertyUtils.getSimpleProperty(bean,
1203 "booleanProperty");
1204 assertNotNull("Got a value", value);
1205 assertTrue("Got correct type", (value instanceof Boolean));
1206 assertTrue("Got correct value",
1207 ((Boolean) value).booleanValue() == true);
1208 } catch (IllegalAccessException e) {
1209 fail("IllegalAccessException");
1210 } catch (IllegalArgumentException e) {
1211 fail("IllegalArgumentException");
1212 } catch (InvocationTargetException e) {
1213 fail("InvocationTargetException");
1214 } catch (NoSuchMethodException e) {
1215 fail("NoSuchMethodException");
1216 }
1217
1218 }
1219
1220
1221 /***
1222 * Test getSimpleProperty on a double property.
1223 */
1224 public void testGetSimpleDouble() {
1225
1226 try {
1227 Object value =
1228 PropertyUtils.getSimpleProperty(bean,
1229 "doubleProperty");
1230 assertNotNull("Got a value", value);
1231 assertTrue("Got correct type", (value instanceof Double));
1232 assertEquals("Got correct value",
1233 ((Double) value).doubleValue(), 321.0, 0.005);
1234 } catch (IllegalAccessException e) {
1235 fail("IllegalAccessException");
1236 } catch (IllegalArgumentException e) {
1237 fail("IllegalArgumentException");
1238 } catch (InvocationTargetException e) {
1239 fail("InvocationTargetException");
1240 } catch (NoSuchMethodException e) {
1241 fail("NoSuchMethodException");
1242 }
1243
1244 }
1245
1246
1247 /***
1248 * Test getSimpleProperty on a float property.
1249 */
1250 public void testGetSimpleFloat() {
1251
1252 try {
1253 Object value =
1254 PropertyUtils.getSimpleProperty(bean,
1255 "floatProperty");
1256 assertNotNull("Got a value", value);
1257 assertTrue("Got correct type", (value instanceof Float));
1258 assertEquals("Got correct value",
1259 ((Float) value).floatValue(),
1260 (float) 123.0,
1261 (float) 0.005);
1262 } catch (IllegalAccessException e) {
1263 fail("IllegalAccessException");
1264 } catch (IllegalArgumentException e) {
1265 fail("IllegalArgumentException");
1266 } catch (InvocationTargetException e) {
1267 fail("InvocationTargetException");
1268 } catch (NoSuchMethodException e) {
1269 fail("NoSuchMethodException");
1270 }
1271
1272 }
1273
1274
1275 /***
1276 * Negative test getSimpleProperty on an indexed property.
1277 */
1278 public void testGetSimpleIndexed() {
1279
1280 Object value = null;
1281 try {
1282 value = PropertyUtils.getSimpleProperty(bean,
1283 "intIndexed[0]");
1284 fail("Should have thrown IllegalArgumentException");
1285 } catch (IllegalAccessException e) {
1286 fail("IllegalAccessException");
1287 } catch (IllegalArgumentException e) {
1288
1289 } catch (InvocationTargetException e) {
1290 fail("InvocationTargetException");
1291 } catch (NoSuchMethodException e) {
1292 fail("NoSuchMethodException");
1293 }
1294
1295 }
1296
1297
1298 /***
1299 * Test getSimpleProperty on an int property.
1300 */
1301 public void testGetSimpleInt() {
1302
1303 try {
1304 Object value =
1305 PropertyUtils.getSimpleProperty(bean,
1306 "intProperty");
1307 assertNotNull("Got a value", value);
1308 assertTrue("Got correct type", (value instanceof Integer));
1309 assertEquals("Got correct value",
1310 ((Integer) value).intValue(),
1311 123);
1312 } catch (IllegalAccessException e) {
1313 fail("IllegalAccessException");
1314 } catch (IllegalArgumentException e) {
1315 fail("IllegalArgumentException");
1316 } catch (InvocationTargetException e) {
1317 fail("InvocationTargetException");
1318 } catch (NoSuchMethodException e) {
1319 fail("NoSuchMethodException");
1320 }
1321
1322 }
1323
1324
1325 /***
1326 * Test getSimpleProperty on a long property.
1327 */
1328 public void testGetSimpleLong() {
1329
1330 try {
1331 Object value =
1332 PropertyUtils.getSimpleProperty(bean,
1333 "longProperty");
1334 assertNotNull("Got a value", value);
1335 assertTrue("Got correct type", (value instanceof Long));
1336 assertEquals("Got correct value",
1337 ((Long) value).longValue(),
1338 321);
1339 } catch (IllegalAccessException e) {
1340 fail("IllegalAccessException");
1341 } catch (IllegalArgumentException e) {
1342 fail("IllegalArgumentException");
1343 } catch (InvocationTargetException e) {
1344 fail("InvocationTargetException");
1345 } catch (NoSuchMethodException e) {
1346 fail("NoSuchMethodException");
1347 }
1348
1349 }
1350
1351
1352 /***
1353 * Negative test getSimpleProperty on a nested property.
1354 */
1355 public void testGetSimpleNested() {
1356
1357 Object value = null;
1358 try {
1359 value = PropertyUtils.getSimpleProperty(bean,
1360 "nested.stringProperty");
1361 fail("Should have thrown IllegaArgumentException");
1362 } catch (IllegalAccessException e) {
1363 fail("IllegalAccessException");
1364 } catch (IllegalArgumentException e) {
1365
1366 } catch (InvocationTargetException e) {
1367 fail("InvocationTargetException");
1368 } catch (NoSuchMethodException e) {
1369 fail("NoSuchMethodException");
1370 }
1371
1372 }
1373
1374
1375 /***
1376 * Test getSimpleProperty on a short property.
1377 */
1378 public void testGetSimpleShort() {
1379
1380 try {
1381 Object value =
1382 PropertyUtils.getSimpleProperty(bean,
1383 "shortProperty");
1384 assertNotNull("Got a value", value);
1385 assertTrue("Got correct type", (value instanceof Short));
1386 assertEquals("Got correct value",
1387 ((Short) value).shortValue(),
1388 (short) 987);
1389 } catch (IllegalAccessException e) {
1390 fail("IllegalAccessException");
1391 } catch (IllegalArgumentException e) {
1392 fail("IllegalArgumentException");
1393 } catch (InvocationTargetException e) {
1394 fail("InvocationTargetException");
1395 } catch (NoSuchMethodException e) {
1396 fail("NoSuchMethodException");
1397 }
1398
1399 }
1400
1401
1402 /***
1403 * Test getSimpleProperty on a String property.
1404 */
1405 public void testGetSimpleString() {
1406
1407 try {
1408 Object value =
1409 PropertyUtils.getSimpleProperty(bean,
1410 "stringProperty");
1411 assertNotNull("Got a value", value);
1412 assertTrue("Got correct type", (value instanceof String));
1413 assertEquals("Got correct value",
1414 (String) value,
1415 "This is a string");
1416 } catch (IllegalAccessException e) {
1417 fail("IllegalAccessException");
1418 } catch (IllegalArgumentException e) {
1419 fail("IllegalArgumentException");
1420 } catch (InvocationTargetException e) {
1421 fail("InvocationTargetException");
1422 } catch (NoSuchMethodException e) {
1423 fail("NoSuchMethodException");
1424 }
1425
1426 }
1427
1428
1429 /***
1430 * Negative test getSimpleProperty on an unknown property.
1431 */
1432 public void testGetSimpleUnknown() {
1433
1434 try {
1435 PropertyUtils.getSimpleProperty(bean, "unknown");
1436 fail("Should have thrown NoSuchMethodException");
1437 } catch (IllegalAccessException e) {
1438 fail("IllegalAccessException");
1439 } catch (IllegalArgumentException e) {
1440 fail("IllegalArgumentException");
1441 } catch (InvocationTargetException e) {
1442 fail("InvocationTargetException");
1443 } catch (NoSuchMethodException e) {
1444
1445 assertEquals("Unknown property 'unknown' on dynaclass '" +
1446 ((DynaBean) bean).getDynaClass() + "'", e.getMessage() );
1447 }
1448
1449 }
1450
1451
1452 /***
1453 * Corner cases on setIndexedProperty invalid arguments.
1454 */
1455 public void testSetIndexedArguments() {
1456
1457
1458
1459 try {
1460 PropertyUtils.setIndexedProperty(null, "intArray", 0,
1461 new Integer(1));
1462 fail("Should throw IllegalArgumentException 1");
1463 } catch (IllegalArgumentException e) {
1464
1465 } catch (Throwable t) {
1466 fail("Threw " + t + " instead of IllegalArgumentException 1");
1467 }
1468
1469 try {
1470 PropertyUtils.setIndexedProperty(bean, null, 0,
1471 new Integer(1));
1472 fail("Should throw IllegalArgumentException 2");
1473 } catch (IllegalArgumentException e) {
1474
1475 } catch (Throwable t) {
1476 fail("Threw " + t + " instead of IllegalArgumentException 2");
1477 }
1478
1479
1480
1481 try {
1482 PropertyUtils.setIndexedProperty(null,
1483 "intArray[0]",
1484 new Integer(1));
1485 fail("Should throw IllegalArgumentException 3");
1486 } catch (IllegalArgumentException e) {
1487
1488 } catch (Throwable t) {
1489 fail("Threw " + t + " instead of IllegalArgumentException 3");
1490 }
1491
1492 try {
1493 PropertyUtils.setIndexedProperty(bean, "[0]",
1494 new Integer(1));
1495 fail("Should throw NoSuchMethodException 4");
1496 } catch (NoSuchMethodException e) {
1497
1498 } catch (Throwable t) {
1499 fail("Threw " + t + " instead of NoSuchMethodException 4");
1500 }
1501
1502 try {
1503 PropertyUtils.setIndexedProperty(bean, "intArray",
1504 new Integer(1));
1505 fail("Should throw IllegalArgumentException 5");
1506 } catch (IllegalArgumentException e) {
1507
1508 } catch (Throwable t) {
1509 fail("Threw " + t + " instead of IllegalArgumentException 5");
1510 }
1511
1512
1513
1514 try {
1515 PropertyUtils.setIndexedProperty(null, "intIndexed", 0,
1516 new Integer(1));
1517 fail("Should throw IllegalArgumentException 1");
1518 } catch (IllegalArgumentException e) {
1519
1520 } catch (Throwable t) {
1521 fail("Threw " + t + " instead of IllegalArgumentException 1");
1522 }
1523
1524 try {
1525 PropertyUtils.setIndexedProperty(bean, null, 0,
1526 new Integer(1));
1527 fail("Should throw IllegalArgumentException 2");
1528 } catch (IllegalArgumentException e) {
1529
1530 } catch (Throwable t) {
1531 fail("Threw " + t + " instead of IllegalArgumentException 2");
1532 }
1533
1534
1535
1536 try {
1537 PropertyUtils.setIndexedProperty(null,
1538 "intIndexed[0]",
1539 new Integer(1));
1540 fail("Should throw IllegalArgumentException 3");
1541 } catch (IllegalArgumentException e) {
1542
1543 } catch (Throwable t) {
1544 fail("Threw " + t + " instead of IllegalArgumentException 3");
1545 }
1546
1547 try {
1548 PropertyUtils.setIndexedProperty(bean, "[0]",
1549 new Integer(1));
1550 fail("Should throw NoSuchMethodException 4");
1551 } catch (NoSuchMethodException e) {
1552
1553 } catch (Throwable t) {
1554 fail("Threw " + t + " instead of NoSuchMethodException 4");
1555 }
1556
1557 try {
1558 PropertyUtils.setIndexedProperty(bean, "intIndexed",
1559 new Integer(1));
1560 fail("Should throw IllegalArgumentException 5");
1561 } catch (IllegalArgumentException e) {
1562
1563 } catch (Throwable t) {
1564 fail("Threw " + t + " instead of IllegalArgumentException 5");
1565 }
1566
1567 }
1568
1569
1570 /***
1571 * Positive and negative tests on setIndexedProperty valid arguments.
1572 */
1573 public void testSetIndexedValues() {
1574
1575 Object value = null;
1576
1577
1578
1579 try {
1580 PropertyUtils.setIndexedProperty(bean,
1581 "intArray", 0,
1582 new Integer(1));
1583 value =
1584 PropertyUtils.getIndexedProperty(bean,
1585 "intArray", 0);
1586 assertNotNull("Returned new value 0", value);
1587 assertTrue("Returned Integer new value 0",
1588 value instanceof Integer);
1589 assertEquals("Returned correct new value 0", 1,
1590 ((Integer) value).intValue());
1591 } catch (Throwable t) {
1592 fail("Threw " + t);
1593 }
1594
1595 try {
1596 PropertyUtils.setIndexedProperty(bean,
1597 "intIndexed", 1,
1598 new Integer(11));
1599 value =
1600 PropertyUtils.getIndexedProperty(bean,
1601 "intIndexed", 1);
1602 assertNotNull("Returned new value 1", value);
1603 assertTrue("Returned Integer new value 1",
1604 value instanceof Integer);
1605 assertEquals("Returned correct new value 1", 11,
1606 ((Integer) value).intValue());
1607 } catch (Throwable t) {
1608 fail("Threw " + t);
1609 }
1610
1611 try {
1612 PropertyUtils.setIndexedProperty(bean,
1613 "listIndexed", 2,
1614 "New Value 2");
1615 value =
1616 PropertyUtils.getIndexedProperty(bean,
1617 "listIndexed", 2);
1618 assertNotNull("Returned new value 2", value);
1619 assertTrue("Returned String new value 2",
1620 value instanceof String);
1621 assertEquals("Returned correct new value 2", "New Value 2",
1622 (String) value);
1623 } catch (Throwable t) {
1624 fail("Threw " + t);
1625 }
1626
1627 try {
1628 PropertyUtils.setIndexedProperty(bean,
1629 "stringArray", 2,
1630 "New Value 2");
1631 value =
1632 PropertyUtils.getIndexedProperty(bean,
1633 "stringArray", 2);
1634 assertNotNull("Returned new value 2", value);
1635 assertTrue("Returned String new value 2",
1636 value instanceof String);
1637 assertEquals("Returned correct new value 2", "New Value 2",
1638 (String) value);
1639 } catch (Throwable t) {
1640 fail("Threw " + t);
1641 }
1642
1643 try {
1644 PropertyUtils.setIndexedProperty(bean,
1645 "stringArray", 3,
1646 "New Value 3");
1647 value =
1648 PropertyUtils.getIndexedProperty(bean,
1649 "stringArray", 3);
1650 assertNotNull("Returned new value 3", value);
1651 assertTrue("Returned String new value 3",
1652 value instanceof String);
1653 assertEquals("Returned correct new value 3", "New Value 3",
1654 (String) value);
1655 } catch (Throwable t) {
1656 fail("Threw " + t);
1657 }
1658
1659
1660
1661 try {
1662 PropertyUtils.setIndexedProperty(bean,
1663 "intArray[4]",
1664 new Integer(1));
1665 value =
1666 PropertyUtils.getIndexedProperty(bean,
1667 "intArray[4]");
1668 assertNotNull("Returned new value 4", value);
1669 assertTrue("Returned Integer new value 4",
1670 value instanceof Integer);
1671 assertEquals("Returned correct new value 4", 1,
1672 ((Integer) value).intValue());
1673 } catch (Throwable t) {
1674 fail("Threw " + t);
1675 }
1676
1677 try {
1678 PropertyUtils.setIndexedProperty(bean,
1679 "intIndexed[3]",
1680 new Integer(11));
1681 value =
1682 PropertyUtils.getIndexedProperty(bean,
1683 "intIndexed[3]");
1684 assertNotNull("Returned new value 5", value);
1685 assertTrue("Returned Integer new value 5",
1686 value instanceof Integer);
1687 assertEquals("Returned correct new value 5", 11,
1688 ((Integer) value).intValue());
1689 } catch (Throwable t) {
1690 fail("Threw " + t);
1691 }
1692
1693 try {
1694 PropertyUtils.setIndexedProperty(bean,
1695 "listIndexed[1]",
1696 "New Value 2");
1697 value =
1698 PropertyUtils.getIndexedProperty(bean,
1699 "listIndexed[1]");
1700 assertNotNull("Returned new value 6", value);
1701 assertTrue("Returned String new value 6",
1702 value instanceof String);
1703 assertEquals("Returned correct new value 6", "New Value 2",
1704 (String) value);
1705 } catch (Throwable t) {
1706 fail("Threw " + t);
1707 }
1708
1709 try {
1710 PropertyUtils.setIndexedProperty(bean,
1711 "stringArray[1]",
1712 "New Value 2");
1713 value =
1714 PropertyUtils.getIndexedProperty(bean,
1715 "stringArray[2]");
1716 assertNotNull("Returned new value 6", value);
1717 assertTrue("Returned String new value 6",
1718 value instanceof String);
1719 assertEquals("Returned correct new value 6", "New Value 2",
1720 (String) value);
1721 } catch (Throwable t) {
1722 fail("Threw " + t);
1723 }
1724
1725 try {
1726 PropertyUtils.setIndexedProperty(bean,
1727 "stringArray[0]",
1728 "New Value 3");
1729 value =
1730 PropertyUtils.getIndexedProperty(bean,
1731 "stringArray[0]");
1732 assertNotNull("Returned new value 7", value);
1733 assertTrue("Returned String new value 7",
1734 value instanceof String);
1735 assertEquals("Returned correct new value 7", "New Value 3",
1736 (String) value);
1737 } catch (Throwable t) {
1738 fail("Threw " + t);
1739 }
1740
1741
1742
1743 try {
1744 PropertyUtils.setIndexedProperty(bean,
1745 "intArray", -1,
1746 new Integer(0));
1747 fail("Should have thrown ArrayIndexOutOfBoundsException");
1748 } catch (ArrayIndexOutOfBoundsException t) {
1749
1750 } catch (Throwable t) {
1751 fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException");
1752 }
1753
1754 try {
1755 PropertyUtils.setIndexedProperty(bean,
1756 "intArray", 5,
1757 new Integer(0));
1758 fail("Should have thrown ArrayIndexOutOfBoundsException");
1759 } catch (ArrayIndexOutOfBoundsException t) {
1760
1761 } catch (Throwable t) {
1762 fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException");
1763 }
1764
1765 try {
1766 PropertyUtils.setIndexedProperty(bean,
1767 "intIndexed", -1,
1768 new Integer(0));
1769 fail("Should have thrown ArrayIndexOutOfBoundsException");
1770 } catch (ArrayIndexOutOfBoundsException t) {
1771
1772 } catch (Throwable t) {
1773 fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException");
1774 }
1775
1776 try {
1777 PropertyUtils.setIndexedProperty(bean,
1778 "intIndexed", 5,
1779 new Integer(0));
1780 fail("Should have thrown ArrayIndexOutOfBoundsException");
1781 } catch (ArrayIndexOutOfBoundsException t) {
1782
1783 } catch (Throwable t) {
1784 fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException");
1785 }
1786
1787 try {
1788 PropertyUtils.setIndexedProperty(bean,
1789 "listIndexed", 5,
1790 "New String");
1791 fail("Should have thrown IndexOutOfBoundsException");
1792 } catch (IndexOutOfBoundsException t) {
1793
1794 } catch (Throwable t) {
1795 fail("Threw " + t + " instead of IndexOutOfBoundsException");
1796 }
1797
1798 try {
1799 PropertyUtils.setIndexedProperty(bean,
1800 "listIndexed", -1,
1801 "New String");
1802 fail("Should have thrown IndexOutOfBoundsException");
1803 } catch (IndexOutOfBoundsException t) {
1804
1805 } catch (Throwable t) {
1806 fail("Threw " + t + " instead of IndexOutOfBoundsException");
1807 }
1808
1809 try {
1810 PropertyUtils.setIndexedProperty(bean,
1811 "stringArray", -1,
1812 "New String");
1813 fail("Should have thrown ArrayIndexOutOfBoundsException");
1814 } catch (ArrayIndexOutOfBoundsException t) {
1815
1816 } catch (Throwable t) {
1817 fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException");
1818 }
1819
1820 try {
1821 PropertyUtils.setIndexedProperty(bean,
1822 "stringArray", 5,
1823 "New String");
1824 fail("Should have thrown ArrayIndexOutOfBoundsException");
1825 } catch (ArrayIndexOutOfBoundsException t) {
1826
1827 } catch (Throwable t) {
1828 fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException");
1829 }
1830
1831 try {
1832 PropertyUtils.setIndexedProperty(bean,
1833 "stringIndexed", -1,
1834 "New String");
1835 fail("Should have thrown ArrayIndexOutOfBoundsException");
1836 } catch (ArrayIndexOutOfBoundsException t) {
1837
1838 } catch (Throwable t) {
1839 fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException");
1840 }
1841
1842 try {
1843 PropertyUtils.setIndexedProperty(bean,
1844 "stringIndexed", 5,
1845 "New String");
1846 fail("Should have thrown ArrayIndexOutOfBoundsException");
1847 } catch (ArrayIndexOutOfBoundsException t) {
1848
1849 } catch (Throwable t) {
1850 fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException");
1851 }
1852
1853 }
1854
1855
1856 /***
1857 * Corner cases on getMappedProperty invalid arguments.
1858 */
1859 public void testSetMappedArguments() {
1860
1861
1862
1863 try {
1864 PropertyUtils.setMappedProperty(null, "mappedProperty",
1865 "First Key", "First Value");
1866 fail("Should throw IllegalArgumentException 1");
1867 } catch (IllegalArgumentException e) {
1868
1869 } catch (Throwable t) {
1870 fail("Threw " + t + " instead of IllegalArgumentException 1");
1871 }
1872
1873 try {
1874 PropertyUtils.setMappedProperty(bean, null, "First Key",
1875 "First Value");
1876 fail("Should throw IllegalArgumentException 2");
1877 } catch (IllegalArgumentException e) {
1878
1879 } catch (Throwable t) {
1880 fail("Threw " + t + " instead of IllegalArgumentException 2");
1881 }
1882
1883 try {
1884 PropertyUtils.setMappedProperty(bean, "mappedProperty", null,
1885 "First Value");
1886 fail("Should throw IllegalArgumentException 3");
1887 } catch (IllegalArgumentException e) {
1888
1889 } catch (Throwable t) {
1890 fail("Threw " + t + " instead of IllegalArgumentException 3");
1891 }
1892
1893
1894
1895 try {
1896 PropertyUtils.setMappedProperty(null,
1897 "mappedProperty(First Key)",
1898 "First Value");
1899 fail("Should throw IllegalArgumentException 4");
1900 } catch (IllegalArgumentException e) {
1901
1902 } catch (Throwable t) {
1903 fail("Threw " + t + " instead of IllegalArgumentException 4");
1904 }
1905
1906 try {
1907 PropertyUtils.setMappedProperty(bean, "(Second Key)",
1908 "Second Value");
1909 fail("Should throw IllegalArgumentException 5");
1910 } catch (NoSuchMethodException e) {
1911
1912 } catch (Throwable t) {
1913 fail("Threw " + t + " instead of NoSuchMethodException 5");
1914 }
1915
1916 try {
1917 PropertyUtils.setMappedProperty(bean, "mappedProperty",
1918 "Third Value");
1919 fail("Should throw IllegalArgumentException 6");
1920 } catch (IllegalArgumentException e) {
1921
1922 } catch (Throwable t) {
1923 fail("Threw " + t + " instead of IllegalArgumentException 6");
1924 }
1925
1926 }
1927
1928
1929 /***
1930 * Positive and negative tests on setMappedProperty valid arguments.
1931 */
1932 public void testSetMappedValues() {
1933
1934 Object value = null;
1935
1936
1937
1938 try {
1939 value = PropertyUtils.getMappedProperty(bean, "mappedProperty",
1940 "Fourth Key");
1941 assertNull("Can not find fourth value", value);
1942 } catch (Throwable t) {
1943 fail("Finding fourth value threw " + t);
1944 }
1945
1946 try {
1947 PropertyUtils.setMappedProperty(bean, "mappedProperty",
1948 "Fourth Key", "Fourth Value");
1949 } catch (Throwable t) {
1950 fail("Setting fourth value threw " + t);
1951 }
1952
1953 try {
1954 value = PropertyUtils.getMappedProperty(bean, "mappedProperty",
1955 "Fourth Key");
1956 assertEquals("Can find fourth value", "Fourth Value", value);
1957 } catch (Throwable t) {
1958 fail("Finding fourth value threw " + t);
1959 }
1960
1961
1962
1963 try {
1964 value =
1965 PropertyUtils.getMappedProperty(bean,
1966 "mappedProperty(Fifth Key)");
1967 assertNull("Can not find fifth value", value);
1968 } catch (Throwable t) {
1969 fail("Finding fifth value threw " + t);
1970 }
1971
1972 try {
1973 PropertyUtils.setMappedProperty(bean,
1974 "mappedProperty(Fifth Key)",
1975 "Fifth Value");
1976 } catch (Throwable t) {
1977 fail("Setting fifth value threw " + t);
1978 }
1979
1980 try {
1981 value =
1982 PropertyUtils.getMappedProperty(bean,
1983 "mappedProperty(Fifth Key)");
1984 assertEquals("Can find fifth value", "Fifth Value", value);
1985 } catch (Throwable t) {
1986 fail("Finding fifth value threw " + t);
1987 }
1988
1989
1990
1991 try {
1992 value =
1993 PropertyUtils.getNestedProperty(bean,
1994 "mapProperty.Sixth Key");
1995 assertNull("Can not find sixth value", value);
1996 } catch (Throwable t) {
1997 fail("Finding fifth value threw " + t);
1998 }
1999
2000 try {
2001 PropertyUtils.setNestedProperty(bean,
2002 "mapProperty.Sixth Key",
2003 "Sixth Value");
2004 } catch (Throwable t) {
2005 fail("Setting sixth value threw " + t);
2006 }
2007
2008 try {
2009 value =
2010 PropertyUtils.getNestedProperty(bean,
2011 "mapProperty.Sixth Key");
2012 assertEquals("Can find sixth value", "Sixth Value", value);
2013 } catch (Throwable t) {
2014 fail("Finding sixth value threw " + t);
2015 }
2016
2017 }
2018
2019
2020 /***
2021 * Corner cases on setNestedProperty invalid arguments.
2022 */
2023 public void testSetNestedArguments() {
2024
2025 try {
2026 PropertyUtils.setNestedProperty(null, "stringProperty", "");
2027 fail("Should throw IllegalArgumentException 1");
2028 } catch (IllegalArgumentException e) {
2029
2030 } catch (Throwable t) {
2031 fail("Threw " + t + " instead of IllegalArgumentException 1");
2032 }
2033
2034 try {
2035 PropertyUtils.setNestedProperty(bean, null, "");
2036 fail("Should throw IllegalArgumentException 2");
2037 } catch (IllegalArgumentException e) {
2038
2039 } catch (Throwable t) {
2040 fail("Threw " + t + " instead of IllegalArgumentException 2");
2041 }
2042
2043 }
2044
2045
2046 /***
2047 * Test setNextedProperty on a boolean property.
2048 */
2049 public void testSetNestedBoolean() {
2050
2051 try {
2052 boolean oldValue = nested.getBooleanProperty();
2053 boolean newValue = !oldValue;
2054 PropertyUtils.setNestedProperty(bean,
2055 "nested.booleanProperty",
2056 new Boolean(newValue));
2057 assertTrue("Matched new value",
2058 newValue ==
2059 nested.getBooleanProperty());
2060 } catch (IllegalAccessException e) {
2061 fail("IllegalAccessException");
2062 } catch (IllegalArgumentException e) {
2063 fail("IllegalArgumentException");
2064 } catch (InvocationTargetException e) {
2065 fail("InvocationTargetException");
2066 } catch (NoSuchMethodException e) {
2067 fail("NoSuchMethodException");
2068 }
2069
2070 }
2071
2072
2073 /***
2074 * Test setNestedProperty on a double property.
2075 */
2076 public void testSetNestedDouble() {
2077
2078 try {
2079 double oldValue = nested.getDoubleProperty();
2080 double newValue = oldValue + 1.0;
2081 PropertyUtils.setNestedProperty(bean,
2082 "nested.doubleProperty",
2083 new Double(newValue));
2084 assertEquals("Matched new value",
2085 newValue,
2086 nested.getDoubleProperty(),
2087 0.005);
2088 } catch (IllegalAccessException e) {
2089 fail("IllegalAccessException");
2090 } catch (IllegalArgumentException e) {
2091 fail("IllegalArgumentException");
2092 } catch (InvocationTargetException e) {
2093 fail("InvocationTargetException");
2094 } catch (NoSuchMethodException e) {
2095 fail("NoSuchMethodException");
2096 }
2097
2098 }
2099
2100
2101 /***
2102 * Test setNestedProperty on a float property.
2103 */
2104 public void testSetNestedFloat() {
2105
2106 try {
2107 float oldValue = nested.getFloatProperty();
2108 float newValue = oldValue + (float) 1.0;
2109 PropertyUtils.setNestedProperty(bean,
2110 "nested.floatProperty",
2111 new Float(newValue));
2112 assertEquals("Matched new value",
2113 newValue,
2114 nested.getFloatProperty(),
2115 (float) 0.005);
2116 } catch (IllegalAccessException e) {
2117 fail("IllegalAccessException");
2118 } catch (IllegalArgumentException e) {
2119 fail("IllegalArgumentException");
2120 } catch (InvocationTargetException e) {
2121 fail("InvocationTargetException");
2122 } catch (NoSuchMethodException e) {
2123 fail("NoSuchMethodException");
2124 }
2125
2126 }
2127
2128
2129 /***
2130 * Test setNestedProperty on a int property.
2131 */
2132 public void testSetNestedInt() {
2133
2134 try {
2135 int oldValue = nested.getIntProperty();
2136 int newValue = oldValue + 1;
2137 PropertyUtils.setNestedProperty(bean,
2138 "nested.intProperty",
2139 new Integer(newValue));
2140 assertEquals("Matched new value",
2141 newValue,
2142 nested.getIntProperty());
2143 } catch (IllegalAccessException e) {
2144 fail("IllegalAccessException");
2145 } catch (IllegalArgumentException e) {
2146 fail("IllegalArgumentException");
2147 } catch (InvocationTargetException e) {
2148 fail("InvocationTargetException");
2149 } catch (NoSuchMethodException e) {
2150 fail("NoSuchMethodException");
2151 }
2152
2153 }
2154
2155
2156 /***
2157 * Test setNestedProperty on a long property.
2158 */
2159 public void testSetNestedLong() {
2160
2161 try {
2162 long oldValue = nested.getLongProperty();
2163 long newValue = oldValue + 1;
2164 PropertyUtils.setNestedProperty(bean,
2165 "nested.longProperty",
2166 new Long(newValue));
2167 assertEquals("Matched new value",
2168 newValue,
2169 nested.getLongProperty());
2170 } catch (IllegalAccessException e) {
2171 fail("IllegalAccessException");
2172 } catch (IllegalArgumentException e) {
2173 fail("IllegalArgumentException");
2174 } catch (InvocationTargetException e) {
2175 fail("InvocationTargetException");
2176 } catch (NoSuchMethodException e) {
2177 fail("NoSuchMethodException");
2178 }
2179
2180 }
2181
2182
2183 /***
2184 * Test setNestedProperty on a read-only String property.
2185 */
2186 public void testSetNestedReadOnly() {
2187
2188 try {
2189 String oldValue = nested.getWriteOnlyPropertyValue();
2190 String newValue = oldValue + " Extra Value";
2191 PropertyUtils.setNestedProperty(bean,
2192 "nested.readOnlyProperty",
2193 newValue);
2194 fail("Should have thrown NoSuchMethodException");
2195 } catch (IllegalAccessException e) {
2196 fail("IllegalAccessException");
2197 } catch (IllegalArgumentException e) {
2198 fail("IllegalArgumentException");
2199 } catch (InvocationTargetException e) {
2200 fail("InvocationTargetException");
2201 } catch (NoSuchMethodException e) {
2202
2203 }
2204
2205 }
2206
2207
2208 /***
2209 * Test setNestedProperty on a short property.
2210 */
2211 public void testSetNestedShort() {
2212
2213 try {
2214 short oldValue = nested.getShortProperty();
2215 short newValue = oldValue;
2216 newValue++;
2217 PropertyUtils.setNestedProperty(bean,
2218 "nested.shortProperty",
2219 new Short(newValue));
2220 assertEquals("Matched new value",
2221 newValue,
2222 nested.getShortProperty());
2223 } catch (IllegalAccessException e) {
2224 fail("IllegalAccessException");
2225 } catch (IllegalArgumentException e) {
2226 fail("IllegalArgumentException");
2227 } catch (InvocationTargetException e) {
2228 fail("InvocationTargetException");
2229 } catch (NoSuchMethodException e) {
2230 fail("NoSuchMethodException");
2231 }
2232
2233 }
2234
2235
2236 /***
2237 * Test setNestedProperty on a String property.
2238 */
2239 public void testSetNestedString() {
2240
2241 try {
2242 String oldValue = nested.getStringProperty();
2243 String newValue = oldValue + " Extra Value";
2244 PropertyUtils.setNestedProperty(bean,
2245 "nested.stringProperty",
2246 newValue);
2247 assertEquals("Matched new value",
2248 newValue,
2249 nested.getStringProperty());
2250 } catch (IllegalAccessException e) {
2251 fail("IllegalAccessException");
2252 } catch (IllegalArgumentException e) {
2253 fail("IllegalArgumentException");
2254 } catch (InvocationTargetException e) {
2255 fail("InvocationTargetException");
2256 } catch (NoSuchMethodException e) {
2257 fail("NoSuchMethodException");
2258 }
2259
2260 }
2261
2262
2263 /***
2264 * Test setNestedProperty on an unknown property name.
2265 */
2266 public void testSetNestedUnknown() {
2267
2268 try {
2269 String newValue = "New String Value";
2270 PropertyUtils.setNestedProperty(bean,
2271 "nested.unknown",
2272 newValue);
2273 fail("Should have thrown NoSuchMethodException");
2274 } catch (IllegalAccessException e) {
2275 fail("IllegalAccessException");
2276 } catch (IllegalArgumentException e) {
2277 fail("IllegalArgumentException");
2278 } catch (InvocationTargetException e) {
2279 fail("InvocationTargetException");
2280 } catch (NoSuchMethodException e) {
2281
2282 }
2283
2284 }
2285
2286
2287 /***
2288 * Test setNestedProperty on a write-only String property.
2289 */
2290 public void testSetNestedWriteOnly() {
2291
2292 try {
2293 String oldValue = nested.getWriteOnlyPropertyValue();
2294 String newValue = oldValue + " Extra Value";
2295 PropertyUtils.setNestedProperty(bean,
2296 "nested.writeOnlyProperty",
2297 newValue);
2298 assertEquals("Matched new value",
2299 newValue,
2300 nested.getWriteOnlyPropertyValue());
2301 } catch (IllegalAccessException e) {
2302 fail("IllegalAccessException");
2303 } catch (IllegalArgumentException e) {
2304 fail("IllegalArgumentException");
2305 } catch (InvocationTargetException e) {
2306 fail("InvocationTargetException");
2307 } catch (NoSuchMethodException e) {
2308 fail("NoSuchMethodException");
2309 }
2310
2311 }
2312
2313
2314 /***
2315 * Corner cases on setSimpleProperty invalid arguments.
2316 */
2317 public void testSetSimpleArguments() {
2318
2319 try {
2320 PropertyUtils.setSimpleProperty(null, "stringProperty", "");
2321 fail("Should throw IllegalArgumentException 1");
2322 } catch (IllegalArgumentException e) {
2323
2324 } catch (Throwable t) {
2325 fail("Threw " + t + " instead of IllegalArgumentException 1");
2326 }
2327
2328 try {
2329 PropertyUtils.setSimpleProperty(bean, null, "");
2330 fail("Should throw IllegalArgumentException 2");
2331 } catch (IllegalArgumentException e) {
2332
2333 } catch (Throwable t) {
2334 fail("Threw " + t + " instead of IllegalArgumentException 2");
2335 }
2336
2337 }
2338
2339
2340 /***
2341 * Test setSimpleProperty on a boolean property.
2342 */
2343 public void testSetSimpleBoolean() {
2344
2345 try {
2346 boolean oldValue = ((Boolean) bean.get("booleanProperty")).booleanValue();
2347 boolean newValue = !oldValue;
2348 PropertyUtils.setSimpleProperty(bean,
2349 "booleanProperty",
2350 new Boolean(newValue));
2351 assertTrue("Matched new value",
2352 newValue ==
2353 ((Boolean) bean.get("booleanProperty")).booleanValue());
2354 } catch (IllegalAccessException e) {
2355 fail("IllegalAccessException");
2356 } catch (IllegalArgumentException e) {
2357 fail("IllegalArgumentException");
2358 } catch (InvocationTargetException e) {
2359 fail("InvocationTargetException");
2360 } catch (NoSuchMethodException e) {
2361 fail("NoSuchMethodException");
2362 }
2363
2364 }
2365
2366
2367 /***
2368 * Test setSimpleProperty on a double property.
2369 */
2370 public void testSetSimpleDouble() {
2371
2372 try {
2373 double oldValue = ((Double) bean.get("doubleProperty")).doubleValue();
2374 double newValue = oldValue + 1.0;
2375 PropertyUtils.setSimpleProperty(bean,
2376 "doubleProperty",
2377 new Double(newValue));
2378 assertEquals("Matched new value",
2379 newValue,
2380 ((Double) bean.get("doubleProperty")).doubleValue(),
2381 0.005);
2382 } catch (IllegalAccessException e) {
2383 fail("IllegalAccessException");
2384 } catch (IllegalArgumentException e) {
2385 fail("IllegalArgumentException");
2386 } catch (InvocationTargetException e) {
2387 fail("InvocationTargetException");
2388 } catch (NoSuchMethodException e) {
2389 fail("NoSuchMethodException");
2390 }
2391
2392 }
2393
2394
2395 /***
2396 * Test setSimpleProperty on a float property.
2397 */
2398 public void testSetSimpleFloat() {
2399
2400 try {
2401 float oldValue = ((Float) bean.get("floatProperty")).floatValue();
2402 float newValue = oldValue + (float) 1.0;
2403 PropertyUtils.setSimpleProperty(bean,
2404 "floatProperty",
2405 new Float(newValue));
2406 assertEquals("Matched new value",
2407 newValue,
2408 ((Float) bean.get("floatProperty")).floatValue(),
2409 (float) 0.005);
2410 } catch (IllegalAccessException e) {
2411 fail("IllegalAccessException");
2412 } catch (IllegalArgumentException e) {
2413 fail("IllegalArgumentException");
2414 } catch (InvocationTargetException e) {
2415 fail("InvocationTargetException");
2416 } catch (NoSuchMethodException e) {
2417 fail("NoSuchMethodException");
2418 }
2419
2420 }
2421
2422
2423 /***
2424 * Negative test setSimpleProperty on an indexed property.
2425 */
2426 public void testSetSimpleIndexed() {
2427
2428 try {
2429 PropertyUtils.setSimpleProperty(bean,
2430 "stringIndexed[0]",
2431 "New String Value");
2432 fail("Should have thrown IllegalArgumentException");
2433 } catch (IllegalAccessException e) {
2434 fail("IllegalAccessException");
2435 } catch (IllegalArgumentException e) {
2436
2437 } catch (InvocationTargetException e) {
2438 fail("InvocationTargetException");
2439 } catch (NoSuchMethodException e) {
2440 fail("NoSuchMethodException");
2441 }
2442
2443 }
2444
2445
2446 /***
2447 * Test setSimpleProperty on a int property.
2448 */
2449 public void testSetSimpleInt() {
2450
2451 try {
2452 int oldValue = ((Integer) bean.get("intProperty")).intValue();
2453 int newValue = oldValue + 1;
2454 PropertyUtils.setSimpleProperty(bean,
2455 "intProperty",
2456 new Integer(newValue));
2457 assertEquals("Matched new value",
2458 newValue,
2459 ((Integer) bean.get("intProperty")).intValue());
2460 } catch (IllegalAccessException e) {
2461 fail("IllegalAccessException");
2462 } catch (IllegalArgumentException e) {
2463 fail("IllegalArgumentException");
2464 } catch (InvocationTargetException e) {
2465 fail("InvocationTargetException");
2466 } catch (NoSuchMethodException e) {
2467 fail("NoSuchMethodException");
2468 }
2469
2470 }
2471
2472
2473 /***
2474 * Test setSimpleProperty on a long property.
2475 */
2476 public void testSetSimpleLong() {
2477
2478 try {
2479 long oldValue = ((Long) bean.get("longProperty")).longValue();
2480 long newValue = oldValue + 1;
2481 PropertyUtils.setSimpleProperty(bean,
2482 "longProperty",
2483 new Long(newValue));
2484 assertEquals("Matched new value",
2485 newValue,
2486 ((Long) bean.get("longProperty")).longValue());
2487 } catch (IllegalAccessException e) {
2488 fail("IllegalAccessException");
2489 } catch (IllegalArgumentException e) {
2490 fail("IllegalArgumentException");
2491 } catch (InvocationTargetException e) {
2492 fail("InvocationTargetException");
2493 } catch (NoSuchMethodException e) {
2494 fail("NoSuchMethodException");
2495 }
2496
2497 }
2498
2499
2500 /***
2501 * Negative test setSimpleProperty on a nested property.
2502 */
2503 public void testSetSimpleNested() {
2504
2505 try {
2506 PropertyUtils.setSimpleProperty(bean,
2507 "nested.stringProperty",
2508 "New String Value");
2509 fail("Should have thrown IllegalArgumentException");
2510 } catch (IllegalAccessException e) {
2511 fail("IllegalAccessException");
2512 } catch (IllegalArgumentException e) {
2513
2514 } catch (InvocationTargetException e) {
2515 fail("InvocationTargetException");
2516 } catch (NoSuchMethodException e) {
2517 fail("NoSuchMethodException");
2518 }
2519
2520 }
2521
2522
2523 /***
2524 * Test setSimpleProperty on a short property.
2525 */
2526 public void testSetSimpleShort() {
2527
2528 try {
2529 short oldValue = ((Short) bean.get("shortProperty")).shortValue();
2530 short newValue = oldValue;
2531 newValue++;
2532 PropertyUtils.setSimpleProperty(bean,
2533 "shortProperty",
2534 new Short(newValue));
2535 assertEquals("Matched new value",
2536 newValue,
2537 ((Short) bean.get("shortProperty")).shortValue());
2538 } catch (IllegalAccessException e) {
2539 fail("IllegalAccessException");
2540 } catch (IllegalArgumentException e) {
2541 fail("IllegalArgumentException");
2542 } catch (InvocationTargetException e) {
2543 fail("InvocationTargetException");
2544 } catch (NoSuchMethodException e) {
2545 fail("NoSuchMethodException");
2546 }
2547
2548 }
2549
2550
2551 /***
2552 * Test setSimpleProperty on a String property.
2553 */
2554 public void testSetSimpleString() {
2555
2556 try {
2557 String oldValue = (String) bean.get("stringProperty");
2558 String newValue = oldValue + " Extra Value";
2559 PropertyUtils.setSimpleProperty(bean,
2560 "stringProperty",
2561 newValue);
2562 assertEquals("Matched new value",
2563 newValue,
2564 (String) bean.get("stringProperty"));
2565 } catch (IllegalAccessException e) {
2566 fail("IllegalAccessException");
2567 } catch (IllegalArgumentException e) {
2568 fail("IllegalArgumentException");
2569 } catch (InvocationTargetException e) {
2570 fail("InvocationTargetException");
2571 } catch (NoSuchMethodException e) {
2572 fail("NoSuchMethodException");
2573 }
2574
2575 }
2576
2577
2578 /***
2579 * Test setSimpleProperty on an unknown property name.
2580 */
2581 public void testSetSimpleUnknown() {
2582
2583 try {
2584 String newValue = "New String Value";
2585 PropertyUtils.setSimpleProperty(bean,
2586 "unknown",
2587 newValue);
2588 fail("Should have thrown NoSuchMethodException");
2589 } catch (IllegalAccessException e) {
2590 fail("IllegalAccessException");
2591 } catch (IllegalArgumentException e) {
2592 fail("IllegalArgumentException");
2593 } catch (InvocationTargetException e) {
2594 fail("InvocationTargetException");
2595 } catch (NoSuchMethodException e) {
2596
2597 assertEquals("Unknown property 'unknown' on dynaclass '" +
2598 ((DynaBean) bean).getDynaClass() + "'", e.getMessage() );
2599 }
2600
2601 }
2602
2603
2604
2605
2606
2607 /***
2608 * Create and return a <code>DynaClass</code> instance for our test
2609 * <code>DynaBean</code>.
2610 */
2611 protected DynaClass createDynaClass() {
2612
2613 int intArray[] = new int[0];
2614 String stringArray[] = new String[0];
2615
2616 DynaClass dynaClass = new BasicDynaClass
2617 ("TestDynaClass", null,
2618 new DynaProperty[]{
2619 new DynaProperty("booleanProperty", Boolean.TYPE),
2620 new DynaProperty("booleanSecond", Boolean.TYPE),
2621 new DynaProperty("doubleProperty", Double.TYPE),
2622 new DynaProperty("dupProperty", stringArray.getClass()),
2623 new DynaProperty("floatProperty", Float.TYPE),
2624 new DynaProperty("intArray", intArray.getClass()),
2625 new DynaProperty("intIndexed", intArray.getClass()),
2626 new DynaProperty("intProperty", Integer.TYPE),
2627 new DynaProperty("listIndexed", List.class),
2628 new DynaProperty("longProperty", Long.TYPE),
2629 new DynaProperty("mapProperty", Map.class),
2630 new DynaProperty("mappedObjects", Map.class),
2631 new DynaProperty("mappedProperty", Map.class),
2632 new DynaProperty("mappedIntProperty", Map.class),
2633 new DynaProperty("nested", TestBean.class),
2634 new DynaProperty("nullProperty", String.class),
2635 new DynaProperty("shortProperty", Short.TYPE),
2636 new DynaProperty("stringArray", stringArray.getClass()),
2637 new DynaProperty("stringIndexed", stringArray.getClass()),
2638 new DynaProperty("stringProperty", String.class),
2639 });
2640 return (dynaClass);
2641
2642 }
2643
2644
2645 }