1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.beanutils;
18
19
20 import java.lang.reflect.InvocationTargetException;
21 import java.util.HashMap;
22 import java.util.Iterator;
23 import java.util.Map;
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
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 describes[] =
73 { "booleanProperty",
74 "booleanSecond",
75 "byteProperty",
76 "doubleProperty",
77 "dupProperty",
78 "floatProperty",
79 "intArray",
80
81 "longProperty",
82 "listIndexed",
83 "longProperty",
84
85
86 "nested",
87 "nullProperty",
88 "readOnlyProperty",
89 "shortProperty",
90 "stringArray",
91
92 "stringProperty"
93 };
94
95
96
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 name) {
104 super(name);
105 }
106
107
108
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
135
136
137 /**
138 * Test the copyProperties() method from a DynaBean.
139 */
140 public void testCopyPropertiesDynaBean() {
141
142
143 DynaClass dynaClass = DynaBeanUtilsTestCase.createDynaClass();
144 DynaBean orig = null;
145 try {
146 orig = dynaClass.newInstance();
147 } catch (Exception e) {
148 fail("newInstance(): " + e);
149 }
150 orig.set("booleanProperty", Boolean.FALSE);
151 orig.set("byteProperty", new Byte((byte) 111));
152 orig.set("doubleProperty", new Double(333.33));
153 orig.set("dupProperty",
154 new String[] { "New 0", "New 1", "New 2" });
155 orig.set("intArray", new int[] { 100, 200, 300 });
156 orig.set("intProperty", new Integer(333));
157 orig.set("longProperty", new Long(3333));
158 orig.set("shortProperty", new Short((short) 33));
159 orig.set("stringArray", new String[] { "New 0", "New 1" });
160 orig.set("stringProperty", "Custom string");
161
162
163 try {
164 BeanUtils.copyProperties(bean, orig);
165 } catch (Exception e) {
166 fail("Threw exception: " + e);
167 }
168
169
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
194 String 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 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 map = new HashMap();
221 map.put("booleanProperty", "false");
222 map.put("byteProperty", "111");
223 map.put("doubleProperty", "333.0");
224 map.put("dupProperty", new String[] { "New 0", "New 1", "New 2" });
225 map.put("floatProperty", "222.0");
226 map.put("intArray", new String[] { "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 t) {
235 fail("Threw " + t.toString());
236 }
237
238
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
257 String 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
279 TestBean orig = new TestBean();
280 orig.setBooleanProperty(false);
281 orig.setByteProperty((byte) 111);
282 orig.setDoubleProperty(333.33);
283 orig.setDupProperty(new String[] { "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[] { "New 0", "New 1" });
289 orig.setStringProperty("Custom string");
290
291
292 try {
293 BeanUtils.copyProperties(bean, orig);
294 } catch (Exception e) {
295 fail("Threw exception: " + e);
296 }
297
298
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
323 String 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 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 map = null;
350 try {
351 map = BeanUtils.describe(bean);
352 } catch (Exception e) {
353 fail("Threw exception " + e);
354 }
355
356
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
365 assertEquals("Value of 'booleanProperty'",
366 "true",
367 (String) map.get("booleanProperty"));
368 assertEquals("Value of 'byteProperty'",
369 "121",
370 (String) map.get("byteProperty"));
371 assertEquals("Value of 'doubleProperty'",
372 "321.0",
373 (String) map.get("doubleProperty"));
374 assertEquals("Value of 'floatProperty'",
375 "123.0",
376 (String) map.get("floatProperty"));
377 assertEquals("Value of 'intProperty'",
378 "123",
379 (String) map.get("intProperty"));
380 assertEquals("Value of 'longProperty'",
381 "321",
382 (String) map.get("longProperty"));
383 assertEquals("Value of 'shortProperty'",
384 "987",
385 (String) map.get("shortProperty"));
386 assertEquals("Value of 'stringProperty'",
387 "This is a string",
388 (String) 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 arr[] = BeanUtils.getArrayProperty(bean, "stringArray");
399 String 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 e) {
410 fail("IllegalAccessException");
411 } catch (InvocationTargetException e) {
412 fail("InvocationTargetException");
413 } catch (NoSuchMethodException 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 val = BeanUtils.getIndexedProperty(bean, "intIndexed[3]");
426 String 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 e) {
433 fail("IllegalAccessException");
434 } catch (InvocationTargetException e) {
435 fail("InvocationTargetException");
436 } catch (NoSuchMethodException e) {
437 fail("NoSuchMethodException");
438 }
439 }
440
441
442 /**
443 * tests getting an indexed property
444 */
445 public void testGetIndexedProperty2() {
446 try {
447 String val = BeanUtils.getIndexedProperty(bean, "intIndexed", 3);
448 String 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 e) {
458 fail("IllegalAccessException");
459 } catch (InvocationTargetException e) {
460 fail("InvocationTargetException");
461 } catch (NoSuchMethodException e) {
462 fail("NoSuchMethodException");
463 }
464 }
465
466
467 /**
468 * tests getting a nested property
469 */
470 public void testGetNestedProperty() {
471 try {
472 String val = BeanUtils.getNestedProperty(bean, "nested.stringProperty");
473 String comp = bean.getNested().getStringProperty();
474 assertTrue("nested.StringProperty == " + comp,
475 val.equals(comp));
476 } catch (IllegalAccessException e) {
477 fail("IllegalAccessException");
478 } catch (InvocationTargetException e) {
479 fail("InvocationTargetException");
480 } catch (NoSuchMethodException e) {
481 fail("NoSuchMethodException");
482 }
483 }
484
485
486 /**
487 * tests getting a 'whatever' property
488 */
489 public void testGetGeneralProperty() {
490 try {
491 String val = BeanUtils.getProperty(bean, "nested.intIndexed[2]");
492 String comp = String.valueOf(bean.getIntIndexed(2));
493
494 assertTrue("nested.intIndexed[2] == " + comp,
495 val.equals(comp));
496 } catch (IllegalAccessException e) {
497 fail("IllegalAccessException");
498 } catch (InvocationTargetException e) {
499 fail("InvocationTargetException");
500 } catch (NoSuchMethodException e) {
501 fail("NoSuchMethodException");
502 }
503 }
504
505
506 /**
507 * tests getting a 'whatever' property
508 */
509 public void testGetSimpleProperty() {
510 try {
511 String val = BeanUtils.getSimpleProperty(bean, "shortProperty");
512 String comp = String.valueOf(bean.getShortProperty());
513
514 assertTrue("shortProperty == " + comp,
515 val.equals(comp));
516 } catch (IllegalAccessException e) {
517 fail("IllegalAccessException");
518 } catch (InvocationTargetException e) {
519 fail("InvocationTargetException");
520 } catch (NoSuchMethodException 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 map = new HashMap();
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 e) {
569 fail("IllegalAccessException");
570 } catch (InvocationTargetException 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 map = new HashMap();
585 int intArray[] = new int[] { 123, 456, 789 };
586 map.put("intArray", intArray);
587 String stringArray[] = new String[]
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 e) {
607 fail("IllegalAccessException");
608 } catch (InvocationTargetException 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 map = new HashMap();
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 e) {
641 fail("IllegalAccessException");
642 } catch (InvocationTargetException 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 map = new HashMap();
657 map.put("nested.booleanProperty", "false");
658
659 map.put("nested.doubleProperty", "432.0");
660
661 map.put("nested.intProperty", "543");
662
663 map.put("nested.shortProperty", "654");
664
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 e) {
695 fail("IllegalAccessException");
696 } catch (InvocationTargetException 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 map = new HashMap();
713 map.put("booleanProperty", "false");
714
715 map.put("byteProperty", "111");
716 map.put("doubleProperty", "432.0");
717
718 map.put("intProperty", "543");
719 map.put("longProperty", "");
720 map.put("nullProperty", null);
721 map.put("shortProperty", "654");
722
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 e) {
756 fail("IllegalAccessException");
757 } catch (InvocationTargetException 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 {
768
769 Object oldValue = null;
770 Object newValue = null;
771
772
773 oldValue = PropertyUtils.getSimpleProperty(bean, "stringArray");
774 BeanUtils.setProperty(bean, "stringArray", (String) null);
775 newValue = PropertyUtils.getSimpleProperty(bean, "stringArray");
776 assertNotNull("stringArray is not null", newValue);
777 assertTrue("stringArray of correct type",
778 newValue instanceof String[]);
779 assertEquals("stringArray length",
780 1, ((String[]) newValue).length);
781 assertTrue("stringArray[0] is null",
782 ((String[]) newValue)[0] == null);
783 PropertyUtils.setProperty(bean, "stringArray", oldValue);
784
785
786 oldValue = PropertyUtils.getSimpleProperty(bean, "stringArray");
787 BeanUtils.setProperty(bean, "stringArray[2]", (String) null);
788 newValue = PropertyUtils.getSimpleProperty(bean, "stringArray");
789 assertNotNull("stringArray is not null", newValue);
790 assertTrue("stringArray of correct type",
791 newValue instanceof String[]);
792 assertEquals("stringArray length",
793 5, ((String[]) newValue).length);
794 assertTrue("stringArray[2] is null",
795 ((String[]) newValue)[2] == null);
796 PropertyUtils.setProperty(bean, "stringArray", oldValue);
797
798
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 {
810
811 BeanUtils.setProperty(bean,"intProperty", new Integer(1));
812 assertEquals(1,bean.getIntProperty());
813 BeanUtils.setProperty(bean,"stringProperty", new Integer(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 {
823
824 BeanUtils.setProperty(bean, "byteProperty", new Byte((byte) 123));
825 assertEquals((byte) 123, bean.getByteProperty());
826
827
828
829
830
831
832 BeanUtils.setProperty(bean, "byteProperty", new Integer((int) 123));
833 assertEquals((byte) 123, bean.getByteProperty());
834 BeanUtils.setProperty(bean, "byteProperty", new Long((long) 123));
835 assertEquals((byte) 123, bean.getByteProperty());
836 BeanUtils.setProperty(bean, "byteProperty", new Short((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 {
846
847 BeanUtils.setProperty(bean, "doubleProperty", new Byte((byte) 123));
848 assertEquals((double) 123, bean.getDoubleProperty(), 0.005);
849 BeanUtils.setProperty(bean, "doubleProperty", new Double((double) 123));
850 assertEquals((double) 123, bean.getDoubleProperty(), 0.005);
851 BeanUtils.setProperty(bean, "doubleProperty", new Float((float) 123));
852 assertEquals((double) 123, bean.getDoubleProperty(), 0.005);
853 BeanUtils.setProperty(bean, "doubleProperty", new Integer((int) 123));
854 assertEquals((double) 123, bean.getDoubleProperty(), 0.005);
855 BeanUtils.setProperty(bean, "doubleProperty", new Long((long) 123));
856 assertEquals((double) 123, bean.getDoubleProperty(), 0.005);
857 BeanUtils.setProperty(bean, "doubleProperty", new Short((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 {
867
868 BeanUtils.setProperty(bean, "floatProperty", new Byte((byte) 123));
869 assertEquals((float) 123, bean.getFloatProperty(), 0.005);
870 BeanUtils.setProperty(bean, "floatProperty", new Double((double) 123));
871 assertEquals((float) 123, bean.getFloatProperty(), 0.005);
872 BeanUtils.setProperty(bean, "floatProperty", new Float((float) 123));
873 assertEquals((float) 123, bean.getFloatProperty(), 0.005);
874 BeanUtils.setProperty(bean, "floatProperty", new Integer((int) 123));
875 assertEquals((float) 123, bean.getFloatProperty(), 0.005);
876 BeanUtils.setProperty(bean, "floatProperty", new Long((long) 123));
877 assertEquals((float) 123, bean.getFloatProperty(), 0.005);
878 BeanUtils.setProperty(bean, "floatProperty", new Short((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 {
888
889 BeanUtils.setProperty(bean, "longProperty", new Byte((byte) 123));
890 assertEquals((int) 123, bean.getIntProperty());
891
892
893
894
895
896
897 BeanUtils.setProperty(bean, "longProperty", new Integer((int) 123));
898 assertEquals((int) 123, bean.getIntProperty());
899 BeanUtils.setProperty(bean, "longProperty", new Long((long) 123));
900 assertEquals((int) 123, bean.getIntProperty());
901 BeanUtils.setProperty(bean, "longProperty", new Short((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 {
911
912 BeanUtils.setProperty(bean, "longProperty", new Byte((byte) 123));
913 assertEquals((long) 123, bean.getLongProperty());
914
915
916
917
918
919
920 BeanUtils.setProperty(bean, "longProperty", new Integer((int) 123));
921 assertEquals((long) 123, bean.getLongProperty());
922 BeanUtils.setProperty(bean, "longProperty", new Long((long) 123));
923 assertEquals((long) 123, bean.getLongProperty());
924 BeanUtils.setProperty(bean, "longProperty", new Short((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 {
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 {
946
947 BeanUtils.setProperty(bean, "shortProperty", new Byte((byte) 123));
948 assertEquals((short) 123, bean.getShortProperty());
949
950
951
952
953
954
955 BeanUtils.setProperty(bean, "shortProperty", new Integer((int) 123));
956 assertEquals((short) 123, bean.getShortProperty());
957 BeanUtils.setProperty(bean, "shortProperty", new Long((long) 123));
958 assertEquals((short) 123, bean.getShortProperty());
959 BeanUtils.setProperty(bean, "shortProperty", new Short((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 {
969
970 BeanUtils.copyProperty(bean, "byteProperty", new Byte((byte) 123));
971 assertEquals((byte) 123, bean.getByteProperty());
972 BeanUtils.copyProperty(bean, "byteProperty", new Double((double) 123));
973 assertEquals((byte) 123, bean.getByteProperty());
974 BeanUtils.copyProperty(bean, "byteProperty", new Float((float) 123));
975 assertEquals((byte) 123, bean.getByteProperty());
976 BeanUtils.copyProperty(bean, "byteProperty", new Integer((int) 123));
977 assertEquals((byte) 123, bean.getByteProperty());
978 BeanUtils.copyProperty(bean, "byteProperty", new Long((long) 123));
979 assertEquals((byte) 123, bean.getByteProperty());
980 BeanUtils.copyProperty(bean, "byteProperty", new Short((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 {
990
991 BeanUtils.copyProperty(bean, "doubleProperty", new Byte((byte) 123));
992 assertEquals((double) 123, bean.getDoubleProperty(), 0.005);
993 BeanUtils.copyProperty(bean, "doubleProperty", new Double((double) 123));
994 assertEquals((double) 123, bean.getDoubleProperty(), 0.005);
995 BeanUtils.copyProperty(bean, "doubleProperty", new Float((float) 123));
996 assertEquals((double) 123, bean.getDoubleProperty(), 0.005);
997 BeanUtils.copyProperty(bean, "doubleProperty", new Integer((int) 123));
998 assertEquals((double) 123, bean.getDoubleProperty(), 0.005);
999 BeanUtils.copyProperty(bean, "doubleProperty", new Long((long) 123));
1000 assertEquals((double) 123, bean.getDoubleProperty(), 0.005);
1001 BeanUtils.copyProperty(bean, "doubleProperty", new Short((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 {
1011
1012 BeanUtils.copyProperty(bean, "floatProperty", new Byte((byte) 123));
1013 assertEquals((float) 123, bean.getFloatProperty(), 0.005);
1014 BeanUtils.copyProperty(bean, "floatProperty", new Double((double) 123));
1015 assertEquals((float) 123, bean.getFloatProperty(), 0.005);
1016 BeanUtils.copyProperty(bean, "floatProperty", new Float((float) 123));
1017 assertEquals((float) 123, bean.getFloatProperty(), 0.005);
1018 BeanUtils.copyProperty(bean, "floatProperty", new Integer((int) 123));
1019 assertEquals((float) 123, bean.getFloatProperty(), 0.005);
1020 BeanUtils.copyProperty(bean, "floatProperty", new Long((long) 123));
1021 assertEquals((float) 123, bean.getFloatProperty(), 0.005);
1022 BeanUtils.copyProperty(bean, "floatProperty", new Short((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 {
1032
1033 BeanUtils.copyProperty(bean, "longProperty", new Byte((byte) 123));
1034 assertEquals((int) 123, bean.getIntProperty());
1035 BeanUtils.copyProperty(bean, "longProperty", new Double((double) 123));
1036 assertEquals((int) 123, bean.getIntProperty());
1037 BeanUtils.copyProperty(bean, "longProperty", new Float((float) 123));
1038 assertEquals((int) 123, bean.getIntProperty());
1039 BeanUtils.copyProperty(bean, "longProperty", new Integer((int) 123));
1040 assertEquals((int) 123, bean.getIntProperty());
1041 BeanUtils.copyProperty(bean, "longProperty", new Long((long) 123));
1042 assertEquals((int) 123, bean.getIntProperty());
1043 BeanUtils.copyProperty(bean, "longProperty", new Short((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 {
1053
1054 BeanUtils.copyProperty(bean, "longProperty", new Byte((byte) 123));
1055 assertEquals((long) 123, bean.getLongProperty());
1056 BeanUtils.copyProperty(bean, "longProperty", new Double((double) 123));
1057 assertEquals((long) 123, bean.getLongProperty());
1058 BeanUtils.copyProperty(bean, "longProperty", new Float((float) 123));
1059 assertEquals((long) 123, bean.getLongProperty());
1060 BeanUtils.copyProperty(bean, "longProperty", new Integer((int) 123));
1061 assertEquals((long) 123, bean.getLongProperty());
1062 BeanUtils.copyProperty(bean, "longProperty", new Long((long) 123));
1063 assertEquals((long) 123, bean.getLongProperty());
1064 BeanUtils.copyProperty(bean, "longProperty", new Short((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 {
1074
1075 BeanUtils.copyProperty(bean, "shortProperty", new Byte((byte) 123));
1076 assertEquals((short) 123, bean.getShortProperty());
1077 BeanUtils.copyProperty(bean, "shortProperty", new Double((double) 123));
1078 assertEquals((short) 123, bean.getShortProperty());
1079 BeanUtils.copyProperty(bean, "shortProperty", new Float((float) 123));
1080 assertEquals((short) 123, bean.getShortProperty());
1081 BeanUtils.copyProperty(bean, "shortProperty", new Integer((int) 123));
1082 assertEquals((short) 123, bean.getShortProperty());
1083 BeanUtils.copyProperty(bean, "shortProperty", new Long((long) 123));
1084 assertEquals((short) 123, bean.getShortProperty());
1085 BeanUtils.copyProperty(bean, "shortProperty", new Short((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 {
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
1103 BeanUtils.copyProperty(bean, "nested.intArray[1]", new Integer(1));
1104 checkIntArray(bean.getIntArray(), origArray);
1105 intChanged[1] = 1;
1106 checkIntArray(bean.getNested().getIntArray(), intChanged);
1107
1108
1109 BeanUtils.copyProperty(bean, "nested.intArray[1]", new Byte((byte) 2));
1110 checkIntArray(bean.getIntArray(), origArray);
1111 intChanged[1] = 2;
1112 checkIntArray(bean.getNested().getIntArray(), intChanged);
1113
1114
1115 BeanUtils.copyProperty(bean, "nested.intArray[1]", new Long((long) 3));
1116 checkIntArray(bean.getIntArray(), origArray);
1117 intChanged[1] = 3;
1118 checkIntArray(bean.getNested().getIntArray(), intChanged);
1119
1120
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 {
1133
1134 Map origMap = new HashMap();
1135 origMap.put("First Key", "First Value");
1136 origMap.put("Second Key", "Second Value");
1137 Map changedMap = new HashMap();
1138 changedMap.put("First Key", "First Value");
1139 changedMap.put("Second Key", "Second Value");
1140
1141
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 {
1156
1157 bean.setIntProperty(0);
1158 bean.getNested().setIntProperty(0);
1159
1160
1161 BeanUtils.copyProperty(bean, "nested.intProperty", new Integer(1));
1162 assertNotNull(bean.getNested());
1163 assertEquals(0, bean.getIntProperty());
1164 assertEquals(1, bean.getNested().getIntProperty());
1165
1166
1167 BeanUtils.copyProperty(bean, "nested.intProperty", new Byte((byte) 2));
1168 assertNotNull(bean.getNested());
1169 assertEquals(0, bean.getIntProperty());
1170 assertEquals(2, bean.getNested().getIntProperty());
1171
1172
1173 BeanUtils.copyProperty(bean, "nested.intProperty", new Long((long) 3));
1174 assertNotNull(bean.getNested());
1175 assertEquals(0, bean.getIntProperty());
1176 assertEquals(3, bean.getNested().getIntProperty());
1177
1178
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 {
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 {
1204
1205 bean.setWriteOnlyProperty("Original value");
1206
1207
1208 BeanUtils.copyProperty(bean, "writeOnlyProperty", "New value");
1209 assertEquals("New value", bean.getWriteOnlyPropertyValue());
1210
1211
1212 BeanUtils.copyProperty(bean, "writeOnlyProperty", new Integer(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 {
1223
1224 bean.setWriteOnlyProperty("Original value");
1225
1226
1227 BeanUtils.setProperty(bean, "writeOnlyProperty", "New value");
1228 assertEquals("New value", bean.getWriteOnlyPropertyValue());
1229
1230
1231 BeanUtils.setProperty(bean, "writeOnlyProperty", new Integer(123));
1232 assertEquals("123", bean.getWriteOnlyPropertyValue());
1233
1234 }
1235
1236 /** Tests that separate instances can register separate instances */
1237 public void testSeparateInstances() throws Exception {
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
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
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) {
1267
1268
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 {
1281 BeanUtilsBean beanUtils = new BeanUtilsBean(
1282 new ConvertUtilsBean(),
1283 new PropertyUtilsBean());
1284 beanUtils.getConvertUtils().register(
1285 new Converter () {
1286 public Object convert(Class type, Object value) {
1287 return "Spam, spam, spam, spam!";
1288 }
1289 },
1290 String.class);
1291
1292 TestBean bean = new TestBean();
1293 String [] 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
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
1320 protected void checkMap(Map actual, Map expected) {
1321 assertNotNull("actual map not null", actual);
1322 assertEquals("actual map size", expected.size(), actual.size());
1323 Iterator keys = expected.keySet().iterator();
1324 while (keys.hasNext()) {
1325 Object 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 {
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 }