KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17
18 package org.apache.commons.beanutils;
19
20
21 import java.io.ByteArrayInputStream JavaDoc;
22 import java.io.ByteArrayOutputStream JavaDoc;
23 import java.io.ObjectInputStream JavaDoc;
24 import java.io.ObjectOutputStream JavaDoc;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import junit.framework.TestCase;
32 import junit.framework.Test;
33 import junit.framework.TestSuite;
34
35
36 /**
37  * <p>Test Case for the <code>BasicDynaBean</code> implementation class.
38  * These tests were based on the ones in <code>PropertyUtilsTestCase</code>
39  * because the two classes provide similar levels of functionality.</p>
40  *
41  * @author Craig R. McClanahan
42  * @version $Revision: 1.10 $ $Date: 2004/02/28 13:18:36 $
43  */

44
45 public class BasicDynaBeanTestCase extends TestCase {
46
47
48     // ---------------------------------------------------- Instance Variables
49

50
51     /**
52      * The basic test bean for each test.
53      */

54     protected DynaBean bean = null;
55
56
57     /**
58      * The set of property names we expect to have returned when calling
59      * <code>getDynaProperties()</code>. You should update this list
60      * when new properties are added to TestBean.
61      */

62     protected final static String JavaDoc[] 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     // ---------------------------------------------------------- Constructors
83

84
85     /**
86      * Construct a new instance of this test case.
87      *
88      * @param name Name of the test case
89      */

90     public BasicDynaBeanTestCase(String JavaDoc name) {
91
92         super(name);
93
94     }
95
96
97     // -------------------------------------------------- Overall Test Methods
98

99
100     /**
101      * Set up instance variables required by this test case.
102      */

103     public void setUp() throws Exception JavaDoc {
104
105         // Instantiate a new DynaBean instance
106
DynaClass dynaClass = createDynaClass();
107         bean = dynaClass.newInstance();
108
109         // Initialize the DynaBean's property values (like TestBean)
110
bean.set("booleanProperty", new Boolean JavaDoc(true));
111         bean.set("booleanSecond", new Boolean JavaDoc(true));
112         bean.set("doubleProperty", new Double JavaDoc(321.0));
113         bean.set("floatProperty", new Float JavaDoc((float) 123.0));
114         int intArray[] = { 0, 10, 20, 30, 40 };
115         bean.set("intArray", intArray);
116         int intIndexed[] = { 0, 10, 20, 30, 40 };
117         bean.set("intIndexed", intIndexed);
118         bean.set("intProperty", new Integer JavaDoc(123));
119         List JavaDoc listIndexed = new ArrayList JavaDoc();
120         listIndexed.add("String 0");
121         listIndexed.add("String 1");
122         listIndexed.add("String 2");
123         listIndexed.add("String 3");
124         listIndexed.add("String 4");
125         bean.set("listIndexed", listIndexed);
126         bean.set("longProperty", new Long JavaDoc((long) 321));
127         HashMap JavaDoc mappedProperty = new HashMap JavaDoc();
128         mappedProperty.put("First Key", "First Value");
129         mappedProperty.put("Second Key", "Second Value");
130         bean.set("mappedProperty", mappedProperty);
131         HashMap JavaDoc mappedIntProperty = new HashMap JavaDoc();
132         mappedIntProperty.put("One", new Integer JavaDoc(1));
133         mappedIntProperty.put("Two", new Integer JavaDoc(2));
134         bean.set("mappedIntProperty", mappedIntProperty);
135         // Property "nullProperty" is not initialized, so it should return null
136
bean.set("shortProperty", new Short JavaDoc((short) 987));
137         String JavaDoc stringArray[] =
138                 { "String 0", "String 1", "String 2", "String 3", "String 4" };
139         bean.set("stringArray", stringArray);
140         String JavaDoc stringIndexed[] =
141                 { "String 0", "String 1", "String 2", "String 3", "String 4" };
142         bean.set("stringIndexed", stringIndexed);
143         bean.set("stringProperty", "This is a string");
144
145     }
146
147
148     /**
149      * Return the tests included in this test suite.
150      */

151     public static Test suite() {
152
153         return (new TestSuite(BasicDynaBeanTestCase.class));
154
155     }
156
157
158     /**
159      * Tear down instance variables required by this test case.
160      */

161     public void tearDown() {
162
163         bean = null;
164
165     }
166
167
168
169     // ------------------------------------------------ Individual Test Methods
170

171
172     /**
173      * Corner cases on getDynaProperty invalid arguments.
174      */

175     public void testGetDescriptorArguments() {
176
177         try {
178             DynaProperty descriptor =
179                     bean.getDynaClass().getDynaProperty("unknown");
180             assertNull("Unknown property descriptor should be null",
181                     descriptor);
182         } catch (Throwable JavaDoc t) {
183             fail("Threw " + t + " instead of returning null");
184         }
185
186         try {
187             bean.getDynaClass().getDynaProperty(null);
188             fail("Should throw IllegalArgumentException");
189         } catch (IllegalArgumentException JavaDoc e) {
190             ; // Expected response
191
} catch (Throwable JavaDoc t) {
192             fail("Threw " + t + " instead of IllegalArgumentException");
193         }
194
195     }
196
197
198     /**
199      * Positive getDynaProperty on property <code>booleanProperty</code>.
200      */

201     public void testGetDescriptorBoolean() {
202
203         testGetDescriptorBase("booleanProperty", Boolean.TYPE);
204
205     }
206
207
208     /**
209      * Positive getDynaProperty on property <code>doubleProperty</code>.
210      */

211     public void testGetDescriptorDouble() {
212
213         testGetDescriptorBase("doubleProperty", Double.TYPE);
214
215     }
216
217
218     /**
219      * Positive getDynaProperty on property <code>floatProperty</code>.
220      */

221     public void testGetDescriptorFloat() {
222
223         testGetDescriptorBase("floatProperty", Float.TYPE);
224
225     }
226
227
228     /**
229      * Positive getDynaProperty on property <code>intProperty</code>.
230      */

231     public void testGetDescriptorInt() {
232
233         testGetDescriptorBase("intProperty", Integer.TYPE);
234
235     }
236
237
238     /**
239      * Positive getDynaProperty on property <code>longProperty</code>.
240      */

241     public void testGetDescriptorLong() {
242
243         testGetDescriptorBase("longProperty", Long.TYPE);
244
245     }
246
247
248     /**
249      * Positive getDynaProperty on property <code>booleanSecond</code>
250      * that uses an "is" method as the getter.
251      */

252     public void testGetDescriptorSecond() {
253
254         testGetDescriptorBase("booleanSecond", Boolean.TYPE);
255
256     }
257
258
259     /**
260      * Positive getDynaProperty on property <code>shortProperty</code>.
261      */

262     public void testGetDescriptorShort() {
263
264         testGetDescriptorBase("shortProperty", Short.TYPE);
265
266     }
267
268
269     /**
270      * Positive getDynaProperty on property <code>stringProperty</code>.
271      */

272     public void testGetDescriptorString() {
273
274         testGetDescriptorBase("stringProperty", String JavaDoc.class);
275
276     }
277
278
279     /**
280      * Positive test for getDynaPropertys(). Each property name
281      * listed in <code>properties</code> should be returned exactly once.
282      */

283     public void testGetDescriptors() {
284
285         DynaProperty pd[] = bean.getDynaClass().getDynaProperties();
286         assertNotNull("Got descriptors", pd);
287         int count[] = new int[properties.length];
288         for (int i = 0; i < pd.length; i++) {
289             String JavaDoc name = pd[i].getName();
290             for (int j = 0; j < properties.length; j++) {
291                 if (name.equals(properties[j]))
292                     count[j]++;
293             }
294         }
295         for (int j = 0; j < properties.length; j++) {
296             if (count[j] < 0)
297                 fail("Missing property " + properties[j]);
298             else if (count[j] > 1)
299                 fail("Duplicate property " + properties[j]);
300         }
301
302     }
303
304
305     /**
306      * Corner cases on getIndexedProperty invalid arguments.
307      */

308     public void testGetIndexedArguments() {
309
310         try {
311             bean.get("intArray", -1);
312             fail("Should throw IndexOutOfBoundsException");
313         } catch (IndexOutOfBoundsException JavaDoc e) {
314             ; // Expected response
315
} catch (Throwable JavaDoc t) {
316             fail("Threw " + t + " instead of IndexOutOfBoundsException");
317         }
318
319
320     }
321
322
323     /**
324      * Positive and negative tests on getIndexedProperty valid arguments.
325      */

326     public void testGetIndexedValues() {
327
328         Object JavaDoc value = null;
329
330         for (int i = 0; i < 5; i++) {
331
332             try {
333                 value = bean.get("intArray", i);
334                 assertNotNull("intArray returned value " + i, value);
335                 assertTrue("intArray returned Integer " + i,
336                         value instanceof Integer JavaDoc);
337                 assertEquals("intArray returned correct " + i, i * 10,
338                         ((Integer JavaDoc) value).intValue());
339             } catch (Throwable JavaDoc t) {
340                 fail("intArray " + i + " threw " + t);
341             }
342
343             try {
344                 value = bean.get("intIndexed", i);
345                 assertNotNull("intIndexed returned value " + i, value);
346                 assertTrue("intIndexed returned Integer " + i,
347                         value instanceof Integer JavaDoc);
348                 assertEquals("intIndexed returned correct " + i, i * 10,
349                         ((Integer JavaDoc) value).intValue());
350             } catch (Throwable JavaDoc t) {
351                 fail("intIndexed " + i + " threw " + t);
352             }
353
354             try {
355                 value = bean.get("listIndexed", i);
356                 assertNotNull("listIndexed returned value " + i, value);
357                 assertTrue("list returned String " + i,
358                         value instanceof String JavaDoc);
359                 assertEquals("listIndexed returned correct " + i,
360                         "String " + i, (String JavaDoc) value);
361             } catch (Throwable JavaDoc t) {
362                 fail("listIndexed " + i + " threw " + t);
363             }
364
365             try {
366                 value = bean.get("stringArray", i);
367                 assertNotNull("stringArray returned value " + i, value);
368                 assertTrue("stringArray returned String " + i,
369                         value instanceof String JavaDoc);
370                 assertEquals("stringArray returned correct " + i,
371                         "String " + i, (String JavaDoc) value);
372             } catch (Throwable JavaDoc t) {
373                 fail("stringArray " + i + " threw " + t);
374             }
375
376             try {
377                 value = bean.get("stringIndexed", i);
378                 assertNotNull("stringIndexed returned value " + i, value);
379                 assertTrue("stringIndexed returned String " + i,
380                         value instanceof String JavaDoc);
381                 assertEquals("stringIndexed returned correct " + i,
382                         "String " + i, (String JavaDoc) value);
383             } catch (Throwable JavaDoc t) {
384                 fail("stringIndexed " + i + " threw " + t);
385             }
386
387         }
388
389
390     }
391
392
393     /**
394      * Corner cases on getMappedProperty invalid arguments.
395      */

396     public void testGetMappedArguments() {
397
398
399         try {
400             Object JavaDoc value = bean.get("mappedProperty", "unknown");
401             assertNull("Should not return a value", value);
402         } catch (Throwable JavaDoc t) {
403             fail("Threw " + t + " instead of returning null");
404         }
405
406
407     }
408
409
410     /**
411      * Positive and negative tests on getMappedProperty valid arguments.
412      */

413     public void testGetMappedValues() {
414
415         Object JavaDoc value = null;
416
417         try {
418             value = bean.get("mappedProperty", "First Key");
419             assertEquals("Can find first value", "First Value", value);
420         } catch (Throwable JavaDoc t) {
421             fail("Finding first value threw " + t);
422         }
423
424         try {
425             value = bean.get("mappedProperty", "Second Key");
426             assertEquals("Can find second value", "Second Value", value);
427         } catch (Throwable JavaDoc t) {
428             fail("Finding second value threw " + t);
429         }
430
431         try {
432             value = bean.get("mappedProperty", "Third Key");
433             assertNull("Can not find third value", value);
434         } catch (Throwable JavaDoc t) {
435             fail("Finding third value threw " + t);
436         }
437
438     }
439
440
441     /**
442      * Corner cases on getSimpleProperty invalid arguments.
443      */

444     public void testGetSimpleArguments() {
445
446         try {
447             bean.get(null);
448             fail("Should throw IllegalArgumentException");
449         } catch (IllegalArgumentException JavaDoc e) {
450             ; // Expected response
451
} catch (Throwable JavaDoc t) {
452             fail("Threw " + t + " instead of IllegalArgumentException");
453         }
454
455     }
456
457
458     /**
459      * Test getSimpleProperty on a boolean property.
460      */

461     public void testGetSimpleBoolean() {
462
463         try {
464             Object JavaDoc value = bean.get("booleanProperty");
465             assertNotNull("Got a value", value);
466             assertTrue("Got correct type", (value instanceof Boolean JavaDoc));
467             assertTrue("Got correct value",
468                     ((Boolean JavaDoc) value).booleanValue() == true);
469         } catch (Throwable JavaDoc e) {
470             fail("Exception: " + e);
471         }
472
473     }
474
475
476     /**
477      * Test getSimpleProperty on a double property.
478      */

479     public void testGetSimpleDouble() {
480
481         try {
482             Object JavaDoc value = bean.get("doubleProperty");
483             assertNotNull("Got a value", value);
484             assertTrue("Got correct type", (value instanceof Double JavaDoc));
485             assertEquals("Got correct value",
486                     ((Double JavaDoc) value).doubleValue(),
487                     (double) 321.0,
488                     (double) 0.005);
489         } catch (Throwable JavaDoc t) {
490             fail("Exception: " + t);
491         }
492
493     }
494
495
496     /**
497      * Test getSimpleProperty on a float property.
498      */

499     public void testGetSimpleFloat() {
500
501         try {
502             Object JavaDoc value = bean.get("floatProperty");
503             assertNotNull("Got a value", value);
504             assertTrue("Got correct type", (value instanceof Float JavaDoc));
505             assertEquals("Got correct value",
506                     ((Float JavaDoc) value).floatValue(),
507                     (float) 123.0,
508                     (float) 0.005);
509         } catch (Throwable JavaDoc t) {
510             fail("Exception: " + t);
511         }
512
513     }
514
515
516     /**
517      * Test getSimpleProperty on a int property.
518      */

519     public void testGetSimpleInt() {
520
521         try {
522             Object JavaDoc value = bean.get("intProperty");
523             assertNotNull("Got a value", value);
524             assertTrue("Got correct type", (value instanceof Integer JavaDoc));
525             assertEquals("Got correct value",
526                     ((Integer JavaDoc) value).intValue(),
527                     (int) 123);
528         } catch (Throwable JavaDoc t) {
529             fail("Exception: " + t);
530         }
531
532     }
533
534
535     /**
536      * Test getSimpleProperty on a long property.
537      */

538     public void testGetSimpleLong() {
539
540         try {
541             Object JavaDoc value = bean.get("longProperty");
542             assertNotNull("Got a value", value);
543             assertTrue("Got correct type", (value instanceof Long JavaDoc));
544             assertEquals("Got correct value",
545                     ((Long JavaDoc) value).longValue(),
546                     (long) 321);
547         } catch (Throwable JavaDoc t) {
548             fail("Exception: " + t);
549         }
550
551     }
552
553
554     /**
555      * Test getSimpleProperty on a short property.
556      */

557     public void testGetSimpleShort() {
558
559         try {
560             Object JavaDoc value = bean.get("shortProperty");
561             assertNotNull("Got a value", value);
562             assertTrue("Got correct type", (value instanceof Short JavaDoc));
563             assertEquals("Got correct value",
564                     ((Short JavaDoc) value).shortValue(),
565                     (short) 987);
566         } catch (Throwable JavaDoc t) {
567             fail("Exception: " + t);
568         }
569
570     }
571
572
573     /**
574      * Test getSimpleProperty on a String property.
575      */

576     public void testGetSimpleString() {
577
578         try {
579             Object JavaDoc value = bean.get("stringProperty");
580             assertNotNull("Got a value", value);
581             assertTrue("Got correct type", (value instanceof String JavaDoc));
582             assertEquals("Got correct value",
583                     (String JavaDoc) value,
584                     "This is a string");
585         } catch (Throwable JavaDoc t) {
586             fail("Exception: " + t);
587         }
588
589     }
590
591
592     /**
593      * Test <code>contains()</code> method for mapped properties.
594      */

595     public void testMappedContains() {
596
597         try {
598             assertTrue("Can see first key",
599                     bean.contains("mappedProperty", "First Key"));
600         } catch (Throwable JavaDoc t) {
601             fail("Exception: " + t);
602         }
603
604
605         try {
606             assertTrue("Can not see unknown key",
607                     !bean.contains("mappedProperty", "Unknown Key"));
608         } catch (Throwable JavaDoc t) {
609             fail("Exception: " + t);
610         }
611
612     }
613
614
615     /**
616      * Test <code>remove()</code> method for mapped properties.
617      */

618     public void testMappedRemove() {
619
620         try {
621             assertTrue("Can see first key",
622                     bean.contains("mappedProperty", "First Key"));
623             bean.remove("mappedProperty", "First Key");
624             assertTrue("Can not see first key",
625                     !bean.contains("mappedProperty", "First Key"));
626         } catch (Throwable JavaDoc t) {
627             fail("Exception: " + t);
628         }
629
630         try {
631             assertTrue("Can not see unknown key",
632                     !bean.contains("mappedProperty", "Unknown Key"));
633             bean.remove("mappedProperty", "Unknown Key");
634             assertTrue("Can not see unknown key",
635                     !bean.contains("mappedProperty", "Unknown Key"));
636         } catch (Throwable JavaDoc t) {
637             fail("Exception: " + t);
638         }
639
640     }
641
642
643     /**
644      * Test serialization and deserialization.
645      */

646     public void testSerialization() {
647
648         // Serialize the test bean
649
ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
650         try {
651             ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
652             oos.writeObject(bean);
653             oos.flush();
654             oos.close();
655         } catch (Exception JavaDoc e) {
656             fail("Exception during serialization: " + e);
657         }
658
659         // Deserialize the test bean
660
try {
661             bean = null;
662             ByteArrayInputStream JavaDoc bais =
663                 new ByteArrayInputStream JavaDoc(baos.toByteArray());
664             ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bais);
665             bean = (DynaBean) ois.readObject();
666             bais.close();
667         } catch (Exception JavaDoc e) {
668             fail("Exception during deserialization: " + e);
669         }
670
671         // Confirm property values
672
testGetDescriptorArguments();
673         testGetDescriptorBoolean();
674         testGetDescriptorDouble();
675         testGetDescriptorFloat();
676         testGetDescriptorInt();
677         testGetDescriptorLong();
678         testGetDescriptorSecond();
679         testGetDescriptorShort();
680         testGetDescriptorString();
681         testGetDescriptors();
682         testGetIndexedArguments();
683         testGetIndexedValues();
684         testGetMappedArguments();
685         testGetMappedValues();
686         testGetSimpleArguments();
687         testGetSimpleBoolean();
688         testGetSimpleDouble();
689         testGetSimpleFloat();
690         testGetSimpleInt();
691         testGetSimpleLong();
692         testGetSimpleShort();
693         testGetSimpleString();
694         testMappedContains();
695         testMappedRemove();
696
697         // Ensure that we can create a new instance of the same DynaClass
698
try {
699             bean = bean.getDynaClass().newInstance();
700         } catch (Exception JavaDoc e) {
701             fail("Exception creating new instance: " + e);
702         }
703         testGetDescriptorArguments();
704         testGetDescriptorBoolean();
705         testGetDescriptorDouble();
706         testGetDescriptorFloat();
707         testGetDescriptorInt();
708         testGetDescriptorLong();
709         testGetDescriptorSecond();
710         testGetDescriptorShort();
711         testGetDescriptorString();
712         testGetDescriptors();
713
714     }
715
716
717     /**
718      * Corner cases on setIndexedProperty invalid arguments.
719      */

