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                          "This is a string", bean.getStringProperty());
748             assertEquals("writeOnlyProperty is \"New writeOnlyProperty value\"",
749                          "New writeOnlyProperty value",
750                          bean.getWriteOnlyPropertyValue());
751             assertEquals("readOnlyProperty is \"Read Only String Property\"",
752                          "Read Only String Property",
753                          bean.getReadOnlyProperty());
754
755         } catch (IllegalAccessException JavaDoc e) {
756             fail("IllegalAccessException");
757         } catch (InvocationTargetException JavaDoc e) {
758             fail("InvocationTargetException");
759         }
760
761     }
762
763
764     /**
765      * Test calling setProperty() with null property values.
766      */

767     public void testSetPropertyNullValues() throws Exception JavaDoc {
768
769         Object JavaDoc oldValue = null;
770         Object JavaDoc newValue = null;
771
772         // Scalar value into array
773
oldValue = PropertyUtils.getSimpleProperty(bean, "stringArray");
774         BeanUtils.setProperty(bean, "stringArray", (String JavaDoc) null);
775         newValue = PropertyUtils.getSimpleProperty(bean, "stringArray");
776         assertNotNull("stringArray is not null", newValue);
777         assertTrue("stringArray of correct type",
778                    newValue instanceof String JavaDoc[]);
779         assertEquals("stringArray length",
780                      1, ((String JavaDoc[]) newValue).length);
781         assertTrue("stringArray[0] is null",
782                    ((String JavaDoc[]) newValue)[0] == null);
783         PropertyUtils.setProperty(bean, "stringArray", oldValue);
784
785         // Indexed value into array
786
oldValue = PropertyUtils.getSimpleProperty(bean, "stringArray");
787         BeanUtils.setProperty(bean, "stringArray[2]", (String JavaDoc) null);
788         newValue = PropertyUtils.getSimpleProperty(bean, "stringArray");
789         assertNotNull("stringArray is not null", newValue);
790         assertTrue("stringArray of correct type",
791                    newValue instanceof String JavaDoc[]);
792         assertEquals("stringArray length",
793                      5, ((String JavaDoc[]) newValue).length);
794         assertTrue("stringArray[2] is null",
795                    ((String JavaDoc[]) newValue)[2] == null);
796         PropertyUtils.setProperty(bean, "stringArray", oldValue);
797
798         // Value into scalar
799
BeanUtils.setProperty(bean, "stringProperty", null);
800         assertTrue("stringProperty is now null",
801                    BeanUtils.getProperty(bean, "stringProperty") == null);
802
803     }
804
805
806     /**
807      * Test converting to and from primitive wrapper types.
808      */

809     public void testSetPropertyOnPrimitiveWrappers() throws Exception JavaDoc {
810
811         BeanUtils.setProperty(bean,"intProperty", new Integer JavaDoc(1));
812         assertEquals(1,bean.getIntProperty());
813         BeanUtils.setProperty(bean,"stringProperty", new Integer JavaDoc(1));
814         assertEquals(1, Integer.parseInt(bean.getStringProperty()));
815
816     }
817
818
819     /**
820      * Test narrowing and widening conversions on byte.
821      */

822     public void testSetPropertyByte() throws Exception JavaDoc {
823
824         BeanUtils.setProperty(bean, "byteProperty", new Byte JavaDoc((byte) 123));
825         assertEquals((byte) 123, bean.getByteProperty());
826 /*
827         BeanUtils.setProperty(bean, "byteProperty", new Double((double) 123));
828         assertEquals((byte) 123, bean.getByteProperty());
829         BeanUtils.setProperty(bean, "byteProperty", new Float((float) 123));
830         assertEquals((byte) 123, bean.getByteProperty());
831 */

832         BeanUtils.setProperty(bean, "byteProperty", new Integer JavaDoc((int) 123));
833         assertEquals((byte) 123, bean.getByteProperty());
834         BeanUtils.setProperty(bean, "byteProperty", new Long JavaDoc((long) 123));
835         assertEquals((byte) 123, bean.getByteProperty());
836         BeanUtils.setProperty(bean, "byteProperty", new Short JavaDoc((short) 123));
837         assertEquals((byte) 123, bean.getByteProperty());
838
839     }
840
841
842     /**
843      * Test narrowing and widening conversions on double.
844      */

