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