720     public void testSetIndexedArguments() {
721
722         try {
723             bean.set("intArray", -1, new Integer JavaDoc(0));
724             fail("Should throw IndexOutOfBoundsException");
725         } catch (IndexOutOfBoundsException JavaDoc e) {
726             ; // Expected response
727
} catch (Throwable JavaDoc t) {
728             fail("Threw " + t + " instead of IndexOutOfBoundsException");
729         }
730
731     }
732
733
734     /**
735      * Positive and negative tests on setIndexedProperty valid arguments.
736      */

737     public void testSetIndexedValues() {
738
739         Object JavaDoc value = null;
740
741         try {
742             bean.set("intArray", 0, new Integer JavaDoc(1));
743             value = (Integer JavaDoc) bean.get("intArray", 0);
744             assertNotNull("Returned new value 0", value);
745             assertTrue("Returned Integer new value 0",
746                     value instanceof Integer JavaDoc);
747             assertEquals("Returned correct new value 0", 1,
748                     ((Integer JavaDoc) value).intValue());
749         } catch (Throwable JavaDoc t) {
750             fail("Threw " + t);
751         }
752
753         try {
754             bean.set("intIndexed", 1, new Integer JavaDoc(11));
755             value = (Integer JavaDoc) bean.get("intIndexed", 1);
756             assertNotNull("Returned new value 1", value);
757             assertTrue("Returned Integer new value 1",
758                     value instanceof Integer JavaDoc);
759             assertEquals("Returned correct new value 1", 11,
760                     ((Integer JavaDoc) value).intValue());
761         } catch (Throwable JavaDoc t) {
762             fail("Threw " + t);
763         }
764
765         try {
766             bean.set("listIndexed", 2, "New Value 2");
767             value = (String JavaDoc) bean.get("listIndexed", 2);
768             assertNotNull("Returned new value 2", value);
769             assertTrue("Returned String new value 2",
770                     value instanceof String JavaDoc);
771             assertEquals("Returned correct new value 2", "New Value 2",
772                     (String JavaDoc) value);
773         } catch (Throwable JavaDoc t) {
774             fail("Threw " + t);
775         }
776
777         try {
778             bean.set("stringArray", 3, "New Value 3");
779             value = (String JavaDoc) bean.get("stringArray", 3);
780             assertNotNull("Returned new value 3", value);
781             assertTrue("Returned String new value 3",
782                     value instanceof String JavaDoc);
783             assertEquals("Returned correct new value 3", "New Value 3",
784                     (String JavaDoc) value);
785         } catch (Throwable JavaDoc t) {
786             fail("Threw " + t);
787         }
788
789         try {
790             bean.set("stringIndexed", 4, "New Value 4");
791             value = (String JavaDoc) bean.get("stringIndexed", 4);
792             assertNotNull("Returned new value 4", value);
793             assertTrue("Returned String new value 4",
794                     value instanceof String JavaDoc);
795             assertEquals("Returned correct new value 4", "New Value 4",
796                     (String JavaDoc) value);
797         } catch (Throwable JavaDoc t) {
798             fail("Threw " + t);
799         }
800
801
802     }
803
804
805     /**
806      * Positive and negative tests on setMappedProperty valid arguments.
807      */