845     public void testSetPropertyDouble() throws Exception JavaDoc {
846
847         BeanUtils.setProperty(bean, "doubleProperty", new Byte JavaDoc((byte) 123));
848         assertEquals((double) 123, bean.getDoubleProperty(), 0.005);
849         BeanUtils.setProperty(bean, "doubleProperty", new Double JavaDoc((double) 123));
850         assertEquals((double) 123, bean.getDoubleProperty(), 0.005);
851         BeanUtils.setProperty(bean, "doubleProperty", new Float JavaDoc((float) 123));
852         assertEquals((double) 123, bean.getDoubleProperty(), 0.005);
853         BeanUtils.setProperty(bean, "doubleProperty", new Integer JavaDoc((int) 123));
854         assertEquals((double) 123, bean.getDoubleProperty(), 0.005);
855         BeanUtils.setProperty(bean, "doubleProperty", new Long JavaDoc((long) 123));
856         assertEquals((double) 123, bean.getDoubleProperty(), 0.005);
857         BeanUtils.setProperty(bean, "doubleProperty", new Short JavaDoc((short) 123));
858         assertEquals((double) 123, bean.getDoubleProperty(), 0.005);
859
860     }
861
862
863     /**
864      * Test narrowing and widening conversions on float.
865      */

866     public void testSetPropertyFloat() throws Exception JavaDoc {
867
868         BeanUtils.setProperty(bean, "floatProperty", new Byte JavaDoc((byte) 123));
869         assertEquals((float) 123, bean.getFloatProperty(), 0.005);
870         BeanUtils.setProperty(bean, "floatProperty", new Double JavaDoc((double) 123));
871         assertEquals((float) 123, bean.getFloatProperty(), 0.005);
872         BeanUtils.setProperty(bean, "floatProperty", new Float JavaDoc((float) 123));
873         assertEquals((float) 123, bean.getFloatProperty(), 0.005);
874         BeanUtils.setProperty(bean, "floatProperty", new Integer JavaDoc((int) 123));
875         assertEquals((float) 123, bean.getFloatProperty(), 0.005);
876         BeanUtils.setProperty(bean, "floatProperty", new Long JavaDoc((long) 123));
877         assertEquals((float) 123, bean.getFloatProperty(), 0.005);
878         BeanUtils.setProperty(bean, "floatProperty", new Short JavaDoc((short) 123));
879         assertEquals((float) 123, bean.getFloatProperty(), 0.005);
880
881     }
882
883
884     /**
885      * Test narrowing and widening conversions on int.
886      */

887     public void testSetPropertyInteger() throws Exception JavaDoc {
888
889         BeanUtils.setProperty(bean, "longProperty", new Byte JavaDoc((byte) 123));
890         assertEquals((int) 123, bean.getIntProperty());
891 /*
892         BeanUtils.setProperty(bean, "longProperty", new Double((double) 123));
893         assertEquals((int) 123, bean.getIntProperty());
894         BeanUtils.setProperty(bean, "longProperty", new Float((float) 123));
895         assertEquals((int) 123, bean.getIntProperty());
896 */

897         BeanUtils.setProperty(bean, "longProperty", new Integer JavaDoc((int) 123));
898         assertEquals((int) 123, bean.getIntProperty());
899         BeanUtils.setProperty(bean, "longProperty", new Long JavaDoc((long) 123));
900         assertEquals((int) 123, bean.getIntProperty());
901         BeanUtils.setProperty(bean, "longProperty", new Short JavaDoc((short) 123));
902         assertEquals((int) 123, bean.getIntProperty());
903
904     }
905
906
907     /**
908      * Test narrowing and widening conversions on long.
909      */

