1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.beanutils;
20
21
22 import java.io.ByteArrayInputStream;
23 import java.io.ByteArrayOutputStream;
24 import java.io.ObjectInputStream;
25 import java.io.ObjectOutputStream;
26
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31
32 import junit.framework.TestCase;
33 import junit.framework.Test;
34 import junit.framework.TestSuite;
35
36
37
38
39
40
41
42
43
44
45 public class BasicDynaBeanTestCase extends TestCase {
46
47
48
49
50
51
52
53
54 protected DynaBean bean = null;
55
56
57
58
59
60
61
62 protected final static String[] properties = {
63 "booleanProperty",
64 "booleanSecond",
65 "doubleProperty",
66 "floatProperty",
67 "intArray",
68 "intIndexed",
69 "intProperty",
70 "listIndexed",
71 "longProperty",
72 "mappedProperty",
73 "mappedIntProperty",
74 "nullProperty",
75 "shortProperty",
76 "stringArray",
77 "stringIndexed",
78 "stringProperty",
79 };
80
81
82
83
84
85
86
87
88
89
90 public BasicDynaBeanTestCase(final String name) {
91
92 super(name);
93
94 }
95
96
97
98
99
100
101
102
103 @Override
104 public void setUp() throws Exception {
105
106
107 final DynaClass dynaClass = createDynaClass();
108 bean = dynaClass.newInstance();
109
110
111 bean.set("booleanProperty", new Boolean(true));
112 bean.set("booleanSecond", new Boolean(true));
113 bean.set("doubleProperty", new Double(321.0));
114 bean.set("floatProperty", new Float((float) 123.0));
115 final int intArray[] = { 0, 10, 20, 30, 40 };
116 bean.set("intArray", intArray);
117 final int intIndexed[] = { 0, 10, 20, 30, 40 };
118 bean.set("intIndexed", intIndexed);
119 bean.set("intProperty", new Integer(123));
120 final List<String> listIndexed = new ArrayList<String>();
121 listIndexed.add("String 0");
122 listIndexed.add("String 1");
123 listIndexed.add("String 2");
124 listIndexed.add("String 3");
125 listIndexed.add("String 4");
126 bean.set("listIndexed", listIndexed);
127 bean.set("longProperty", new Long(321));
128 final HashMap<String, String> mappedProperty = new HashMap<String, String>();
129 mappedProperty.put("First Key", "First Value");
130 mappedProperty.put("Second Key", "Second Value");
131 bean.set("mappedProperty", mappedProperty);
132 final HashMap<String, Integer> mappedIntProperty = new HashMap<String, Integer>();
133 mappedIntProperty.put("One", new Integer(1));
134 mappedIntProperty.put("Two", new Integer(2));
135 bean.set("mappedIntProperty", mappedIntProperty);
136
137 bean.set("shortProperty", new Short((short) 987));
138 final String stringArray[] =
139 { "String 0", "String 1", "String 2", "String 3", "String 4" };
140 bean.set("stringArray", stringArray);
141 final String stringIndexed[] =
142 { "String 0", "String 1", "String 2", "String 3", "String 4" };
143 bean.set("stringIndexed", stringIndexed);
144 bean.set("stringProperty", "This is a string");
145
146 }
147
148
149
150
151
152 public static Test suite() {
153
154 return (new TestSuite(BasicDynaBeanTestCase.class));
155
156 }
157
158
159
160
161
162 @Override
163 public void tearDown() {
164
165 bean = null;
166
167 }
168
169
170
171
172
173
174
175
176
177 public void testGetDescriptorArguments() {
178
179 try {
180 final DynaProperty descriptor =
181 bean.getDynaClass().getDynaProperty("unknown");
182 assertNull("Unknown property descriptor should be null",
183 descriptor);
184 } catch (final Throwable t) {
185 fail("Threw " + t + " instead of returning null");
186 }
187
188 try {
189 bean.getDynaClass().getDynaProperty(null);
190 fail("Should throw IllegalArgumentException");
191 } catch (final IllegalArgumentException e) {
192
193 } catch (final Throwable t) {
194 fail("Threw " + t + " instead of IllegalArgumentException");
195 }
196
197 }
198
199
200
201
202
203 public void testGetDescriptorBoolean() {
204
205 testGetDescriptorBase("booleanProperty", Boolean.TYPE);
206
207 }
208
209
210
211
212
213 public void testGetDescriptorDouble() {
214
215 testGetDescriptorBase("doubleProperty", Double.TYPE);
216
217 }
218
219
220
221
222
223 public void testGetDescriptorFloat() {
224
225 testGetDescriptorBase("floatProperty", Float.TYPE);
226
227 }
228
229
230
231
232
233 public void testGetDescriptorInt() {
234
235 testGetDescriptorBase("intProperty", Integer.TYPE);
236
237 }
238
239
240
241
242
243 public void testGetDescriptorLong() {
244
245 testGetDescriptorBase("longProperty", Long.TYPE);
246
247 }
248
249
250
251
252
253
254 public void testGetDescriptorSecond() {
255
256 testGetDescriptorBase("booleanSecond", Boolean.TYPE);
257
258 }
259
260
261
262
263
264 public void testGetDescriptorShort() {
265
266 testGetDescriptorBase("shortProperty", Short.TYPE);
267
268 }
269
270
271
272
273
274 public void testGetDescriptorString() {
275
276 testGetDescriptorBase("stringProperty", String.class);
277
278 }
279
280
281
282
283
284
285 public void testGetDescriptors() {
286
287 final DynaProperty pd[] = bean.getDynaClass().getDynaProperties();
288 assertNotNull("Got descriptors", pd);
289 final int count[] = new int[properties.length];
290 for (DynaProperty element : pd) {
291 final String name = element.getName();
292 for (int j = 0; j < properties.length; j++) {
293 if (name.equals(properties[j])) {
294 count[j]++;
295 }
296 }
297 }
298 for (int j = 0; j < properties.length; j++) {
299 if (count[j] < 0) {
300 fail("Missing property " + properties[j]);
301 } else if (count[j] > 1) {
302 fail("Duplicate property " + properties[j]);
303 }
304 }
305
306 }
307
308
309
310
311
312 public void testGetIndexedArguments() {
313
314 try {
315 bean.get("intArray", -1);
316 fail("Should throw IndexOutOfBoundsException");
317 } catch (final IndexOutOfBoundsException e) {
318
319 } catch (final Throwable t) {
320 fail("Threw " + t + " instead of IndexOutOfBoundsException");
321 }
322
323
324 }
325
326
327
328
329
330 public void testGetIndexedValues() {
331
332 Object value = null;
333
334 for (int i = 0; i < 5; i++) {
335
336 try {
337 value = bean.get("intArray", i);
338 assertNotNull("intArray returned value " + i, value);
339 assertTrue("intArray returned Integer " + i,
340 value instanceof Integer);
341 assertEquals("intArray returned correct " + i, i * 10,
342 ((Integer) value).intValue());
343 } catch (final Throwable t) {
344 fail("intArray " + i + " threw " + t);
345 }
346
347 try {
348 value = bean.get("intIndexed", i);
349 assertNotNull("intIndexed returned value " + i, value);
350 assertTrue("intIndexed returned Integer " + i,
351 value instanceof Integer);
352 assertEquals("intIndexed returned correct " + i, i * 10,
353 ((Integer) value).intValue());
354 } catch (final Throwable t) {
355 fail("intIndexed " + i + " threw " + t);
356 }
357
358 try {
359 value = bean.get("listIndexed", i);
360 assertNotNull("listIndexed returned value " + i, value);
361 assertTrue("list returned String " + i,
362 value instanceof String);
363 assertEquals("listIndexed returned correct " + i,
364 "String " + i, (String) value);
365 } catch (final Throwable t) {
366 fail("listIndexed " + i + " threw " + t);
367 }
368
369 try {
370 value = bean.get("stringArray", i);
371 assertNotNull("stringArray returned value " + i, value);
372 assertTrue("stringArray returned String " + i,
373 value instanceof String);
374 assertEquals("stringArray returned correct " + i,
375 "String " + i, (String) value);
376 } catch (final Throwable t) {
377 fail("stringArray " + i + " threw " + t);
378 }
379
380 try {
381 value = bean.get("stringIndexed", i);
382 assertNotNull("stringIndexed returned value " + i, value);
383 assertTrue("stringIndexed returned String " + i,
384 value instanceof String);
385 assertEquals("stringIndexed returned correct " + i,
386 "String " + i, (String) value);
387 } catch (final Throwable t) {
388 fail("stringIndexed " + i + " threw " + t);
389 }
390
391 }
392
393
394 }
395
396
397
398
399
400 public void testGetMappedArguments() {
401
402
403 try {
404 final Object value = bean.get("mappedProperty", "unknown");
405 assertNull("Should not return a value", value);
406 } catch (final Throwable t) {
407 fail("Threw " + t + " instead of returning null");
408 }
409
410
411 }
412
413
414
415
416
417 public void testGetMappedValues() {
418
419 Object value = null;
420
421 try {
422 value = bean.get("mappedProperty", "First Key");
423 assertEquals("Can find first value", "First Value", value);
424 } catch (final Throwable t) {
425 fail("Finding first value threw " + t);
426 }
427
428 try {
429 value = bean.get("mappedProperty", "Second Key");
430 assertEquals("Can find second value", "Second Value", value);
431 } catch (final Throwable t) {
432 fail("Finding second value threw " + t);
433 }
434
435 try {
436 value = bean.get("mappedProperty", "Third Key");
437 assertNull("Can not find third value", value);
438 } catch (final Throwable t) {
439 fail("Finding third value threw " + t);
440 }
441
442 }
443
444
445
446
447
448 public void testGetSimpleArguments() {
449
450 try {
451 bean.get(null);
452 fail("Should throw IllegalArgumentException");
453 } catch (final IllegalArgumentException e) {
454
455 } catch (final Throwable t) {
456 fail("Threw " + t + " instead of IllegalArgumentException");
457 }
458
459 }
460
461
462
463
464
465 public void testGetSimpleBoolean() {
466
467 try {
468 final Object value = bean.get("booleanProperty");
469 assertNotNull("Got a value", value);
470 assertTrue("Got correct type", (value instanceof Boolean));
471 assertTrue("Got correct value",
472 ((Boolean) value).booleanValue() == true);
473 } catch (final Throwable e) {
474 fail("Exception: " + e);
475 }
476
477 }
478
479
480
481
482
483 public void testGetSimpleDouble() {
484
485 try {
486 final Object value = bean.get("doubleProperty");
487 assertNotNull("Got a value", value);
488 assertTrue("Got correct type", (value instanceof Double));
489 assertEquals("Got correct value",
490 ((Double) value).doubleValue(),
491 321.0, 0.005);
492 } catch (final Throwable t) {
493 fail("Exception: " + t);
494 }
495
496 }
497
498
499
500
501
502 public void testGetSimpleFloat() {
503
504 try {
505 final Object value = bean.get("floatProperty");
506 assertNotNull("Got a value", value);
507 assertTrue("Got correct type", (value instanceof Float));
508 assertEquals("Got correct value",
509 ((Float) value).floatValue(),
510 (float) 123.0,
511 (float) 0.005);
512 } catch (final Throwable t) {
513 fail("Exception: " + t);
514 }
515
516 }
517
518
519
520
521
522 public void testGetSimpleInt() {
523
524 try {
525 final Object value = bean.get("intProperty");
526 assertNotNull("Got a value", value);
527 assertTrue("Got correct type", (value instanceof Integer));
528 assertEquals("Got correct value",
529 ((Integer) value).intValue(),
530 123);
531 } catch (final Throwable t) {
532 fail("Exception: " + t);
533 }
534
535 }
536
537
538
539
540
541 public void testGetSimpleLong() {
542
543 try {
544 final Object value = bean.get("longProperty");
545 assertNotNull("Got a value", value);
546 assertTrue("Got correct type", (value instanceof Long));
547 assertEquals("Got correct value",
548 ((Long) value).longValue(),
549 321);
550 } catch (final Throwable t) {
551 fail("Exception: " + t);
552 }
553
554 }
555
556
557
558
559
560 public void testGetSimpleShort() {
561
562 try {
563 final Object value = bean.get("shortProperty");
564 assertNotNull("Got a value", value);
565 assertTrue("Got correct type", (value instanceof Short));
566 assertEquals("Got correct value",
567 ((Short) value).shortValue(),
568 (short) 987);
569 } catch (final Throwable t) {
570 fail("Exception: " + t);
571 }
572
573 }
574
575
576
577
578
579 public void testGetSimpleString() {
580
581 try {
582 final Object value = bean.get("stringProperty");
583 assertNotNull("Got a value", value);
584 assertTrue("Got correct type", (value instanceof String));
585 assertEquals("Got correct value",
586 (String) value,
587 "This is a string");
588 } catch (final Throwable t) {
589 fail("Exception: " + t);
590 }
591
592 }
593
594
595
596
597
598 public void testMappedContains() {
599
600 try {
601 assertTrue("Can see first key",
602 bean.contains("mappedProperty", "First Key"));
603 } catch (final Throwable t) {
604 fail("Exception: " + t);
605 }
606
607
608 try {
609 assertTrue("Can not see unknown key",
610 !bean.contains("mappedProperty", "Unknown Key"));
611 } catch (final Throwable t) {
612 fail("Exception: " + t);
613 }
614
615 }
616
617
618
619
620
621 public void testMappedRemove() {
622
623 try {
624 assertTrue("Can see first key",
625 bean.contains("mappedProperty", "First Key"));
626 bean.remove("mappedProperty", "First Key");
627 assertTrue("Can not see first key",
628 !bean.contains("mappedProperty", "First Key"));
629 } catch (final Throwable t) {
630 fail("Exception: " + t);
631 }
632
633 try {
634 assertTrue("Can not see unknown key",
635 !bean.contains("mappedProperty", "Unknown Key"));
636 bean.remove("mappedProperty", "Unknown Key");
637 assertTrue("Can not see unknown key",
638 !bean.contains("mappedProperty", "Unknown Key"));
639 } catch (final Throwable t) {
640 fail("Exception: " + t);
641 }
642
643 }
644
645
646
647
648
649 public void testSerialization() {
650
651
652 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
653 try {
654 final ObjectOutputStream oos = new ObjectOutputStream(baos);
655 oos.writeObject(bean);
656 oos.flush();
657 oos.close();
658 } catch (final Exception e) {
659 fail("Exception during serialization: " + e);
660 }
661
662
663 try {
664 bean = null;
665 final ByteArrayInputStream bais =
666 new ByteArrayInputStream(baos.toByteArray());
667 final ObjectInputStream ois = new ObjectInputStream(bais);
668 bean = (DynaBean) ois.readObject();
669 bais.close();
670 } catch (final Exception e) {
671 fail("Exception during deserialization: " + e);
672 }
673
674
675 testGetDescriptorArguments();
676 testGetDescriptorBoolean();
677 testGetDescriptorDouble();
678 testGetDescriptorFloat();
679 testGetDescriptorInt();
680 testGetDescriptorLong();
681 testGetDescriptorSecond();
682 testGetDescriptorShort();
683 testGetDescriptorString();
684 testGetDescriptors();
685 testGetIndexedArguments();
686 testGetIndexedValues();
687 testGetMappedArguments();
688 testGetMappedValues();
689 testGetSimpleArguments();
690 testGetSimpleBoolean();
691 testGetSimpleDouble();
692 testGetSimpleFloat();
693 testGetSimpleInt();
694 testGetSimpleLong();
695 testGetSimpleShort();
696 testGetSimpleString();
697 testMappedContains();
698 testMappedRemove();
699
700
701 try {
702 bean = bean.getDynaClass().newInstance();
703 } catch (final Exception e) {
704 fail("Exception creating new instance: " + e);
705 }
706 testGetDescriptorArguments();
707 testGetDescriptorBoolean();
708 testGetDescriptorDouble();
709 testGetDescriptorFloat();
710 testGetDescriptorInt();
711 testGetDescriptorLong();
712 testGetDescriptorSecond();
713 testGetDescriptorShort();
714 testGetDescriptorString();
715 testGetDescriptors();
716
717 }
718
719
720
721
722
723 public void testSetIndexedArguments() {
724
725 try {
726 bean.set("intArray", -1, new Integer(0));
727 fail("Should throw IndexOutOfBoundsException");
728 } catch (final IndexOutOfBoundsException e) {
729
730 } catch (final Throwable t) {
731 fail("Threw " + t + " instead of IndexOutOfBoundsException");
732 }
733
734 }
735
736
737
738
739
740 public void testSetIndexedValues() {
741
742 Object value = null;
743
744 try {
745 bean.set("intArray", 0, new Integer(1));
746 value = bean.get("intArray", 0);
747 assertNotNull("Returned new value 0", value);
748 assertTrue("Returned Integer new value 0",
749 value instanceof Integer);
750 assertEquals("Returned correct new value 0", 1,
751 ((Integer) value).intValue());
752 } catch (final Throwable t) {
753 fail("Threw " + t);
754 }
755
756 try {
757 bean.set("intIndexed", 1, new Integer(11));
758 value = bean.get("intIndexed", 1);
759 assertNotNull("Returned new value 1", value);
760 assertTrue("Returned Integer new value 1",
761 value instanceof Integer);
762 assertEquals("Returned correct new value 1", 11,
763 ((Integer) value).intValue());
764 } catch (final Throwable t) {
765 fail("Threw " + t);
766 }
767
768 try {
769 bean.set("listIndexed", 2, "New Value 2");
770 value = bean.get("listIndexed", 2);
771 assertNotNull("Returned new value 2", value);
772 assertTrue("Returned String new value 2",
773 value instanceof String);
774 assertEquals("Returned correct new value 2", "New Value 2",
775 (String) value);
776 } catch (final Throwable t) {
777 fail("Threw " + t);
778 }
779
780 try {
781 bean.set("stringArray", 3, "New Value 3");
782 value = bean.get("stringArray", 3);
783 assertNotNull("Returned new value 3", value);
784 assertTrue("Returned String new value 3",
785 value instanceof String);
786 assertEquals("Returned correct new value 3", "New Value 3",
787 (String) value);
788 } catch (final Throwable t) {
789 fail("Threw " + t);
790 }
791
792 try {
793 bean.set("stringIndexed", 4, "New Value 4");
794 value = bean.get("stringIndexed", 4);
795 assertNotNull("Returned new value 4", value);
796 assertTrue("Returned String new value 4",
797 value instanceof String);
798 assertEquals("Returned correct new value 4", "New Value 4",
799 (String) value);
800 } catch (final Throwable t) {
801 fail("Threw " + t);
802 }
803
804
805 }
806
807
808
809
810
811 public void testSetMappedValues() {
812
813 try {
814 bean.set("mappedProperty", "First Key", "New First Value");
815 assertEquals("Can replace old value",
816 "New First Value",
817 (String) bean.get("mappedProperty", "First Key"));
818 } catch (final Throwable t) {
819 fail("Finding fourth value threw " + t);
820 }
821
822 try {
823 bean.set("mappedProperty", "Fourth Key", "Fourth Value");
824 assertEquals("Can set new value",
825 "Fourth Value",
826 (String) bean.get("mappedProperty", "Fourth Key"));
827 } catch (final Throwable t) {
828 fail("Finding fourth value threw " + t);
829 }
830
831
832 }
833
834
835
836
837
838 public void testSetSimpleBoolean() {
839
840 try {
841 final boolean oldValue =
842 ((Boolean) bean.get("booleanProperty")).booleanValue();
843 final boolean newValue = !oldValue;
844 bean.set("booleanProperty", new Boolean(newValue));
845 assertTrue("Matched new value",
846 newValue ==
847 ((Boolean) bean.get("booleanProperty")).booleanValue());
848 } catch (final Throwable e) {
849 fail("Exception: " + e);
850 }
851
852 }
853
854
855
856
857
858 public void testSetSimpleDouble() {
859
860 try {
861 final double oldValue =
862 ((Double) bean.get("doubleProperty")).doubleValue();
863 final double newValue = oldValue + 1.0;
864 bean.set("doubleProperty", new Double(newValue));
865 assertEquals("Matched new value",
866 newValue,
867 ((Double) bean.get("doubleProperty")).doubleValue(),
868 0.005);
869 } catch (final Throwable e) {
870 fail("Exception: " + e);
871 }
872
873 }
874
875
876
877
878
879 public void testSetSimpleFloat() {
880
881 try {
882 final float oldValue =
883 ((Float) bean.get("floatProperty")).floatValue();
884 final float newValue = oldValue + (float) 1.0;
885 bean.set("floatProperty", new Float(newValue));
886 assertEquals("Matched new value",
887 newValue,
888 ((Float) bean.get("floatProperty")).floatValue(),
889 (float) 0.005);
890 } catch (final Throwable e) {
891 fail("Exception: " + e);
892 }
893
894 }
895
896
897
898
899
900 public void testSetSimpleInt() {
901
902 try {
903 final int oldValue =
904 ((Integer) bean.get("intProperty")).intValue();
905 final int newValue = oldValue + 1;
906 bean.set("intProperty", new Integer(newValue));
907 assertEquals("Matched new value",
908 newValue,
909 ((Integer) bean.get("intProperty")).intValue());
910 } catch (final Throwable e) {
911 fail("Exception: " + e);
912 }
913
914 }
915
916
917
918
919
920 public void testSetSimpleLong() {
921
922 try {
923 final long oldValue =
924 ((Long) bean.get("longProperty")).longValue();
925 final long newValue = oldValue + 1;
926 bean.set("longProperty", new Long(newValue));
927 assertEquals("Matched new value",
928 newValue,
929 ((Long) bean.get("longProperty")).longValue());
930 } catch (final Throwable e) {
931 fail("Exception: " + e);
932 }
933
934 }
935
936
937
938
939
940 public void testSetSimpleShort() {
941
942 try {
943 final short oldValue =
944 ((Short) bean.get("shortProperty")).shortValue();
945 final short newValue = (short) (oldValue + 1);
946 bean.set("shortProperty", new Short(newValue));
947 assertEquals("Matched new value",
948 newValue,
949 ((Short) bean.get("shortProperty")).shortValue());
950 } catch (final Throwable e) {
951 fail("Exception: " + e);
952 }
953
954 }
955
956
957
958
959
960 public void testSetSimpleString() {
961
962 try {
963 final String oldValue = (String) bean.get("stringProperty");
964 final String newValue = oldValue + " Extra Value";
965 bean.set("stringProperty", newValue);
966 assertEquals("Matched new value",
967 newValue,
968 (String) bean.get("stringProperty"));
969 } catch (final Throwable e) {
970 fail("Exception: " + e);
971 }
972
973 }
974
975
976
977
978
979
980
981
982
983 protected DynaClass createDynaClass() {
984
985 final int intArray[] = new int[0];
986 final String stringArray[] = new String[0];
987
988 final DynaClass dynaClass = new BasicDynaClass
989 ("TestDynaClass", null,
990 new DynaProperty[]{
991 new DynaProperty("booleanProperty", Boolean.TYPE),
992 new DynaProperty("booleanSecond", Boolean.TYPE),
993 new DynaProperty("doubleProperty", Double.TYPE),
994 new DynaProperty("floatProperty", Float.TYPE),
995 new DynaProperty("intArray", intArray.getClass()),
996 new DynaProperty("intIndexed", intArray.getClass()),
997 new DynaProperty("intProperty", Integer.TYPE),
998 new DynaProperty("listIndexed", List.class),
999 new DynaProperty("longProperty", Long.TYPE),
1000 new DynaProperty("mappedProperty", Map.class),
1001 new DynaProperty("mappedIntProperty", Map.class),
1002 new DynaProperty("nullProperty", String.class),
1003 new DynaProperty("shortProperty", Short.TYPE),
1004 new DynaProperty("stringArray", stringArray.getClass()),
1005 new DynaProperty("stringIndexed", stringArray.getClass()),
1006 new DynaProperty("stringProperty", String.class),
1007 });
1008 return (dynaClass);
1009
1010 }
1011
1012
1013
1014
1015
1016
1017
1018
1019 protected void testGetDescriptorBase(final String name, final Class<?> type) {
1020
1021 try {
1022 final DynaProperty descriptor =
1023 bean.getDynaClass().getDynaProperty(name);
1024 assertNotNull("Got descriptor", descriptor);
1025 assertEquals("Got correct type", type, descriptor.getType());
1026 } catch (final Throwable t) {
1027 fail("Threw an exception: " + t);
1028 }
1029
1030 }
1031
1032
1033 }