Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/apache/commons/beanutils/DynaBeanUtilsTestCase.java


1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */ 
16  
17  package org.apache.commons.beanutils;
18  
19  
20  import java.lang.reflect.InvocationTargetException;
21  import java.util.ArrayList;
22  import java.util.HashMap;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.Map;
26  
27  import junit.framework.TestCase;
28  import junit.framework.Test;
29  import junit.framework.TestSuite;
30  
31  
32  /**
33   * Test case for BeanUtils when the underlying bean is actually a DynaBean.
34   *
35   * @author Craig R. McClanahan
36   * @version $Revision: 1.22 $ $Date: 2004/02/28 13:18:36 $
37   */
38  
39  public class DynaBeanUtilsTestCase 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 nested bean pointed at by the "nested" property.
53       */
54      protected TestBean nested = null;
55  
56  
57      /**
58       * The set of properties that should be described.
59       */
60      protected String describes[] =
61      { "booleanProperty",
62        "booleanSecond",
63        "byteProperty",
64        "doubleProperty",
65        "dupProperty",
66        "floatProperty",
67        "intArray",
68        "intIndexed",
69        "intProperty",
70        "listIndexed",
71        "longProperty",
72        "mapProperty",
73        "mappedProperty",
74        "mappedIntProperty",
75        "nested",
76        "nullProperty",
77        //      "readOnlyProperty",
78        "shortProperty",
79        "stringArray",
80        "stringIndexed",
81        "stringProperty"
82      };
83  
84  
85      // ----------------------------------------------------------- Constructors
86  
87  
88      /**
89       * Construct a new instance of this test case.
90       *
91       * @param name Name of the test case
92       */
93      public DynaBeanUtilsTestCase(String name) {
94  
95          super(name);
96  
97      }
98  
99  
100     // --------------------------------------------------- Overall Test Methods
101 
102 
103     /**
104      * Set up instance variables required by this test case.
105      */
106     public void setUp() throws Exception {
107 
108         // Instantiate a new DynaBean instance
109         DynaClass dynaClass = createDynaClass();
110         bean = dynaClass.newInstance();
111 
112         // Initialize the DynaBean's property values (like TestBean)
113         bean.set("booleanProperty", new Boolean(true));
114         bean.set("booleanSecond", new Boolean(true));
115         bean.set("byteProperty", new Byte((byte) 121));
116         bean.set("doubleProperty", new Double(321.0));
117         bean.set("floatProperty", new Float((float) 123.0));
118         String dupProperty[] = { "Dup 0", "Dup 1", "Dup 2", "Dup 3", "Dup 4"};
119         bean.set("dupProperty", dupProperty);
120         int intArray[] = { 0, 10, 20, 30, 40 };
121         bean.set("intArray", intArray);
122         int intIndexed[] = { 0, 10, 20, 30, 40 };
123         bean.set("intIndexed", intIndexed);
124         bean.set("intProperty", new Integer(123));
125         List listIndexed = new ArrayList();
126         listIndexed.add("String 0");
127         listIndexed.add("String 1");
128         listIndexed.add("String 2");
129         listIndexed.add("String 3");
130         listIndexed.add("String 4");
131         bean.set("listIndexed", listIndexed);
132         bean.set("longProperty", new Long((long) 321));
133         HashMap mapProperty = new HashMap();
134         mapProperty.put("First Key", "First Value");
135         mapProperty.put("Second Key", "Second Value");
136         bean.set("mapProperty", mapProperty);
137         HashMap mappedProperty = new HashMap();
138         mappedProperty.put("First Key", "First Value");
139         mappedProperty.put("Second Key", "Second Value");
140         bean.set("mappedProperty", mappedProperty);
141         HashMap mappedIntProperty = new HashMap();
142         mappedIntProperty.put("One", new Integer(1));
143         mappedIntProperty.put("Two", new Integer(2));
144         bean.set("mappedIntProperty", mappedIntProperty);
145         nested = new TestBean();
146         bean.set("nested", nested);
147         // Property "nullProperty" is not initialized, so it should return null
148         bean.set("shortProperty", new Short((short) 987));
149         String stringArray[] =
150                 { "String 0", "String 1", "String 2", "String 3", "String 4" };
151         bean.set("stringArray", stringArray);
152         String stringIndexed[] =
153                 { "String 0", "String 1", "String 2", "String 3", "String 4" };
154         bean.set("stringIndexed", stringIndexed);
155         bean.set("stringProperty", "This is a string");
156 
157     }
158 
159 
160     /**
161      * Return the tests included in this test suite.
162      */
163     public static Test suite() {
164 
165         return (new TestSuite(DynaBeanUtilsTestCase.class));
166 
167     }
168 
169 
170     /**
171      * Tear down instance variables required by this test case.
172      */
173     public void tearDown() {
174 
175         bean = null;
176         nested = null;
177 
178     }
179 
180 
181 
182     // ------------------------------------------------ Individual Test Methods
183 
184     /**
185      * Test the cloneBean() method from a DynaBean.
186      */
187     public void testCloneDynaBean() {
188 
189         // Set up an origin bean with customized properties
190         DynaClass dynaClass = DynaBeanUtilsTestCase.createDynaClass();
191         DynaBean orig = null;
192         try {
193             orig = dynaClass.newInstance();
194         } catch (Exception e) {
195             fail("newInstance(): " + e);
196         }
197         orig.set("booleanProperty", Boolean.FALSE);
198         orig.set("byteProperty", new Byte((byte)111));
199         orig.set("doubleProperty", new Double(333.33));
200         orig.set("dupProperty", new String[] { "New 0", "New 1", "New 2" });
201         orig.set("intArray", new int[] { 100, 200, 300 });
202         orig.set("intProperty", new Integer(333));
203         orig.set("longProperty", new Long(3333));
204         orig.set("shortProperty", new Short((short) 33));
205         orig.set("stringArray", new String[] { "New 0", "New 1" });
206         orig.set("stringProperty", "Custom string");
207 
208         // Copy the origin bean to our destination test bean
209         DynaBean clonedBean = null;
210         try {
211             clonedBean = (DynaBean) BeanUtils.cloneBean(orig);
212         } catch (Exception e) {
213             fail("Threw exception: " + e);
214         }
215 
216         // Validate the results for scalar properties
217         assertEquals("Cloned boolean property",
218                      false,
219                      ((Boolean) clonedBean.get("booleanProperty")).booleanValue());
220         assertEquals("Cloned byte property",
221                      (byte) 111,
222                      ((Byte) clonedBean.get("byteProperty")).byteValue());
223         assertEquals("Cloned double property",
224                      333.33,
225                      ((Double) clonedBean.get("doubleProperty")).doubleValue(),
226                      0.005);
227         assertEquals("Cloned int property",
228                      333,
229                      ((Integer) clonedBean.get("intProperty")).intValue());
230         assertEquals("Cloned long property",
231                      (long) 3333,
232                      ((Long) clonedBean.get("longProperty")).longValue());
233         assertEquals("Cloned short property",
234                      (short) 33,
235                      ((Short) clonedBean.get("shortProperty")).shortValue());
236         assertEquals("Cloned string property",
237                      "Custom string",
238                      (String) clonedBean.get("stringProperty"));
239 
240         // Validate the results for array properties
241         String dupProperty[] = (String[]) clonedBean.get("dupProperty");
242         assertNotNull("dupProperty present", dupProperty);
243         assertEquals("dupProperty length", 3, dupProperty.length);
244         assertEquals("dupProperty[0]", "New 0", dupProperty[0]);
245         assertEquals("dupProperty[1]", "New 1", dupProperty[1]);
246         assertEquals("dupProperty[2]", "New 2", dupProperty[2]);
247         int intArray[] = (int[]) clonedBean.get("intArray");
248         assertNotNull("intArray present", intArray);
249         assertEquals("intArray length", 3, intArray.length);
250         assertEquals("intArray[0]", 100, intArray[0]);
251         assertEquals("intArray[1]", 200, intArray[1]);
252         assertEquals("intArray[2]", 300, intArray[2]);
253         String stringArray[] = (String[]) clonedBean.get("stringArray");
254         assertNotNull("stringArray present", stringArray);
255         assertEquals("stringArray length", 2, stringArray.length);
256         assertEquals("stringArray[0]", "New 0", stringArray[0]);
257         assertEquals("stringArray[1]", "New 1", stringArray[1]);
258 
259     }
260 
261     /**
262      * Test the copyProperties() method from a DynaBean.
263      */
264     public void testCopyPropertiesDynaBean() {
265 
266         // Set up an origin bean with customized properties
267         DynaClass dynaClass = DynaBeanUtilsTestCase.createDynaClass();
268         DynaBean orig = null;
269         try {
270             orig = dynaClass.newInstance();
271         } catch (Exception e) {
272             fail("newInstance(): " + e);
273         }
274         orig.set("booleanProperty", Boolean.FALSE);
275         orig.set("byteProperty", new Byte((byte)111));
276         orig.set("doubleProperty", new Double(333.33));
277         orig.set("dupProperty", new String[] { "New 0", "New 1", "New 2" });
278         orig.set("intArray", new int[] { 100, 200, 300 });
279         orig.set("intProperty", new Integer(333));
280         orig.set("longProperty", new Long(3333));
281         orig.set("shortProperty", new Short((short) 33));
282         orig.set("stringArray", new String[] { "New 0", "New 1" });
283         orig.set("stringProperty", "Custom string");
284 
285         // Copy the origin bean to our destination test bean
286         try {
287             BeanUtils.copyProperties(bean, orig);
288         } catch (Exception e) {
289             fail("Threw exception: " + e);
290         }
291 
292         // Validate the results for scalar properties
293         assertEquals("Copied boolean property",
294                      false,
295                      ((Boolean) bean.get("booleanProperty")).booleanValue());
296         assertEquals("Copied byte property",
297                      (byte) 111,
298                      ((Byte) bean.get("byteProperty")).byteValue());
299         assertEquals("Copied double property",
300                      333.33,
301                      ((Double) bean.get("doubleProperty")).doubleValue(),
302                      0.005);
303         assertEquals("Copied int property",
304                      333,
305                      ((Integer) bean.get("intProperty")).intValue());
306         assertEquals("Copied long property",
307                      (long) 3333,
308                      ((Long) bean.get("longProperty")).longValue());
309         assertEquals("Copied short property",
310                      (short) 33,
311                      ((Short) bean.get("shortProperty")).shortValue());
312         assertEquals("Copied string property",
313                      "Custom string",
314                      (String) bean.get("stringProperty"));
315 
316         // Validate the results for array properties
317         String dupProperty[] = (String[]) bean.get("dupProperty");
318         assertNotNull("dupProperty present", dupProperty);
319         assertEquals("dupProperty length", 3, dupProperty.length);
320         assertEquals("dupProperty[0]", "New 0", dupProperty[0]);
321         assertEquals("dupProperty[1]", "New 1", dupProperty[1]);
322         assertEquals("dupProperty[2]", "New 2", dupProperty[2]);
323         int intArray[] = (int[]) bean.get("intArray");
324         assertNotNull("intArray present", intArray);
325         assertEquals("intArray length", 3, intArray.length);
326         assertEquals("intArray[0]", 100, intArray[0]);
327         assertEquals("intArray[1]", 200, intArray[1]);
328         assertEquals("intArray[2]", 300, intArray[2]);
329         String stringArray[] = (String[]) bean.get("stringArray");
330         assertNotNull("stringArray present", stringArray);
331         assertEquals("stringArray length", 2, stringArray.length);
332         assertEquals("stringArray[0]", "New 0", stringArray[0]);
333         assertEquals("stringArray[1]", "New 1", stringArray[1]);
334 
335     }
336 
337 
338     /**
339      * Test copyProperties() when the origin is a a <code>Map</code>.
340      */
341     public void testCopyPropertiesMap() {
342 
343         Map map = new HashMap();
344         map.put("booleanProperty", "false");
345         map.put("byteProperty", "111");
346         map.put("doubleProperty", "333.0");
347         map.put("dupProperty", new String[] { "New 0", "New 1", "New 2" });
348         map.put("floatProperty", "222.0");
349         map.put("intArray", new String[] { "0", "100", "200" });
350         map.put("intProperty", "111");
351         map.put("longProperty", "444");
352         map.put("shortProperty", "555");
353         map.put("stringProperty", "New String Property");
354 
355         try {
356             BeanUtils.copyProperties(bean, map);
357         } catch (Throwable t) {
358             fail("Threw " + t.toString());
359         }
360 
361         // Scalar properties
362         assertEquals("booleanProperty", false,
363                      ((Boolean) bean.get("booleanProperty")).booleanValue());
364         assertEquals("byteProperty", (byte) 111,
365                      ((Byte) bean.get("byteProperty")).byteValue());
366         assertEquals("doubleProperty", 333.0,
367                      ((Double) bean.get("doubleProperty")).doubleValue(),
368                      0.005);
369         assertEquals("floatProperty", (float) 222.0,
370                      ((Float) bean.get("floatProperty")).floatValue(),
371                      (float) 0.005);
372         assertEquals("intProperty", 111,
373                      ((Integer) bean.get("intProperty")).intValue());
374         assertEquals("longProperty", (long) 444,
375                      ((Long) bean.get("longProperty")).longValue());
376         assertEquals("shortProperty", (short) 555,
377                      ((Short) bean.get("shortProperty")).shortValue());
378         assertEquals("stringProperty", "New String Property",
379                      (String) bean.get("stringProperty"));
380 
381         // Indexed Properties
382         String dupProperty[] = (String[]) bean.get("dupProperty");
383         assertNotNull("dupProperty present", dupProperty);
384         assertEquals("dupProperty length", 3, dupProperty.length);
385         assertEquals("dupProperty[0]", "New 0", dupProperty[0]);
386         assertEquals("dupProperty[1]", "New 1", dupProperty[1]);
387         assertEquals("dupProperty[2]", "New 2", dupProperty[2]);
388         int intArray[] = (int[]) bean.get("intArray");
389         assertNotNull("intArray present", intArray);
390         assertEquals("intArray length", 3, intArray.length);
391         assertEquals("intArray[0]", 0, intArray[0]);
392         assertEquals("intArray[1]", 100, intArray[1]);
393         assertEquals("intArray[2]", 200, intArray[2]);
394 
395     }
396 
397 
398     /**
399      * Test the copyProperties() method from a standard JavaBean.
400      */
401     public void testCopyPropertiesStandard() {
402 
403         // Set up an origin bean with customized properties
404         TestBean orig = new TestBean();
405         orig.setBooleanProperty(false);
406         orig.setByteProperty((byte) 111);
407         orig.setDoubleProperty(333.33);
408         orig.setDupProperty(new String[] { "New 0", "New 1", "New 2" });
409         orig.setIntArray(new int[] { 100, 200, 300 });
410         orig.setIntProperty(333);
411         orig.setLongProperty(3333);
412         orig.setShortProperty((short) 33);
413         orig.setStringArray(new String[] { "New 0", "New 1" });
414         orig.setStringProperty("Custom string");
415 
416         // Copy the origin bean to our destination test bean
417         try {
418             BeanUtils.copyProperties(bean, orig);
419         } catch (Exception e) {
420             fail("Threw exception: " + e);
421         }
422 
423         // Validate the results for scalar properties
424         assertEquals("Copied boolean property",
425                      false,
426                      ((Boolean) bean.get("booleanProperty")).booleanValue());
427         assertEquals("Copied byte property",
428                      (byte) 111,
429                      ((Byte) bean.get("byteProperty")).byteValue());
430         assertEquals("Copied double property",
431                      333.33,
432                      ((Double) bean.get("doubleProperty")).doubleValue(),
433                      0.005);
434         assertEquals("Copied int property",
435                      333,
436                      ((Integer) bean.get("intProperty")).intValue());
437         assertEquals("Copied long property",
438                      (long) 3333,
439                      ((Long) bean.get("longProperty")).longValue());
440         assertEquals("Copied short property",
441                      (short) 33,
442                      ((Short) bean.get("shortProperty")).shortValue());
443         assertEquals("Copied string property",
444                      "Custom string",
445                      (String) bean.get("stringProperty"));
446 
447         // Validate the results for array properties
448         String dupProperty[] = (String[]) bean.get("dupProperty");
449         assertNotNull("dupProperty present", dupProperty);
450         assertEquals("dupProperty length", 3, dupProperty.length);
451         assertEquals("dupProperty[0]", "New 0", dupProperty[0]);
452         assertEquals("dupProperty[1]", "New 1", dupProperty[1]);
453         assertEquals("dupProperty[2]", "New 2", dupProperty[2]);
454         int intArray[] = (int[]) bean.get("intArray");
455         assertNotNull("intArray present", intArray);
456         assertEquals("intArray length", 3, intArray.length);
457         assertEquals("intArray[0]", 100, intArray[0]);
458         assertEquals("intArray[1]", 200, intArray[1]);
459         assertEquals("intArray[2]", 300, intArray[2]);
460         String stringArray[] = (String[]) bean.get("stringArray");
461         assertNotNull("stringArray present", stringArray);
462         assertEquals("stringArray length", 2, stringArray.length);
463         assertEquals("stringArray[0]", "New 0", stringArray[0]);
464         assertEquals("stringArray[1]", "New 1", stringArray[1]);
465 
466     }
467 
468 
469     /**
470      * Test the describe() method.
471      */
472     public void testDescribe() {
473 
474         Map map = null;
475         try {
476             map = PropertyUtils.describe(bean);
477         } catch (Exception e) {
478             fail("Threw exception " + e);
479         }
480 
481         // Verify existence of all the properties that should be present
482         for (int i = 0; i < describes.length; i++) {
483             assertTrue("Property '" + describes[i] + "' is present",
484                        map.containsKey(describes[i]));
485         }
486         assertTrue("Property 'writeOnlyProperty' is not present",
487                    !map.containsKey("writeOnlyProperty"));
488 
489         // Verify the values of scalar properties
490         assertEquals("Value of 'booleanProperty'",
491                      Boolean.TRUE,
492                      (Boolean) map.get("booleanProperty"));
493         assertEquals("Value of 'byteProperty'",
494                      new Byte((byte) 121),
495                      (Byte) map.get("byteProperty"));
496         assertEquals("Value of 'doubleProperty'",
497                      new Double(321.0),
498                      (Double) map.get("doubleProperty"));
499         assertEquals("Value of 'floatProperty'",
500                      new Float((float) 123.0),
501                      (Float) map.get("floatProperty"));
502         assertEquals("Value of 'intProperty'",
503                      new Integer(123),
504                      (Integer) map.get("intProperty"));
505         assertEquals("Value of 'longProperty'",
506                      new Long(321),
507                      (Long) map.get("longProperty"));
508         assertEquals("Value of 'shortProperty'",
509                      new Short((short) 987),
510                      (Short) map.get("shortProperty"));
511         assertEquals("Value of 'stringProperty'",
512                      "This is a string",
513                      (String) map.get("stringProperty"));
514 
515     }
516 
517 
518     /**
519      * Test populate() method on array properties as a whole.
520      */
521     public void testPopulateArrayProperties() {
522 
523         try {
524 
525             HashMap map = new HashMap();
526             //            int intArray[] = new int[] { 123, 456, 789 };
527             String intArrayIn[] = new String[] { "123", "456", "789" };
528             map.put("intArray", intArrayIn);
529             String stringArray[] = new String[]
530                 { "New String 0", "New String 1" };
531             map.put("stringArray", stringArray);
532 
533             BeanUtils.populate(bean, map);
534 
535             int intArray[] = (int[]) bean.get("intArray");
536             assertNotNull("intArray is present", intArray);
537             assertEquals("intArray length",
538                          3, intArray.length);
539             assertEquals("intArray[0]", 123, intArray[0]);
540             assertEquals("intArray[1]", 456, intArray[1]);
541             assertEquals("intArray[2]", 789, intArray[2]);
542             stringArray = (String[]) bean.get("stringArray");
543             assertNotNull("stringArray is present", stringArray);
544             assertEquals("stringArray length", 2, stringArray.length);
545             assertEquals("stringArray[0]", "New String 0", stringArray[0]);
546             assertEquals("stringArray[1]", "New String 1", stringArray[1]);
547 
548         } catch (IllegalAccessException e) {
549             fail("IllegalAccessException");
550         } catch (InvocationTargetException e) {
551             fail("InvocationTargetException");
552         }
553 
554     }
555 
556 
557     /**
558      *  tests the string and int arrays of TestBean
559      */
560     public void testGetArrayProperty() {
561         try {
562             String arr[] = BeanUtils.getArrayProperty(bean, "stringArray");
563             String comp[] = (String[]) bean.get("stringArray");
564 
565             assertTrue("String array length = " + comp.length,
566                     (comp.length == arr.length));
567 
568             arr = BeanUtils.getArrayProperty(bean, "intArray");
569             int iarr[] = (int[]) bean.get("intArray");
570 
571             assertTrue("String array length = " + iarr.length,
572                     (iarr.length == arr.length));
573         } catch (IllegalAccessException e) {
574             fail("IllegalAccessException");
575         } catch (InvocationTargetException e) {
576             fail("InvocationTargetException");
577         } catch (NoSuchMethodException e) {
578             fail("NoSuchMethodException");
579         }
580 
581     }
582 
583 
584     /**
585      *  tests getting an indexed property
586      */
587     public void testGetIndexedProperty1() {
588         try {
589             String val = BeanUtils.getIndexedProperty(bean, "intIndexed[3]");
590             String comp = String.valueOf(bean.get("intIndexed", 3));
591             assertTrue("intIndexed[3] == " + comp, val.equals(comp));
592 
593             val = BeanUtils.getIndexedProperty(bean, "stringIndexed[3]");
594             comp = (String) bean.get("stringIndexed", 3);
595             assertTrue("stringIndexed[3] == " + comp, val.equals(comp));
596         } catch (IllegalAccessException e) {
597             fail("IllegalAccessException");
598         } catch (InvocationTargetException e) {
599             fail("InvocationTargetException");
600         } catch (NoSuchMethodException e) {
601             fail("NoSuchMethodException");
602         }
603     }
604 
605 
606     /**
607      *  tests getting an indexed property
608      */
609     public void testGetIndexedProperty2() {
610         try {
611             String val = BeanUtils.getIndexedProperty(bean, "intIndexed", 3);
612             String comp = String.valueOf(bean.get("intIndexed", 3));
613 
614             assertTrue("intIndexed,3 == " + comp, val.equals(comp));
615 
616             val = BeanUtils.getIndexedProperty(bean, "stringIndexed", 3);
617             comp = (String) bean.get("stringIndexed", 3);
618 
619             assertTrue("stringIndexed,3 == " + comp, val.equals(comp));
620 
621         } catch (IllegalAccessException e) {
622             fail("IllegalAccessException");
623         } catch (InvocationTargetException e) {
624             fail("InvocationTargetException");
625         } catch (NoSuchMethodException e) {
626             fail("NoSuchMethodException");
627         }
628     }
629 
630 
631     /**
632      *  tests getting a nested property
633      */
634     public void testGetNestedProperty() {
635         try {
636             String val = BeanUtils.getNestedProperty(bean, "nested.stringProperty");
637             String comp = nested.getStringProperty();
638             assertTrue("nested.StringProperty == " + comp,
639                     val.equals(comp));
640         } catch (IllegalAccessException e) {
641             fail("IllegalAccessException");
642         } catch (InvocationTargetException e) {
643             fail("InvocationTargetException");
644         } catch (NoSuchMethodException e) {
645             fail("NoSuchMethodException");
646         }
647     }
648 
649 
650     /**
651      *  tests getting a 'whatever' property
652      */
653     public void testGetGeneralProperty() {
654         try {
655             String val = BeanUtils.getProperty(bean, "nested.intIndexed[2]");
656             String comp = String.valueOf(bean.get("intIndexed", 2));
657 
658             assertTrue("nested.intIndexed[2] == " + comp,
659                     val.equals(comp));
660         } catch (IllegalAccessException e) {
661             fail("IllegalAccessException");
662         } catch (InvocationTargetException e) {
663             fail("InvocationTargetException");
664         } catch (NoSuchMethodException e) {
665             fail("NoSuchMethodException");
666         }
667     }
668 
669 
670     /**
671      *  tests getting a 'whatever' property
672      */
673     public void testGetSimpleProperty() {
674         try {
675             String val = BeanUtils.getSimpleProperty(bean, "shortProperty");
676             String comp = String.valueOf(bean.get("shortProperty"));
677 
678             assertTrue("shortProperty == " + comp,
679                     val.equals(comp));
680         } catch (IllegalAccessException e) {
681             fail("IllegalAccessException");
682         } catch (InvocationTargetException e) {
683             fail("InvocationTargetException");
684         } catch (NoSuchMethodException e) {
685             fail("NoSuchMethodException");
686         }
687     }
688 
689 
690     /**
691      * Test populate() method on individual array elements.
692      */
693     public void testPopulateArrayElements() {
694 
695         try {
696 
697             HashMap map = new HashMap();
698             map.put("intIndexed[0]", "100");
699             map.put("intIndexed[2]", "120");
700             map.put("intIndexed[4]", "140");
701 
702             BeanUtils.populate(bean, map);
703             Integer intIndexed0 = (Integer) bean.get("intIndexed", 0);
704             assertEquals("intIndexed[0] is 100",
705                          100, intIndexed0.intValue());
706             Integer intIndexed1 = (Integer) bean.get("intIndexed", 1);
707             assertEquals("intIndexed[1] is 10",
708                          10, intIndexed1.intValue());
709             Integer intIndexed2 = (Integer) bean.get("intIndexed", 2);
710             assertEquals("intIndexed[2] is 120",
711                          120, intIndexed2.intValue());
712             Integer intIndexed3 = (Integer) bean.get("intIndexed", 3);
713             assertEquals("intIndexed[3] is 30",
714                          30, intIndexed3.intValue());
715             Integer intIndexed4 = (Integer) bean.get("intIndexed", 4);
716             assertEquals("intIndexed[4] is 140",
717                          140, intIndexed4.intValue());
718 
719             map.clear();
720             map.put("stringIndexed[1]", "New String 1");
721             map.put("stringIndexed[3]", "New String 3");
722 
723             BeanUtils.populate(bean, map);
724 
725             assertEquals("stringIndexed[0] is \"String 0\"",
726                          "String 0",
727                          (String) bean.get("stringIndexed", 0));
728             assertEquals("stringIndexed[1] is \"New String 1\"",
729                          "New String 1",
730                          (String) bean.get("stringIndexed", 1));
731             assertEquals("stringIndexed[2] is \"String 2\"",
732                          "String 2",
733                          (String) bean.get("stringIndexed", 2));
734             assertEquals("stringIndexed[3] is \"New String 3\"",
735                          "New String 3",
736                          (String) bean.get("stringIndexed", 3));
737             assertEquals("stringIndexed[4] is \"String 4\"",
738                          "String 4",
739                          (String) bean.get("stringIndexed", 4));
740 
741         } catch (IllegalAccessException e) {
742             fail("IllegalAccessException");
743         } catch (InvocationTargetException e) {
744             fail("InvocationTargetException");
745         }
746 
747     }
748 
749 
750     /**
751      * Test populate() on mapped properties.
752      */
753     public void testPopulateMapped() {
754 
755         try {
756 
757             HashMap map = new HashMap();
758             map.put("mappedProperty(First Key)", "New First Value");
759             map.put("mappedProperty(Third Key)", "New Third Value");
760 
761             BeanUtils.populate(bean, map);
762 
763             assertEquals("mappedProperty(First Key)",
764                          "New First Value",
765                          (String) bean.get("mappedProperty", "First Key"));
766             assertEquals("mappedProperty(Second Key)",
767                          "Second Value",
768                          (String) bean.get("mappedProperty", "Second Key"));
769             assertEquals("mappedProperty(Third Key)",
770                          "New Third Value",
771                          (String) bean.get("mappedProperty", "Third Key"));
772             assertNull("mappedProperty(Fourth Key",
773                        (String) bean.get("mappedProperty", "Fourth Key"));
774 
775         } catch (IllegalAccessException e) {
776             fail("IllegalAccessException");
777         } catch (InvocationTargetException e) {
778             fail("InvocationTargetException");
779         }
780 
781     }
782 
783 
784     /**
785      * Test populate() method on nested properties.
786      */
787     public void testPopulateNested() {
788 
789         try {
790 
791             HashMap map = new HashMap();
792             map.put("nested.booleanProperty", "false");
793             // booleanSecond is left at true
794             map.put("nested.doubleProperty", "432.0");
795             // floatProperty is left at 123.0
796             map.put("nested.intProperty", "543");
797             // longProperty is left at 321
798             map.put("nested.shortProperty", "654");
799             // stringProperty is left at "This is a string"
800 
801             BeanUtils.populate(bean, map);
802 
803             TestBean nested = (TestBean) bean.get("nested");
804             assertTrue("booleanProperty is false",
805                        !nested.getBooleanProperty());
806             assertTrue("booleanSecond is true",
807                        nested.isBooleanSecond());
808             assertEquals("doubleProperty is 432.0",
809                          (double) 432.0,
810                          nested.getDoubleProperty(),
811                          (double) 0.005);
812             assertEquals("floatProperty is 123.0",
813                          (float) 123.0,
814                          nested.getFloatProperty(),
815                          (float) 0.005);
816             assertEquals("intProperty is 543",
817                          543, nested.getIntProperty());
818             assertEquals("longProperty is 321",
819                          (long) 321, nested.getLongProperty());
820             assertEquals("shortProperty is 654",
821                          (short) 654, nested.getShortProperty());
822             assertEquals("stringProperty is \"This is a string\"",
823                          "This is a string",
824                          nested.getStringProperty());
825 
826         } catch (IllegalAccessException e) {
827             fail("IllegalAccessException");
828         } catch (InvocationTargetException e) {
829             fail("InvocationTargetException");
830         }
831 
832     }
833 
834 
835     /**
836      * Test populate() method on scalar properties.
837      */
838     public void testPopulateScalar() {
839 
840         try {
841 
842             bean.set("nullProperty", "non-null value");
843 
844             HashMap map = new HashMap();
845             map.put("booleanProperty", "false");
846             // booleanSecond is left at true
847             map.put("doubleProperty", "432.0");
848             // floatProperty is left at 123.0
849             map.put("intProperty", "543");
850             // longProperty is left at 321
851             map.put("nullProperty", null);
852             map.put("shortProperty", "654");
853             // stringProperty is left at "This is a string"
854 
855             BeanUtils.populate(bean, map);
856 
857             Boolean booleanProperty = (Boolean) bean.get("booleanProperty");
858             assertTrue("booleanProperty is false", !booleanProperty.booleanValue());
859             Boolean booleanSecond = (Boolean) bean.get("booleanSecond");
860             assertTrue("booleanSecond is true", booleanSecond.booleanValue());
861             Double doubleProperty = (Double) bean.get("doubleProperty");
862             assertEquals("doubleProperty is 432.0",
863                          (double) 432.0, doubleProperty.doubleValue(),
864                          (double) 0.005);
865             Float floatProperty = (Float) bean.get("floatProperty");
866             assertEquals("floatProperty is 123.0",
867                          (float) 123.0, floatProperty.floatValue(),
868                          (float) 0.005);
869             Integer intProperty = (Integer) bean.get("intProperty");
870             assertEquals("intProperty is 543",
871                          543, intProperty.intValue());
872             Long longProperty = (Long) bean.get("longProperty");
873             assertEquals("longProperty is 321",
874                          (long) 321, longProperty.longValue());
875             assertNull("nullProperty is null", bean.get("nullProperty"));
876             Short shortProperty = (Short) bean.get("shortProperty");
877             assertEquals("shortProperty is 654",
878                          (short) 654, shortProperty.shortValue());
879             assertEquals("stringProperty is \"This is a string\"",
880                          "This is a string",
881                          (String) bean.get("stringProperty"));
882 
883         } catch (IllegalAccessException e) {
884             fail("IllegalAccessException");
885         } catch (InvocationTargetException e) {
886             fail("InvocationTargetException");
887         }
888 
889     }
890 
891 
892     /**
893      * Test calling setProperty() with null property values.
894      */
895     public void testSetPropertyNullValues() throws Exception {
896 
897         Object oldValue = null;
898         Object newValue = null;
899 
900         // Scalar value into array
901         oldValue = PropertyUtils.getSimpleProperty(bean, "stringArray");
902         BeanUtils.setProperty(bean, "stringArray", (String) null);
903         newValue = PropertyUtils.getSimpleProperty(bean, "stringArray");
904         assertNotNull("stringArray is not null", newValue);
905         assertTrue("stringArray of correct type",
906                    newValue instanceof String[]);
907         assertEquals("stringArray length",
908                      1, ((String[]) newValue).length);
909         assertTrue("stringArray[0] is null",
910                    ((String[]) newValue)[0] == null);
911         PropertyUtils.setProperty(bean, "stringArray", oldValue);
912 
913         // Indexed value into array
914         oldValue = PropertyUtils.getSimpleProperty(bean, "stringArray");
915         BeanUtils.setProperty(bean, "stringArray[2]", (String) null);
916         newValue = PropertyUtils.getSimpleProperty(bean, "stringArray");
917         assertNotNull("stringArray is not null", newValue);
918         assertTrue("stringArray of correct type",
919                    newValue instanceof String[]);
920         assertEquals("stringArray length",
921                      5, ((String[]) newValue).length);
922         assertTrue("stringArray[2] is null",
923                    ((String[]) newValue)[2] == null);
924         PropertyUtils.setProperty(bean, "stringArray", oldValue);
925 
926         // Value into scalar
927         BeanUtils.setProperty(bean, "stringProperty", null);
928         assertTrue("stringProperty is now null",
929                    BeanUtils.getProperty(bean, "stringProperty") == null);
930 
931     }
932 
933 
934     /**
935      * Test converting to and from primitive wrapper types.
936      */
937     public void testSetPropertyOnPrimitiveWrappers() throws Exception {
938 
939         BeanUtils.setProperty(bean,"intProperty", new Integer(1));
940         assertEquals(1,((Integer) bean.get("intProperty")).intValue());
941         BeanUtils.setProperty(bean,"stringProperty", new Integer(1));
942         assertEquals(1, Integer.parseInt((String) bean.get("stringProperty")));
943 
944     }
945 
946 
947     /**
948      * Test setting a null property value.
949      */
950     public void testSetPropertyNull() throws Exception {
951 
952         bean.set("nullProperty", "non-null value");
953         BeanUtils.setProperty(bean, "nullProperty", null);
954         assertNull("nullProperty is null", bean.get("nullProperty"));
955 
956     }
957 
958 
959     /**
960      * Test narrowing and widening conversions on byte.
961      */
962     public void testCopyPropertyByte() throws Exception {
963 
964         BeanUtils.setProperty(bean, "byteProperty", new Byte((byte) 123));
965         assertEquals((byte) 123, ((Byte) bean.get("byteProperty")).byteValue());
966 /*
967         BeanUtils.setProperty(bean, "byteProperty", new Double((double) 123));
968         assertEquals((byte) 123, ((Byte) bean.get("byteProperty")).byteValue());
969         BeanUtils.setProperty(bean, "byteProperty", new Float((float) 123));
970         assertEquals((byte) 123, ((Byte) bean.get("byteProperty")).byteValue());
971 */
972         BeanUtils.setProperty(bean, "byteProperty", new Integer((int) 123));
973         assertEquals((byte) 123, ((Byte) bean.get("byteProperty")).byteValue());
974         BeanUtils.setProperty(bean, "byteProperty", new Long((long) 123));
975         assertEquals((byte) 123, ((Byte) bean.get("byteProperty")).byteValue());
976         BeanUtils.setProperty(bean, "byteProperty", new Short((short) 123));
977         assertEquals((byte) 123, ((Byte) bean.get("byteProperty")).byteValue());
978 
979     }
980 
981 
982     /**
983      * Test narrowing and widening conversions on double.
984      */
985     public void testCopyPropertyDouble() throws Exception {
986 
987         BeanUtils.setProperty(bean, "doubleProperty", new Byte((byte) 123));
988         assertEquals((double) 123, ((Double) bean.get("doubleProperty")).doubleValue(), 0.005);
989         BeanUtils.setProperty(bean, "doubleProperty", new Double((double) 123));
990         assertEquals((double) 123, ((Double) bean.get("doubleProperty")).doubleValue(), 0.005);
991         BeanUtils.setProperty(bean, "doubleProperty", new Float((float) 123));
992         assertEquals((double) 123, ((Double) bean.get("doubleProperty")).doubleValue(), 0.005);
993         BeanUtils.setProperty(bean, "doubleProperty", new Integer((int) 123));
994         assertEquals((double) 123, ((Double) bean.get("doubleProperty")).doubleValue(), 0.005);
995         BeanUtils.setProperty(bean, "doubleProperty", new Long((long) 123));
996         assertEquals((double) 123, ((Double) bean.get("doubleProperty")).doubleValue(), 0.005);
997         BeanUtils.setProperty(bean, "doubleProperty", new Short((short) 123));
998         assertEquals((double) 123, ((Double) bean.get("doubleProperty")).doubleValue(), 0.005);
999 
1000    }
1001
1002
1003    /**
1004     * Test narrowing and widening conversions on float.
1005     */
1006    public void testCopyPropertyFloat() throws Exception {
1007
1008        BeanUtils.setProperty(bean, "floatProperty", new Byte((byte) 123));
1009        assertEquals((float) 123, ((Float) bean.get("floatProperty")).floatValue(), 0.005);
1010        BeanUtils.setProperty(bean, "floatProperty", new Double((double) 123));
1011        assertEquals((float) 123, ((Float) bean.get("floatProperty")).floatValue(), 0.005);
1012        BeanUtils.setProperty(bean, "floatProperty", new Float((float) 123));
1013        assertEquals((float) 123, ((Float) bean.get("floatProperty")).floatValue(), 0.005);
1014        BeanUtils.setProperty(bean, "floatProperty", new Integer((int) 123));
1015        assertEquals((float) 123, ((Float) bean.get("floatProperty")).floatValue(), 0.005);
1016        BeanUtils.setProperty(bean, "floatProperty", new Long((long) 123));
1017        assertEquals((float) 123, ((Float) bean.get("floatProperty")).floatValue(), 0.005);
1018        BeanUtils.setProperty(bean, "floatProperty", new Short((short) 123));
1019        assertEquals((float) 123, ((Float) bean.get("floatProperty")).floatValue(), 0.005);
1020
1021    }
1022
1023
1024    /**
1025     * Test narrowing and widening conversions on int.
1026     */
1027    public void testCopyPropertyInteger() throws Exception {
1028
1029        BeanUtils.setProperty(bean, "longProperty", new Byte((byte) 123));
1030        assertEquals((int) 123, ((Integer) bean.get("intProperty")).intValue());
1031/*
1032        BeanUtils.setProperty(bean, "longProperty", new Double((double) 123));
1033        assertEquals((int) 123, ((Integer) bean.get("intProperty")).intValue());
1034        BeanUtils.setProperty(bean, "longProperty", new Float((float) 123));
1035        assertEquals((int) 123, ((Integer) bean.get("intProperty")).intValue());
1036*/
1037        BeanUtils.setProperty(bean, "longProperty", new Integer((int) 123));
1038        assertEquals((int) 123, ((Integer) bean.get("intProperty")).intValue());
1039        BeanUtils.setProperty(bean, "longProperty", new Long((long) 123));
1040        assertEquals((int) 123, ((Integer) bean.get("intProperty")).intValue());
1041        BeanUtils.setProperty(bean, "longProperty", new Short((short) 123));
1042        assertEquals((int) 123, ((Integer) bean.get("intProperty")).intValue());
1043
1044    }
1045
1046
1047    /**
1048     * Test narrowing and widening conversions on long.
1049     */
1050    public void testCopyPropertyLong() throws Exception {
1051
1052        BeanUtils.setProperty(bean, "longProperty", new Byte((byte) 123));
1053        assertEquals((long) 123, ((Long) bean.get("longProperty")).longValue());
1054/*
1055        BeanUtils.setProperty(bean, "longProperty", new Double((double) 123));
1056        assertEquals((long) 123, ((Long) bean.get("longProperty")).longValue());
1057        BeanUtils.setProperty(bean, "longProperty", new Float((float) 123));
1058        assertEquals((long) 123, ((Long) bean.get("longProperty")).longValue());
1059*/
1060        BeanUtils.setProperty(bean, "longProperty", new Integer((int) 123));
1061        assertEquals((long) 123, ((Long) bean.get("longProperty")).longValue());
1062        BeanUtils.setProperty(bean, "longProperty", new Long((long) 123));
1063        assertEquals((long) 123, ((Long) bean.get("longProperty")).longValue());
1064        BeanUtils.setProperty(bean, "longProperty", new Short((short) 123));
1065        assertEquals((long) 123, ((Long) bean.get("longProperty")).longValue());
1066
1067    }
1068
1069
1070    /**
1071     * Test copying a null property value.
1072     */
1073    public void testCopyPropertyNull() throws Exception {
1074
1075        bean.set("nullProperty", "non-null value");
1076        BeanUtils.copyProperty(bean, "nullProperty", null);
1077        assertNull("nullProperty is null", bean.get("nullProperty"));
1078
1079    }
1080
1081
1082    /**
1083     * Test narrowing and widening conversions on short.
1084     */
1085    public void testCopyPropertyShort() throws Exception {
1086
1087        BeanUtils.setProperty(bean, "shortProperty", new Byte((byte) 123));
1088        assertEquals((short) 123, ((Short) bean.get("shortProperty")).shortValue());
1089/*
1090        BeanUtils.