910     public void testSetPropertyLong() throws Exception JavaDoc {
911
912         BeanUtils.setProperty(bean, "longProperty", new Byte JavaDoc((byte) 123));
913         assertEquals((long) 123, bean.getLongProperty());
914 /*
915         BeanUtils.setProperty(bean, "longProperty", new Double((double) 123));
916         assertEquals((long) 123, bean.getLongProperty());
917         BeanUtils.setProperty(bean, "longProperty", new Float((float) 123));
918         assertEquals((long) 123, bean.getLongProperty());
919 */

920         BeanUtils.setProperty(bean, "longProperty", new Integer JavaDoc((int) 123));
921         assertEquals((long) 123, bean.getLongProperty());
922         BeanUtils.setProperty(bean, "longProperty", new Long JavaDoc((long) 123));
923         assertEquals((long) 123, bean.getLongProperty());
924         BeanUtils.setProperty(bean, "longProperty", new Short JavaDoc((short) 123));
925         assertEquals((long) 123, bean.getLongProperty());
926
927     }
928
929
930     /**
931      * Test setting a null property value.
932      */

933     public void testSetPropertyNull() throws Exception JavaDoc {
934
935         bean.setNullProperty("non-null value");
936         BeanUtils.setProperty(bean, "nullProperty", null);
937         assertNull("nullProperty is null", bean.getNullProperty());
938
939     }
940
941
942     /**
943      * Test narrowing and widening conversions on short.
944      */

945     public void testSetPropertyShort() throws Exception JavaDoc {
946
947         BeanUtils.setProperty(bean, "shortProperty", new Byte JavaDoc((byte) 123));
948         assertEquals((short) 123, bean.getShortProperty());
949 /*
950         BeanUtils.setProperty(bean, "shortProperty", new Double((double) 123));
951         assertEquals((short) 123, bean.getShortProperty());
952         BeanUtils.setProperty(bean, "shortProperty", new Float((float) 123));
953         assertEquals((short) 123, bean.getShortProperty());
954 */

955         BeanUtils.setProperty(bean, "shortProperty", new Integer JavaDoc((int) 123));
956         assertEquals((short) 123, bean.getShortProperty());
957         BeanUtils.setProperty(bean, "shortProperty", new Long JavaDoc((long) 123));
958         assertEquals((short) 123, bean.getShortProperty());
959         BeanUtils.setProperty(bean, "shortProperty", new Short JavaDoc((short) 123));
960         assertEquals((short) 123, bean.getShortProperty());
961
962     }
963
964
965     /**
966      * Test narrowing and widening conversions on byte.
967      */

968     public void testCopyPropertyByte() throws Exception JavaDoc {
969
970         BeanUtils.copyProperty(bean, "byteProperty", new Byte JavaDoc((byte) 123));
971         assertEquals((byte) 123, bean.getByteProperty());
972         BeanUtils.copyProperty(bean, "byteProperty", new Double JavaDoc((double) 123));
973         assertEquals((byte) 123, bean.getByteProperty());
974         BeanUtils.copyProperty(bean, "byteProperty", new Float JavaDoc((float) 123));
975         assertEquals((byte) 123, bean.getByteProperty());
976         BeanUtils.copyProperty(bean, "byteProperty", new Integer JavaDoc((int) 123));
977         assertEquals((byte) 123, bean.getByteProperty());
978         BeanUtils.copyProperty(bean, "byteProperty", new Long JavaDoc((long) 123));
979         assertEquals((byte) 123, bean.getByteProperty());
980         BeanUtils.copyProperty(bean, "byteProperty", new Short JavaDoc((short) 123));
981         assertEquals((byte) 123, bean.getByteProperty());
982
983     }
984
985
986     /**
987      * Test narrowing and widening conversions on double.
988      */

