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