KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > configuration > beanutils > TestConfigurationDynaBean


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

16
17 package org.apache.commons.configuration.beanutils;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21
22 import junit.framework.AssertionFailedError;
23 import junit.framework.TestCase;
24
25 import org.apache.commons.beanutils.DynaProperty;
26 import org.apache.commons.configuration.BaseConfiguration;
27
28 /**
29  * <p>Test Case for the <code>ConfigurationDynaBean</code> implementation class.
30  * These tests were based on the ones in <code>BasicDynaBeanTestCase</code>
31  * because the two classes provide similar levels of functionality.</p>
32  *
33  * @author <a HREF="mailto:ricardo.gladwell@btinternet.com">Ricardo Gladwell</a>
34  * @version $Revision: 155408 $
35  */

36 public class TestConfigurationDynaBean extends TestCase
37 {
38     /**
39      * The basic test bean for each test.
40      */

41     protected ConfigurationDynaBean bean = null;
42
43     /**
44      * The set of property names we expect to have returned when calling
45      * <code>getDynaProperties()</code>. You should update this list
46      * when new properties are added to TestBean.
47      */

48     String JavaDoc[] properties = {
49         "booleanProperty",
50         "booleanSecond",
51         "doubleProperty",
52         "floatProperty",
53         "intProperty",
54         "longProperty",
55         "mappedProperty.key1",
56         "mappedProperty.key2",
57         "mappedProperty.key3",
58         "mappedIntProperty.key1",
59         "shortProperty",
60         "stringProperty",
61         "byteProperty",
62         "charProperty"
63     };
64
65     Object JavaDoc[] values = {
66         Boolean.TRUE,
67         Boolean.TRUE,
68         new Double JavaDoc(Double.MAX_VALUE),
69         new Float JavaDoc(Float.MAX_VALUE),
70         new Integer JavaDoc(Integer.MAX_VALUE),
71         new Long JavaDoc(Long.MAX_VALUE),
72         "First Value",
73         "Second Value",
74         "Third Value",
75         new Integer JavaDoc(Integer.MAX_VALUE),
76         new Short JavaDoc(Short.MAX_VALUE),
77         "This is a string",
78         new Byte JavaDoc(Byte.MAX_VALUE),
79         new Character JavaDoc(Character.MAX_VALUE)
80     };
81
82     int[] intArray = {0, 10, 20, 30, 40};
83     boolean[] booleanArray = {true, false, true, false, true};
84     char[] charArray = {'a', 'b', 'c', 'd', 'e'};
85     byte[] byteArray = {0, 10, 20, 30, 40};
86     long[] longArray = {0, 10, 20, 30, 40};
87     short[] shortArray = {0, 10, 20, 30, 40};
88     float[] floatArray = {0, 10, 20, 30, 40};
89     double[] doubleArray = {0.0, 10.0, 20.0, 30.0, 40.0};
90     String JavaDoc[] stringArray = {"String 0", "String 1", "String 2", "String 3", "String 4"};
91
92     /**
93      * Construct a new instance of this test case.
94      * @param name Name of the test case
95      */

96     public TestConfigurationDynaBean(String JavaDoc name)
97     {
98         super(name);
99     }
100
101     /**
102      * Set up instance variables required by this test case.
103      */

104     public void setUp() throws Exception JavaDoc
105     {
106         BaseConfiguration configuration = new BaseConfiguration();
107
108         for (int i = 0; i < properties.length; i++)
109         {
110             configuration.setProperty(properties[i], values[i]);
111         }
112
113         for (int a = 0; a < intArray.length; a++)
114         {
115             configuration.addProperty("intIndexed", new Integer JavaDoc(intArray[a]));
116         }
117
118         for (int a = 0; a < stringArray.length; a++)
119         {
120             configuration.addProperty("stringIndexed", stringArray[a]);
121         }
122
123         List JavaDoc list = new ArrayList JavaDoc();
124         for (int i = 0; i < stringArray.length; i++)
125         {
126             list.add(stringArray[i]);
127         }
128         configuration.addProperty("listIndexed", list);
129
130         bean = new ConfigurationDynaBean(configuration);
131
132         bean.set("listIndexed", list);
133         bean.set("intArray", intArray);
134         bean.set("booleanArray", booleanArray);
135         bean.set("charArray", charArray);
136         bean.set("longArray", longArray);
137         bean.set("shortArray", shortArray);
138         bean.set("floatArray", floatArray);
139         bean.set("doubleArray", doubleArray);
140         bean.set("byteArray", byteArray);
141         bean.set("stringArray", stringArray);
142     }
143
144
145     /**
146      * Tear down instance variables required by this test case.
147      */

148     public void tearDown()
149     {
150         bean = null;
151     }
152
153     /**
154      * Corner cases on getDynaProperty invalid arguments.
155      */

156     public void testGetDescriptorArguments()
157     {
158         try
159         {
160             DynaProperty descriptor = bean.getDynaClass().getDynaProperty("unknown");
161             assertNull("Unknown property descriptor should be null", descriptor);
162         }
163         catch (Throwable JavaDoc t)
164         {
165             fail("Threw " + t + " instead of returning null");
166         }
167
168         try
169         {
170             bean.getDynaClass().getDynaProperty(null);
171             fail("Should throw IllegalArgumentException");
172         }
173         catch (java.lang.IllegalArgumentException JavaDoc e)
174         {
175             ; // Expected response
176
}
177         catch (AssertionFailedError e)
178         {
179             ; // ignore other failed responses
180
}
181         catch (Throwable JavaDoc t)
182         {
183             fail("Threw '" + t + "' instead of 'IllegalArgumentException'");
184         }
185     }
186
187     /**
188      * Positive getDynaProperty on property <code>booleanProperty</code>.
189      */

190     public void testGetDescriptorBoolean()
191     {
192         testGetDescriptorBase("booleanProperty", Boolean.TYPE);
193     }
194
195     /**
196      * Positive getDynaProperty on property <code>doubleProperty</code>.
197      */

198     public void testGetDescriptorDouble()
199     {
200         testGetDescriptorBase("doubleProperty", Double.TYPE);
201     }
202
203     /**
204      * Positive getDynaProperty on property <code>floatProperty</code>.
205      */

206     public void testGetDescriptorFloat()
207     {
208         testGetDescriptorBase("floatProperty", Float.TYPE);
209     }
210
211     /**
212      * Positive getDynaProperty on property <code>intProperty</code>.
213      */

214     public void testGetDescriptorInt()
215     {
216         testGetDescriptorBase("intProperty", Integer.TYPE);
217     }
218
219     /**
220      * Positive getDynaProperty on property <code>longProperty</code>.
221      */

222     public void testGetDescriptorLong()
223     {
224         testGetDescriptorBase("longProperty", Long.TYPE);
225     }
226
227     /**
228      * Positive getDynaProperty on property <code>booleanSecond</code>
229      * that uses an "is" method as the getter.
230      */

231     public void testGetDescriptorSecond()
232     {
233         testGetDescriptorBase("booleanSecond", Boolean.TYPE);
234     }
235
236     /**
237      * Positive getDynaProperty on property <code>shortProperty</code>.
238      */

239     public void testGetDescriptorShort()
240     {
241         testGetDescriptorBase("shortProperty", Short.TYPE);
242     }
243
244     /**
245      * Positive getDynaProperty on property <code>stringProperty</code>.
246      */

247     public void testGetDescriptorString()
248     {
249         testGetDescriptorBase("stringProperty", String JavaDoc.class);
250     }
251
252     /**
253      * Positive test for getDynaPropertys(). Each property name
254      * listed in <code>properties</code> should be returned exactly once.
255      */

256     public void testGetDescriptors()
257     {
258         DynaProperty pd[] = bean.getDynaClass().getDynaProperties();
259         assertNotNull("Got descriptors", pd);
260         int count[] = new int[properties.length];
261         for (int i = 0; i < pd.length; i++)
262         {
263             String JavaDoc name = pd[i].getName();
264             for (int j = 0; j < properties.length; j++)
265             {
266                 if (name.equals(properties[j]))
267                 {
268                     count[j]++;
269                 }
270             }
271         }
272         
273         for (int j = 0; j < properties.length; j++)
274         {
275             if (count[j] < 0)
276             {
277                 fail("Missing property " + properties[j]);
278             }
279             else if (count[j] > 1)
280             {
281                 fail("Duplicate property " + properties[j]);
282             }
283         }
284     }
285
286     /**
287      * Corner cases on getIndexedProperty invalid arguments.
288      */

289     public void testGetIndexedArguments()
290     {
291         try
292         {
293             bean.get("intArray", -1);
294         }
295         catch (IndexOutOfBoundsException JavaDoc e)
296         {
297             return; // Expected response
298
}
299         catch (Throwable JavaDoc t)
300         {
301             fail("Threw '" + t + "' instead of 'IndexOutOfBoundsException'");
302             return;
303         }
304
305         fail("Should throw IndexOutOfBoundsException");
306     }
307
308     /**
309      * Positive and negative tests on getIndexedProperty valid arguments.
310      */

311     public void testGetIndexedValues()
312     {
313         Object JavaDoc value = null;
314
315         for (int i = 0; i < 5; i++)
316         {
317             try
318             {
319                 value = bean.get("intArray", i);
320             }
321             catch (Throwable JavaDoc t)
322             {
323                 fail("intArray " + i + " threw " + t);
324             }
325
326             assertNotNull("intArray index " + i + " did not return value.", value);
327             assertTrue("intArray index " + i + " did not return Integer.", (value instanceof Integer JavaDoc));
328             assertEquals("intArray " + i + " returned incorrect value.", i * 10, ((Integer JavaDoc) value).intValue());
329
330             try
331             {
332                 value = bean.get("intIndexed", i);
333             }
334             catch (Throwable JavaDoc t)
335             {
336                 fail("intIndexed index " + i + " threw " + t);
337             }
338
339             assertNotNull("intIndexed index " + i + "returned value " + i, value);
340             assertTrue("intIndexed index " + i + "returned Integer " + i, value instanceof Integer JavaDoc);
341             assertEquals("intIndexed index " + i + "returned correct " + i, i * 10, ((Integer JavaDoc) value).intValue());
342
343             try
344             {
345                 value = bean.get("listIndexed", i);
346             }
347             catch (Throwable JavaDoc t)
348             {
349                 fail("listIndexed index " + i + " threw " + t);
350             }
351
352             assertNotNull("listIndexed index " + i + "returned value " + i, value);
353             assertTrue("list index " + i + "returned String " + i, value instanceof String JavaDoc);
354             assertEquals("listIndexed index " + i + "returned correct " + i, "String " + i, (String JavaDoc) value);
355
356             try
357             {
358                 value = bean.get("stringArray", i);
359             }
360             catch (Throwable JavaDoc t)
361             {
362                 fail("stringArray index " + i + " threw " + t);
363             }
364
365             assertNotNull("stringArray index " + i + " returnde null.", value);
366             assertFalse("stringArray index " + i + " returned array instead of String.", value.getClass().isArray());
367             assertTrue("stringArray index " + i + " returned "
368                     + value.getClass().getName() + "=[" + value + "]"
369                     + " instead of String.",
370                     value instanceof String JavaDoc);
371             assertEquals("stringArray returned correct " + i, "String " + i, (String JavaDoc) value);
372
373             try
374             {
375                 value = bean.get("stringIndexed", i);
376             }
377             catch (Throwable JavaDoc t)
378             {
379                 fail("stringIndexed " + i + " threw " + t);
380             }
381
382             assertNotNull("stringIndexed returned value " + i, value);
383             assertTrue("stringIndexed returned String " + i, value instanceof String JavaDoc);
384             assertEquals("stringIndexed returned correct " + i, "String " + i, (String JavaDoc) value);
385         }
386     }
387
388     /**
389      * Corner cases on getMappedProperty invalid arguments.
390      */

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

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

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

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

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

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

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

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

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

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

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

615     public void testMappedRemove()
616     {
617         try
618         {
619             assertTrue("Can see first key", bean.contains("mappedProperty", "key1"));
620             bean.remove("mappedProperty", "key1");
621             assertTrue("Can not see first key", !bean.contains("mappedProperty", "key1"));
622         }
623         catch (Throwable JavaDoc t)
624         {
625             fail("Exception: " + t);
626         }
627
628         try
629         {
630             assertTrue("Can not see unknown key", !bean.contains("mappedProperty", "key4"));
631             bean.remove("mappedProperty", "key4");
632             assertTrue("Can not see unknown key", !bean.contains("mappedProperty", "key4"));
633         }
634         catch (Throwable JavaDoc t)
635         {
636             fail("Exception: " + t);
637         }
638     }
639
640     /**
641      * Corner cases on setIndexedProperty invalid arguments.
642      */

643     public void testSetIndexedArguments()
644     {
645         try
646         {
647             bean.set("intArray", -1, new Integer JavaDoc(0));
648         }
649         catch (IndexOutOfBoundsException JavaDoc e)
650         {
651             return; // Expected response
652
}
653         catch (Throwable JavaDoc t)
654         {
655             fail("Threw " + t + " instead of IndexOutOfBoundsException");
656         }
657
658         fail("Should throw IndexOutOfBoundsException");
659     }
660
661     /**
662      * Positive and negative tests on setIndexedProperty valid arguments.
663      */

664     public void testSetIndexedValues()
665     {
666         Object JavaDoc value = null;
667
668         try
669         {
670             bean.set("intArray", 0, new Integer JavaDoc(1));
671             value = bean.get("intArray", 0);
672         }
673         catch (Throwable JavaDoc t)
674         {
675             fail("Threw " + t);
676         }
677
678         assertNotNull("Returned new value 0", value);
679         assertTrue("Returned Integer new value 0", value instanceof Integer JavaDoc);
680         assertEquals("Returned correct new value 0", 1, ((Integer JavaDoc) value).intValue());
681
682         try
683         {
684             bean.set("intIndexed", 1, new Integer JavaDoc(11));
685             value = bean.get("intIndexed", 1);
686         }
687         catch (Throwable JavaDoc t)
688         {
689             fail("Threw " + t);
690         }
691
692         assertNotNull("Returned new value 1", value);
693         assertTrue("Returned Integer new value 1", value instanceof Integer JavaDoc);
694         assertEquals("Returned correct new value 1", 11, ((Integer JavaDoc) value).intValue());
695
696         try
697         {
698             bean.set("listIndexed", 2, "New Value 2");
699             value = bean.get("listIndexed", 2);
700         }
701         catch (Throwable JavaDoc t)
702         {
703             fail("Threw " + t);
704         }
705
706         assertNotNull("Returned new value 2", value);
707         assertTrue("Returned String new value 2", value instanceof String JavaDoc);
708         assertEquals("Returned correct new value 2", "New Value 2", (String JavaDoc) value);
709
710         try
711         {
712             bean.set("stringArray", 3, "New Value 3");
713             value = bean.get("stringArray", 3);
714         }
715         catch (Throwable JavaDoc t)
716         {
717             fail("Threw " + t);
718         }
719
720         assertNotNull("Returned new value 3", value);
721         assertTrue("Returned String new value 3", value instanceof String JavaDoc);
722         assertEquals("Returned correct new value 3", "New Value 3", (String JavaDoc) value);
723
724         try
725         {
726             bean.set("stringIndexed", 4, "New Value 4");
727             value = bean.get("stringIndexed", 4);
728         }
729         catch (Throwable JavaDoc t)
730         {
731             fail("Threw " + t);
732         }
733
734         assertNotNull("Returned new value 4", value);
735         assertTrue("Returned String new value 4", value instanceof String JavaDoc);
736         assertEquals("Returned correct new value 4", "New Value 4", (String JavaDoc) value);
737     }
738
739     /**
740      * Positive and negative tests on setMappedProperty valid arguments.
741      */

742     public void testSetMappedValues()
743     {
744         try
745         {
746             bean.set("mappedProperty", "First Key", "New First Value");
747             assertEquals("Can replace old value", "New First Value", (String JavaDoc) bean.get("mappedProperty", "First Key"));
748         }
749         catch (Throwable JavaDoc t)
750         {
751             fail("Finding fourth value threw " + t);
752         }
753
754         try
755         {
756             bean.set("mappedProperty", "Fourth Key", "Fourth Value");
757             assertEquals("Can set new value", "Fourth Value", (String JavaDoc) bean.get("mappedProperty", "Fourth Key"));
758         }
759         catch (Throwable JavaDoc t)
760         {
761             fail("Finding fourth value threw " + t);
762         }
763     }
764
765     /**
766      * Test setSimpleProperty on a boolean property.
767      */

768     public void testSetSimpleBoolean()
769     {
770         try
771         {
772             boolean oldValue = ((Boolean JavaDoc) bean.get("booleanProperty")).booleanValue();
773             boolean newValue = !oldValue;
774             bean.set("booleanProperty", new Boolean JavaDoc(newValue));
775             assertTrue("Matched new value", newValue == ((Boolean JavaDoc) bean.get("booleanProperty")).booleanValue());
776         }
777         catch (Throwable JavaDoc e)
778         {
779             fail("Exception: " + e);
780         }
781     }
782
783     /**
784      * Test setSimpleProperty on a double property.
785      */

786     public void testSetSimpleDouble()
787     {
788         try
789         {
790             double oldValue = ((Double JavaDoc) bean.get("doubleProperty")).doubleValue();
791             double newValue = oldValue + 1.0;
792             bean.set("doubleProperty", new Double JavaDoc(newValue));
793             assertEquals("Matched new value", newValue, ((Double JavaDoc) bean.get("doubleProperty")).doubleValue(), 0.005);
794         }
795         catch (Throwable JavaDoc e)
796         {
797             fail("Exception: " + e);
798         }
799     }
800
801     /**
802      * Test setSimpleProperty on a float property.
803      */

804     public void testSetSimpleFloat()
805     {
806         try
807         {
808             float oldValue = ((Float JavaDoc) bean.get("floatProperty")).floatValue();
809             float newValue = oldValue + (float) 1.0;
810             bean.set("floatProperty", new Float JavaDoc(newValue));
811             assertEquals("Matched new value", newValue, ((Float JavaDoc) bean.get("floatProperty")).floatValue(), 0.005f);
812         }
813         catch (Throwable JavaDoc e)
814         {
815             fail("Exception: " + e);
816         }
817     }
818
819     /**
820      * Test setSimpleProperty on a int property.
821      */

822     public void testSetSimpleInt()
823     {
824         try
825         {
826             int oldValue = ((Integer JavaDoc) bean.get("intProperty")).intValue();
827             int newValue = oldValue + 1;
828             bean.set("intProperty", new Integer JavaDoc(newValue));
829             assertEquals("Matched new value", newValue, ((Integer JavaDoc) bean.get("intProperty")).intValue());
830         }
831         catch (Throwable JavaDoc e)
832         {
833             fail("Exception: " + e);
834         }
835     }
836
837     /**
838      * Test setSimpleProperty on a long property.
839      */

840     public void testSetSimpleLong()
841     {
842         try
843         {
844             long oldValue = ((Long JavaDoc) bean.get("longProperty")).longValue();
845             long newValue = oldValue + 1;
846             bean.set("longProperty", new Long JavaDoc(newValue));
847             assertEquals("Matched new value", newValue, ((Long JavaDoc) bean.get("longProperty")).longValue());
848         }
849         catch (Throwable JavaDoc e)
850         {
851             fail("Exception: " + e);
852         }
853     }
854
855     /**
856      * Test setSimpleProperty on a short property.
857      */

858     public void testSetSimpleShort()
859     {
860         try
861         {
862             short oldValue = ((Short JavaDoc) bean.get("shortProperty")).shortValue();
863             short newValue = (short) (oldValue + 1);
864             bean.set("shortProperty", new Short JavaDoc(newValue));
865             assertEquals("Matched new value", newValue, ((Short JavaDoc) bean.get("shortProperty")).shortValue());
866         }
867         catch (Throwable JavaDoc e)
868         {
869             fail("Exception: " + e);
870         }
871     }
872
873     /**
874      * Test setSimpleProperty on a String property.
875      */

876     public void testSetSimpleString()
877     {
878         try
879         {
880             String JavaDoc oldValue = (String JavaDoc) bean.get("stringProperty");
881             String JavaDoc newValue = oldValue + " Extra Value";
882             bean.set("stringProperty", newValue);
883             assertEquals("Matched new value", newValue, (String JavaDoc) bean.get("stringProperty"));
884         }
885         catch (Throwable JavaDoc e)
886         {
887             fail("Exception: " + e);
888         }
889     }
890
891     /**
892      * Tests set on a null value: should throw NPE.
893      */

894     public void testAddNullPropertyValue()
895     {
896         try
897         {
898             bean.set("nullProperty", null);
899         }
900         catch (NullPointerException JavaDoc e)
901         {
902             return;
903         }
904         catch (Throwable JavaDoc t)
905         {
906             fail("Threw " + t + " instead of NullPointerException");
907             return;
908         }
909         fail("Should have thrown NullPointerException");
910     }
911
912     /**
913      * Test the retrieval of a non-existent property.
914      */

915     public void testGetNonExistentProperty()
916     {
917         try
918         {
919             bean.get("nonexistProperty");
920         }
921         catch (IllegalArgumentException JavaDoc e)
922         {
923             return;
924         }
925         catch (Exception JavaDoc e)
926         {
927             fail("Threw '" + e + "' instead of java.lang.IllegalArgumentException");
928         }
929
930         fail("Get non-existent property failed to throw java.lang.IllegalArgumentException");
931     }
932
933     /**
934      * Base for testGetDescriptorXxxxx() series of tests.
935      *
936      * @param name Name of the property to be retrieved
937      * @param type Expected class type of this property
938      */

939     protected void testGetDescriptorBase(String JavaDoc name, Class JavaDoc type)
940     {
941         DynaProperty descriptor = null;
942         try
943         {
944             descriptor = bean.getDynaClass().getDynaProperty(name);
945         }
946         catch (Throwable JavaDoc t)
947         {
948             fail("Threw an exception: " + t);
949         }
950
951         assertNotNull("Failed to get descriptor", descriptor);
952         assertEquals("Got incorrect type", type, descriptor.getType());
953     }
954
955     /**
956      * Tests if accessing a non-indexed property using the index
957      * get method throws an IllegalArgumentException as it
958      * should.
959      */

960     public void testNonIndexedPropeties()
961     {
962         ConfigurationDynaBean nested = (ConfigurationDynaBean) bean.get("mappedProperty");
963         try
964         {
965             String JavaDoc value = (String JavaDoc) nested.get("key1");
966             assertEquals("Can find first value", "First Value", value);
967         }
968         catch (Throwable JavaDoc t)
969         {
970             fail("Finding first value threw " + t);
971         }
972
973         try
974         {
975             nested.set("key1", "undefined");
976             assertEquals("Incorrect value returned", "undefined", bean.get("mappedProperty.key1"));
977         }
978         catch (Throwable JavaDoc t)
979         {
980             fail("Finding setting first value threw " + t);
981         }
982     }
983
984     /**
985      * Tests if accessing a non-indexed property using the index
986      * get method throws an IllegalArgumentException as it
987      * should.
988      */

989     public void testNestedPropeties()
990     {
991         try
992         {
993             bean.get("booleanProperty", 0);
994         }
995         catch (IllegalArgumentException JavaDoc e)
996         {
997             return;
998         }
999         catch (Throwable JavaDoc t)
1000        {
1001            fail("Threw " + t + " instead of IllegalArgumentException");
1002            return;
1003        }
1004
1005        fail("Should have thrown IllegalArgumentException");
1006
1007        try
1008        {
1009            bean.set("booleanProperty", 0, new Boolean JavaDoc(true));
1010        }
1011        catch (IllegalArgumentException JavaDoc e)
1012        {
1013            return;
1014        }
1015        catch (Throwable JavaDoc t)
1016        {
1017            fail("Threw " + t + " instead of IllegalArgumentException");
1018            return;
1019        }
1020
1021        fail("Should have thrown IllegalArgumentException");
1022    }
1023
1024
1025}
1026
Popular Tags