KickJava   Java API By Example, From Geeks To Geeks.

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


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.NestedBeanProperty;
14 import com.inversoft.util.typeconverter.TypeConversionException;
15
16
17 /**
18  * This class tests the nested bean properties.
19  *
20  * @author Brian Pontarelli
21  */

22 public class NestedBeanPropertyTest extends TestCase {
23
24     /** Construct a new test instance */
25     public NestedBeanPropertyTest(String JavaDoc name) {
26         super(name);
27     }
28
29     /**
30      * Test the no creation setting of property values
31      */

32     public void testDeepPropertyNoCreate() {
33
34         Bean1 bean = new Bean1();
35         try {
36             NestedBeanProperty first = create("property1.property2.property3", bean.getClass());
37             first.setStrict(true);
38             assertTrue(first.isStrict());
39             first.setPropertyValue(bean, "foo", false);
40             fail("Should have failed because the property1 is null");
41         } catch (BeanException be) {
42             //System.err.println("Good! " + be.toString());
43
assertTrue("Should have no root cause or target",
44                 be.getCause() == null && be.getTarget() == null);
45         } catch (TypeConversionException tce) {
46             fail(tce.toString());
47         }
48
49         try {
50             NestedBeanProperty first = create("property1.property2.property3",
51                 bean.getClass(), true);
52             assertTrue(first.isStrict());
53             first.setPropertyValue(bean, "foo", false);
54             fail("Should have failed because the property1 is null");
55         } catch (BeanException be) {
56             //System.err.println("Good! " + be.toString());
57
assertTrue("Should have no root cause or target",
58                 be.getCause() == null && be.getTarget() == null);
59         } catch (TypeConversionException tce) {
60             fail(tce.toString());
61         }
62     }
63
64     /**
65      * Test the creation setting of property values
66      */

67     public void testDeepPropertyCreate() {
68
69         Bean1 bean = new Bean1();
70         try {
71             NestedBeanProperty first = create("property1.property2.property3", bean.getClass());
72             assertFalse(first.isStrict());
73             first.setPropertyValue(bean, "foo", false);
74             assertTrue("Should be completely filled in",
75                 bean.getProperty1().getProperty2().getProperty3().equals("foo"));
76             assertTrue("Should be foo", first.getPropertyValue(bean).equals("foo"));
77             assertEquals(String JavaDoc.class, first.getPropertyType());
78         } catch (BeanException be) {
79             fail(be.toString());
80         } catch (TypeConversionException tce) {
81             fail(tce.toString());
82         }
83     }
84
85     /**
86      * Test the local properties using the nested class
87      */

88     public void testLocalProperty() {
89
90         try {
91             Bean3 bean3 = new Bean3();
92             NestedBeanProperty local = create("property3", bean3.getClass());
93             local.setPropertyValue(bean3, "foo", false);
94             assertTrue("Shoold be set to foo", bean3.getProperty3().equals("foo"));
95             assertTrue("should be foo again", local.getPropertyValue(bean3).equals("foo"));
96         } catch (BeanException be) {
97             fail(be.toString());
98         } catch (TypeConversionException tce) {
99             fail(tce.toString());
100         }
101     }
102
103     /**
104      * Test the nested indexed properties retrieval
105      */

106     public void testIndexedDeepPropertyGet() {
107
108         try {
109             Bean1 bean = new Bean1();
110             Bean2 bean2 = new Bean2();
111             Bean3 bean3 = new Bean3();
112
113             bean.setIndexed(0, bean2);
114             bean2.setProperty2(bean3);
115             bean3.setProperty3("foo");
116             NestedBeanProperty prop = create("indexed[0].property2.property3", Bean1.class);
117             prop.setStrict(true);
118             String JavaDoc value = (String JavaDoc) prop.getPropertyValue(bean);
119
120             assertTrue("Should have a value of foo", value != null && value.equals("foo"));
121         } catch (BeanException be) {
122             fail(be.toString());
123         }
124     }
125
126    /**
127      * Test the nested indexed properties with no creation
128      */

129     public void testIndexedDeepPropertyNoCreate() {
130
131         try {
132             Bean1 bean = new Bean1();
133             NestedBeanProperty prop = create("indexed[0].property2.property3", Bean1.class);
134             prop.setStrict(true);
135             prop.setPropertyValue(bean, "foo", false);
136             fail("Should have thrown an exception because the indexed property is null");
137         } catch (BeanException be) {
138             //System.err.println("Good! " + be.toString());
139
assertTrue("Should have no root cause or target",
140                 be.getCause() == null && be.getTarget() == null);
141         } catch (TypeConversionException tce) {
142             fail(tce.toString());
143         }
144     }
145
146     /**
147      * Test the nested indexed properties with creation
148      */

149     public void testIndexedDeepPropertyCreate() {
150
151         try {
152             Bean1 bean = new Bean1();
153             NestedBeanProperty prop = create("indexed[0].property2.property3", Bean1.class);
154             prop.setPropertyValue(bean, "foo", false);
155             assertTrue("Should be completely filled in",
156                 bean.getIndexed(0).getProperty2().getProperty3().equals("foo"));
157             assertTrue("Should be foo", prop.getPropertyValue(bean).equals("foo"));
158         } catch (BeanException be) {
159             be.printStackTrace();
160             fail("Should NOT have thrown an exception " + be.toString());
161         } catch (TypeConversionException tce) {
162             fail(tce.toString());
163         }
164     }
165
166     /**
167      * Test the overridden constructor
168      */

169     public void testSecondConstructor() {
170
171         try {
172             Bean1 bean = new Bean1();
173             NestedBeanProperty prop = create("indexed[0].property2.property3", "com.inversoft.beans.test.Bean1");
174             prop.setPropertyValue(bean, "foo", false);
175             assertTrue("Should be completely filled in",
176                 bean.getIndexed(0).getProperty2().getProperty3().equals("foo"));
177             assertTrue("Should be foo", prop.getPropertyValue(bean).equals("foo"));
178         } catch (BeanException be) {
179             be.printStackTrace();
180             fail("Should NOT have thrown an exception " + be.toString());
181         } catch (TypeConversionException tce) {
182             fail(tce.toString());
183         }
184     }
185
186     /**
187      * Test auto-generation failure
188      */

189     public void testAutoGenerationFailure() {
190
191         try {
192             Bean1 bean = new Bean1();
193             NestedBeanProperty prop = create("property4.string1", Bean1.class);
194
195             prop.setPropertyValue(bean, "foo");
196             fail("Should have failed because Bean4 doesn't have a default constructor");
197         } catch (BeanException be) {
198             assertTrue("Should have a root cause of InstantiationException",
199                 be.getCause() != null && be.getCause() instanceof InstantiationException JavaDoc);
200         }
201     }
202
203     /**
204      * Creates the NestedBeanProperty to test. This can be overridden in sub-classes
205      * to test other types of NestedBeanProperties.
206      */

207     protected NestedBeanProperty create(String JavaDoc property, Class JavaDoc bean)
208     throws BeanException {
209         return new NestedBeanProperty(property, bean);
210     }
211
212     /**
213      * Creates the NestedBeanProperty to test. This can be overridden in sub-classes
214      * to test other types of NestedBeanProperties.
215      */

216     protected NestedBeanProperty create(String JavaDoc property, Class JavaDoc bean, boolean strict)
217     throws BeanException {
218         return new NestedBeanProperty(property, bean, strict);
219     }
220
221     /**
222      * Creates the NestedBeanProperty to test. This can be overridden in sub-classes
223      * to test other types of NestedBeanProperties.
224      */

225     protected NestedBeanProperty create(String JavaDoc property, String JavaDoc bean)
226     throws BeanException {
227         return new NestedBeanProperty(property, bean);
228     }
229 }
Popular Tags