808     public void testSetMappedValues() {
809
810         try {
811             bean.set("mappedProperty", "First Key", "New First Value");
812             assertEquals("Can replace old value",
813                     "New First Value",
814                     (String JavaDoc) bean.get("mappedProperty", "First Key"));
815         } catch (Throwable JavaDoc t) {
816             fail("Finding fourth value threw " + t);
817         }
818
819         try {
820             bean.set("mappedProperty", "Fourth Key", "Fourth Value");
821             assertEquals("Can set new value",
822                     "Fourth Value",
823                     (String JavaDoc) bean.get("mappedProperty", "Fourth Key"));
824         } catch (Throwable JavaDoc t) {
825             fail("Finding fourth value threw " + t);
826         }
827
828
829     }
830
831
832     /**
833      * Test setSimpleProperty on a boolean property.
834      */

835     public void testSetSimpleBoolean() {
836
837         try {
838             boolean oldValue =
839                     ((Boolean JavaDoc) bean.get("booleanProperty")).booleanValue();
840             boolean newValue = !oldValue;
841             bean.set("booleanProperty", new Boolean JavaDoc(newValue));
842             assertTrue("Matched new value",
843                     newValue ==
844                     ((Boolean JavaDoc) bean.get("booleanProperty")).booleanValue());
845         } catch (Throwable JavaDoc e) {
846             fail("Exception: " + e);
847         }
848
849     }
850
851
852     /**
853      * Test setSimpleProperty on a double property.
854      */

