1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.beanutils;
19
20
21 import java.lang.reflect.InvocationTargetException;
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.Map;
27
28 import junit.framework.Test;
29 import junit.framework.TestCase;
30 import junit.framework.TestSuite;
31
32
33
34
35
36
37
38
39 public class DynaBeanUtilsTestCase extends TestCase {
40
41
42
43
44
45
46
47
48 protected DynaBean bean = null;
49
50
51
52
53
54 protected TestBean nested = null;
55
56
57
58
59
60 protected String describes[] =
61 { "booleanProperty",
62 "booleanSecond",
63 "byteProperty",
64 "doubleProperty",
65 "dupProperty",
66 "floatProperty",
67 "intArray",
68 "intIndexed",
69 "intProperty",
70 "listIndexed",
71 "longProperty",
72 "mapProperty",
73 "mappedProperty",
74 "mappedIntProperty",
75 "nested",
76 "nullProperty",
77
78 "shortProperty",
79 "stringArray",
80 "stringIndexed",
81 "stringProperty"
82 };
83
84
85
86
87
88
89
90
91
92
93 public DynaBeanUtilsTestCase(final String name) {
94
95 super(name);
96
97 }
98
99
100
101
102
103
104
105
106 @Override
107 public void setUp() throws Exception {
108
109 ConvertUtils.deregister();
110
111
112 final DynaClass dynaClass = createDynaClass();
113 bean = dynaClass.newInstance();
114
115
116 bean.set("booleanProperty", new Boolean(true));
117 bean.set("booleanSecond", new Boolean(true));
118 bean.set("byteProperty", new Byte((byte) 121));
119 bean.set("doubleProperty", new Double(321.0));
120 bean.set("floatProperty", new Float((float) 123.0));
121 final String dupProperty[] = { "Dup 0", "Dup 1", "Dup 2", "Dup 3", "Dup 4"};
122 bean.set("dupProperty", dupProperty);
123 final int intArray[] = { 0, 10, 20, 30, 40 };
124 bean.set("intArray", intArray);
125 final int intIndexed[] = { 0, 10, 20, 30, 40 };
126 bean.set("intIndexed", intIndexed);
127 bean.set("intProperty", new Integer(123));
128 final List<String> listIndexed = new ArrayList<String>();
129 listIndexed.add("String 0");
130 listIndexed.add("String 1");
131 listIndexed.add("String 2");
132 listIndexed.add("String 3");
133 listIndexed.add("String 4");
134 bean.set("listIndexed", listIndexed);
135 bean.set("longProperty", new Long(321));
136 final HashMap<String, Object> mapProperty = new HashMap<String, Object>();
137 mapProperty.put("First Key", "First Value");
138 mapProperty.put("Second Key", "Second Value");
139 bean.set("mapProperty", mapProperty);
140 final HashMap<String, Object> mappedProperty = new HashMap<String, Object>();
141 mappedProperty.put("First Key", "First Value");
142 mappedProperty.put("Second Key", "Second Value");
143 bean.set("mappedProperty", mappedProperty);
144 final HashMap<String, Integer> mappedIntProperty = new HashMap<String, Integer>();
145 mappedIntProperty.put("One", new Integer(1));
146 mappedIntProperty.put("Two", new Integer(2));
147 bean.set("mappedIntProperty", mappedIntProperty);
148 nested = new TestBean();
149 bean.set("nested", nested);
150
151 bean.set("shortProperty", new Short((short) 987));
152 final String stringArray[] =
153 { "String 0", "String 1", "String 2", "String 3", "String 4" };
154 bean.set("stringArray", stringArray);
155 final String stringIndexed[] =
156 { "String 0", "String 1", "String 2", "String 3", "String 4" };
157 bean.set("stringIndexed", stringIndexed);
158 bean.set("stringProperty", "This is a string");
159
160 }
161
162
163
164
165
166 public static Test suite() {
167
168 return (new TestSuite(DynaBeanUtilsTestCase.class));
169
170 }
171
172
173
174
175
176 @Override
177 public void tearDown() {
178
179 bean = null;
180 nested = null;
181
182 }
183
184
185
186
187
188
189
190
191 public void testCloneDynaBean() {
192
193
194 final DynaClass dynaClass = DynaBeanUtilsTestCase.createDynaClass();
195 DynaBean orig = null;
196 try {
197 orig = dynaClass.newInstance();
198 } catch (final Exception e) {
199 fail("newInstance(): " + e);
200 }
201 orig.set("booleanProperty", Boolean.FALSE);
202 orig.set("byteProperty", new Byte((byte)111));
203 orig.set("doubleProperty", new Double(333.33));
204 orig.set("dupProperty", new String[] { "New 0", "New 1", "New 2" });
205 orig.set("intArray", new int[] { 100, 200, 300 });
206 orig.set("intProperty", new Integer(333));
207 orig.set("longProperty", new Long(3333));
208 orig.set("shortProperty", new Short((short) 33));
209 orig.set("stringArray", new String[] { "New 0", "New 1" });
210 orig.set("stringProperty", "Custom string");
211
212
213 DynaBean clonedBean = null;
214 try {
215 clonedBean = (DynaBean) BeanUtils.cloneBean(orig);
216 } catch (final Exception e) {
217 fail("Threw exception: " + e);
218 }
219
220
221 assertEquals("Cloned boolean property",
222 false,
223 ((Boolean) clonedBean.get("booleanProperty")).booleanValue());
224 assertEquals("Cloned byte property",
225 (byte) 111,
226 ((Byte) clonedBean.get("byteProperty")).byteValue());
227 assertEquals("Cloned double property",
228 333.33,
229 ((Double) clonedBean.get("doubleProperty")).doubleValue(),
230 0.005);
231 assertEquals("Cloned int property",
232 333,
233 ((Integer) clonedBean.get("intProperty")).intValue());
234 assertEquals("Cloned long property",
235 3333,
236 ((Long) clonedBean.get("longProperty")).longValue());
237 assertEquals("Cloned short property",
238 (short) 33,
239 ((Short) clonedBean.get("shortProperty")).shortValue());
240 assertEquals("Cloned string property",
241 "Custom string",
242 (String) clonedBean.get("stringProperty"));
243
244
245 final String dupProperty[] = (String[]) clonedBean.get("dupProperty");
246 assertNotNull("dupProperty present", dupProperty);
247 assertEquals("dupProperty length", 3, dupProperty.length);
248 assertEquals("dupProperty[0]", "New 0", dupProperty[0]);
249 assertEquals("dupProperty[1]", "New 1", dupProperty[1]);
250 assertEquals("dupProperty[2]", "New 2", dupProperty[2]);
251 final int intArray[] = (int[]) clonedBean.get("intArray");
252 assertNotNull("intArray present", intArray);
253 assertEquals("intArray length", 3, intArray.length);
254 assertEquals("intArray[0]", 100, intArray[0]);
255 assertEquals("intArray[1]", 200, intArray[1]);
256 assertEquals("intArray[2]", 300, intArray[2]);
257 final String stringArray[] = (String[]) clonedBean.get("stringArray");
258 assertNotNull("stringArray present", stringArray);
259 assertEquals("stringArray length", 2, stringArray.length);
260 assertEquals("stringArray[0]", "New 0", stringArray[0]);
261 assertEquals("stringArray[1]", "New 1", stringArray[1]);
262
263 }
264
265
266
267
268 public void testCopyPropertiesDynaBean() {
269
270
271 final DynaClass dynaClass = DynaBeanUtilsTestCase.createDynaClass();
272 DynaBean orig = null;
273 try {
274 orig = dynaClass.newInstance();
275 } catch (final Exception e) {
276 fail("newInstance(): " + e);
277 }
278 orig.set("booleanProperty", Boolean.FALSE);
279 orig.set("byteProperty", new Byte((byte)111));
280 orig.set("doubleProperty", new Double(333.33));
281 orig.set("dupProperty", new String[] { "New 0", "New 1", "New 2" });
282 orig.set("intArray", new int[] { 100, 200, 300 });
283 orig.set("intProperty", new Integer(333));
284 orig.set("longProperty", new Long(3333));
285 orig.set("shortProperty", new Short((short) 33));
286 orig.set("stringArray", new String[] { "New 0", "New 1" });
287 orig.set("stringProperty", "Custom string");
288
289
290 try {
291 BeanUtils.copyProperties(bean, orig);
292 } catch (final Exception e) {
293 fail("Threw exception: " + e);
294 }
295
296
297 assertEquals("Copied boolean property",
298 false,
299 ((Boolean) bean.get("booleanProperty")).booleanValue());
300 assertEquals("Copied byte property",
301 (byte) 111,
302 ((Byte) bean.get("byteProperty")).byteValue());
303 assertEquals("Copied double property",
304 333.33,
305 ((Double) bean.get("doubleProperty")).doubleValue(),
306 0.005);
307 assertEquals("Copied int property",
308 333,
309 ((Integer) bean.get("intProperty")).intValue());
310 assertEquals("Copied long property",
311 3333,
312 ((Long) bean.get("longProperty")).longValue());
313 assertEquals("Copied short property",
314 (short) 33,
315 ((Short) bean.get("shortProperty")).shortValue());
316 assertEquals("Copied string property",
317 "Custom string",
318 (String) bean.get("stringProperty"));
319
320
321 final String dupProperty[] = (String[]) bean.get("dupProperty");
322 assertNotNull("dupProperty present", dupProperty);
323 assertEquals("dupProperty length", 3, dupProperty.length);
324 assertEquals("dupProperty[0]", "New 0", dupProperty[0]);
325 assertEquals("dupProperty[1]", "New 1", dupProperty[1]);
326 assertEquals("dupProperty[2]", "New 2", dupProperty[2]);
327 final int intArray[] = (int[]) bean.get("intArray");
328 assertNotNull("intArray present", intArray);
329 assertEquals("intArray length", 3, intArray.length);
330 assertEquals("intArray[0]", 100, intArray[0]);
331 assertEquals("intArray[1]", 200, intArray[1]);
332 assertEquals("intArray[2]", 300, intArray[2]);
333 final String stringArray[] = (String[]) bean.get("stringArray");
334 assertNotNull("stringArray present", stringArray);
335 assertEquals("stringArray length", 2, stringArray.length);
336 assertEquals("stringArray[0]", "New 0", stringArray[0]);
337 assertEquals("stringArray[1]", "New 1", stringArray[1]);
338
339 }
340
341
342
343
344
345 public void testCopyPropertiesMap() {
346
347 final Map<String, Object> map = new HashMap<String, Object>();
348 map.put("booleanProperty", "false");
349 map.put("byteProperty", "111");
350 map.put("doubleProperty", "333.0");
351 map.put("dupProperty", new String[] { "New 0", "New 1", "New 2" });
352 map.put("floatProperty", "222.0");
353 map.put("intArray", new String[] { "0", "100", "200" });
354 map.put("intProperty", "111");
355 map.put("longProperty", "444");
356 map.put("shortProperty", "555");
357 map.put("stringProperty", "New String Property");
358
359 try {
360 BeanUtils.copyProperties(bean, map);
361 } catch (final Throwable t) {
362 fail("Threw " + t.toString());
363 }
364
365
366 assertEquals("booleanProperty", false,
367 ((Boolean) bean.get("booleanProperty")).booleanValue());
368 assertEquals("byteProperty", (byte) 111,
369 ((Byte) bean.get("byteProperty")).byteValue());
370 assertEquals("doubleProperty", 333.0,
371 ((Double) bean.get("doubleProperty")).doubleValue(),
372 0.005);
373 assertEquals("floatProperty", (float) 222.0,
374 ((Float) bean.get("floatProperty")).floatValue(),
375 (float) 0.005);
376 assertEquals("intProperty", 111,
377 ((Integer) bean.get("intProperty")).intValue());
378 assertEquals("longProperty", 444,
379 ((Long) bean.get("longProperty")).longValue());
380 assertEquals("shortProperty", (short) 555,
381 ((Short) bean.get("shortProperty")).shortValue());
382 assertEquals("stringProperty", "New String Property",
383 (String) bean.get("stringProperty"));
384
385
386 final String dupProperty[] = (String[]) bean.get("dupProperty");
387 assertNotNull("dupProperty present", dupProperty);
388 assertEquals("dupProperty length", 3, dupProperty.length);
389 assertEquals("dupProperty[0]", "New 0", dupProperty[0]);
390 assertEquals("dupProperty[1]", "New 1", dupProperty[1]);
391 assertEquals("dupProperty[2]", "New 2", dupProperty[2]);
392 final int intArray[] = (int[]) bean.get("intArray");
393 assertNotNull("intArray present", intArray);
394 assertEquals("intArray length", 3, intArray.length);
395 assertEquals("intArray[0]", 0, intArray[0]);
396 assertEquals("intArray[1]", 100, intArray[1]);
397 assertEquals("intArray[2]", 200, intArray[2]);
398
399 }
400
401
402
403
404
405 public void testCopyPropertiesStandard() {
406
407
408 final TestBean orig = new TestBean();
409 orig.setBooleanProperty(false);
410 orig.setByteProperty((byte) 111);
411 orig.setDoubleProperty(333.33);
412 orig.setDupProperty(new String[] { "New 0", "New 1", "New 2" });
413 orig.setIntArray(new int[] { 100, 200, 300 });
414 orig.setIntProperty(333);
415 orig.setLongProperty(3333);
416 orig.setShortProperty((short) 33);
417 orig.setStringArray(new String[] { "New 0", "New 1" });
418 orig.setStringProperty("Custom string");
419
420
421 try {
422 BeanUtils.copyProperties(bean, orig);
423 } catch (final Exception e) {
424 fail("Threw exception: " + e);
425 }
426
427
428 assertEquals("Copied boolean property",
429 false,
430 ((Boolean) bean.get("booleanProperty")).booleanValue());
431 assertEquals("Copied byte property",
432 (byte) 111,
433 ((Byte) bean.get("byteProperty")).byteValue());
434 assertEquals("Copied double property",
435 333.33,
436 ((Double) bean.get("doubleProperty")).doubleValue(),
437 0.005);
438 assertEquals("Copied int property",
439 333,
440 ((Integer) bean.get("intProperty")).intValue());
441 assertEquals("Copied long property",
442 3333,
443 ((Long) bean.get("longProperty")).longValue());
444 assertEquals("Copied short property",
445 (short) 33,
446 ((Short) bean.get("shortProperty")).shortValue());
447 assertEquals("Copied string property",
448 "Custom string",
449 (String) bean.get("stringProperty"));
450
451
452 final String dupProperty[] = (String[]) bean.get("dupProperty");
453 assertNotNull("dupProperty present", dupProperty);
454 assertEquals("dupProperty length", 3, dupProperty.length);
455 assertEquals("dupProperty[0]", "New 0", dupProperty[0]);
456 assertEquals("dupProperty[1]", "New 1", dupProperty[1]);
457 assertEquals("dupProperty[2]", "New 2", dupProperty[2]);
458 final int intArray[] = (int[]) bean.get("intArray");
459 assertNotNull("intArray present", intArray);
460 assertEquals("intArray length", 3, intArray.length);
461 assertEquals("intArray[0]", 100, intArray[0]);
462 assertEquals("intArray[1]", 200, intArray[1]);
463 assertEquals("intArray[2]", 300, intArray[2]);
464 final String stringArray[] = (String[]) bean.get("stringArray");
465 assertNotNull("stringArray present", stringArray);
466 assertEquals("stringArray length", 2, stringArray.length);
467 assertEquals("stringArray[0]", "New 0", stringArray[0]);
468 assertEquals("stringArray[1]", "New 1", stringArray[1]);
469
470 }
471
472
473
474
475
476 public void testDescribe() {
477
478 Map<String, Object> map = null;
479 try {
480 map = PropertyUtils.describe(bean);
481 } catch (final Exception e) {
482 fail("Threw exception " + e);
483 }
484
485
486 for (String describe : describes) {
487 assertTrue("Property '" + describe + "' is present",
488 map.containsKey(describe));
489 }
490 assertTrue("Property 'writeOnlyProperty' is not present",
491 !map.containsKey("writeOnlyProperty"));
492
493
494 assertEquals("Value of 'booleanProperty'",
495 Boolean.TRUE,
496 map.get("booleanProperty"));
497 assertEquals("Value of 'byteProperty'",
498 new Byte((byte) 121),
499 map.get("byteProperty"));
500 assertEquals("Value of 'doubleProperty'",
501 new Double(321.0),
502 map.get("doubleProperty"));
503 assertEquals("Value of 'floatProperty'",
504 new Float((float) 123.0),
505 map.get("floatProperty"));
506 assertEquals("Value of 'intProperty'",
507 new Integer(123),
508 map.get("intProperty"));
509 assertEquals("Value of 'longProperty'",
510 new Long(321),
511 map.get("longProperty"));
512 assertEquals("Value of 'shortProperty'",
513 new Short((short) 987),
514 map.get("shortProperty"));
515 assertEquals("Value of 'stringProperty'",
516 "This is a string",
517 (String) map.get("stringProperty"));
518
519 }
520
521
522
523
524
525 public void testPopulateArrayProperties() {
526
527 try {
528
529 final HashMap<String, Object> map = new HashMap<String, Object>();
530
531 final String intArrayIn[] = new String[] { "123", "456", "789" };
532 map.put("intArray", intArrayIn);
533 String stringArray[] = new String[]
534 { "New String 0", "New String 1" };
535 map.put("stringArray", stringArray);
536
537 BeanUtils.populate(bean, map);
538
539 final int intArray[] = (int[]) bean.get("intArray");
540 assertNotNull("intArray is present", intArray);
541 assertEquals("intArray length",
542 3, intArray.length);
543 assertEquals("intArray[0]", 123, intArray[0]);
544 assertEquals("intArray[1]", 456, intArray[1]);
545 assertEquals("intArray[2]", 789, intArray[2]);
546 stringArray = (String[]) bean.get("stringArray");
547 assertNotNull("stringArray is present", stringArray);
548 assertEquals("stringArray length", 2, stringArray.length);
549 assertEquals("stringArray[0]", "New String 0", stringArray[0]);
550 assertEquals("stringArray[1]", "New String 1", stringArray[1]);
551
552 } catch (final IllegalAccessException e) {
553 fail("IllegalAccessException");
554 } catch (final InvocationTargetException e) {
555 fail("InvocationTargetException");
556 }
557
558 }
559
560
561
562
563
564 public void testGetArrayProperty() {
565 try {
566 String arr[] = BeanUtils.getArrayProperty(bean, "stringArray");
567 final String comp[] = (String[]) bean.get("stringArray");
568
569 assertTrue("String array length = " + comp.length,
570 (comp.length == arr.length));
571
572 arr = BeanUtils.getArrayProperty(bean, "intArray");
573 final int iarr[] = (int[]) bean.get("intArray");
574
575 assertTrue("String array length = " + iarr.length,
576 (iarr.length == arr.length));
577 } catch (final IllegalAccessException e) {
578 fail("IllegalAccessException");
579 } catch (final InvocationTargetException e) {
580 fail("InvocationTargetException");
581 } catch (final NoSuchMethodException e) {
582 fail("NoSuchMethodException");
583 }
584
585 }
586
587
588
589
590
591 public void testGetIndexedProperty1() {
592 try {
593 String val = BeanUtils.getIndexedProperty(bean, "intIndexed[3]");
594 String comp = String.valueOf(bean.get("intIndexed", 3));
595 assertTrue("intIndexed[3] == " + comp, val.equals(comp));
596
597 val = BeanUtils.getIndexedProperty(bean, "stringIndexed[3]");
598 comp = (String) bean.get("stringIndexed", 3);
599 assertTrue("stringIndexed[3] == " + comp, val.equals(comp));
600 } catch (final IllegalAccessException e) {
601 fail("IllegalAccessException");
602 } catch (final InvocationTargetException e) {
603 fail("InvocationTargetException");
604 } catch (final NoSuchMethodException e) {
605 fail("NoSuchMethodException");
606 }
607 }
608
609
610
611
612
613 public void testGetIndexedProperty2() {
614 try {
615 String val = BeanUtils.getIndexedProperty(bean, "intIndexed", 3);
616 String comp = String.valueOf(bean.get("intIndexed", 3));
617
618 assertTrue("intIndexed,3 == " + comp, val.equals(comp));
619
620 val = BeanUtils.getIndexedProperty(bean, "stringIndexed", 3);
621 comp = (String) bean.get("stringIndexed", 3);
622
623 assertTrue("stringIndexed,3 == " + comp, val.equals(comp));
624
625 } catch (final IllegalAccessException e) {
626 fail("IllegalAccessException");
627 } catch (final InvocationTargetException e) {
628 fail("InvocationTargetException");
629 } catch (final NoSuchMethodException e) {
630 fail("NoSuchMethodException");
631 }
632 }
633
634
635
636
637
638 public void testGetNestedProperty() {
639 try {
640 final String val = BeanUtils.getNestedProperty(bean, "nested.stringProperty");
641 final String comp = nested.getStringProperty();
642 assertTrue("nested.StringProperty == " + comp,
643 val.equals(comp));
644 } catch (final IllegalAccessException e) {
645 fail("IllegalAccessException");
646 } catch (final InvocationTargetException e) {
647 fail("InvocationTargetException");
648 } catch (final NoSuchMethodException e) {
649 fail("NoSuchMethodException");
650 }
651 }
652
653
654
655
656
657 public void testGetGeneralProperty() {
658 try {
659 final String val = BeanUtils.getProperty(bean, "nested.intIndexed[2]");
660 final String comp = String.valueOf(bean.get("intIndexed", 2));
661
662 assertTrue("nested.intIndexed[2] == " + comp,
663 val.equals(comp));
664 } catch (final IllegalAccessException e) {
665 fail("IllegalAccessException");
666 } catch (final InvocationTargetException e) {
667 fail("InvocationTargetException");
668 } catch (final NoSuchMethodException e) {
669 fail("NoSuchMethodException");
670 }
671 }
672
673
674
675
676
677 public void testGetSimpleProperty() {
678 try {
679 final String val = BeanUtils.getSimpleProperty(bean, "shortProperty");
680 final String comp = String.valueOf(bean.get("shortProperty"));
681
682 assertTrue("shortProperty == " + comp,
683 val.equals(comp));
684 } catch (final IllegalAccessException e) {
685 fail("IllegalAccessException");
686 } catch (final InvocationTargetException e) {
687 fail("InvocationTargetException");
688 } catch (final NoSuchMethodException e) {
689 fail("NoSuchMethodException");
690 }
691 }
692
693
694
695
696
697 public void testPopulateArrayElements() {
698
699 try {
700
701 final HashMap<String, Object> map = new HashMap<String, Object>();
702 map.put("intIndexed[0]", "100");
703 map.put("intIndexed[2]", "120");
704 map.put("intIndexed[4]", "140");
705
706 BeanUtils.populate(bean, map);
707 final Integer intIndexed0 = (Integer) bean.get("intIndexed", 0);
708 assertEquals("intIndexed[0] is 100",
709 100, intIndexed0.intValue());
710 final Integer intIndexed1 = (Integer) bean.get("intIndexed", 1);
711 assertEquals("intIndexed[1] is 10",
712 10, intIndexed1.intValue());
713 final Integer intIndexed2 = (Integer) bean.get("intIndexed", 2);
714 assertEquals("intIndexed[2] is 120",
715 120, intIndexed2.intValue());
716 final Integer intIndexed3 = (Integer) bean.get("intIndexed", 3);
717 assertEquals("intIndexed[3] is 30",
718 30, intIndexed3.intValue());
719 final Integer intIndexed4 = (Integer) bean.get("intIndexed", 4);
720 assertEquals("intIndexed[4] is 140",
721 140, intIndexed4.intValue());
722
723 map.clear();
724 map.put("stringIndexed[1]", "New String 1");
725 map.put("stringIndexed[3]", "New String 3");
726
727 BeanUtils.populate(bean, map);
728
729 assertEquals("stringIndexed[0] is \"String 0\"",
730 "String 0",
731 (String) bean.get("stringIndexed", 0));
732 assertEquals("stringIndexed[1] is \"New String 1\"",
733 "New String 1",
734 (String) bean.get("stringIndexed", 1));
735 assertEquals("stringIndexed[2] is \"String 2\"",
736 "String 2",
737 (String) bean.get("stringIndexed", 2));
738 assertEquals("stringIndexed[3] is \"New String 3\"",
739 "New String 3",
740 (String) bean.get("stringIndexed", 3));
741 assertEquals("stringIndexed[4] is \"String 4\"",
742 "String 4",
743 (String) bean.get("stringIndexed", 4));
744
745 } catch (final IllegalAccessException e) {
746 fail("IllegalAccessException");
747 } catch (final InvocationTargetException e) {
748 fail("InvocationTargetException");
749 }
750
751 }
752
753
754
755
756
757 public void testPopulateMapped() {
758
759 try {
760
761 final HashMap<String, Object> map = new HashMap<String, Object>();
762 map.put("mappedProperty(First Key)", "New First Value");
763 map.put("mappedProperty(Third Key)", "New Third Value");
764
765 BeanUtils.populate(bean, map);
766
767 assertEquals("mappedProperty(First Key)",
768 "New First Value",
769 (String) bean.get("mappedProperty", "First Key"));
770 assertEquals("mappedProperty(Second Key)",
771 "Second Value",
772 (String) bean.get("mappedProperty", "Second Key"));
773 assertEquals("mappedProperty(Third Key)",
774 "New Third Value",
775 (String) bean.get("mappedProperty", "Third Key"));
776 assertNull("mappedProperty(Fourth Key",
777 bean.get("mappedProperty", "Fourth Key"));
778
779 } catch (final IllegalAccessException e) {
780 fail("IllegalAccessException");
781 } catch (final InvocationTargetException e) {
782 fail("InvocationTargetException");
783 }
784
785 }
786
787
788
789
790
791 public void testPopulateNested() {
792
793 try {
794
795 final HashMap<String, Object> map = new HashMap<String, Object>();
796 map.put("nested.booleanProperty", "false");
797
798 map.put("nested.doubleProperty", "432.0");
799
800 map.put("nested.intProperty", "543");
801
802 map.put("nested.shortProperty", "654");
803
804
805 BeanUtils.populate(bean, map);
806
807 final TestBean nested = (TestBean) bean.get("nested");
808 assertTrue("booleanProperty is false",
809 !nested.getBooleanProperty());
810 assertTrue("booleanSecond is true",
811 nested.isBooleanSecond());
812 assertEquals("doubleProperty is 432.0",
813 432.0,
814 nested.getDoubleProperty(),
815 0.005);
816 assertEquals("floatProperty is 123.0",
817 (float) 123.0,
818 nested.getFloatProperty(),
819 (float) 0.005);
820 assertEquals("intProperty is 543",
821 543, nested.getIntProperty());
822 assertEquals("longProperty is 321",
823 321, nested.getLongProperty());
824 assertEquals("shortProperty is 654",
825 (short) 654, nested.getShortProperty());
826 assertEquals("stringProperty is \"This is a string\"",
827 "This is a string",
828 nested.getStringProperty());
829
830 } catch (final IllegalAccessException e) {
831 fail("IllegalAccessException");
832 } catch (final InvocationTargetException e) {
833 fail("InvocationTargetException");
834 }
835
836 }
837
838
839
840
841
842 public void testPopulateScalar() {
843
844 try {
845
846 bean.set("nullProperty", "non-null value");
847
848 final HashMap<String, Object> map = new HashMap<String, Object>();
849 map.put("booleanProperty", "false");
850
851 map.put("doubleProperty", "432.0");
852
853 map.put("intProperty", "543");
854
855 map.put("nullProperty", null);
856 map.put("shortProperty", "654");
857
858
859 BeanUtils.populate(bean, map);
860
861 final Boolean booleanProperty = (Boolean) bean.get("booleanProperty");
862 assertTrue("booleanProperty is false", !booleanProperty.booleanValue());
863 final Boolean booleanSecond = (Boolean) bean.get("booleanSecond");
864 assertTrue("booleanSecond is true", booleanSecond.booleanValue());
865 final Double doubleProperty = (Double) bean.get("doubleProperty");
866 assertEquals("doubleProperty is 432.0",
867 432.0, doubleProperty.doubleValue(), 0.005);
868 final Float floatProperty = (Float) bean.get("floatProperty");
869 assertEquals("floatProperty is 123.0",
870 (float) 123.0, floatProperty.floatValue(),
871 (float) 0.005);
872 final Integer intProperty = (Integer) bean.get("intProperty");
873 assertEquals("intProperty is 543",
874 543, intProperty.intValue());
875 final Long longProperty = (Long) bean.get("longProperty");
876 assertEquals("longProperty is 321",
877 321, longProperty.longValue());
878 assertNull("nullProperty is null", bean.get("nullProperty"));
879 final Short shortProperty = (Short) bean.get("shortProperty");
880 assertEquals("shortProperty is 654",
881 (short) 654, shortProperty.shortValue());
882 assertEquals("stringProperty is \"This is a string\"",
883 "This is a string",
884 (String) bean.get("stringProperty"));
885
886 } catch (final IllegalAccessException e) {
887 fail("IllegalAccessException");
888 } catch (final InvocationTargetException e) {
889 fail("InvocationTargetException");
890 }
891
892 }
893
894
895
896
897
898 public void testSetPropertyNullValues() throws Exception {
899
900 Object oldValue = null;
901 Object newValue = null;
902
903
904 oldValue = PropertyUtils.getSimpleProperty(bean, "stringArray");
905 BeanUtils.setProperty(bean, "stringArray", (String) null);
906 newValue = PropertyUtils.getSimpleProperty(bean, "stringArray");
907 assertNotNull("stringArray is not null", newValue);
908 assertTrue("stringArray of correct type",
909 newValue instanceof String[]);
910 assertEquals("stringArray length",
911 1, ((String[]) newValue).length);
912 PropertyUtils.setProperty(bean, "stringArray", oldValue);
913
914
915 oldValue = PropertyUtils.getSimpleProperty(bean, "stringArray");
916 BeanUtils.setProperty(bean, "stringArray[2]", (String) null);
917 newValue = PropertyUtils.getSimpleProperty(bean, "stringArray");
918 assertNotNull("stringArray is not null", newValue);
919 assertTrue("stringArray of correct type",
920 newValue instanceof String[]);
921 assertEquals("stringArray length",
922 5, ((String[]) newValue).length);
923 assertTrue("stringArray[2] is null",
924 ((String[]) newValue)[2] == null);
925 PropertyUtils.setProperty(bean, "stringArray", oldValue);
926
927
928 BeanUtils.setProperty(bean, "stringProperty", null);
929 assertTrue("stringProperty is now null",
930 BeanUtils.getProperty(bean, "stringProperty") == null);
931
932 }
933
934
935
936
937
938 public void testSetPropertyOnPrimitiveWrappers() throws Exception {
939
940 BeanUtils.setProperty(bean,"intProperty", new Integer(1));
941 assertEquals(1,((Integer) bean.get("intProperty")).intValue());
942 BeanUtils.setProperty(bean,"stringProperty", new Integer(1));
943 assertEquals(1, Integer.parseInt((String) bean.get("stringProperty")));
944
945 }
946
947
948
949
950
951 public void testSetPropertyNull() throws Exception {
952
953 bean.set("nullProperty", "non-null value");
954 BeanUtils.setProperty(bean, "nullProperty", null);
955 assertNull("nullProperty is null", bean.get("nullProperty"));
956
957 }
958
959
960
961
962
963 public void testCopyPropertyByte() throws Exception {
964
965 BeanUtils.setProperty(bean, "byteProperty", new Byte((byte) 123));
966 assertEquals((byte) 123, ((Byte) bean.get("byteProperty")).byteValue());
967
968
969
970
971
972
973 BeanUtils.setProperty(bean, "byteProperty", new Integer(123));
974 assertEquals((byte) 123, ((Byte) bean.get("byteProperty")).byteValue());
975 BeanUtils.setProperty(bean, "byteProperty", new Long(123));
976 assertEquals((byte) 123, ((Byte) bean.get("byteProperty")).byteValue());
977 BeanUtils.setProperty(bean, "byteProperty", new Short((short) 123));
978 assertEquals((byte) 123, ((Byte) bean.get("byteProperty")).byteValue());
979
980 }
981
982
983
984
985
986 public void testCopyPropertyDouble() throws Exception {
987
988 BeanUtils.setProperty(bean, "doubleProperty", new Byte((byte) 123));
989 assertEquals(123, ((Double) bean.get("doubleProperty")).doubleValue(), 0.005);
990 BeanUtils.setProperty(bean, "doubleProperty", new Double(123));
991 assertEquals(123, ((Double) bean.get("doubleProperty")).doubleValue(), 0.005);
992 BeanUtils.setProperty(bean, "doubleProperty", new Float(123));
993 assertEquals(123, ((Double) bean.get("doubleProperty")).doubleValue(), 0.005);
994 BeanUtils.setProperty(bean, "doubleProperty", new Integer(123));
995 assertEquals(123, ((Double) bean.get("doubleProperty")).doubleValue(), 0.005);
996 BeanUtils.setProperty(bean, "doubleProperty", new Long(123));
997 assertEquals(123, ((Double) bean.get("doubleProperty")).doubleValue(), 0.005);
998 BeanUtils.setProperty(bean, "doubleProperty", new Short((short) 123));
999 assertEquals(123, ((Double) bean.get("doubleProperty")).doubleValue(), 0.005);
1000
1001 }
1002
1003
1004
1005
1006
1007 public void testCopyPropertyFloat() throws Exception {
1008
1009 BeanUtils.setProperty(bean, "floatProperty", new Byte((byte) 123));
1010 assertEquals(123, ((Float) bean.get("floatProperty")).floatValue(), 0.005);
1011 BeanUtils.setProperty(bean, "floatProperty", new Double(123));
1012 assertEquals(123, ((Float) bean.get("floatProperty")).floatValue(), 0.005);
1013 BeanUtils.setProperty(bean, "floatProperty", new Float(123));
1014 assertEquals(123, ((Float) bean.get("floatProperty")).floatValue(), 0.005);
1015 BeanUtils.setProperty(bean, "floatProperty", new Integer(123));
1016 assertEquals(123, ((Float) bean.get("floatProperty")).floatValue(), 0.005);
1017 BeanUtils.setProperty(bean, "floatProperty", new Long(123));
1018 assertEquals(123, ((Float) bean.get("floatProperty")).floatValue(), 0.005);
1019 BeanUtils.setProperty(bean, "floatProperty", new Short((short) 123));
1020 assertEquals(123, ((Float) bean.get("floatProperty")).floatValue(), 0.005);
1021
1022 }
1023
1024
1025
1026
1027
1028 public void testCopyPropertyInteger() throws Exception {
1029
1030 BeanUtils.setProperty(bean, "longProperty", new Byte((byte) 123));
1031 assertEquals(123, ((Integer) bean.get("intProperty")).intValue());
1032
1033
1034
1035
1036
1037
1038 BeanUtils.setProperty(bean, "longProperty", new Integer(123));
1039 assertEquals(123, ((Integer) bean.get("intProperty")).intValue());
1040 BeanUtils.setProperty(bean, "longProperty", new Long(123));
1041 assertEquals(123, ((Integer) bean.get("intProperty")).intValue());
1042 BeanUtils.setProperty(bean, "longProperty", new Short((short) 123));
1043 assertEquals(123, ((Integer) bean.get("intProperty")).intValue());
1044
1045 }
1046
1047
1048
1049
1050
1051 public void testCopyPropertyLong() throws Exception {
1052
1053 BeanUtils.setProperty(bean, "longProperty", new Byte((byte) 123));
1054 assertEquals(123, ((Long) bean.get("longProperty")).longValue());
1055
1056
1057
1058
1059
1060
1061 BeanUtils.setProperty(bean, "longProperty", new Integer(123));
1062 assertEquals(123, ((Long) bean.get("longProperty")).longValue());
1063 BeanUtils.setProperty(bean, "longProperty", new Long(123));
1064 assertEquals(123, ((Long) bean.get("longProperty")).longValue());
1065 BeanUtils.setProperty(bean, "longProperty", new Short((short) 123));
1066 assertEquals(123, ((Long) bean.get("longProperty")).longValue());
1067
1068 }
1069
1070
1071
1072
1073
1074 public void testCopyPropertyNull() throws Exception {
1075
1076 bean.set("nullProperty", "non-null value");
1077 BeanUtils.copyProperty(bean, "nullProperty", null);
1078 assertNull("nullProperty is null", bean.get("nullProperty"));
1079
1080 }
1081
1082
1083
1084
1085
1086 public void testCopyPropertyShort() throws Exception {
1087
1088 BeanUtils.setProperty(bean, "shortProperty", new Byte((byte) 123));
1089 assertEquals((short) 123, ((Short) bean.get("shortProperty")).shortValue());
1090
1091
1092
1093
1094
1095
1096 BeanUtils.setProperty(bean, "shortProperty", new Integer(123));
1097 assertEquals((short) 123, ((Short) bean.get("shortProperty")).shortValue());
1098 BeanUtils.setProperty(bean, "shortProperty", new Long(123));
1099 assertEquals((short) 123, ((Short) bean.get("shortProperty")).shortValue());
1100 BeanUtils.setProperty(bean, "shortProperty", new Short((short) 123));
1101 assertEquals((short) 123, ((Short) bean.get("shortProperty")).shortValue());
1102
1103 }
1104
1105
1106
1107
1108
1109
1110 public void testCopyPropertyNestedIndexedArray() throws Exception {
1111
1112 final int origArray[] = { 0, 10, 20, 30, 40};
1113 final int intArray[] = { 0, 0, 0 };
1114 ((TestBean) bean.get("nested")).setIntArray(intArray);
1115 final int intChanged[] = { 0, 0, 0 };
1116
1117
1118 BeanUtils.copyProperty(bean, "nested.intArray[1]", new Integer(1));
1119 checkIntArray((int[]) bean.get("intArray"), origArray);
1120 intChanged[1] = 1;
1121 checkIntArray(((TestBean) bean.get("nested")).getIntArray(),
1122 intChanged);
1123
1124
1125 BeanUtils.copyProperty(bean, "nested.intArray[1]", new Byte((byte) 2));
1126 checkIntArray((int[]) bean.get("intArray"), origArray);
1127 intChanged[1] = 2;
1128 checkIntArray(((TestBean) bean.get("nested")).getIntArray(),
1129 intChanged);
1130
1131
1132 BeanUtils.copyProperty(bean, "nested.intArray[1]", new Long(3));
1133 checkIntArray((int[]) bean.get("intArray"), origArray);
1134 intChanged[1] = 3;
1135 checkIntArray(((TestBean) bean.get("nested")).getIntArray(),
1136 intChanged);
1137
1138
1139 BeanUtils.copyProperty(bean, "nested.intArray[1]", "4");
1140 checkIntArray((int[]) bean.get("intArray"), origArray);
1141 intChanged[1] = 4;
1142 checkIntArray(((TestBean) bean.get("nested")).getIntArray(),
1143 intChanged);
1144
1145 }
1146
1147
1148
1149
1150
1151 public void testCopyPropertyNestedMappedMap() throws Exception {
1152
1153 final Map<String, Object> origMap = new HashMap<String, Object>();
1154 origMap.put("First Key", "First Value");
1155 origMap.put("Second Key", "Second Value");
1156 final Map<String, Object> changedMap = new HashMap<String, Object>();
1157 changedMap.put("First Key", "First Value");
1158 changedMap.put("Second Key", "Second Value");
1159
1160
1161 BeanUtils.copyProperty(bean, "nested.mapProperty(Second Key)",
1162 "New Second Value");
1163 checkMap((Map<?, ?>) bean.get("mapProperty"), origMap);
1164 changedMap.put("Second Key", "New Second Value");
1165 checkMap(((TestBean) bean.get("nested")).getMapProperty(), changedMap);
1166
1167 }
1168
1169
1170
1171
1172
1173
1174 public void testCopyPropertyNestedSimple() throws Exception {
1175
1176 bean.set("intProperty", new Integer(0));
1177 nested.setIntProperty(0);
1178
1179
1180 BeanUtils.copyProperty(bean, "nested.intProperty", new Integer(1));
1181 assertEquals(0, ((Integer) bean.get("intProperty")).intValue());
1182 assertEquals(1, nested.getIntProperty());
1183
1184
1185 BeanUtils.copyProperty(bean, "nested.intProperty", new Byte((byte) 2));
1186 assertEquals(0, ((Integer) bean.get("intProperty")).intValue());
1187 assertEquals(2, nested.getIntProperty());
1188
1189
1190 BeanUtils.copyProperty(bean, "nested.intProperty", new Long(3));
1191 assertEquals(0, ((Integer) bean.get("intProperty")).intValue());
1192 assertEquals(3, nested.getIntProperty());
1193
1194
1195 BeanUtils.copyProperty(bean, "nested.intProperty", "4");
1196 assertEquals(0, ((Integer) bean.get("intProperty")).intValue());
1197 assertEquals(4, nested.getIntProperty());
1198
1199 }
1200
1201
1202
1203
1204
1205
1206 protected void checkIntArray(final int actual[], final int expected[]) {
1207 assertNotNull("actual array not null", actual);
1208 assertEquals("actual array length", expected.length, actual.length);
1209 for (int i = 0; i < actual.length; i++) {
1210 assertEquals("actual array value[" + i + "]",
1211 expected[i], actual[i]);
1212 }
1213 }
1214
1215
1216
1217 protected void checkMap(final Map<?, ?> actual, final Map<?, ?> expected) {
1218 assertNotNull("actual map not null", actual);
1219 assertEquals("actual map size", expected.size(), actual.size());
1220 final Iterator<?> keys = expected.keySet().iterator();
1221 while (keys.hasNext()) {
1222 final Object key = keys.next();
1223 assertEquals("actual map value(" + key + ")",
1224 expected.get(key), actual.get(key));
1225 }
1226 }
1227
1228
1229
1230
1231
1232
1233 protected static DynaClass createDynaClass() {
1234
1235 final int intArray[] = new int[0];
1236 final String stringArray[] = new String[0];
1237
1238 final DynaClass dynaClass = new BasicDynaClass
1239 ("TestDynaClass", null,
1240 new DynaProperty[]{
1241 new DynaProperty("booleanProperty", Boolean.TYPE),
1242 new DynaProperty("booleanSecond", Boolean.TYPE),
1243 new DynaProperty("byteProperty", Byte.TYPE),
1244 new DynaProperty("doubleProperty", Double.TYPE),
1245 new DynaProperty("dupProperty", stringArray.getClass()),
1246 new DynaProperty("floatProperty", Float.TYPE),
1247 new DynaProperty("intArray", intArray.getClass()),
1248 new DynaProperty("intIndexed", intArray.getClass()),
1249 new DynaProperty("intProperty", Integer.TYPE),
1250 new DynaProperty("listIndexed", List.class),
1251 new DynaProperty("longProperty", Long.TYPE),
1252 new DynaProperty("mapProperty", Map.class),
1253 new DynaProperty("mappedProperty", Map.class),
1254 new DynaProperty("mappedIntProperty", Map.class),
1255 new DynaProperty("nested", TestBean.class),
1256 new DynaProperty("nullProperty", String.class),
1257 new DynaProperty("shortProperty", Short.TYPE),
1258 new DynaProperty("stringArray", stringArray.getClass()),
1259 new DynaProperty("stringIndexed", stringArray.getClass()),
1260 new DynaProperty("stringProperty", String.class),
1261 });
1262 return (dynaClass);
1263
1264 }
1265
1266
1267 }