KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import junit.framework.Test;
26 import junit.framework.TestCase;
27 import junit.framework.TestSuite;
28
29
30 /**
31  * <p>
32  * Test Case for the BeanUtils class. The majority of these tests use
33  * instances of the TestBean class, so be sure to update the tests if you
34  * change the characteristics of that class.
35  * </p>
36  *
37  * <p>
38  * Template for this stolen from Craigs PropertyUtilsTestCase
39  * </p>
40  *
41  * <p>
42  * Note that the tests are dependant upon the static aspects
43  * (such as array sizes...) of the TestBean.java class, so ensure
44  * than all changes to TestBean are reflected here.
45  * </p>
46  *
47  * <p>
48  * So far, this test case has tests for the following methods of the
49  * <code>BeanUtils</code> class:
50  * </p>
51  * <ul>
52  * <li>getArrayProperty(Object bean, String name)</li>
53  * </ul>
54  *
55  * @author <a HREF="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
56  * @version $Revision: 1.29 $
57  */

58
59 public class BeanUtilsTestCase extends TestCase {
60
61     // ---------------------------------------------------- Instance Variables
62

63     /**
64      * The test bean for each test.
65      */

66     protected TestBean bean = null;
67
68
69     /**
70      * The set of properties that should be described.
71      */

72     protected String JavaDoc describes[] =
73     { "booleanProperty",
74       "booleanSecond",
75       "byteProperty",
76       "doubleProperty",
77       "dupProperty",
78       "floatProperty",
79       "intArray",
80       // "intIndexed",
81
"longProperty",
82       "listIndexed",
83       "longProperty",
84       // "mappedProperty",
85
// "mappedIntProperty",
86
"nested",
87       "nullProperty",
88       "readOnlyProperty",
89       "shortProperty",
90       "stringArray",
91       // "stringIndexed",
92
"stringProperty"
93     };
94
95
96     // ---------------------------------------------------------- Constructors
97

98     /**
99      * Construct a new instance of this test case.
100      *
101      * @param name Name of the test case
102      */

103     public BeanUtilsTestCase(String JavaDoc name) {
104         super(name);
105     }
106
107
108     // -------------------------------------------------- Overall Test Methods
109

110
111     /**
112      * Set up instance variables required by this test case.
113      */

114     public void setUp() {
115         bean = new TestBean();
116     }
117
118
119     /**
120      * Return the tests included in this test suite.
121      */

122     public static Test suite() {
123         return (new TestSuite(BeanUtilsTestCase.class));
124     }
125
126     /**
127      * Tear down instance variables required by this test case.
128      */

129     public void tearDown() {
130         bean = null;
131     }
132
133
134     // ------------------------------------------------ Individual Test Methods
135

136
137     /**
138      * Test the copyProperties() method from a DynaBean.
139      */

140     public void testCopyPropertiesDynaBean() {
141
142         // Set up an origin bean with customized properties
143
DynaClass dynaClass = DynaBeanUtilsTestCase.createDynaClass();
144         DynaBean orig = null;
145         try {
146             orig = dynaClass.newInstance();
147         } catch (Exception JavaDoc e) {
148             fail("newInstance(): " + e);
149         }
150         orig.set("booleanProperty", Boolean.FALSE);
151         orig.set("byteProperty", new Byte JavaDoc((byte) 111));
152         orig.set("doubleProperty", new Double JavaDoc(333.33));
153         orig.set("dupProperty",
154                  new String JavaDoc[] { "New 0", "New 1", "New 2" });
155         orig.set("intArray", new int[] { 100, 200, 300 });
156         orig.set("intProperty", new Integer JavaDoc(333));
157         orig.set("longProperty", new Long JavaDoc(3333));
158         orig.set("shortProperty", new Short JavaDoc((short) 33));
159         orig.set("stringArray", new String JavaDoc[] { "New 0", "New 1" });
160         orig.set("stringProperty", "Custom string");
161
162         // Copy the origin bean to our destination test bean
163
try {
164             BeanUtils.copyProperties(bean, orig);
165         } catch (Exception JavaDoc e) {
166             fail("Threw exception: " + e);
167         }
168
169         // Validate the results for scalar properties
170
assertEquals("Copied boolean property",
171                      false,
172                      bean.getBooleanProperty());
173         assertEquals("Copied byte property",
174                      (byte) 111,
175                      bean.getByteProperty());
176         assertEquals("Copied double property",
177                      333.33,
178                      bean.getDoubleProperty(),
179                      0.005);
180         assertEquals("Copied int property",
181                      333,
182                      bean.getIntProperty());
183         assertEquals("Copied long property",
184                      (long) 3333,
185                      bean.getLongProperty());
186         assertEquals("Copied short property",
187                      (short) 33,
188                      bean.getShortProperty());
189         assertEquals("Copied string property",
190                      "Custom string",
191                      bean.getStringProperty());
192
193         // Validate the results for array properties
194
String JavaDoc dupProperty[] = bean.getDupProperty();
195         assertNotNull("dupProperty present", dupProperty);
196         assertEquals("dupProperty length", 3, dupProperty.length);
197         assertEquals("dupProperty[0]", "New 0", dupProperty[0]);
198         assertEquals("dupProperty[1]", "New 1", dupProperty[1]);
199         assertEquals("dupProperty[2]", "New 2", dupProperty[2]);
200         int intArray[] = bean.getIntArray();
201         assertNotNull("intArray present", intArray);
202         assertEquals("intArray length", 3, intArray.length);
203         assertEquals("intArray[0]", 100, intArray[0]);
204         assertEquals("intArray[1]", 200, intArray[1]);
205         assertEquals("intArray[2]", 300, intArray[2]);
206         String JavaDoc stringArray[] = bean.getStringArray();
207         assertNotNull("stringArray present", stringArray);
208         assertEquals("stringArray length", 2, stringArray.length);
209         assertEquals("stringArray[0]", "New 0", stringArray[0]);
210         assertEquals("stringArray[1]", "New 1", stringArray[1]);
211
212     }
213
214
215     /**
216      * Test copyProperties() when the origin is a a <code>Map</code>.
217      */

218     public void testCopyPropertiesMap() {
219
220         Map JavaDoc map = new HashMap JavaDoc();
221         map.put("booleanProperty", "false");
222         map.put("byteProperty", "111");
223         map.put("doubleProperty", "333.0");
224         map.put("dupProperty", new String JavaDoc[] { "New 0", "New 1", "New 2" });
225         map.put("floatProperty", "222.0");
226         map.put("intArray", new String JavaDoc[] { "0", "100", "200" });
227         map.put("intProperty", "111");
228         map.put("longProperty", "444");
229         map.put("shortProperty", "555");
230         map.put("stringProperty", "New String Property");
231
232         try {
233             BeanUtils.copyProperties(bean, map);
234         } catch (Throwable JavaDoc t) {
235             fail("Threw " + t.toString());
236         }
237
238         // Scalar properties
239
assertEquals("booleanProperty", false,
240                      bean.getBooleanProperty());
241         assertEquals("byteProperty", (byte) 111,
242                      bean.getByteProperty());
243         assertEquals("doubleProperty", 333.0,
244                      bean.getDoubleProperty(), 0.005);
245         assertEquals("floatProperty", (float) 222.0,
246                      bean.getFloatProperty(), (float) 0.005);
247         assertEquals("longProperty", 111,
248                      bean.getIntProperty());
249         assertEquals("longProperty", (long) 444,
250                      bean.getLongProperty());
251         assertEquals("shortProperty", (short) 555,
252                      bean.getShortProperty());
253         assertEquals("stringProperty", "New String Property",
254                      bean.getStringProperty());
255
256         // Indexed Properties
257
String JavaDoc dupProperty[] = bean.getDupProperty();
258         assertNotNull("dupProperty present", dupProperty);
259         assertEquals("dupProperty length", 3, dupProperty.length);
260         assertEquals("dupProperty[0]", "New 0", dupProperty[0]);
261         assertEquals("dupProperty[1]", "New 1", dupProperty[1]);
262         assertEquals("dupProperty[2]", "New 2", dupProperty[2]);
263         int intArray[] = bean.getIntArray();
264         assertNotNull("intArray present", intArray);
265         assertEquals("intArray length", 3, intArray.length);
266         assertEquals("intArray[0]", 0, intArray[0]);
267         assertEquals("intArray[1]", 100, intArray[1]);
268         assertEquals("intArray[2]", 200, intArray[2]);
269
270     }
271
272
273     /**
274      * Test the copyProperties() method from a standard JavaBean.
275      */

276     public void testCopyPropertiesStandard() {
277
278         // Set up an origin bean with customized properties
279
TestBean orig = new TestBean();
280         orig.setBooleanProperty(false);
281         orig.setByteProperty((byte) 111);
282         orig.setDoubleProperty(333.33);
283         orig.setDupProperty(new String JavaDoc[] { "New 0", "New 1", "New 2" });
284         orig.setIntArray(new int[] { 100, 200, 300 });
285         orig.setIntProperty(333);
286         orig.setLongProperty(3333);
287         orig.setShortProperty((short) 33);
288         orig.setStringArray(new String JavaDoc[] { "New 0", "New 1" });
289         orig.setStringProperty("Custom string");
290
291         // Copy the origin bean to our destination test bean
292
try {
293             BeanUtils.copyProperties(bean, orig);
294         } catch (Exception JavaDoc e) {
295             fail("Threw exception: " + e);
296         }
297
298         // Validate the results for scalar properties
299
assertEquals("Copied boolean property",
300                      false,
301                      bean.getBooleanProperty());
302         assertEquals("Copied byte property",
303                      (byte) 111,
304                      bean.getByteProperty());
305         assertEquals("Copied double property",
306                      333.33,
307                      bean.getDoubleProperty(),
308                      0.005);
309         assertEquals("Copied int property",
310                      333,
311                      bean.getIntProperty());
312         assertEquals("Copied long property",
313                      (long) 3333,
314                      bean.getLongProperty());
315         assertEquals("Copied short property",
316                      (short) 33,
317                      bean.getShortProperty());
318         assertEquals("Copied string property",
319                      "Custom string",
320                      bean.getStringProperty());
321
322         // Validate the results for array properties
323
String JavaDoc dupProperty[] = bean.getDupProperty();
324         assertNotNull("dupProperty present", dupProperty);
325         assertEquals("dupProperty length", 3, dupProperty.length);
326         assertEquals("dupProperty[0]", "New 0", dupProperty[0]);
327         assertEquals("dupProperty[1]", "New 1", dupProperty[1]);
328         assertEquals("dupProperty[2]", "New 2", dupProperty[2]);
329         int intArray[] = bean.getIntArray();
330         assertNotNull("intArray present", intArray);
331         assertEquals("intArray length", 3, intArray.length);
332         assertEquals("intArray[0]", 100, intArray[0]);
333         assertEquals("intArray[1]", 200, intArray[1]);
334         assertEquals("intArray[2]", 300, intArray[2]);
335         String JavaDoc stringArray[] = bean.getStringArray();
336         assertNotNull("stringArray present", stringArray);
337         assertEquals("stringArray length", 2, stringArray.length);
338         assertEquals("stringArray[0]", "New 0", stringArray[0]);
339         assertEquals("stringArray[1]", "New 1", stringArray[1]);
340
341     }
342
343
344     /**
345      * Test the describe() method.
346      */

347     public void testDescribe() {
348
349         Map JavaDoc map = null;
350         try {
351             map = BeanUtils.describe(bean);
352         } catch (Exception JavaDoc e) {
353             fail("Threw exception " + e);
354         }
355
356         // Verify existence of all the properties that should be present
357
for (int i = 0; i < describes.length; i++) {
358             assertTrue("Property '" + describes[i] + "' is present",
359                        map.containsKey(describes[i]));
360         }
361         assertTrue("Property 'writeOnlyProperty' is not present",
362                    !map.containsKey("writeOnlyProperty"));
363
364         // Verify the values of scalar properties
365
assertEquals("Value of 'booleanProperty'",
366                      "true",
367                      (String JavaDoc) map.get("booleanProperty"));
368         assertEquals("Value of 'byteProperty'",
369                      "121",
370                      (String JavaDoc) map.get("byteProperty"));
371         assertEquals("Value of 'doubleProperty'",
372                      "321.0",
373                      (String JavaDoc) map.get("doubleProperty"));
374         assertEquals("Value of 'floatProperty'",
375                      "123.0",
376                      (String JavaDoc) map.get("floatProperty"));
377         assertEquals("Value of 'intProperty'",
378                      "123",
379                      (String JavaDoc) map.get("intProperty"));
380         assertEquals("Value of 'longProperty'",
381                      "321",
382                      (String JavaDoc) map.get("longProperty"));
383         assertEquals("Value of 'shortProperty'",
384                      "987",
385                      (String JavaDoc) map.get("shortProperty"));
386         assertEquals("Value of 'stringProperty'",
387                      "This is a string",
388                      (String JavaDoc) map.get("stringProperty"));
389
390     }
391
392
393     /**
394      * tests the string and int arrays of TestBean
395      */

396     public void testGetArrayProperty() {
397         try {
398             String JavaDoc arr[] = BeanUtils.getArrayProperty(bean, "stringArray");
399             String JavaDoc comp[] = bean.getStringArray();
400
401             assertTrue("String array length = " + comp.length,
402                     (comp.length == arr.length));
403
404             arr = BeanUtils.getArrayProperty(bean, "intArray");
405             int iarr[] = bean.getIntArray();
406
407             assertTrue("String array length = " + iarr.length,
408                     (iarr.length == arr.length));
409         } catch (IllegalAccessException JavaDoc e) {
410             fail("IllegalAccessException");
411         } catch (InvocationTargetException JavaDoc e) {
412             fail("InvocationTargetException");
413         } catch (NoSuchMethodException JavaDoc e) {
414             fail("NoSuchMethodException");
415         }
416
417     }
418
419
420     /**
421      * tests getting an indexed property
422      */

423     public void testGetIndexedProperty1() {
424         try {
425             String JavaDoc val = BeanUtils.getIndexedProperty(bean, "intIndexed[3]");
426             String JavaDoc comp = String.valueOf(bean.getIntIndexed(3));
427             assertTrue("intIndexed[3] == " + comp, val.equals(comp));
428
429             val = BeanUtils.getIndexedProperty(bean, "stringIndexed[3]");
430             comp = bean.getStringIndexed(3);
431             assertTrue("stringIndexed[3] == " + comp, val.equals(comp));
432         } catch (IllegalAccessException JavaDoc e) {
433             fail("IllegalAccessException");
434         } catch (InvocationTargetException JavaDoc e) {
435             fail("InvocationTargetException");
436         } catch (NoSuchMethodException JavaDoc e) {
437             fail("NoSuchMethodException");
438         }
439     }
440
441
442     /**
443      * tests getting an indexed property
444      */

445     public void testGetIndexedProperty2() {
446         try {
447             String JavaDoc val = BeanUtils.getIndexedProperty(bean, "intIndexed", 3);
448             String JavaDoc comp = String.valueOf(bean.getIntIndexed(3));
449
450             assertTrue("intIndexed,3 == " + comp, val.equals(comp));
451
452             val = BeanUtils.getIndexedProperty(bean, "stringIndexed", 3);
453             comp = bean.getStringIndexed(3);
454
455             assertTrue("stringIndexed,3 == " + comp, val.equals(comp));
456
457         } catch (IllegalAccessException JavaDoc e) {
458             fail("IllegalAccessException");
459         } catch (InvocationTargetException JavaDoc e) {
460             fail("InvocationTargetException");
461         } catch (NoSuchMethodException JavaDoc e) {
462             fail("NoSuchMethodException");
463         }
464     }
465
466
467     /**
468      * tests getting a nested property
469      */

470     public void testGetNestedProperty() {
471         try {
472             String JavaDoc val = BeanUtils.getNestedProperty(bean, "nested.stringProperty");
473             String JavaDoc comp = bean.getNested().getStringProperty();
474             assertTrue("nested.StringProperty == " + comp,
475                     val.equals(comp));
476         } catch (IllegalAccessException JavaDoc e) {
477             fail("IllegalAccessException");
478         } catch (InvocationTargetException JavaDoc e) {
479             fail("InvocationTargetException");
480         } catch (NoSuchMethodException JavaDoc e) {
481             fail("NoSuchMethodException");
482         }
483     }
484
485
486     /**
487      * tests getting a 'whatever' property
488      */

489     public void testGetGeneralProperty() {
490         try {
491             String JavaDoc val = BeanUtils.getProperty(bean, "nested.intIndexed[2]");
492             String JavaDoc comp = String.valueOf(bean.getIntIndexed(2));
493
494             assertTrue("nested.intIndexed[2] == " + comp,
495                     val.equals(comp));
496         } catch (IllegalAccessException JavaDoc e) {
497             fail("IllegalAccessException");
498         } catch (InvocationTargetException JavaDoc e) {
499             fail("InvocationTargetException");
500         } catch (NoSuchMethodException JavaDoc e) {
501             fail("NoSuchMethodException");
502         }
503     }
504
505
506     /**
507      * tests getting a 'whatever' property
508      */

509     public void testGetSimpleProperty() {
510         try {
511             String JavaDoc val = BeanUtils.getSimpleProperty(bean, "shortProperty");
512             String JavaDoc comp = String.valueOf(bean.getShortProperty());
513
514             assertTrue("shortProperty == " + comp,
515                     val.equals(comp));
516         } catch (IllegalAccessException JavaDoc e) {
517             fail("IllegalAccessException");
518         } catch (InvocationTargetException JavaDoc e) {
519             fail("InvocationTargetException");
520         } catch (NoSuchMethodException JavaDoc e) {
521             fail("NoSuchMethodException");
522         }
523     }
524
525
526     /**
527      * Test populate() method on individual array elements.
528      */

529     public void testPopulateArrayElements() {
530
531         try {
532
533             HashMap JavaDoc map = new HashMap JavaDoc();
534             map.put("intIndexed[0]", "100");
535             map.put("intIndexed[2]", "120");
536             map.put("intIndexed[4]", "140");
537
538             BeanUtils.populate(bean, map);
539
540             assertEquals("intIndexed[0] is 100",
541                          100, bean.getIntIndexed(0));
542             assertEquals("intIndexed[1] is 10",
543                          10, bean.getIntIndexed(1));
544             assertEquals("intIndexed[2] is 120",
545                          120, bean.getIntIndexed(2));
546             assertEquals("intIndexed[3] is 30",
547                          30, bean.getIntIndexed(3));
548             assertEquals("intIndexed[4] is 140",
549                          140, bean.getIntIndexed(4));
550
551             map.clear();
552             map.put("stringIndexed[1]", "New String 1");
553             map.put("stringIndexed[3]", "New String 3");
554
555             BeanUtils.populate(bean, map);
556
557             assertEquals("stringIndexed[0] is \"String 0\"",
558                          "String 0", bean.getStringIndexed(0));
559             assertEquals("stringIndexed[1] is \"New String 1\"",
560                          "New String 1", bean.getStringIndexed(1));
561             assertEquals("stringIndexed[2] is \"String 2\"",
562                          "String 2", bean.getStringIndexed(2));
563             assertEquals("stringIndexed[3] is \"New String 3\"",
564                          "New String 3", bean.getStringIndexed(3));
565             assertEquals("stringIndexed[4] is \"String 4\"",
566                          "String 4", bean.getStringIndexed(4));
567
568         } catch (IllegalAccessException JavaDoc e) {
569             fail("IllegalAccessException");
570         } catch (InvocationTargetException JavaDoc e) {
571             fail("InvocationTargetException");
572         }
573
574     }
575
576
577     /**
578      * Test populate() method on array properties as a whole.
579      */

580     public void testPopulateArrayProperties() {
581
582         try {
583
584             HashMap JavaDoc map = new HashMap JavaDoc();
585             int intArray[] = new int[] { 123, 456, 789 };
586             map.put("intArray", intArray);
587             String JavaDoc stringArray[] = new String JavaDoc[]
588                 { "New String 0", "New String 1" };
589             map.put("stringArray", stringArray);
590
591             BeanUtils.populate(bean, map);
592
593             intArray = bean.getIntArray();
594             assertNotNull("intArray is present", intArray);
595             assertEquals("intArray length",
596                          3, intArray.length);
597             assertEquals("intArray[0]", 123, intArray[0]);
598             assertEquals("intArray[1]", 456, intArray[1]);
599             assertEquals("intArray[2]", 789, intArray[2]);
600             stringArray = bean.getStringArray();
601             assertNotNull("stringArray is present", stringArray);
602             assertEquals("stringArray length", 2, stringArray.length);
603             assertEquals("stringArray[0]", "New String 0", stringArray[0]);
604             assertEquals("stringArray[1]", "New String 1", stringArray[1]);
605
606         } catch (IllegalAccessException JavaDoc e) {
607             fail("IllegalAccessException");
608         } catch (InvocationTargetException JavaDoc e) {
609             fail("InvocationTargetException");
610         }
611
612     }
613
614
615     /**
616      * Test populate() on mapped properties.
617      */

618     public void testPopulateMapped() {
619
620         try {
621
622             HashMap JavaDoc map = new HashMap JavaDoc();
623             map.put("mappedProperty(First Key)", "New First Value");
624             map.put("mappedProperty(Third Key)", "New Third Value");
625
626             BeanUtils.populate(bean, map);
627
628             assertEquals("mappedProperty(First Key)",
629                          "New First Value",
630                          bean.getMappedProperty("First Key"));
631             assertEquals("mappedProperty(Second Key)",
632                          "Second Value",
633                          bean.getMappedProperty("Second Key"));
634             assertEquals("mappedProperty(Third Key)",
635                          "New Third Value",
636                          bean.getMappedProperty("Third Key"));
637             assertNull("mappedProperty(Fourth Key",
638                        bean.getMappedProperty("Fourth Key"));
639
640         } catch (IllegalAccessException JavaDoc e) {
641             fail("IllegalAccessException");
642         } catch (InvocationTargetException JavaDoc e) {
643             fail("InvocationTargetException");
644         }
645
646     }
647
648
649     /**
650      * Test populate() method on nested properties.
651      */

652     public void testPopulateNested() {
653
654         try {
655
656             HashMap JavaDoc map = new HashMap JavaDoc();
657             map.put("nested.booleanProperty", "false");
658             // booleanSecond is left at true
659
map.put("nested.doubleProperty", "432.0");
660             // floatProperty is left at 123.0
661
map.put("nested.intProperty", "543");
662             // longProperty is left at 321
663
map.put("nested.shortProperty", "654");
664             // stringProperty is left at "This is a string"
665
map.put("nested.writeOnlyProperty", "New writeOnlyProperty value");
666
667             BeanUtils.populate(bean, map);
668
669             assertTrue("booleanProperty is false",
670                        !bean.getNested().getBooleanProperty());
671             assertTrue("booleanSecond is true",
672                        bean.getNested().isBooleanSecond());
673             assertEquals("doubleProperty is 432.0",
674                          (double) 432.0,
675                          bean.getNested().getDoubleProperty(),
676                          (double) 0.005);
677             assertEquals("floatProperty is 123.0",
678                          (float) 123.0,
679                          bean.getNested().getFloatProperty(),
680                          (float) 0.005);
681             assertEquals("intProperty is 543",
682                          543, bean.getNested().getIntProperty());
683             assertEquals("longProperty is 321",
684                          (long) 321, bean.getNested().getLongProperty());
685             assertEquals("shortProperty is 654",
686                          (short) 654, bean.getNested().getShortProperty());
687             assertEquals("stringProperty is \"This is a string\"",
688                          "This is a string",
689                          bean.getNested().getStringProperty());
690             assertEquals("writeOnlyProperty is \"New writeOnlyProperty value\"",
691                          "New writeOnlyProperty value",
692                          bean.getNested().getWriteOnlyPropertyValue());
693
694         } catch (IllegalAccessException JavaDoc e) {
695             fail("IllegalAccessException");
696         } catch (InvocationTargetException JavaDoc e) {
697             fail("InvocationTargetException");
698         }
699
700     }
701
702
703     /**
704      * Test populate() method on scalar properties.
705      */

706     public void testPopulateScalar() {
707
708         try {
709
710             bean.setNullProperty("Non-null value");
711
712             HashMap JavaDoc map = new HashMap JavaDoc();
713             map.put("booleanProperty", "false");
714             // booleanSecond is left at true
715
map.put("byteProperty", "111");
716             map.put("doubleProperty", "432.0");
717             // floatProperty is left at 123.0
718
map.put("intProperty", "543");
719             map.put("longProperty", "");
720             map.put("nullProperty", null);
721             map.put("shortProperty", "654");
722             // stringProperty is left at "This is a string"
723
map.put("writeOnlyProperty", "New writeOnlyProperty value");
724             map.put("readOnlyProperty", "New readOnlyProperty value");
725
726             BeanUtils.populate(bean, map);
727
728             assertTrue("booleanProperty is false", !bean.getBooleanProperty());
729             assertTrue("booleanSecond is true", bean.isBooleanSecond());
730             assertEquals("byteProperty is 111",
731                          (byte) 111, bean.getByteProperty());
732             assertEquals("doubleProperty is 432.0",
733                          (double) 432.0, bean.getDoubleProperty(),
734                          (double) 0.005);
735             assertEquals("floatProperty is 123.0",
736                          (float) 123.0, bean.getFloatProperty(),
737                          (float) 0.005);
738             assertEquals("intProperty is 543",
739                          543, bean.getIntProperty());
740             assertEquals("longProperty is 0",
741                          (long) 0, bean.getLongProperty());
742             assertNull("nullProperty is null",
743                        bean.getNullProperty());
744             assertEquals("shortProperty is 654",
745                          (short) 654, bean.getShortProperty());
746             assertEquals("stringProperty is \"This is a string\"",
747