KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > beanutils > DynaBeanUtilsTestCase


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 JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
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 JavaDoc 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 JavaDoc 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 JavaDoc {
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 JavaDoc(true));
114         bean.set("booleanSecond", new Boolean JavaDoc(true));
115         bean.set("byteProperty", new Byte JavaDoc((byte) 121));
116         bean.set("doubleProperty", new Double JavaDoc(321.0));
117         bean.set("floatProperty", new Float JavaDoc((float) 123.0));
118         String JavaDoc 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 JavaDoc(123));
125         List JavaDoc listIndexed = new ArrayList JavaDoc();
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 JavaDoc((long) 321));
133         HashMap JavaDoc mapProperty = new HashMap JavaDoc();
134         mapProperty.put("First Key", "First Value");
135         mapProperty.put("Second Key", "Second Value");
136         bean.set("mapProperty", mapProperty);
137         HashMap JavaDoc mappedProperty = new HashMap JavaDoc();
138         mappedProperty.put("First Key", "First Value");
139         mappedProperty.put("Second Key", "Second Value");
140         bean.set("mappedProperty", mappedProperty);
141         HashMap JavaDoc mappedIntProperty = new HashMap JavaDoc();
142         mappedIntProperty.put("One", new Integer JavaDoc(1));
143         mappedIntProperty.put("Two", new Integer JavaDoc(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 JavaDoc((short) 987));
149         String JavaDoc stringArray[] =
150                 { "String 0", "String 1", "String 2", "String 3", "String 4" };
151         bean.set("stringArray", stringArray);
152         String JavaDoc 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 JavaDoc e) {
195             fail("newInstance(): " + e);
196         }
197         orig.set("booleanProperty", Boolean.FALSE);
198         orig.set("byteProperty", new Byte JavaDoc((byte)111));
199         orig.set("doubleProperty", new Double JavaDoc(333.33));
200         orig.set("dupProperty", new String JavaDoc[] { "New 0", "New 1", "New 2" });
201         orig.set("intArray", new int[] { 100, 200, 300 });
202         orig.set("intProperty", new Integer JavaDoc(333));
203         orig.set("longProperty", new Long JavaDoc(3333));
204         orig.set("shortProperty", new Short JavaDoc((short) 33));
205         orig.set("stringArray", new String JavaDoc[] { "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 JavaDoc e) {
213             fail("Threw exception: " + e);
214         }
215
216         // Validate the results for scalar properties
217
assertEquals("Cloned boolean property",
218                      false,
219                      ((Boolean JavaDoc) clonedBean.get("booleanProperty")).booleanValue());
220         assertEquals("Cloned byte property",
221                      (byte) 111,
222                      ((Byte JavaDoc) clonedBean.get("byteProperty")).byteValue());
223         assertEquals("Cloned double property",
224                      333.33,
225                      ((Double JavaDoc) clonedBean.get("doubleProperty")).doubleValue(),
226                      0.005);
227         assertEquals("Cloned int property",
228                      333,
229                      ((Integer JavaDoc) clonedBean.get("intProperty")).intValue());
230         assertEquals("Cloned long property",
231                      (long) 3333,
232                      ((Long JavaDoc) clonedBean.get("longProperty")).longValue());
233         assertEquals("Cloned short property",
234                      (short) 33,
235                      ((Short JavaDoc) clonedBean.get("shortProperty")).shortValue());
236         assertEquals("Cloned string property",
237                      "Custom string",
238                      (String JavaDoc) clonedBean.get("stringProperty"));
239
240         // Validate the results for array properties
241
String JavaDoc dupProperty[] = (String JavaDoc[]) 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 JavaDoc stringArray[] = (String JavaDoc[]) 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 JavaDoc e) {
272             fail("newInstance(): " + e);
273         }
274         orig.set("booleanProperty", Boolean.FALSE);
275         orig.set("byteProperty", new Byte JavaDoc((byte)111));
276         orig.set("doubleProperty", new Double JavaDoc(333.33));
277         orig.set("dupProperty", new String JavaDoc[] { "New 0", "New 1", "New 2" });
278         orig.set("intArray", new int[] { 100, 200, 300 });
279         orig.set("intProperty", new Integer JavaDoc(333));
280         orig.set("longProperty", new Long JavaDoc(3333));
281         orig.set("shortProperty", new Short JavaDoc((short) 33));
282         orig.set("stringArray", new String JavaDoc[] { "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 JavaDoc e) {
289             fail("Threw exception: " + e);
290         }
291
292         // Validate the results for scalar properties
293
assertEquals("Copied boolean property",
294                      false,
295                      ((Boolean JavaDoc) bean.get("booleanProperty")).booleanValue());
296         assertEquals("Copied byte property",
297                      (byte) 111,
298                      ((Byte JavaDoc) bean.get("byteProperty")).byteValue());
299         assertEquals("Copied double property",
300                      333.33,
301                      ((Double JavaDoc) bean.get("doubleProperty")).doubleValue(),
302                      0.005);
303         assertEquals("Copied int property",
304                      333,
305                      ((Integer JavaDoc) bean.get("intProperty")).intValue());
306         assertEquals("Copied long property",
307                      (long) 3333,
308                      ((Long JavaDoc) bean.get("longProperty")).longValue());
309         assertEquals("Copied short property",
310                      (short) 33,
311                      ((Short JavaDoc) bean.get("shortProperty")).shortValue());
312         assertEquals("Copied string property",
313                      "Custom string",
314                      (String JavaDoc) bean.get("stringProperty"));
315
316         // Validate the results for array properties
317
String JavaDoc dupProperty[] = (String JavaDoc[]) 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 JavaDoc stringArray[] = (String JavaDoc[]) 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 JavaDoc map = new HashMap JavaDoc();
344         map.put("booleanProperty", "false");
345         map.put("byteProperty", "111");
346         map.put("doubleProperty", "333.0");
347         map.put("dupProperty", new String JavaDoc[] { "New 0", "New 1", "New 2" });
348         map.put("floatProperty", "222.0");
349         map.put("intArray", new String JavaDoc[] { "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 JavaDoc t) {
358             fail("Threw " + t.toString());
359         }
360
361         // Scalar properties
362
assertEquals("booleanProperty", false,
363                      ((Boolean JavaDoc) bean.get("booleanProperty")).booleanValue());
364         assertEquals("byteProperty", (byte) 111,
365                      ((Byte JavaDoc) bean.get("byteProperty")).byteValue());
366         assertEquals("doubleProperty", 333.0,
367                      ((Double JavaDoc) bean.get("doubleProperty")).doubleValue(),
368                      0.005);
369         assertEquals("floatProperty", (float) 222.0,
370                      ((Float JavaDoc) bean.get("floatProperty")).floatValue(),
371                      (float) 0.005);
372         assertEquals("intProperty", 111,
373                      ((Integer JavaDoc) bean.get("intProperty")).intValue());
374         assertEquals("longProperty", (long) 444,
375                      ((Long JavaDoc) bean.get("longProperty")).longValue());
376         assertEquals("shortProperty", (short) 555,
377                      ((Short JavaDoc) bean.get("shortProperty")).shortValue());
378         assertEquals("stringProperty", "New String Property",
379                      (String JavaDoc) bean.get("stringProperty"));
380
381         // Indexed Properties
382
String JavaDoc dupProperty[] = (String JavaDoc[]) 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 JavaDoc[] { "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 JavaDoc[] { "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 JavaDoc e) {
420             fail("Threw exception: " + e);
421         }
422
423         // Validate the results for scalar properties
424
assertEquals("Copied boolean property",
425                      false,
426                      ((Boolean JavaDoc) bean.get("booleanProperty")).booleanValue());
427         assertEquals("Copied byte property",
428                      (byte) 111,
429                      ((Byte JavaDoc) bean.get("byteProperty")).byteValue());
430         assertEquals("Copied double property",
431                      333.33,
432                      ((Double JavaDoc) bean.get("doubleProperty")).doubleValue(),
433                      0.005);
434         assertEquals("Copied int property",
435                      333,
436                      ((Integer JavaDoc) bean.get("intProperty")).intValue());
437         assertEquals("Copied long property",
438                      (long) 3333,
439                      ((Long JavaDoc) bean.get("longProperty")).longValue());
440         assertEquals("Copied short property",
441                      (short) 33,
442                      ((Short JavaDoc) bean.get("shortProperty")).shortValue());
443         assertEquals("Copied string property",
444                      "Custom string",
445                      (String JavaDoc) bean.get("stringProperty"));
446
447         // Validate the results for array properties
448
String JavaDoc dupProperty[] = (String JavaDoc[]) 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 JavaDoc stringArray[] = (String JavaDoc[]) 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 JavaDoc map = null;
475         try {
476             map = PropertyUtils.describe(bean);
477         } catch (Exception JavaDoc 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 JavaDoc) map.get("booleanProperty"));
493         assertEquals("Value of 'byteProperty'",
494                      new Byte JavaDoc((byte) 121),
495                      (Byte JavaDoc) map.get("byteProperty"));
496         assertEquals("Value of 'doubleProperty'",
497                      new Double JavaDoc(321.0),
498                      (Double JavaDoc) map.get("doubleProperty"));
499         assertEquals("Value of 'floatProperty'",
500                      new Float JavaDoc((float) 123.0),
501                      (Float JavaDoc) map.get("floatProperty"));
502         assertEquals("Value of 'intProperty'",
503                      new Integer JavaDoc(123),
504                      (Integer JavaDoc) map.get("intProperty"));
505         assertEquals("Value of 'longProperty'",
506                      new Long JavaDoc(321),
507                      (Long JavaDoc) map.get("longProperty"));
508         assertEquals("Value of 'shortProperty'",
509                      new Short JavaDoc((short) 987),
510                      (Short JavaDoc) map.get("shortProperty"));
511         assertEquals("Value of 'stringProperty'",
512                      "This is a string",
513                      (String JavaDoc) 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 JavaDoc map = new HashMap JavaDoc();
526             // int intArray[] = new int[] { 123, 456, 789 };
527
String JavaDoc intArrayIn[] = new String JavaDoc[] { "123", "456", "789" };
528             map.put("intArray", intArrayIn);
529             String JavaDoc stringArray[] = new String JavaDoc[]
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 JavaDoc[]) 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 JavaDoc e) {
549             fail("IllegalAccessException");
550         } catch (InvocationTargetException JavaDoc 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 JavaDoc arr[] = BeanUtils.getArrayProperty(bean, "stringArray");
563             String JavaDoc comp[] = (String JavaDoc[]) 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 JavaDoc e) {
574             fail("IllegalAccessException");
575         } catch (InvocationTargetException JavaDoc e) {
576             fail("InvocationTargetException");
577         } catch (NoSuchMethodException JavaDoc 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 JavaDoc val = BeanUtils.getIndexedProperty(bean, "intIndexed[3]");
590             String JavaDoc 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 JavaDoc) bean.get("stringIndexed", 3);
595             assertTrue("stringIndexed[3] == " + comp, val.equals(comp));
596         } catch (IllegalAccessException JavaDoc e) {
597             fail("IllegalAccessException");
598         } catch (InvocationTargetException JavaDoc e) {
599             fail("InvocationTargetException");
600         } catch (NoSuchMethodException JavaDoc e) {
601             fail("NoSuchMethodException");
602         }
603     }
604
605
606     /**
607      * tests getting an indexed property
608      */