989     public void testCopyPropertyDouble() throws Exception JavaDoc {
990
991         BeanUtils.copyProperty(bean, "doubleProperty", new Byte JavaDoc((byte) 123));
992         assertEquals((double) 123, bean.getDoubleProperty(), 0.005);
993         BeanUtils.copyProperty(bean, "doubleProperty", new Double JavaDoc((double) 123));
994         assertEquals((double) 123, bean.getDoubleProperty(), 0.005);
995         BeanUtils.copyProperty(bean, "doubleProperty", new Float JavaDoc((float) 123));
996         assertEquals((double) 123, bean.getDoubleProperty(), 0.005);
997         BeanUtils.copyProperty(bean, "doubleProperty", new Integer JavaDoc((int) 123));
998         assertEquals((double) 123, bean.getDoubleProperty(), 0.005);
999         BeanUtils.copyProperty(bean, "doubleProperty", new Long JavaDoc((long) 123));
1000        assertEquals((double) 123, bean.getDoubleProperty(), 0.005);
1001        BeanUtils.copyProperty(bean, "doubleProperty", new Short JavaDoc((short) 123));
1002        assertEquals((double) 123, bean.getDoubleProperty(), 0.005);
1003
1004    }
1005
1006
1007    /**
1008     * Test narrowing and widening conversions on float.
1009     */

1010    public void testCopyPropertyFloat() throws Exception JavaDoc {
1011
1012        BeanUtils.copyProperty(bean, "floatProperty", new Byte JavaDoc((byte) 123));
1013        assertEquals((float) 123, bean.getFloatProperty(), 0.005);
1014        BeanUtils.copyProperty(bean, "floatProperty", new Double JavaDoc((double) 123));
1015        assertEquals((float) 123, bean.getFloatProperty(), 0.005);
1016        BeanUtils.copyProperty(bean, "floatProperty", new Float JavaDoc((float) 123));
1017        assertEquals((float) 123, bean.getFloatProperty(), 0.005);
1018        BeanUtils.copyProperty(bean, "floatProperty", new Integer JavaDoc((int) 123));
1019        assertEquals((float) 123, bean.getFloatProperty(), 0.005);
1020        BeanUtils.copyProperty(bean, "floatProperty", new Long JavaDoc((long) 123));
1021        assertEquals((float) 123, bean.getFloatProperty(), 0.005);
1022        BeanUtils.copyProperty(bean, "floatProperty", new Short JavaDoc((short) 123));
1023        assertEquals((float) 123, bean.getFloatProperty(), 0.005);
1024
1025    }
1026
1027
1028    /**
1029     * Test narrowing and widening conversions on int.
1030     */

1031    public void testCopyPropertyInteger() throws Exception JavaDoc {
1032
1033        BeanUtils.copyProperty(bean, "longProperty", new Byte JavaDoc((byte) 123));
1034        assertEquals((int) 123, bean.getIntProperty());
1035        BeanUtils.copyProperty(bean, "longProperty", new Double JavaDoc((double) 123));
1036        assertEquals((int) 123, bean.getIntProperty());
1037        BeanUtils.copyProperty(bean, "longProperty", new Float JavaDoc((float) 123));
1038        assertEquals((int) 123, bean.getIntProperty());
1039        BeanUtils.copyProperty(bean, "longProperty", new Integer JavaDoc((int) 123));
1040        assertEquals((int) 123, bean.getIntProperty());
1041        BeanUtils.copyProperty(bean, "longProperty", new Long JavaDoc((long) 123));
1042        assertEquals((int) 123, bean.getIntProperty());
1043        BeanUtils.copyProperty(bean, "longProperty", new Short JavaDoc((short) 123));
1044        assertEquals((int) 123, bean.getIntProperty());
1045
1046    }
1047
1048
1049    /**
1050     * Test narrowing and widening conversions on long.
1051     */

1052    public void testCopyPropertyLong() throws Exception JavaDoc {
1053
1054        BeanUtils.copyProperty(bean, "longProperty", new Byte JavaDoc((byte) 123));
1055        assertEquals((long) 123, bean.getLongProperty());
1056        BeanUtils.copyProperty(bean, "longProperty", new Double JavaDoc((double) 123));
1057        assertEquals((long) 123, bean.getLongProperty());
1058        BeanUtils.copyProperty(bean, "longProperty", new Float JavaDoc((float) 123));
1059        assertEquals((long) 123, bean.getLongProperty());
1060        BeanUtils.copyProperty(bean, "longProperty", new Integer JavaDoc((int) 123));
1061        assertEquals((long) 123, bean.getLongProperty());
1062        BeanUtils.copyProperty(bean, "longProperty", new Long JavaDoc((long) 123));
1063        assertEquals((long) 123, bean.getLongProperty());
1064        BeanUtils.copyProperty(bean, "longProperty", new Short JavaDoc((short) 123));
1065        assertEquals((long) 123, bean.getLongProperty());
1066
1067    }
1068
1069
1070    /**
1071     * Test narrowing and widening conversions on short.
1072     */

