KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.HashMap JavaDoc;
11
12 import junit.framework.TestCase;
13
14 import com.inversoft.beans.BeanException;
15 import com.inversoft.beans.NestedBeanProperty;
16 import com.inversoft.util.typeconverter.TypeConversionException;
17
18
19 /**
20  * This class tests the dynamic nature nested bean properties
21  * @author Brian Pontarelli
22  */

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

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

54     public void testDeepPropertyCreate() {
55
56         Bean1 bean = new Bean1();
57         try {
58             NestedBeanProperty first = new NestedBeanProperty("property1.property2.property3", bean.getClass());
59             first.setPropertyValue(bean, "foo", false);
60             assertFalse(first.isStrict());
61             assertTrue("Should be completely filled in",
62                 bean.getProperty1().getProperty2().getProperty3().equals("foo"));
63             assertTrue("Should be foo", first.getPropertyValue(bean).equals("foo"));
64         } catch (BeanException be) {
65             be.printStackTrace();
66             fail(be.toString());
67         } catch (TypeConversionException tce) {
68             fail(tce.toString());
69         }
70     }
71
72     /**
73      * Test the local properties using the nested class
74      */

75     public void testLocalProperty() {
76
77         try {
78             Bean3 bean3 = new Bean3();
79             NestedBeanProperty local = new NestedBeanProperty("property3", bean3.getClass());
80             local.setPropertyValue(bean3, "foo", false);
81             assertTrue("Shoold be set to foo", bean3.getProperty3().equals("foo"));
82             assertTrue("should be foo again", local.getPropertyValue(bean3).equals("foo"));
83         } catch (BeanException be) {
84             fail(be.toString());
85         } catch (TypeConversionException tce) {
86             fail(tce.toString());
87         }
88     }
89
90     /**
91      * Test the nested indexed properties retrieval
92      */

93     public void testIndexedDeepPropertyGet() {
94
95         try {
96             Bean1 bean = new Bean1();
97             Bean2 bean2 = new Bean2();
98             Bean3 bean3 = new Bean3();
99
100             bean.setIndexed(0, bean2);
101             bean2.setProperty2(bean3);
102             bean3.setProperty3("foo");
103             NestedBeanProperty prop = new NestedBeanProperty("indexed.property2.property3", Bean1.class);
104             prop.setStrict(true);
105             String JavaDoc value = (String JavaDoc) prop.getPropertyValue(bean, new int[][]{{0}});
106
107             assertTrue("Should have a value of foo", value != null && value.equals("foo"));
108         } catch (BeanException be) {
109             fail(be.toString());
110         }
111     }
112
113     /**
114      * Test the nested indexed properties retrieval
115      */

116     public void testIndexedDeepPropertyGetCollections() {
117
118         try {
119             Bean1 bean = new Bean1();
120             Bean2 bean2 = new Bean2();
121             Bean3 bean3 = new Bean3();
122             Bean4 bean4 = new Bean4("test");
123
124             bean.setIndexed(0, bean2);
125             bean2.setProperty2(bean3);
126             bean3.setMap(new HashMap JavaDoc());
127             bean3.getMap().put(new Integer JavaDoc(1), bean4);
128             bean4.setString1("test");
129             NestedBeanProperty prop = new NestedBeanProperty("indexed.property2.map.string1", Bean1.class);
130             prop.setStrict(true);
131             String JavaDoc value = (String JavaDoc) prop.getPropertyValue(bean, new int[][]{{0},{},{1}});
132
133             assertEquals("Should have a value of test", "test", value);
134         } catch (BeanException be) {
135             be.printStackTrace();
136             fail(be.toString());
137         }
138     }
139
140     /**
141      * Test the nested indexed properties with no creation
142      */

143     public void testIndexedDeepPropertyNoCreate() {
144
145         try {
146             Bean1 bean = new Bean1();
147             NestedBeanProperty prop = new NestedBeanProperty("indexed.property2.property3", Bean1.class);
148             prop.setStrict(true);
149             prop.setPropertyValue(bean, new int[][]{{0}}, "foo", false);
150             fail("Should have thrown an exception because the indexed property is null");
151         } catch (BeanException be) {
152             //System.err.println("Good! " + be.toString());
153
assertTrue("Should have no root cause or target",
154                 be.getCause() == null && be.getTarget() == null);
155         } catch (TypeConversionException tce) {
156             fail(tce.toString());
157         }
158     }
159
160     /**
161      * Test the nested indexed properties with creation
162      */

163     public void testIndexedDeepPropertyCreate() {
164
165         try {
166             Bean1 bean = new Bean1();
167             NestedBeanProperty prop = new NestedBeanProperty("indexed.property2.property3", Bean1.class);
168             prop.setPropertyValue(bean, new int[][]{{0}}, "foo", false);
169             assertTrue("Should be completely filled in",
170                 bean.getIndexed(0).getProperty2().getProperty3().equals("foo"));
171             assertTrue("Should be foo", prop.getPropertyValue(bean, new int[][]{{0}}).equals("foo"));
172         } catch (BeanException be) {
173             be.printStackTrace();
174             fail("Should NOT have thrown an exception " + be.toString());
175         } catch (TypeConversionException tce) {
176             fail(tce.toString());
177         }
178     }
179
180     /**
181      * Test the overridden constructor
182      */

183     public void testSecondConstructor() {
184
185         try {
186             Bean1 bean = new Bean1();
187             NestedBeanProperty prop = new NestedBeanProperty("indexed.property2.property3", "com.inversoft.beans.test.Bean1");
188             prop.setPropertyValue(bean, new int[][]{{0}}, "foo", false);
189             assertTrue("Should be completely filled in",
190                 bean.getIndexed(0).getProperty2().getProperty3().equals("foo"));
191             assertTrue("Should be foo", prop.getPropertyValue(bean, new int[][]{{0}}).equals("foo"));
192         } catch (BeanException be) {
193             be.printStackTrace();
194             fail("Should NOT have thrown an exception " + be.toString());
195         } catch (TypeConversionException tce) {
196             fail(tce.toString());
197         }
198     }
199
200     /**
201      * Test auto-generation failure
202      */

203     public void testAutoGenerationFailure() {
204
205         try {
206             Bean1 bean = new Bean1();
207             NestedBeanProperty prop = new NestedBeanProperty("property4.string1", Bean1.class);
208
209             prop.setPropertyValue(bean, "foo");
210             fail("Should have failed because Bean4 doesn't have a default constructor");
211         } catch (BeanException be) {
212             assertTrue("Should have a root cause of InstantiationException",
213                 be.getCause() != null && be.getCause() instanceof InstantiationException JavaDoc);
214         }
215     }
216 }
Popular Tags