KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > beans > test > BeanPropertyTest


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.beans.test;
8
9
10 import java.lang.reflect.InvocationTargetException JavaDoc;
11
12 import junit.framework.TestCase;
13
14 import com.inversoft.beans.BeanException;
15 import com.inversoft.beans.BeanProperty;
16 import com.inversoft.beans.IndexedBeanProperty;
17 import com.inversoft.util.typeconverter.TypeConversionException;
18
19
20 /**
21  * This class contains all the tests for the BeanProperty
22  * class.
23  *
24  * @author Brian Pontarelli
25  */

26 public class BeanPropertyTest extends TestCase {
27
28     /**
29      * Constructs a new test case for the BeanProperty class
30      */

31     public BeanPropertyTest(String JavaDoc name) {
32         super(name);
33     }
34
35
36     /**
37      * This tests the read only properties of a JavaBean using the BeanProperty
38      * class.
39      */

40     public void testReadOnlyBeanProperty() {
41
42         // Tests that the read only property returns the correct value
43
try {
44             Bean1 bean1 = new Bean1();
45             BeanProperty prop = create("readOnly", Bean1.class);
46
47             assertTrue("Should return readOnly", prop.getPropertyValue(bean1).equals("readOnly"));
48
49         } catch (BeanException be) {
50             fail(be.toString());
51         }
52
53         // Tests that the read only property fails correctly when being set
54
try {
55             Bean1 bean1 = new Bean1();
56             BeanProperty prop = create("readOnly", Bean1.class);
57
58             // Should fail
59
prop.setPropertyValue(bean1, "foo", true);
60             fail("Should have failed because read-only");
61
62         } catch (BeanException be) {
63             //System.err.println(be.toString());
64
assertTrue("Should NOT have a root cause or target exception because this is read-only",
65                 be.getCause() == null && be.getTarget() == null);
66         } catch (TypeConversionException tce) {
67             fail(tce.toString());
68         }
69     }
70
71     /**
72      * This tests the instantiation in the BaseBeanProperty class.
73      */

74     public void testInstantiation() {
75         try {
76             BeanProperty prop = create("readOnly", Bean1.class);
77             Bean1 bean1 = (Bean1) prop.instantiate();
78             assertNotNull(bean1);
79         } catch (BeanException be) {
80             fail(be.toString());
81         }
82     }
83
84     /**
85      * Tests the simple bean property getting and setting operations
86      */

87     public void testSimpleBeanProperty() {
88
89         // Tests the bean property gets set correctly and no object mangling happens
90
try {
91             Bean1 bean1 = new Bean1();
92             BeanProperty prop = create("string1", Bean1.class);
93             String JavaDoc value = "foo";
94
95             // Test the simple value setting
96
prop.setPropertyValue(bean1, value);
97
98             // Test that the value getting is the same object reference
99
assertTrue("Should point to same object", prop.getPropertyValue(bean1) == value);
100         } catch (BeanException be) {
101             fail(be.toString());
102         }
103     }
104
105     /**
106      * Test setting null values into a JavaBean property and also tests the handling
107      * of null values when auto-converting values when setting
108      */

109     public void testNullValue() {
110
111         // Test the simple null value setting
112
try {
113             Bean1 bean1 = new Bean1();
114             BeanProperty prop = create("string1", Bean1.class);
115             String JavaDoc [] values = null;
116
117             prop.setPropertyValue(bean1, values, true);
118         } catch (BeanException be) {
119             fail(be.toString());
120         } catch (TypeConversionException tce) {
121             fail(tce.toString());
122         }
123
124         // Test the non-simple null value setting with conversion
125
try {
126             Bean1 bean1 = new Bean1();
127             BeanProperty prop = create("integer1", Bean1.class);
128             String JavaDoc [] values = null;
129
130             prop.setPropertyValue(bean1, values, true);
131         } catch (BeanException be) {
132             fail(be.toString());
133         } catch (TypeConversionException tce) {
134             fail(tce.toString());
135         }
136
137         // Test the super-non-simple null value setting with conversion and an
138
// empty array
139
try {
140             Bean1 bean1 = new Bean1();
141             BeanProperty prop = create("integer1", Bean1.class);
142             String JavaDoc [] values = new String JavaDoc[0];
143
144             prop.setPropertyValue(bean1, values, true);
145             assertTrue("Should be null", bean1.getInteger1() == null);
146         } catch (BeanException be) {
147             fail(be.toString());
148         } catch (TypeConversionException tce) {
149             fail(tce.toString());
150         }
151
152         // Test the non-simple null value setting with conversion to boolean
153
try {
154             Bean1 bean1 = new Bean1();
155             BeanProperty prop = create("boolean1", Bean1.class);
156             Object JavaDoc value = null;
157
158             bean1.setBoolean1(true);
159             prop.setPropertyValue(bean1, value, true);
160             assertTrue("Should have been set to false", !bean1.isBoolean1());
161         } catch (BeanException be) {
162             fail(be.toString());
163         } catch (TypeConversionException tce) {
164             fail(tce.toString());
165         }
166
167         // Test the non-simple null value setting with conversion to primitive
168
try {
169             Bean1 bean1 = new Bean1();
170             BeanProperty prop = create("int1", Bean1.class);
171             Object JavaDoc value = null;
172
173             bean1.setInt1(42);
174             prop.setPropertyValue(bean1, value, true);
175             assertTrue("Should have been set to 0", bean1.getInt1() == 0);
176         } catch (BeanException be) {
177             fail(be.toString());
178         } catch (TypeConversionException tce) {
179             fail(tce.toString());
180         }
181
182         // Test the non-simple null value setting with conversion to primitive
183
try {
184             Bean1 bean1 = new Bean1();
185             BeanProperty prop = create("char1", Bean1.class);
186             Object JavaDoc value = null;
187
188             bean1.setChar1('a');
189             prop.setPropertyValue(bean1, value, true);
190             assertTrue("Should have been set to \u0000", bean1.getChar1() == '\u0000');
191         } catch (BeanException be) {
192             fail(be.toString());
193         } catch (TypeConversionException tce) {
194             fail(tce.toString());
195         }
196     }
197
198     /**
199      * Tests the auto-conversion of something simple like string to Integer
200      */

201     public void testAutoConversion() {
202
203         // Tests a simple automatic conversion from a string to an integer
204
try {
205             Bean1 bean1 = new Bean1();
206             BeanProperty prop = create("integer1", Bean1.class);
207             String JavaDoc value = "1024";
208
209             prop.setPropertyValue(bean1, value, true);
210
211             assertTrue("Should have converted value to new Integer(1024)",
212                        bean1.getInteger1().intValue() == 1024);
213         } catch (BeanException be) {
214             fail(be.toString());
215         } catch (TypeConversionException tce) {
216             fail(tce.toString());
217         }
218     }
219
220     /**
221      * Tests the overridden constructor
222      */

223     public void testSecondConstructor() {
224
225         try {
226             Bean1 bean1 = new Bean1();
227             BeanProperty prop = create("integer1", "com.inversoft.beans.test.Bean1");
228             String JavaDoc value = "1024";
229
230             prop.setPropertyValue(bean1, value, true);
231
232             assertTrue("Should have converted value to new Integer(1024)",
233                        bean1.getInteger1().intValue() == 1024);
234         } catch (BeanException be) {
235             fail(be.toString());
236         } catch (TypeConversionException tce) {
237             fail(tce.toString());
238         }
239     }
240
241     /**
242      * Tests the overridden constructor failure
243      */

244     public void testSecondConstructorFailure() {
245
246         try {
247             /*BeanProperty prop =*/ create("integer1", "com.inversoft.beans.test.NotAClass");
248             fail("Should have failed because class String is incorrect");
249         } catch (BeanException be) {
250             assertTrue("Should have a root cause of ClassNotFoundException",
251                 be.getCause() != null && be.getCause() instanceof ClassNotFoundException JavaDoc);
252         }
253     }
254
255     /**
256      * Tests the invalid property failure
257      */

258     public void testInvalidProperty() {
259
260         try {
261             /*BeanProperty prop =*/ create("notAProperty", Bean1.class);
262             fail("Should have failed because notAProperty does not exist");
263         } catch (BeanException be) {
264             assertTrue("Should have a root cause of NoSuchMethodException",
265                 be.getCause() != null && be.getCause() instanceof NoSuchMethodException JavaDoc);
266             assertTrue("Should NOT have a target", be.getTarget() == null);
267         }
268
269         /*
270         */

271         try {
272             /*BeanProperty prop =*/ create(" ", Bean1.class);
273             fail("Should have failed because the property is white space");
274         } catch (BeanException be) {
275             //System.err.println(be.toString());
276
assertTrue("Should NOT have a root cause", be.getCause() == null);
277             assertTrue("Should NOT have a target", be.getTarget() == null);
278         }
279
280         try {
281             /*BeanProperty prop =*/ create(" a", Bean1.class);
282             fail("Should have failed because the property starts with white space");
283         } catch (BeanException be) {
284             //System.err.println(be.toString());
285
assertTrue("Should have a root cause of NoSuchMethodException",
286                 be.getCause() != null && be.getCause() instanceof NoSuchMethodException JavaDoc);
287             assertTrue("Should NOT have a target", be.getTarget() == null);
288         }
289
290         try {
291             /*BeanProperty prop =*/ create("", Bean1.class);
292             fail("Should have failed because the property is empty");
293         } catch (BeanException be) {
294             //System.err.println(be.toString());
295
assertTrue("Should NOT have a root cause", be.getCause() == null);
296             assertTrue("Should NOT have a target", be.getTarget() == null);
297         }
298     }
299
300     /**
301      * Tests the property that throws exception failure
302      */

303     public void testExceptionalProperty() {
304
305         try {
306             Bean1 bean1 = new Bean1();
307             BeanProperty prop = create("throwsAnException", Bean1.class);
308
309             prop.getPropertyValue(bean1);
310             fail("Should have failed because throwsAnException throws a TestException");
311         } catch (BeanException be) {
312             assertTrue("Should have a root cause of InvocationTargetException",
313                 be.getCause() instanceof InvocationTargetException JavaDoc);
314             assertTrue("Should have a target of TestException",
315                 be.getTarget() instanceof TestException);
316         }
317
318         try {
319             Bean1 bean1 = new Bean1();
320             BeanProperty prop = create("throwsAnException", Bean1.class);
321
322             prop.setPropertyValue(bean1, "foo");
323             fail("Should have failed because throwsAnException throws a TestException");
324         } catch (BeanException be) {
325             assertTrue("Should have a root cause of InvocationTargetException",
326                 be.getCause() instanceof InvocationTargetException JavaDoc);
327             assertTrue("Should have a target of TestException",
328                 be.getTarget() instanceof TestException);
329         }
330     }
331
332     /**
333      * Tests the property that is not accessible
334      */

335     public void testNonAccessibleProperty() {
336
337         try {
338             /*BeanProperty prop =*/ create("notAccessible", Bean1.class);
339             fail("Should have failed because notAccessible is private");
340         } catch (BeanException be) {
341             assertTrue("Should have a root cause of NoSuchMethodException",
342                 be.getCause() instanceof NoSuchMethodException JavaDoc);
343         }
344     }
345
346     /**
347      * Tests the index methods fail
348      */

349     public void testIndexOnly() {
350
351         try {
352             BeanProperty prop = create("string1", Bean1.class);
353             prop.getPropertyValue(new Bean1(), "key");
354             fail("Should have failed because key operation not supported");
355         } catch (BeanException be) {
356             // Expected
357
}
358     }
359
360     /**
361      * Test that the cache lookup works correctly
362      */

363     public void testCache() {
364         try {
365             BeanProperty bp = BeanProperty.getInstance("string1", Bean1.class);
366             BeanProperty bp2 = BeanProperty.getInstance("string1", Class.forName("com.inversoft.beans.test.Bean1"));
367             BeanProperty bp3 = BeanProperty.getInstance("string1", new Bean1().getClass());
368
369             assertSame("Shoudl be same object instance", bp, bp2);
370             assertSame("Shoudl be same object instance", bp, bp3);
371         } catch (BeanException be) {
372             fail(be.toString());
373         } catch (Exception JavaDoc e) {
374             fail(e.toString());
375         }
376
377         try {
378             BeanProperty.getInstance("", Bean1.class);
379             fail("Should have failed");
380         } catch (BeanException be) {
381             // Expected
382
}
383
384         try {
385             BeanProperty.getInstance("foo.bar", Bean1.class);
386             fail("Should have failed");
387         } catch (BeanException be) {
388             // Expected
389
}
390
391         // Since these objects share a cache, verify that they failed properly
392
try {
393             IndexedBeanProperty.getIndexedInstance("stringIndexed", Bean1.class);
394             BeanProperty.getInstance("stringIndexed", Bean1.class);
395             fail("Should have failed");
396         } catch (BeanException be) {
397             // Expected
398
}
399     }
400
401     /**
402      * Creates the BeanProperty to test. This can be overridden in sub-classes
403      * to test other types of NestedBeanProperties.
404      */

405     protected BeanProperty create(String JavaDoc property, Class JavaDoc bean)
406     throws BeanException {
407         return new BeanProperty(property, bean);
408     }
409
410     /**
411      * Creates the BeanProperty to test. This can be overridden in sub-classes
412      * to test other types of NestedBeanProperties.
413      */

414     protected BeanProperty create(String JavaDoc property, String JavaDoc bean)
415     throws BeanException {
416         return new BeanProperty(property, bean);
417     }
418 }
Popular Tags