855     public void testSetSimpleDouble() {
856
857         try {
858             double oldValue =
859                     ((Double JavaDoc) bean.get("doubleProperty")).doubleValue();
860             double newValue = oldValue + 1.0;
861             bean.set("doubleProperty", new Double JavaDoc(newValue));
862             assertEquals("Matched new value",
863                     newValue,
864                     ((Double JavaDoc) bean.get("doubleProperty")).doubleValue(),
865                     (double) 0.005);
866         } catch (Throwable JavaDoc e) {
867             fail("Exception: " + e);
868         }
869
870     }
871
872
873     /**
874      * Test setSimpleProperty on a float property.
875      */

876     public void testSetSimpleFloat() {
877
878         try {
879             float oldValue =
880                     ((Float JavaDoc) bean.get("floatProperty")).floatValue();
881             float newValue = oldValue + (float) 1.0;
882             bean.set("floatProperty", new Float JavaDoc(newValue));
883             assertEquals("Matched new value",
884                     newValue,
885                     ((Float JavaDoc) bean.get("floatProperty")).floatValue(),
886                     (float) 0.005);
887         } catch (Throwable JavaDoc e) {
888             fail("Exception: " + e);
889         }
890
891     }
892
893
894     /**
895      * Test setSimpleProperty on a int property.
896      */