1073    public void testCopyPropertyShort() throws Exception JavaDoc {
1074
1075        BeanUtils.copyProperty(bean, "shortProperty", new Byte JavaDoc((byte) 123));
1076        assertEquals((short) 123, bean.getShortProperty());
1077        BeanUtils.copyProperty(bean, "shortProperty", new Double JavaDoc((double) 123));
1078        assertEquals((short) 123, bean.getShortProperty());
1079        BeanUtils.copyProperty(bean, "shortProperty", new Float JavaDoc((float) 123));
1080        assertEquals((short) 123, bean.getShortProperty());
1081        BeanUtils.copyProperty(bean, "shortProperty", new Integer JavaDoc((int) 123));
1082        assertEquals((short) 123, bean.getShortProperty());
1083        BeanUtils.copyProperty(bean, "shortProperty", new Long JavaDoc((long) 123));
1084        assertEquals((short) 123, bean.getShortProperty());
1085        BeanUtils.copyProperty(bean, "shortProperty", new Short JavaDoc((short) 123));
1086        assertEquals((short) 123, bean.getShortProperty());
1087
1088    }
1089
1090
1091    /**
1092     * Test copying a property using a nested indexed array expression,
1093     * with and without conversions.
1094     */

1095    public void testCopyPropertyNestedIndexedArray() throws Exception JavaDoc {
1096
1097        int origArray[] = { 0, 10, 20, 30, 40 };
1098        int intArray[] = { 0, 0, 0 };
1099        bean.getNested().setIntArray(intArray);
1100        int intChanged[] = { 0, 0, 0 };
1101
1102        // No conversion required
1103
BeanUtils.copyProperty(bean, "nested.intArray[1]", new Integer JavaDoc(1));
1104        checkIntArray(bean.getIntArray(), origArray);
1105        intChanged[1] = 1;
1106        checkIntArray(bean.getNested().getIntArray(), intChanged);
1107
1108        // Widening conversion required
1109
BeanUtils.copyProperty(bean, "nested.intArray[1]", new Byte JavaDoc((byte) 2));
1110        checkIntArray(bean.getIntArray(), origArray);
1111        intChanged[1] = 2;
1112        checkIntArray(bean.getNested().getIntArray(), intChanged);
1113
1114        // Narrowing conversion required
1115
BeanUtils.copyProperty(bean, "nested.intArray[1]", new Long JavaDoc((long) 3));
1116        checkIntArray(bean.getIntArray(), origArray);
1117        intChanged[1] = 3;
1118        checkIntArray(bean.getNested().getIntArray(), intChanged);
1119
1120        // String conversion required
1121
BeanUtils.copyProperty(bean, "nested.intArray[1]", "4");
1122        checkIntArray(bean.getIntArray(), origArray);
1123        intChanged[1] = 4;
1124        checkIntArray(bean.getNested().getIntArray(), intChanged);
1125
1126    }
1127
1128
1129    /**
1130     * Test copying a property using a nested mapped map property.
1131     */

1132    public void testCopyPropertyNestedMappedMap() throws Exception JavaDoc {
1133
1134        Map JavaDoc origMap = new HashMap JavaDoc();
1135        origMap.put("First Key", "First Value");
1136        origMap.put("Second Key", "Second Value");
1137        Map JavaDoc changedMap = new HashMap JavaDoc();
1138        changedMap.put("First Key", "First Value");
1139        changedMap.put("Second Key", "Second Value");
1140
1141        // No conversion required
1142
BeanUtils.copyProperty(bean, "nested.mapProperty(Second Key)",
1143                               "New Second Value");
1144        checkMap(bean.getMapProperty(), origMap);
1145        changedMap.put("Second Key", "New Second Value");
1146        checkMap(bean.getNested().getMapProperty(), changedMap);
1147
1148    }
1149
1150
1151    /**
1152     * Test copying a property using a nested simple expression, with and
1153     * without conversions.
1154     */