609     public void testGetIndexedProperty2() {
610         try {
611             String JavaDoc val = BeanUtils.getIndexedProperty(bean, "intIndexed", 3);
612             String JavaDoc 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 JavaDoc) bean.get("stringIndexed", 3);
618
619             assertTrue("stringIndexed,3 == " + comp, val.equals(comp));
620
621         } catch (IllegalAccessException JavaDoc e) {
622             fail("IllegalAccessException");
623         } catch (InvocationTargetException JavaDoc e) {
624             fail("InvocationTargetException");
625         } catch (NoSuchMethodException JavaDoc e) {
626             fail("NoSuchMethodException");
627         }
628     }
629
630
631     /**
632      * tests getting a nested property
633      */

634     public void testGetNestedProperty() {
635         try {
636             String JavaDoc val = BeanUtils.getNestedProperty(bean, "nested.stringProperty");
637             String JavaDoc comp = nested.getStringProperty();
638             assertTrue("nested.StringProperty == " + comp,
639                     val.equals(comp));
640         } catch (IllegalAccessException JavaDoc e) {
641             fail("IllegalAccessException");
642         } catch (InvocationTargetException JavaDoc e) {
643             fail("InvocationTargetException");
644         } catch (NoSuchMethodException JavaDoc e) {
645             fail("NoSuchMethodException");
646         }
647     }
648
649
650     /**
651      * tests getting a 'whatever' property
652      */

