KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > arooa > reflect > BeanUtilsBeanHelperTest


1 /*
2  * (c) Rob Gordon 2005
3  */

4 package org.oddjob.arooa.reflect;
5
6 import java.io.File JavaDoc;
7
8 import junit.framework.TestCase;
9
10 import org.apache.commons.beanutils.BeanUtilsBean;
11 import org.apache.commons.beanutils.DynaBean;
12 import org.apache.commons.beanutils.LazyDynaBean;
13 import org.oddjob.arooa.ArooaException;
14
15 /**
16  *
17  */

18 public class BeanUtilsBeanHelperTest extends TestCase {
19
20     public void testAttributeSetters() throws ArooaException {
21         
22         BeanUtilsBeanHelper bubh = new BeanUtilsBeanHelper(
23                 BeanUtilsBean.getInstance());
24         Object JavaDoc subject = new ThingWithAttributes();
25         try {
26             bubh.setProperty(subject, "one", "test");
27             fail("setOne doesn't exist");
28         } catch (ArooaException be) {
29         }
30         try {
31             bubh.setProperty(subject, "two", "test");
32             fail("setTwo returns non void");
33         } catch (ArooaException be) {
34         }
35         try {
36             bubh.setProperty(subject, "three", "test");
37             fail("setThree takes no args");
38         } catch (ArooaException be) {
39         }
40         
41         try {
42             bubh.setProperty(subject, "four", "test");
43             fail("setFour takes two args");
44         } catch (ArooaException be) {
45         }
46     
47         // test setting null
48
bubh.setProperty(subject, "five", null);
49         
50         // set string on String
51
bubh.setProperty(subject, "five", "test");
52         
53         // set string[] on String
54
bubh.setProperty(subject, "five", new String JavaDoc[] {"test" });
55
56         // set File on String
57
bubh.setProperty(subject, "five", new File JavaDoc("hello.txt"));
58         
59         // test setting null
60
try {
61             bubh.setProperty(subject, "six", null);
62             fail("can't set an int null");
63         } catch (ArooaException e) {
64             // null
65
}
66         
67         // set Integer on int
68
bubh.setProperty(subject, "six", new Integer JavaDoc(2));
69         
70         // set Integer on Integer
71
bubh.setProperty(subject, "seven", new Integer JavaDoc(2));
72         
73         // set String on File
74
bubh.setProperty(subject, "eight", "hello.txt");
75
76         // set String on File[]
77
bubh.setProperty(subject, "nine", "hello.txt");
78         
79         // set File on File[]
80
bubh.setProperty(subject, "nine", new File JavaDoc("hello.txt"));
81         
82         // set Object[] on File[]
83
bubh.setProperty(subject, "nine", new Object JavaDoc[] { "hello.txt" } );
84         
85         // set String[] on File[]
86
bubh.setProperty(subject, "nine", new String JavaDoc[] { "hello.txt" } );
87         
88         // set String on String[]
89
bubh.setProperty(subject, "nine2", "hello.txt");
90         
91         // set File on String[]
92
bubh.setProperty(subject, "nine2", new File JavaDoc("hello.txt"));
93         
94         // set Object[] on String[]
95
bubh.setProperty(subject, "nine2", new Object JavaDoc[] { "hello.txt" } );
96         
97         // set File[] on String[]
98
bubh.setProperty(subject, "nine2", new File JavaDoc[] { new File JavaDoc("hello.txt") } );
99         
100         // set String[] on in[]
101
bubh.setProperty(subject, "ten", new String JavaDoc[] { "1", "2", "3" });
102
103     }
104
105     /**
106      * More complicated cases.
107      *
108      */

109     public void testAttributeSetters2() {
110         BeanUtilsBeanHelper bubh = new BeanUtilsBeanHelper(
111                 BeanUtilsBean.getInstance());
112         Object JavaDoc subject = new ThingWithAttributes();
113         
114         // set Object[] on File[]
115
bubh.setProperty(subject, "nine", new Object JavaDoc[] {
116                 new File JavaDoc("hello.txt"), new File JavaDoc("goodbye.txt") } );
117         
118     }
119     
120     
121     public static class ThingWithAttributes {
122         
123         public int setTwo(String JavaDoc s) {
124             return 0;
125         }
126
127         public void setThree() {}
128
129         public void setFour(String JavaDoc s1, String JavaDoc s2) {}
130
131         public void setFive(String JavaDoc s) {}
132
133         public void setSix(int i) {}
134         
135         public void setSeven(Integer JavaDoc i) {}
136         
137         public void setEight(File JavaDoc f) { }
138         
139         public void setNine(File JavaDoc[] f) {
140             assertEquals( new File JavaDoc("hello.txt"), f[0]);
141         }
142         
143         // extrea test because BeanUtils messes with String arrays.
144
public void setNine2(String JavaDoc[] f) {
145             assertEquals( "hello.txt", f[0]);
146         }
147         
148         public void setTen(int[] i) { }
149         
150         public void set(Object JavaDoc o) { }
151
152     }
153     
154     public void testMappedProperties() throws ArooaException {
155         
156         BeanUtilsBeanHelper bubh = new BeanUtilsBeanHelper(
157                 BeanUtilsBean.getInstance());
158         Object JavaDoc subject = new ThingWithMappedProperties();
159         try {
160             bubh.setMappedProperty(subject, "one", "x", "test");
161             fail("setOne doesn't exist");
162         } catch (ArooaException be) {
163         }
164         // test fails BeanUtils will use setter event if not return type void.
165
// try {
166
// bubh.setMappedProperty(subject, "two", "x", "test");
167
// fail("setTwo returns non void");
168
// } catch (ArooaException be) {
169
// }
170
try {
171             bubh.setMappedProperty(subject, "three", "x", "test");
172             fail("setThree takes no args");
173         } catch (ArooaException be) {
174         }
175         
176         try {
177             bubh.setMappedProperty(subject, "four", "x", "test");
178             fail("setFour takes three args");
179         } catch (ArooaException be) {
180         }
181     
182         // setting null
183
bubh.setMappedProperty(subject, "five", "x", null);
184         
185         // set string on String
186
bubh.setMappedProperty(subject, "five", "x", "test");
187         
188         // set string[] on String
189
bubh.setMappedProperty(subject, "five", "x", new String JavaDoc[] {"test" });
190
191         // set File on String
192
bubh.setMappedProperty(subject, "five", "x", new File JavaDoc("hello.txt"));
193         
194         // set Integer on int
195
bubh.setMappedProperty(subject, "six", "x", new Integer JavaDoc(2));
196         
197     }
198     
199     public static class ThingWithMappedProperties {
200         
201         public int setTwo(String JavaDoc s, String JavaDoc s2) {
202             return 0;
203         }
204         
205         public void setThree() {}
206
207         public void setFour(String JavaDoc s1, String JavaDoc s2, String JavaDoc s3) {}
208
209         public void setFive(String JavaDoc s, String JavaDoc v) {}
210
211         public void setSix(String JavaDoc s, int i) {}
212         
213     }
214
215     public void testPropertyType() {
216         BeanUtilsBeanHelper bubh = new BeanUtilsBeanHelper(
217                 BeanUtilsBean.getInstance());
218         
219         ThingWithAttributes t = new ThingWithAttributes();
220         
221         // none existant - equals object incase DynaBean.
222
assertEquals(Object JavaDoc.class, bubh.getPropertyType(t, "one"));
223         
224         // [] property
225
assertEquals(File JavaDoc[].class, bubh.getPropertyType(t, "nine"));
226         
227         // none existent dyna property
228
DynaBean db = new LazyDynaBean();
229         assertEquals(Object JavaDoc.class, bubh.getPropertyType(db, "foo"));
230
231     }
232 }
233
Popular Tags