1155    public void testCopyPropertyNestedSimple() throws Exception JavaDoc {
1156
1157        bean.setIntProperty(0);
1158        bean.getNested().setIntProperty(0);
1159
1160        // No conversion required
1161
BeanUtils.copyProperty(bean, "nested.intProperty", new Integer JavaDoc(1));
1162        assertNotNull(bean.getNested());
1163        assertEquals(0, bean.getIntProperty());
1164        assertEquals(1, bean.getNested().getIntProperty());
1165
1166        // Widening conversion required
1167
BeanUtils.copyProperty(bean, "nested.intProperty", new Byte JavaDoc((byte) 2));
1168        assertNotNull(bean.getNested());
1169        assertEquals(0, bean.getIntProperty());
1170        assertEquals(2, bean.getNested().getIntProperty());
1171
1172        // Narrowing conversion required
1173
BeanUtils.copyProperty(bean, "nested.intProperty", new Long JavaDoc((long) 3));
1174        assertNotNull(bean.getNested());
1175        assertEquals(0, bean.getIntProperty());
1176        assertEquals(3, bean.getNested().getIntProperty());
1177
1178        // String conversion required
1179
BeanUtils.copyProperty(bean, "nested.intProperty", "4");
1180        assertNotNull(bean.getNested());
1181        assertEquals(0, bean.getIntProperty());
1182        assertEquals(4, bean.getNested().getIntProperty());
1183
1184    }
1185
1186
1187    /**
1188     * Test copying a null property value.
1189     */

1190    public void testCopyPropertyNull() throws Exception JavaDoc {
1191
1192        bean.setNullProperty("non-null value");
1193        BeanUtils.copyProperty(bean, "nullProperty", null);
1194        assertNull("nullProperty is null", bean.getNullProperty());
1195
1196    }
1197
1198
1199    /**
1200     * Test copying a new value to a write-only property, with and without
1201     * conversions.
1202     */

1203    public void testCopyPropertyWriteOnly() throws Exception JavaDoc {
1204
1205        bean.setWriteOnlyProperty("Original value");
1206
1207        // No conversion required
1208
BeanUtils.copyProperty(bean, "writeOnlyProperty", "New value");
1209        assertEquals("New value", bean.getWriteOnlyPropertyValue());
1210
1211        // Integer->String conversion required
1212
BeanUtils.copyProperty(bean, "writeOnlyProperty", new Integer JavaDoc(123));
1213        assertEquals("123", bean.getWriteOnlyPropertyValue());
1214
1215    }
1216
1217
1218    /**
1219     * Test setting a new value to a write-only property, with and without
1220     * conversions.
1221     */