897     public void testSetSimpleInt() {
898
899         try {
900             int oldValue =
901                     ((Integer JavaDoc) bean.get("intProperty")).intValue();
902             int newValue = oldValue + 1;
903             bean.set("intProperty", new Integer JavaDoc(newValue));
904             assertEquals("Matched new value",
905                     newValue,
906                     ((Integer JavaDoc) bean.get("intProperty")).intValue());
907         } catch (Throwable JavaDoc e) {
908             fail("Exception: " + e);
909         }
910
911     }
912
913
914     /**
915      * Test setSimpleProperty on a long property.
916      */

917     public void testSetSimpleLong() {
918
919         try {
920             long oldValue =
921                     ((Long JavaDoc) bean.get("longProperty")).longValue();
922             long newValue = oldValue + 1;
923             bean.set("longProperty", new Long JavaDoc(newValue));
924             assertEquals("Matched new value",
925                     newValue,
926                     ((Long JavaDoc) bean.get("longProperty")).longValue());
927         } catch (Throwable JavaDoc e) {
928             fail("Exception: " + e);
929         }
930
931     }
932
933
934     /**
935      * Test setSimpleProperty on a short property.
936      */

937     public void testSetSimpleShort() {
938
939         try {
940             short oldValue =
941                     ((Short JavaDoc) bean.get("shortProperty")).shortValue();
942             short newValue = (short) (oldValue + 1);
943             bean.set("shortProperty", new Short JavaDoc(newValue));
944             assertEquals("Matched new value",
945                     newValue,
946                     ((Short JavaDoc) bean.get("shortProperty")).shortValue());
947         } catch (Throwable JavaDoc e) {
948             fail("Exception: " + e);
949         }
950
951     }
952
953
954     /**
955      * Test setSimpleProperty on a String property.
956      */

