KickJava   Java API By Example, From Geeks To Geeks.

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


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.Method JavaDoc;
11 import java.util.List JavaDoc;
12
13 import junit.framework.TestCase;
14
15 import com.inversoft.beans.BeanException;
16 import com.inversoft.beans.JavaBeanTools;
17 import com.inversoft.util.ReflectionException;
18 import com.inversoft.util.ReflectionTools;
19
20
21 /**
22  * This class contains all the tests for the JavaBeanTools class. This should
23  * not directly test any other class.
24  * @author Brian Pontarelli
25  */

26 public class JavaBeanToolsTest extends TestCase {
27
28     public JavaBeanToolsTest(String JavaDoc name) {
29         super(name);
30     }
31
32
33     public void testInheritance() {
34
35         try {
36             Method JavaDoc method = JavaBeanTools.findReadMethod("fromSuper", Bean1.class);
37             assertTrue("method should never be null", method != null);
38         } catch (BeanException be) {
39             fail(be.toString());
40         }
41     }
42
43     /**
44      * Tests the retrieve of a property name from a method
45      */

46     public void testGetPropertyName() {
47         try {
48             Method JavaDoc method = ReflectionTools.getMethod(Bean1.class, "getProperty1", new Class JavaDoc[0]);
49             assertEquals("property1", JavaBeanTools.getPropertyName(method));
50             method = ReflectionTools.getMethod(Bean1.class, "setString1", new Class JavaDoc[]{String JavaDoc.class});
51             assertEquals("string1", JavaBeanTools.getPropertyName(method));
52             method = ReflectionTools.getMethod(Bean1.class, "isBoolean1", new Class JavaDoc[0]);
53             assertEquals("boolean1", JavaBeanTools.getPropertyName(method));
54
55             method = ReflectionTools.getMethod(Bean1.class, "get", new Class JavaDoc[0]);
56             assertNull(JavaBeanTools.getPropertyName(method));
57             method = ReflectionTools.getMethod(Bean1.class, "set", new Class JavaDoc[]{String JavaDoc.class});
58             assertNull(JavaBeanTools.getPropertyName(method));
59             method = ReflectionTools.getMethod(Bean1.class, "is", new Class JavaDoc[0]);
60             assertNull(JavaBeanTools.getPropertyName(method));
61         } catch (ReflectionException e) {
62             fail(e.toString());
63         }
64
65         try {
66             Method JavaDoc method = ReflectionTools.getMethod(Bean1.class, "notAProp", new Class JavaDoc[0]);
67             assertNull(JavaBeanTools.getPropertyName(method));
68         } catch (ReflectionException e) {
69             fail(e.toString());
70         }
71     }
72
73     /**
74      * Tests capitalization
75      */

76     public void testCapitalize() {
77         assertEquals("UpperCase", JavaBeanTools.capitalize("upperCase"));
78         assertEquals("URL", JavaBeanTools.capitalize("URL"));
79         assertEquals(" a", JavaBeanTools.capitalize(" a"));
80         assertEquals("Done", JavaBeanTools.capitalize("Done"));
81     }
82
83     /**
84      * Tests making of JavaBean method names.
85      */

86     public void testMaking() {
87         assertEquals("getFoo", JavaBeanTools.makeGetter("foo"));
88         assertEquals("setFoo", JavaBeanTools.makeSetter("foo"));
89         assertEquals("isFoo", JavaBeanTools.makeIs("foo"));
90         assertEquals("handleFoo", JavaBeanTools.makeHandle("foo"));
91     }
92
93     /**
94      * Tests the checking of methods
95      */

96     public void testChecking() {
97         try {
98             Method JavaDoc method = ReflectionTools.getMethod(Bean1.class, "getProperty1", new Class JavaDoc[0]);
99             assertTrue(JavaBeanTools.isValidGetter(method));
100             method = ReflectionTools.getMethod(Bean1.class, "setString1", new Class JavaDoc[]{String JavaDoc.class});
101             assertTrue(JavaBeanTools.isValidSetter(method));
102             method = ReflectionTools.getMethod(Bean1.class, "isBoolean1", new Class JavaDoc[0]);
103             assertTrue(JavaBeanTools.isValidGetter(method));
104
105             method = ReflectionTools.getMethod(Bean1.class, "get", new Class JavaDoc[0]);
106             assertFalse(JavaBeanTools.isValidGetter(method));
107             assertTrue(JavaBeanTools.isValidGetter(method, false));
108             method = ReflectionTools.getMethod(Bean1.class, "set", new Class JavaDoc[]{String JavaDoc.class});
109             assertFalse(JavaBeanTools.isValidSetter(method));
110             assertTrue(JavaBeanTools.isValidSetter(method, false));
111             method = ReflectionTools.getMethod(Bean1.class, "is", new Class JavaDoc[0]);
112             assertFalse(JavaBeanTools.isValidGetter(method));
113             assertTrue(JavaBeanTools.isValidGetter(method, false));
114
115             method = ReflectionTools.getMethod(Bean1.class, "get", new Class JavaDoc[0]);
116             assertFalse(JavaBeanTools.isValidGetter(method));
117             assertFalse(JavaBeanTools.isValidGetter(method, true));
118             method = ReflectionTools.getMethod(Bean1.class, "set", new Class JavaDoc[]{String JavaDoc.class});
119             assertFalse(JavaBeanTools.isValidSetter(method));
120             assertFalse(JavaBeanTools.isValidSetter(method, true));
121             method = ReflectionTools.getMethod(Bean1.class, "is", new Class JavaDoc[0]);
122             assertFalse(JavaBeanTools.isValidGetter(method));
123             assertFalse(JavaBeanTools.isValidGetter(method, true));
124         } catch (ReflectionException e) {
125             fail(e.toString());
126         }
127
128         // Indexed methods
129
try {
130             Method JavaDoc method = ReflectionTools.getMethod(Bean1.class, "getStringIndexed", new Class JavaDoc[]{Integer.TYPE});
131             assertTrue(JavaBeanTools.isValidIndexedGetter(method));
132             method = ReflectionTools.getMethod(Bean1.class, "setStringIndexed", new Class JavaDoc[]{Integer.TYPE, String JavaDoc.class});
133             assertTrue(JavaBeanTools.isValidIndexedSetter(method));
134             method = ReflectionTools.getMethod(Bean1.class, "isBooleanIndexed", new Class JavaDoc[]{Integer.TYPE});
135             assertTrue(JavaBeanTools.isValidIndexedGetter(method));
136
137             method = ReflectionTools.getMethod(Bean1.class, "get", new Class JavaDoc[]{Integer.TYPE});
138             assertFalse(JavaBeanTools.isValidIndexedGetter(method));
139             assertTrue(JavaBeanTools.isValidIndexedGetter(method, false));
140             method = ReflectionTools.getMethod(Bean1.class, "set", new Class JavaDoc[]{Integer.TYPE, String JavaDoc.class});
141             assertFalse(JavaBeanTools.isValidIndexedSetter(method));
142             assertTrue(JavaBeanTools.isValidIndexedSetter(method, false));
143             method = ReflectionTools.getMethod(Bean1.class, "is", new Class JavaDoc[]{Integer.TYPE});
144             assertFalse(JavaBeanTools.isValidIndexedGetter(method));
145             assertTrue(JavaBeanTools.isValidIndexedGetter(method, false));
146
147             method = ReflectionTools.getMethod(Bean1.class, "get", new Class JavaDoc[]{Integer.TYPE});
148             assertFalse(JavaBeanTools.isValidIndexedGetter(method));
149             assertFalse(JavaBeanTools.isValidIndexedGetter(method, true));
150             method = ReflectionTools.getMethod(Bean1.class, "set", new Class JavaDoc[]{Integer.TYPE, String JavaDoc.class});
151             assertFalse(JavaBeanTools.isValidIndexedSetter(method));
152             assertFalse(JavaBeanTools.isValidIndexedSetter(method, true));
153             method = ReflectionTools.getMethod(Bean1.class, "is", new Class JavaDoc[]{Integer.TYPE});
154             assertFalse(JavaBeanTools.isValidIndexedGetter(method));
155             assertFalse(JavaBeanTools.isValidIndexedGetter(method, true));
156         } catch (ReflectionException e) {
157             fail(e.toString());
158         }
159     }
160
161     /**
162      * Tests the finding of methods.
163      */

164     public void testFind() {
165         try {
166             Method JavaDoc m1 = ReflectionTools.getMethod(Bean1.class, "getProperty1", new Class JavaDoc[0]);
167             Method JavaDoc m2 = JavaBeanTools.findReadMethod("property1", Bean1.class);
168             assertNotNull(m2);
169             assertEquals(m1, m2);
170             m1 = ReflectionTools.getMethod(Bean1.class, "setString1", new Class JavaDoc[]{String JavaDoc.class});
171             m2 = JavaBeanTools.findWriteMethod("string1", Bean1.class, String JavaDoc.class);
172             assertNotNull(m2);
173             assertEquals(m1, m2);
174             m1 = ReflectionTools.getMethod(Bean1.class, "isBoolean1", new Class JavaDoc[0]);
175             m2 = JavaBeanTools.findReadMethod("boolean1", Bean1.class);
176             assertNotNull(m2);
177             assertEquals(m1, m2);
178         } catch (ReflectionException e) {
179             fail(e.toString());
180         }
181
182         // Indexed methods
183
try {
184             Method JavaDoc m1 = ReflectionTools.getMethod(Bean1.class, "getStringIndexed", new Class JavaDoc[]{Integer.TYPE});
185             Method JavaDoc m2 = JavaBeanTools.findIndexedReadMethod("stringIndexed", Bean1.class);
186             assertNotNull(m2);
187             assertEquals(m1, m2);
188             m1 = ReflectionTools.getMethod(Bean1.class, "setStringIndexed", new Class JavaDoc[]{Integer.TYPE, String JavaDoc.class});
189             m2 = JavaBeanTools.findIndexedWriteMethod("stringIndexed", Bean1.class, String JavaDoc.class);
190             assertNotNull(m2);
191             assertEquals(m1, m2);
192             m1 = ReflectionTools.getMethod(Bean1.class, "isBooleanIndexed", new Class JavaDoc[]{Integer.TYPE});
193             m2 = JavaBeanTools.findIndexedReadMethod("booleanIndexed", Bean1.class);
194             assertNotNull(m2);
195             assertEquals(m1, m2);
196         } catch (ReflectionException e) {
197             fail(e.toString());
198         }
199
200         // Bad methods
201
try {
202             JavaBeanTools.findReadMethod("badProp", Bean1.class);
203             fail("Should have failed");
204         } catch (BeanException be) {
205             // expected
206
}
207         try {
208             JavaBeanTools.findWriteMethod("badProp", Bean1.class, String JavaDoc.class);
209             fail("Should have failed");
210         } catch (BeanException be) {
211             // expected
212
}
213         try {
214             JavaBeanTools.findReadMethod("badPropBool", Bean1.class);
215             fail("Should have failed");
216         } catch (BeanException be) {
217             // expected
218
}
219         try {
220             JavaBeanTools.findIndexedReadMethod("badProp", Bean1.class);
221             fail("Should have failed");
222         } catch (BeanException be) {
223             // expected
224
}
225         try {
226             JavaBeanTools.findIndexedWriteMethod("badProp", Bean1.class, String JavaDoc.class);
227             fail("Should have failed");
228         } catch (BeanException be) {
229             // expected
230
}
231         try {
232             JavaBeanTools.findIndexedReadMethod("badPropBool", Bean1.class);
233             fail("Should have failed");
234         } catch (BeanException be) {
235             // expected
236
}
237
238         // Bad calls
239
try {
240             JavaBeanTools.findWriteMethod("badProp", Bean1.class, Void.TYPE);
241             fail("Should have failed");
242         } catch (BeanException be) {
243             // expected
244
}
245         try {
246             JavaBeanTools.findWriteMethod("badProp", Bean1.class, null);
247             fail("Should have failed");
248         } catch (BeanException be) {
249             // expected
250
}
251         try {
252             JavaBeanTools.findWriteMethod(" ", Bean1.class, String JavaDoc.class);
253             fail("Should have failed");
254         } catch (BeanException be) {
255             // expected
256
}
257         try {
258             JavaBeanTools.findIndexedWriteMethod("badProp", Bean1.class, Void.TYPE);
259             fail("Should have failed");
260         } catch (BeanException be) {
261             // expected
262
}
263         try {
264             JavaBeanTools.findIndexedWriteMethod("badProp", Bean1.class, null);
265             fail("Should have failed");
266         } catch (BeanException be) {
267             // expected
268
}
269         try {
270             JavaBeanTools.findIndexedWriteMethod(" ", Bean1.class, String JavaDoc.class);
271             fail("Should have failed");
272         } catch (BeanException be) {
273             // expected
274
}
275     }
276
277     /**
278      * Tests spliting of names
279      */

280     public void testSplit() {
281         JavaBeanTools.NameInfo ni = JavaBeanTools.splitNameBack("foo.bar.john");
282         assertTrue(ni.indexOfDot != -1);
283         assertTrue(ni.nested);
284         assertEquals("foo.bar", ni.nestedPropertyName);
285         assertEquals("john", ni.localPropertyName);
286         ni = JavaBeanTools.splitNameBack("foo");
287         assertTrue(ni.indexOfDot == -1);
288         assertFalse(ni.nested);
289         assertEquals(null, ni.nestedPropertyName);
290         assertEquals("foo", ni.localPropertyName);
291
292         ni = JavaBeanTools.splitNameFront("foo.bar.john");
293         assertTrue(ni.indexOfDot != -1);
294         assertTrue(ni.nested);
295         assertEquals("bar.john", ni.nestedPropertyName);
296         assertEquals("foo", ni.localPropertyName);
297         ni = JavaBeanTools.splitNameFront("foo");
298         assertTrue(ni.indexOfDot == -1);
299         assertFalse(ni.nested);
300         assertEquals(null, ni.nestedPropertyName);
301         assertEquals("foo", ni.localPropertyName);
302
303         String JavaDoc[] names = JavaBeanTools.splitNameComplete("foo.bar.john");
304         assertEquals(3, names.length);
305         assertEquals("foo", names[0]);
306         assertEquals("bar", names[1]);
307         assertEquals("john", names[2]);
308
309         names = JavaBeanTools.splitNameComplete("bar");
310         assertEquals(1, names.length);
311         assertEquals("bar", names[0]);
312
313         names = JavaBeanTools.splitNameComplete("");
314         assertEquals(0, names.length);
315     }
316
317     /**
318      * Tests the retrieval of property information.
319      */

320     public void testPropertyInfo() {
321         new JavaBeanTools.PropertyInfo();
322         try {
323             JavaBeanTools.PropertyInfo pi =
324                 JavaBeanTools.retrievePropertyInfo("prop[0][\"foo\"]");
325             assertEquals("prop", pi.propertyName);
326             assertEquals(new Integer JavaDoc(0), pi.indices.get(0));
327             assertEquals("foo", pi.indices.get(1));
328         } catch (BeanException e) {
329             fail(e.toString());
330         }
331
332         try {
333             JavaBeanTools.retrievePropertyInfo("prop[0");
334             fail("Should have failed");
335         } catch (BeanException e) {
336             // Expected
337
}
338
339         try {
340             JavaBeanTools.retrievePropertyInfo("prop[\"0]");
341             fail("Should have failed");
342         } catch (BeanException e) {
343             // Expected
344
}
345
346         try {
347             JavaBeanTools.retrievePropertyInfo("prop['0]");
348             fail("Should have failed");
349         } catch (BeanException e) {
350             // Expected
351
}
352     }
353
354     /**
355      * Tests the retrieval of property information in a list
356      */

357     public void testPropertyInfoList() {
358         try {
359             List JavaDoc list = JavaBeanTools.retrieveAllPropertyInfo("prop[0][\"foo\"].prop2[9]");
360             assertEquals(2, list.size());
361
362             JavaBeanTools.PropertyInfo pi1 = (JavaBeanTools.PropertyInfo) list.get(0);
363             JavaBeanTools.PropertyInfo pi2 = (JavaBeanTools.PropertyInfo) list.get(1);
364             assertEquals("prop", pi1.propertyName);
365             assertEquals(new Integer JavaDoc(0), pi1.indices.get(0));
366             assertEquals("foo", pi1.indices.get(1));
367
368             assertEquals("prop2", pi2.propertyName);
369             assertEquals(new Integer JavaDoc(9), pi2.indices.get(0));
370         } catch (BeanException e) {
371             fail(e.toString());
372         }
373     }
374
375     /**
376      * Tests the retrieval of property information in a list with single quotes.
377      */

378     public void testPropertyInfoListSinlgeQuote() {
379         try {
380             List JavaDoc list = JavaBeanTools.retrieveAllPropertyInfo("prop[0]['foo'].prop2[9]");
381             assertEquals(2, list.size());
382
383             JavaBeanTools.PropertyInfo pi1 = (JavaBeanTools.PropertyInfo) list.get(0);
384             JavaBeanTools.PropertyInfo pi2 = (JavaBeanTools.PropertyInfo) list.get(1);
385             assertEquals("prop", pi1.propertyName);
386             assertEquals(new Integer JavaDoc(0), pi1.indices.get(0));
387             assertEquals("foo", pi1.indices.get(1));
388
389             assertEquals("prop2", pi2.propertyName);
390             assertEquals(new Integer JavaDoc(9), pi2.indices.get(0));
391         } catch (BeanException e) {
392             fail(e.toString());
393         }
394     }
395
396     /**
397      * Tests the retrieval of property information in a list with no quotes.
398      */

399     public void testPropertyInfoListNoQuote() {
400         try {
401             List JavaDoc list = JavaBeanTools.retrieveAllPropertyInfo("prop[0][foo].prop2[9]");
402             assertEquals(2, list.size());
403
404             JavaBeanTools.PropertyInfo pi1 = (JavaBeanTools.PropertyInfo) list.get(0);
405             JavaBeanTools.PropertyInfo pi2 = (JavaBeanTools.PropertyInfo) list.get(1);
406             assertEquals("prop", pi1.propertyName);
407             assertEquals(new Integer JavaDoc(0), pi1.indices.get(0));
408             assertEquals("foo", pi1.indices.get(1));
409
410             assertEquals("prop2", pi2.propertyName);
411             assertEquals(new Integer JavaDoc(9), pi2.indices.get(0));
412         } catch (BeanException e) {
413             fail(e.toString());
414         }
415     }
416 }
Popular Tags