KickJava   Java API By Example, From Geeks To Geeks.

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


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 junit.framework.TestCase;
11
12 import com.inversoft.beans.BeanException;
13 import com.inversoft.beans.IndexedBeanProperty;
14 import com.inversoft.util.typeconverter.TypeConversionException;
15
16
17 /**
18  * This class contains all the tests for the IndexedBeanProperty
19  * class in the jBeans package. Any new tests for that class
20  * should be added here.
21  *
22  * @author Brian Pontarelli
23  */

24 public class IndexedBeanPropertyTest extends TestCase {
25
26     /** Constructs a new test case for the IndexedBeanProperty class */
27     public IndexedBeanPropertyTest(String JavaDoc name) {
28         super(name);
29     }
30
31     /**
32      * This tests the read only properties of a JavaBean using the BeanProperty
33      * class.
34      */

35     public void testReadOnlyBeanProperty() {
36
37         // Tests that the read only property returns the correct value
38
try {
39             Bean1 bean1 = new Bean1();
40             IndexedBeanProperty prop = create("readOnlyIndexed", Bean1.class);
41
42             assertEquals("Should return readOnly", prop.getPropertyValue(bean1, 0), "readOnly");
43
44         } catch (BeanException be) {
45             fail(be.toString());
46         }
47
48         // Tests that the read only property fails correctly when being set
49
try {
50             Bean1 bean1 = new Bean1();
51             IndexedBeanProperty prop = create("readOnlyIndexed", Bean1.class);
52
53             // Should fail
54
prop.setPropertyValue(bean1, 0, "foo", true);
55             fail("Should have failed because read-only");
56
57         } catch (BeanException be) {
58             //System.err.println(be.toString());
59
assertTrue("Should NOT have a root cause or target exception because this is read-only",
60                 be.getCause() == null && be.getTarget() == null);
61         } catch (TypeConversionException tce) {
62             fail(tce.toString());
63         }
64     }
65
66     /**
67      * Tests the simple bean property getting and setting operations
68      */

69     public void testSimpleBeanProperty() {
70
71         // Tests the bean property gets set correctly and no object mangling happens
72
try {
73             Bean1 bean1 = new Bean1();
74             IndexedBeanProperty prop = create("stringIndexed", Bean1.class);
75             String JavaDoc value = "foo";
76
77             // Test the simple value setting
78
prop.setPropertyValue(bean1, 0, value);
79
80             // Test that the value getting is the same object reference
81
assertSame("Should point to same object", bean1.getStringIndexed(0), value);
82             assertSame("Should point to same object", prop.getPropertyValue(bean1, 0), value);
83         } catch (BeanException be) {
84             fail(be.toString());
85         }
86     }
87
88     /**
89      * Tests the methods without indices fail.
90      */

91     public void testNonIndicesFail() {
92         try {
93             Bean1 bean1 = new Bean1();
94             IndexedBeanProperty prop = create("stringIndexed", Bean1.class);
95
96             prop.getPropertyValue(bean1);
97             fail("Should have failed");
98         } catch (BeanException be) {
99             // expected
100
}
101
102         try {
103             Bean1 bean1 = new Bean1();
104             IndexedBeanProperty prop = create("stringIndexed", Bean1.class);
105             String JavaDoc value = "foo";
106
107             prop.setPropertyValue(bean1, value);
108             fail("Should have failed");
109         } catch (BeanException be) {
110             // expected
111
}
112
113         try {
114             Bean1 bean1 = new Bean1();
115             IndexedBeanProperty prop = create("stringIndexed", Bean1.class);
116             String JavaDoc value = "foo";
117
118             prop.setPropertyValue(bean1, value, true);
119             fail("Should have failed");
120         } catch (BeanException be) {
121             // expected
122
}
123     }
124
125     /**
126      * Tests the methods with bad indices fail.
127      */

128     public void testBadIndicesFail() {
129         try {
130             Bean1 bean1 = new Bean1();
131             IndexedBeanProperty prop = create("stringIndexed", Bean1.class);
132
133             prop.getPropertyValue(bean1, "badIndex");
134             fail("Should have failed");
135         } catch (BeanException be) {
136             // expected
137
}
138
139         try {
140             Bean1 bean1 = new Bean1();
141             IndexedBeanProperty prop = create("stringIndexed", Bean1.class);
142             String JavaDoc value = "foo";
143
144             prop.setPropertyValue(bean1, "foo", value, true);
145             fail("Should have failed");
146         } catch (BeanException be) {
147             // expected
148
}
149     }
150
151     /**
152      * Test setting null values into a JavaBean property and also tests the handling
153      * of null values when auto-converting values when setting
154      */

155     public void testNullValue() {
156
157         // Test the simple null value setting
158
try {
159             Bean1 bean1 = new Bean1();
160             IndexedBeanProperty prop = create("stringIndexed", Bean1.class);
161             String JavaDoc value = null;
162
163             prop.setPropertyValue(bean1, 0, value, true);
164         } catch (BeanException be) {
165             fail(be.toString());
166         } catch (TypeConversionException tce) {
167             fail(tce.toString());
168         }
169
170         // Test the non-simple null value setting with conversion
171
try {
172             Bean1 bean1 = new Bean1();
173             IndexedBeanProperty prop = create("integerIndexed", Bean1.class);
174             Integer JavaDoc value = null;
175
176             prop.setPropertyValue(bean1, 0, value, true);
177         } catch (BeanException be) {
178             fail(be.toString());
179         } catch (TypeConversionException tce) {
180             fail(tce.toString());
181         }
182
183         // Test the super-non-simple null value setting with conversion and an
184
// empty array
185
try {
186             Bean1 bean1 = new Bean1();
187             IndexedBeanProperty prop = create("integerIndexed", Bean1.class);
188             String JavaDoc [] values = new String JavaDoc[0];
189
190             prop.setPropertyValue(bean1, 0, values, true);
191             assertTrue("Should be null", bean1.getInteger1() == null);
192         } catch (BeanException be) {
193             fail(be.toString());
194         } catch (TypeConversionException tce) {
195             fail(tce.toString());
196         }
197
198         // Test the non-simple null value setting with conversion to boolean
199
try {
200             Bean1 bean1 = new Bean1();
201             IndexedBeanProperty prop = create("booleanIndexed", Bean1.class);
202             Object JavaDoc value = null;
203
204             bean1.setBooleanIndexed(0, true);
205             prop.setPropertyValue(bean1, 0, value, true);
206             assertTrue("Should have been set to false", !bean1.isBoolean1());
207         } catch (BeanException be) {
208             fail(be.toString());
209         } catch (TypeConversionException tce) {
210             fail(tce.toString());
211         }
212
213         // Test the non-simple null value setting with conversion to primitive
214
try {
215             Bean1 bean1 = new Bean1();
216             IndexedBeanProperty prop = create("intIndexed", Bean1.class);
217             Object JavaDoc value = null;
218
219             bean1.setIntIndexed(0, 42);
220             prop.setPropertyValue(bean1, 0, value, true);
221             assertTrue("Should have been set to 0", bean1.getInt1() == 0);
222         } catch (BeanException be) {
223             fail(be.toString());
224         } catch (TypeConversionException tce) {
225             fail(tce.toString());
226         }
227     }
228
229     /**
230      * Tests the auto-conversion of something simple like string to Integer
231      */

232     public void testAutoConversion() {
233
234         // Tests a simple automatic conversion from a string to an integer
235
try {
236             Bean1 bean1 = new Bean1();
237             IndexedBeanProperty prop = create("integerIndexed", Bean1.class);
238             String JavaDoc value = "1024";
239
240             prop.setPropertyValue(bean1, 0, value, true);
241
242             assertTrue("Should have converted value to new Integer(1024)",
243                        bean1.getIntegerIndexed(0).intValue() == 1024);
244         } catch (BeanException be) {
245             fail(be.toString());
246         } catch (TypeConversionException tce) {
247             fail(tce.toString());
248         }
249     }
250
251     /**
252      * Tests the overridden constructor
253      */

254     public void testSecondConstructor() {
255
256         try {
257             Bean1 bean1 = new Bean1();
258             IndexedBeanProperty prop = create("integerIndexed", "com.inversoft.beans.test.Bean1");
259             String JavaDoc value = "1024";
260
261             prop.setPropertyValue(bean1, 0, value, true);
262
263             assertTrue("Should have converted value to new Integer(1024)",
264                        bean1.getIntegerIndexed(0).intValue() == 1024);
265         } catch (BeanException be) {
266             fail(be.toString());
267         } catch (TypeConversionException tce) {
268             fail(tce.toString());
269         }
270     }
271
272     /**
273      * Tests the overridden constructor failure
274      */

275     public void testSecondConstructorFailure() {
276
277         try {
278             /*BeanProperty prop =*/ create("integerIndexed", "com.inversoft.beans.test.NotAClass");
279             fail("Should have failed because class String is incorrect");
280         } catch (BeanException be) {
281             assertTrue("Should have a root cause of ClassNotFoundException",
282                 be.getCause() != null && be.getCause() instanceof ClassNotFoundException JavaDoc);
283         }
284     }
285
286     /**
287      * Tests the invalid property failure
288      */

289     public void testInvalidProperty() {
290
291         try {
292             /*BeanProperty prop =*/ create("notAProperty", Bean1.class);
293             fail("Should have failed because notAProperty does not exist");
294         } catch (BeanException be) {
295             assertTrue("Should have a root cause of NoSuchMethodException",
296                 be.getCause() != null && be.getCause() instanceof NoSuchMethodException JavaDoc);
297             assertTrue("Should NOT have a target", be.getTarget() == null);
298         }
299
300         try {
301             /*BeanProperty prop =*/ create(" ", Bean1.class);
302             fail("Should have failed because the property is white space");
303         } catch (BeanException be) {
304             //System.err.println(be.toString());
305
assertTrue("Should NOT have a root cause", be.getCause() == null);
306             assertTrue("Should NOT have a target", be.getTarget() == null);
307         }
308
309         try {
310             /*BeanProperty prop =*/ create(" a", Bean1.class);
311             fail("Should have failed because the property starts with white space");
312         } catch (BeanException be) {
313             //System.err.println(be.toString());
314
assertTrue("Should have a root cause of NoSuchMethodException",
315                 be.getCause() != null && be.getCause() instanceof NoSuchMethodException JavaDoc);
316             assertTrue("Should NOT have a target", be.getTarget() == null);
317         }
318
319         try {
320             /*BeanProperty prop =*/ create("", Bean1.class);
321             fail("Should have failed because the property is empty");
322         } catch (BeanException be) {
323             //System.err.println(be.toString());
324
assertTrue("Should NOT have a root cause", be.getCause() == null);
325             assertTrue("Should NOT have a target", be.getTarget() == null);
326         }
327     }
328
329     /**
330      * Test that the cache lookup works correctly
331      */

332     public void testCache() {
333         try {
334             IndexedBeanProperty bp = IndexedBeanProperty.getIndexedInstance("indexed", Bean1.class);
335             IndexedBeanProperty bp2 = IndexedBeanProperty.getIndexedInstance("indexed", Class.forName("com.inversoft.beans.test.Bean1"));
336             IndexedBeanProperty bp3 = IndexedBeanProperty.getIndexedInstance("indexed", new Bean1().getClass());
337
338             assertSame("Shoudl be same object instance", bp, bp2);
339             assertSame("Shoudl be same object instance", bp, bp3);
340         } catch (BeanException be) {
341             fail(be.toString());
342         } catch (Exception JavaDoc e) {
343             fail(e.toString());
344         }
345
346         try {
347             IndexedBeanProperty.getIndexedInstance("foo.bar", Bean1.class);
348             fail("Should have failed");
349         } catch (BeanException be) {
350             // expected
351
}
352     }
353
354     /**
355      * Creates the IndexedBeanProperty to test. This can be overridden in sub-classes
356      * to test other types of NestedBeanProperties.
357      */

358     protected IndexedBeanProperty create(String JavaDoc property, Class JavaDoc bean)
359     throws BeanException {
360         return new IndexedBeanProperty(property, bean);
361     }
362
363     /**
364      * Creates the IndexedBeanProperty to test. This can be overridden in sub-classes
365      * to test other types of NestedBeanProperties.
366      */

367     protected IndexedBeanProperty create(String JavaDoc property, String JavaDoc bean)
368     throws BeanException {
369         return new IndexedBeanProperty(property, bean);
370     }
371 }
Popular Tags