957     public void testSetSimpleString() {
958
959         try {
960             String JavaDoc oldValue = (String JavaDoc) bean.get("stringProperty");
961             String JavaDoc newValue = oldValue + " Extra Value";
962             bean.set("stringProperty", newValue);
963             assertEquals("Matched new value",
964                     newValue,
965                     (String JavaDoc) bean.get("stringProperty"));
966         } catch (Throwable JavaDoc e) {
967             fail("Exception: " + e);
968         }
969
970     }
971
972
973     // ------------------------------------------------------ Protected Methods
974

975
976     /**
977      * Create and return a <code>DynaClass</code> instance for our test
978      * <code>DynaBean</code>.
979      */

980     protected DynaClass createDynaClass() {
981
982         int intArray[] = new int[0];
983         String JavaDoc stringArray[] = new String JavaDoc[0];
984
985         DynaClass dynaClass = new BasicDynaClass
986                 ("TestDynaClass", null,
987                         new DynaProperty[]{
988                             new DynaProperty("booleanProperty", Boolean.TYPE),
989                             new DynaProperty("booleanSecond", Boolean.TYPE),
990                             new DynaProperty("doubleProperty", Double.TYPE),
991                             new DynaProperty("floatProperty", Float.TYPE),
992                             new DynaProperty("intArray", intArray.getClass()),
993                             new DynaProperty("intIndexed", intArray.getClass()),
994                             new DynaProperty("intProperty", Integer.TYPE),
995                             new DynaProperty("listIndexed", List JavaDoc.class),
996                             new DynaProperty("longProperty", Long.TYPE),
997                             new DynaProperty("mappedProperty", Map JavaDoc.class),
998                             new DynaProperty("mappedIntProperty", Map JavaDoc.class),
999                             new DynaProperty("nullProperty", String JavaDoc.class),
1000                            new DynaProperty("shortProperty", Short.TYPE),
1001                            new DynaProperty("stringArray", stringArray.getClass()),
1002                            new DynaProperty("stringIndexed", stringArray.getClass()),
1003                            new DynaProperty("stringProperty", String JavaDoc.class),
1004                        });
1005        return (dynaClass);
1006
1007    }
1008
1009
1010    /**
1011     * Base for testGetDescriptorXxxxx() series of tests.
1012     *
1013     * @param name Name of the property to be retrieved
1014     * @param type Expected class type of this property
1015     */

1016    protected void testGetDescriptorBase(String JavaDoc name, Class JavaDoc type) {
1017
1018        try {
1019            DynaProperty descriptor =
1020                    bean.getDynaClass().getDynaProperty(name);
1021            assertNotNull("Got descriptor", descriptor);
1022            assertEquals("Got correct type", type, descriptor.getType());
1023        } catch (Throwable JavaDoc t) {
1024            fail("Threw an exception: " + t);
1025        }
1026
1027    }
1028
1029
1030}
1031
Popular Tags