653     public void testGetGeneralProperty() {
654         try {
655             String JavaDoc val = BeanUtils.getProperty(bean, "nested.intIndexed[2]");
656             String JavaDoc comp = String.valueOf(bean.get("intIndexed", 2));
657
658             assertTrue("nested.intIndexed[2] == " + comp,
659                     val.equals(comp));
660         } catch (IllegalAccessException JavaDoc e) {
661             fail("IllegalAccessException");
662         } catch (InvocationTargetException JavaDoc e) {
663             fail("InvocationTargetException");
664         } catch (NoSuchMethodException JavaDoc e) {
665             fail("NoSuchMethodException");
666         }
667     }
668
669
670     /**
671      * tests getting a 'whatever' property
672      */

673     public void testGetSimpleProperty() {
674         try {
675             String JavaDoc val = BeanUtils.getSimpleProperty(bean, "shortProperty");
676             String JavaDoc comp = String.valueOf(bean.get("shortProperty"));
677
678             assertTrue("shortProperty == " + comp,
679                     val.equals(comp));
680         } catch (IllegalAccessException JavaDoc e) {
681             fail("IllegalAccessException");
682         } catch (InvocationTargetException JavaDoc e) {
683             fail("InvocationTargetException");
684         } catch (NoSuchMethodException JavaDoc 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 JavaDoc map = new HashMap JavaDoc();
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 JavaDoc intIndexed0 = (Integer JavaDoc) bean.get("intIndexed", 0);
704             assertEquals("intIndexed[0] is 100",
705                          100, intIndexed0.intValue());
706             Integer JavaDoc intIndexed1 = (Integer JavaDoc) bean.get("intIndexed", 1);
707             assertEquals("intIndexed[1] is 10",
708                          10, intIndexed1.intValue());
709             Integer JavaDoc intIndexed2 = (Integer JavaDoc) bean.get("intIndexed", 2);
710             assertEquals("intIndexed[2] is 120",
711                          120, intIndexed2.intValue());
712             Integer JavaDoc intIndexed3 = (Integer JavaDoc) bean.get("intIndexed", 3);
713             assertEquals("intIndexed[3] is 30",
714                          30, intIndexed3.intValue());
715             Integer JavaDoc intIndexed4 = (Integer JavaDoc) 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 JavaDoc) bean.get("stringIndexed", 0));
728             assertEquals("stringIndexed[1] is \"New String 1\"",
729                          "New String 1",
730                          (String JavaDoc) bean.get("stringIndexed", 1));
731             assertEquals("stringIndexed[2] is \"String 2\"",
732                          "String 2",
733                          (String JavaDoc) bean.get("stringIndexed", 2));
734             assertEquals("stringIndexed[3] is \"New String 3\"",
735                          "New String 3",
736                          (String JavaDoc) bean.get("stringIndexed", 3));
737             assertEquals("stringIndexed[4] is \"String 4\"",
738                          "String 4",
739                          (String JavaDoc) bean.get("stringIndexed", 4));
740
741         } catch (IllegalAccessException JavaDoc e) {
742             fail("IllegalAccessException");
743         } catch (InvocationTargetException JavaDoc 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 JavaDoc map = new HashMap JavaDoc();
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 JavaDoc) bean.get("mappedProperty", "First Key"));
766             assertEquals("mappedProperty(Second Key)",
767                          "Second Value",
768                          (String JavaDoc) bean.get("mappedProperty", "Second Key"));
769             assertEquals("mappedProperty(Third Key)",
770                          "New Third Value",
771                          (String JavaDoc) bean.get("mappedProperty", "Third Key"));
772             assertNull("mappedProperty(Fourth Key",
773                        (String JavaDoc) bean.get("mappedProperty", "Fourth Key"));
774
775         } catch (IllegalAccessException JavaDoc e) {
776             fail("IllegalAccessException");
777         } catch (InvocationTargetException JavaDoc 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 JavaDoc map = new HashMap JavaDoc();
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 JavaDoc e) {
827             fail("IllegalAccessException");
828         } catch (InvocationTargetException JavaDoc 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 JavaDoc map = new HashMap JavaDoc();
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 JavaDoc booleanProperty = (Boolean JavaDoc) bean.get("booleanProperty");
858             assertTrue("booleanProperty is false", !booleanProperty.booleanValue());
859             Boolean JavaDoc booleanSecond = (Boolean JavaDoc) bean.get("booleanSecond");
860             assertTrue("booleanSecond is true", booleanSecond.booleanValue());
861             Double JavaDoc doubleProperty = (Double JavaDoc) bean.get("doubleProperty");
862             assertEquals("doubleProperty is 432.0",
863                          (double) 432.0, doubleProperty.doubleValue(),
864                          (double) 0.005);
865             Float JavaDoc floatProperty = (Float JavaDoc) bean.get("floatProperty");
866             assertEquals("floatProperty is 123.0",
867                          (float) 123.0, floatProperty.floatValue(),
868                          (float) 0.005);
869             Integer JavaDoc intProperty = (Integer JavaDoc) bean.get("intProperty");
870             assertEquals("intProperty is 543",
871                          543, intProperty.intValue());
872             Long JavaDoc longProperty = (Long JavaDoc) bean.get("longProperty");
873             assertEquals("longProperty is 321",
874                          (long) 321, longProperty.longValue());
875             assertNull("nullProperty is null", bean.get("nullProperty"));
876             Short JavaDoc shortProperty = (Short JavaDoc) 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 JavaDoc) bean.get("stringProperty"));
882
883         } catch (IllegalAccessException JavaDoc e) {
884             fail("IllegalAccessException");
885         } catch (InvocationTargetException JavaDoc 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 JavaDoc {
896
897         Object JavaDoc oldValue = null;
898         Object JavaDoc newValue = null;
899
900         // Scalar value into array
901
oldValue = PropertyUtils.getSimpleProperty(bean, "stringArray");
902         BeanUtils.setProperty(bean, "stringArray", (String JavaDoc) null);
903         newValue = PropertyUtils.getSimpleProperty(bean, "stringArray");
904         assertNotNull("stringArray is not null", newValue);
905         assertTrue("stringArray of correct type",
906                    newValue instanceof String JavaDoc[]);
907         assertEquals("stringArray length",
908                      1, ((String JavaDoc[]) newValue).length);
909         assertTrue("stringArray[0] is null",
910                    ((String JavaDoc[]) 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 JavaDoc) null);
916         newValue = PropertyUtils.getSimpleProperty(bean, "stringArray");
917         assertNotNull("stringArray is not null", newValue);
918         assertTrue("stringArray of correct type",
919                    newValue instanceof String JavaDoc[]);
920         assertEquals("stringArray length",
921                      5, ((String JavaDoc[]) newValue).length);
922         assertTrue("stringArray[2] is null",
923                    ((String JavaDoc[]) 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 JavaDoc {
938
939         BeanUtils.setProperty(bean,"intProperty", new Integer JavaDoc(1));
940         assertEquals(1,((Integer JavaDoc) bean.get("intProperty")).intValue());
941         BeanUtils.setProperty(bean,"stringProperty", new Integer JavaDoc(1));
942         assertEquals(1, Integer.parseInt((String JavaDoc) bean.get("stringProperty")));
943
944     }
945
946
947     /**
948      * Test setting a null property value.
949      */

950     public void testSetPropertyNull() throws Exception JavaDoc {
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 JavaDoc {
963
964         BeanUtils.setProperty(bean, "byteProperty", new Byte JavaDoc((byte) 123));
965         assertEquals((byte) 123, ((Byte JavaDoc) 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 JavaDoc((int) 123));
973         assertEquals((byte) 123, ((Byte JavaDoc) bean.get("byteProperty")).byteValue());
974         BeanUtils.setProperty(bean, "byteProperty", new Long JavaDoc((long) 123));
975         assertEquals((byte) 123, ((Byte JavaDoc) bean.get("byteProperty")).byteValue());
976         BeanUtils.setProperty(bean, "byteProperty", new Short JavaDoc((short) 123));
977         assertEquals((byte) 123, ((Byte JavaDoc) bean.get("byteProperty")).byteValue());
978
979     }
980
981
982     /**
983      * Test narrowing and widening conversions on double.
984      */

985     public void testCopyPropertyDouble() throws Exception JavaDoc {
986
987         BeanUtils.setProperty(bean, "doubleProperty", new Byte JavaDoc((byte) 123));
988         assertEquals((double) 123, ((Double JavaDoc) bean.get("doubleProperty")).doubleValue(), 0.005);
989         BeanUtils.setProperty(bean, "doubleProperty", new Double JavaDoc((double) 123));
990         assertEquals((double) 123, ((Double JavaDoc) bean.get("doubleProperty")).doubleValue(), 0.005);
991         BeanUtils.setProperty(bean, "doubleProperty", new Float JavaDoc((float) 123));
992         assertEquals((double) 123, ((Double JavaDoc) bean.get("doubleProperty")).doubleValue(), 0.005);
993         BeanUtils.setProperty(bean, "doubleProperty", new Integer JavaDoc((int) 123));
994         assertEquals((double) 123, ((Double JavaDoc) bean.get("doubleProperty")).doubleValue(), 0.005);
995         BeanUtils.setProperty(bean, "doubleProperty", new Long JavaDoc((long) 123));
996         assertEquals((double) 123, ((Double JavaDoc) bean.get("doubleProperty")).doubleValue(), 0.005);
997         BeanUtils.setProperty(bean, "doubleProperty", new Short JavaDoc((short) 123));
998         assertEquals((double) 123, ((Double JavaDoc) 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 JavaDoc {
1007
1008        BeanUtils.setProperty(bean, "floatProperty", new Byte JavaDoc((byte) 123));
1009        assertEquals((float) 123, ((Float JavaDoc) bean.get("floatProperty")).floatValue(), 0.005);
1010        BeanUtils.setProperty(bean, "floatProperty", new Double JavaDoc((double) 123));
1011        assertEquals((float) 123, ((Float JavaDoc) bean.get("floatProperty")).floatValue(), 0.005);
1012        BeanUtils.setProperty(bean, "floatProperty", new Float JavaDoc((float) 123));
1013        assertEquals((float) 123, ((Float JavaDoc) bean.get("floatProperty")).floatValue(), 0.005);
1014        BeanUtils.setProperty(bean, "floatProperty", new Integer JavaDoc((int) 123));
1015        assertEquals((float) 123, ((Float JavaDoc) bean.get("floatProperty")).floatValue(), 0.005);
1016        BeanUtils.setProperty(bean, "floatProperty", new Long JavaDoc((long) 123));
1017        assertEquals((float) 123, ((Float JavaDoc) bean.get("floatProperty")).floatValue(), 0.005);
1018        BeanUtils.setProperty(bean, "floatProperty", new Short JavaDoc((short) 123));
1019        assertEquals((float) 123, ((Float JavaDoc) 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 JavaDoc {
1028
1029        BeanUtils.setProperty(bean, "longProperty", new Byte JavaDoc((byte) 123));
1030        assertEquals((int) 123, ((Integer JavaDoc) 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 JavaDoc((int) 123));
1038        assertEquals((int) 123, ((Integer JavaDoc) bean.get("intProperty")).intValue());
1039        BeanUtils.setProperty(bean, "longProperty", new Long JavaDoc((long) 123));
1040        assertEquals((int) 123, ((Integer JavaDoc) bean.get("intProperty")).intValue());
1041        BeanUtils.setProperty(bean, "longProperty", new Short JavaDoc((short) 123));
1042        assertEquals((int) 123, ((Integer JavaDoc) bean.get("intProperty")).intValue());
1043
1044    }
1045
1046
1047    /**
1048     * Test narrowing and widening conversions on long.
1049     */

1050    public void testCopyPropertyLong() throws Exception JavaDoc {
1051
1052        BeanUtils.setProperty(bean, "longProperty", new Byte JavaDoc((byte) 123));
1053        assertEquals((long) 123, ((Long JavaDoc) 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 JavaDoc((int) 123));
1061        assertEquals((long) 123, ((Long JavaDoc) bean.get("longProperty")).longValue());
1062        BeanUtils.setProperty(bean, "longProperty", new Long JavaDoc((long) 123));
1063        assertEquals((long) 123, ((Long JavaDoc) bean.get("longProperty")).longValue());
1064        BeanUtils.setProperty(bean, "longProperty", new Short JavaDoc((short) 123));
1065        assertEquals((long) 123, ((Long JavaDoc) bean.get("longProperty")).longValue());
1066
1067    }
1068
1069
1070    /**
1071     * Test copying a null property value.
1072     */

1073    public void testCopyPropertyNull() throws Exception JavaDoc {
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 JavaDoc {
1086
1087        BeanUtils.setProperty(bean, "shortProperty", new Byte JavaDoc((byte) 123));
1088        assertEquals((short) 123, ((Short JavaDoc) bean.get("shortProperty")).shortValue());
1089/*
1090        BeanUtils.setProperty(bean, "shortProperty", new Double((double) 123));
1091        assertEquals((short) 123, ((Short) bean.get("shortProperty")).shortValue());
1092        BeanUtils.setProperty(bean, "shortProperty", new Float((float) 123));
1093        assertEquals((short) 123, ((Short) bean.get("shortProperty")).shortValue());
1094*/

1095        BeanUtils.setProperty(bean, "shortProperty", new Integer JavaDoc((int) 123));
1096        assertEquals((short) 123, ((Short JavaDoc) bean.get("shortProperty")).shortValue());
1097        BeanUtils.setProperty(bean, "shortProperty", new Long JavaDoc((long) 123));
1098        assertEquals((short) 123, ((Short JavaDoc) bean.get("shortProperty")).shortValue());
1099        BeanUtils.setProperty(bean, "shortProperty", new Short JavaDoc((short) 123));
1100        assertEquals((short) 123, ((Short JavaDoc) bean.get("shortProperty")).shortValue());
1101
1102    }
1103
1104
1105    /**
1106     * Test copying a property using a nested indexed array expression,
1107     * with and without conversions.
1108     */

1109    public void testCopyPropertyNestedIndexedArray() throws Exception JavaDoc {
1110
1111        int origArray[] = { 0, 10, 20, 30, 40};
1112        int intArray[] = { 0, 0, 0 };
1113        ((TestBean) bean.get("nested")).setIntArray(intArray);
1114        int intChanged[] = { 0, 0, 0 };
1115
1116        // No conversion required
1117
BeanUtils.copyProperty(bean, "nested.intArray[1]", new Integer JavaDoc(1));
1118        checkIntArray((int[]) bean.get("intArray"), origArray);
1119        intChanged[1] = 1;
1120        checkIntArray(((TestBean) bean.get("nested")).getIntArray(),
1121                      intChanged);
1122
1123        // Widening conversion required
1124
BeanUtils.copyProperty(bean, "nested.intArray[1]", new Byte JavaDoc((byte) 2));
1125        checkIntArray((int[]) bean.get("intArray"), origArray);
1126        intChanged[1] = 2;
1127        checkIntArray(((TestBean) bean.get("nested")).getIntArray(),
1128                      intChanged);
1129
1130        // Narrowing conversion required
1131
BeanUtils.copyProperty(bean, "nested.intArray[1]", new Long JavaDoc((long) 3));
1132        checkIntArray((int[]) bean.get("intArray"), origArray);
1133        intChanged[1] = 3;
1134        checkIntArray(((TestBean) bean.get("nested")).getIntArray(),
1135                      intChanged);
1136
1137        // String conversion required
1138
BeanUtils.copyProperty(bean, "nested.intArray[1]", "4");
1139        checkIntArray((int[]) bean.get("intArray"), origArray);
1140        intChanged[1] = 4;
1141        checkIntArray(((TestBean) bean.get("nested")).getIntArray(),
1142                      intChanged);
1143
1144    }
1145
1146
1147    /**
1148     * Test copying a property using a nested mapped map property.
1149     */

1150    public void testCopyPropertyNestedMappedMap() throws Exception JavaDoc {
1151
1152        Map JavaDoc origMap = new HashMap JavaDoc();
1153        origMap.put("First Key", "First Value");
1154        origMap.put("Second Key", "Second Value");
1155        Map JavaDoc changedMap = new HashMap JavaDoc();
1156        changedMap.put("First Key", "First Value");
1157        changedMap.put("Second Key", "Second Value");
1158
1159        // No conversion required
1160
BeanUtils.copyProperty(bean, "nested.mapProperty(Second Key)",
1161                               "New Second Value");
1162        checkMap((Map JavaDoc) bean.get("mapProperty"), origMap);
1163        changedMap.put("Second Key", "New Second Value");
1164        checkMap(((TestBean) bean.get("nested")).getMapProperty(), changedMap);
1165
1166    }
1167
1168
1169    /**
1170     * Test copying a property using a nested simple expression, with and
1171     * without conversions.
1172     */

1173    public void testCopyPropertyNestedSimple() throws Exception JavaDoc {
1174
1175        bean.set("intProperty", new Integer JavaDoc(0));
1176        nested.setIntProperty(0);
1177
1178        // No conversion required
1179
BeanUtils.copyProperty(bean, "nested.intProperty", new Integer JavaDoc(1));
1180        assertEquals(0, ((Integer JavaDoc) bean.get("intProperty")).intValue());
1181        assertEquals(1, nested.getIntProperty());
1182
1183        // Widening conversion required
1184
BeanUtils.copyProperty(bean, "nested.intProperty", new Byte JavaDoc((byte) 2));
1185        assertEquals(0, ((Integer JavaDoc) bean.get("intProperty")).intValue());
1186        assertEquals(2, nested.getIntProperty());
1187
1188        // Narrowing conversion required
1189
BeanUtils.copyProperty(bean, "nested.intProperty", new Long JavaDoc((long) 3));
1190        assertEquals(0, ((Integer JavaDoc) bean.get("intProperty")).intValue());
1191        assertEquals(3, nested.getIntProperty());
1192
1193        // String conversion required
1194
BeanUtils.copyProperty(bean, "nested.intProperty", "4");
1195        assertEquals(0, ((Integer JavaDoc) bean.get("intProperty")).intValue());
1196        assertEquals(4, nested.getIntProperty());
1197
1198    }
1199
1200
1201    // ------------------------------------------------------ Protected Methods
1202

1203
1204    // Ensure that the nested intArray matches the specified values
1205
protected void checkIntArray(int actual[], int expected[]) {
1206        assertNotNull("actual array not null", actual);
1207        assertEquals("actual array length", expected.length, actual.length);
1208        for (int i = 0; i < actual.length; i++) {
1209            assertEquals("actual array value[" + i + "]",
1210                         expected[i], actual[i]);
1211        }
1212    }
1213
1214
1215    // Ensure that the actual Map matches the expected Map
1216
protected void checkMap(Map JavaDoc actual, Map JavaDoc expected) {
1217        assertNotNull("actual map not null", actual);
1218        assertEquals("actual map size", expected.size(), actual.size());
1219        Iterator JavaDoc keys = expected.keySet().iterator();
1220        while (keys.hasNext()) {
1221            Object JavaDoc key = keys.next();
1222            assertEquals("actual map value(" + key + ")",
1223                         expected.get(key), actual.get(key));
1224        }
1225    }
1226
1227
1228    /**
1229     * Create and return a <code>DynaClass</code> instance for our test
1230     * <code>DynaBean</code>.
1231     */

1232    protected static DynaClass createDynaClass() {
1233
1234        int intArray[] = new int[0];
1235        String JavaDoc stringArray[] = new String JavaDoc[0];
1236
1237        DynaClass dynaClass = new BasicDynaClass
1238                ("TestDynaClass", null,
1239                        new DynaProperty[]{
1240                            new DynaProperty("booleanProperty", Boolean.TYPE),
1241                            new DynaProperty("booleanSecond", Boolean.TYPE),
1242                            new DynaProperty("byteProperty", Byte.TYPE),
1243                            new DynaProperty("doubleProperty", Double.TYPE),
1244                            new DynaProperty("dupProperty", stringArray.getClass()),
1245                            new DynaProperty("floatProperty", Float.TYPE),
1246                            new DynaProperty("intArray", intArray.getClass()),
1247                            new DynaProperty("intIndexed", intArray.getClass()),
1248                            new DynaProperty("intProperty", Integer.TYPE),
1249                            new DynaProperty("listIndexed", List JavaDoc.class),
1250                            new DynaProperty("longProperty", Long.TYPE),
1251                            new DynaProperty("mapProperty", Map JavaDoc.class),
1252                            new DynaProperty("mappedProperty", Map JavaDoc.class),
1253                            new DynaProperty("mappedIntProperty", Map JavaDoc.class),
1254                            new DynaProperty("nested", TestBean.class),
1255                            new DynaProperty("nullProperty", String JavaDoc.class),
1256                            new DynaProperty("shortProperty", Short.TYPE),
1257                            new DynaProperty("stringArray", stringArray.getClass()),
1258                            new DynaProperty("stringIndexed", stringArray.getClass()),
1259                            new DynaProperty("stringProperty", String JavaDoc.class),
1260                        });
1261        return (dynaClass);
1262
1263    }
1264
1265
1266}
1267
Popular Tags