1222    public void testSetPropertyWriteOnly() throws Exception JavaDoc {
1223
1224        bean.setWriteOnlyProperty("Original value");
1225
1226        // No conversion required
1227
BeanUtils.setProperty(bean, "writeOnlyProperty", "New value");
1228        assertEquals("New value", bean.getWriteOnlyPropertyValue());
1229
1230        // Integer->String conversion required
1231
BeanUtils.setProperty(bean, "writeOnlyProperty", new Integer JavaDoc(123));
1232        assertEquals("123", bean.getWriteOnlyPropertyValue());
1233
1234    }
1235
1236    /** Tests that separate instances can register separate instances */
1237    public void testSeparateInstances() throws Exception JavaDoc {
1238        BeanUtilsBean utilsOne = new BeanUtilsBean(
1239                                                new ConvertUtilsBean(),
1240                                                new PropertyUtilsBean());
1241        BeanUtilsBean utilsTwo = new BeanUtilsBean(
1242                                                new ConvertUtilsBean(),
1243                                                new PropertyUtilsBean());
1244        
1245        
1246        TestBean bean = new TestBean();
1247        
1248        // Make sure what we're testing works
1249
bean.setBooleanProperty(false);
1250        utilsOne.setProperty(bean, "booleanProperty", "true");
1251        assertEquals("Set property failed (1)", bean.getBooleanProperty(), true);
1252        
1253        bean.setBooleanProperty(false);
1254        utilsTwo.setProperty(bean, "booleanProperty", "true");
1255        assertEquals("Set property failed (2)", bean.getBooleanProperty(), true);
1256        
1257        // now change the registered conversion
1258

1259        utilsOne.getConvertUtils().register(new ThrowExceptionConverter(), Boolean.TYPE);
1260        try {
1261            
1262            bean.setBooleanProperty(false);
1263            utilsOne.setProperty(bean, "booleanProperty", "true");
1264            fail("Registered conversion not used.");
1265            
1266        } catch (PassTestException e) { /* Do nothing */ }
1267        
1268        // make sure that this conversion has no been registered in the other instance
1269
try {
1270        
1271            bean.setBooleanProperty(false);
1272            utilsTwo.setProperty(bean, "booleanProperty", "true");
1273            assertEquals("Set property failed (3)", bean.getBooleanProperty(), true);
1274            
1275        } catch (PassTestException e) {
1276            fail("Registed converter is used by other instances");
1277        }
1278    }
1279
1280    public void testArrayPropertyConversion() throws Exception JavaDoc {
1281        BeanUtilsBean beanUtils = new BeanUtilsBean(
1282                                                    new ConvertUtilsBean(),
1283                                                    new PropertyUtilsBean());
1284        beanUtils.getConvertUtils().register(
1285            new Converter () {
1286                public Object JavaDoc convert(Class JavaDoc type, Object JavaDoc value) {
1287                    return "Spam, spam, spam, spam!";
1288                }
1289            },
1290            String JavaDoc.class);
1291            
1292        TestBean bean = new TestBean();
1293        String JavaDoc [] results = beanUtils.getArrayProperty(bean, "intArray");
1294                
1295        int[] values = bean.getIntArray();
1296        assertEquals(
1297                    "Converted array size not equal to property array size.",
1298                    results.length,
1299                    values.length);
1300        for (int i=0, size=values.length ; i<size; i++) {
1301            assertEquals(
1302                    "Value " + i + " incorrectly converted ",
1303                    "Spam, spam, spam, spam!",
1304                    results[i]);
1305        }
1306    }
1307
1308    // Ensure that the actual int[] matches the expected int[]
1309
protected void checkIntArray(int actual[], int expected[]) {
1310        assertNotNull("actual array not null", actual);
1311        assertEquals("actual array length", expected.length, actual.length);
1312        for (int i = 0; i < actual.length; i++) {
1313            assertEquals("actual array value[" + i + "]",
1314                         expected[i], actual[i]);
1315        }
1316    }
1317
1318
1319    // Ensure that the actual Map matches the expected Map
1320
protected void checkMap(Map JavaDoc actual, Map JavaDoc expected) {
1321        assertNotNull("actual map not null", actual);
1322        assertEquals("actual map size", expected.size(), actual.size());
1323        Iterator JavaDoc keys = expected.keySet().iterator();
1324        while (keys.hasNext()) {
1325            Object JavaDoc key = keys.next();
1326            assertEquals("actual map value(" + key + ")",
1327                         expected.get(key), actual.get(key));
1328        }
1329    }
1330
1331    public void testMappedProperty() throws Exception JavaDoc {
1332        MappedPropertyTestBean bean = new MappedPropertyTestBean();
1333        
1334        BeanUtils.setProperty(bean, "mapproperty(this.that.the-other)", "some.dotty.value");
1335        
1336        assertEquals(
1337                        "Mapped property set correctly",
1338                        "some.dotty.value",
1339                        bean.getMapproperty("this.that.the-other"));
1340    }
1341}
1342
Popular Tags