KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > util > test > ReflectionToolsTest


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.util.test;
8
9
10 import java.lang.reflect.Method JavaDoc;
11 import java.security.Permission JavaDoc;
12 import java.util.AbstractList JavaDoc;
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15
16 import junit.framework.TestCase;
17
18 import com.inversoft.util.BaseException;
19 import com.inversoft.util.ReflectionException;
20 import com.inversoft.util.ReflectionTools;
21
22
23 /**
24  * This class contains all the tests for the ReflectionTools
25  * class. This should not directly test any other class.
26  *
27  * @author Brian Pontarelli
28  */

29 public class ReflectionToolsTest extends TestCase {
30
31     public ReflectionToolsTest(String JavaDoc name) {
32         super(name);
33     }
34
35
36     public void testFindClass() {
37         try {
38             assertSame(ReflectionTools.class,
39                 ReflectionTools.findClass("com.inversoft.util.ReflectionTools"));
40             assertSame(ReflectionTools.class,
41                 ReflectionTools.findClass("ReflectionTools", "com.inversoft.util"));
42             assertSame(ReflectionTools.class,
43                 ReflectionTools.findClass("com.inversoft.util.ReflectionTools", null));
44         } catch (ReflectionException re) {
45             fail(re.toString());
46         }
47
48         try {
49             ReflectionTools.findClass("bad.class");
50             fail("Should have failed");
51         } catch (ReflectionException e) {
52             // Expected
53
}
54
55         try {
56             ReflectionTools.findClass(null);
57             fail("Should have failed");
58         } catch (Throwable JavaDoc t) {
59             // Expected
60
assertSame(AssertionError JavaDoc.class, t.getClass());
61         }
62     }
63
64     public void testGetMethod() {
65         try {
66             Method JavaDoc m = ReflectionTools.getMethod(Bean.class, "getName", new Class JavaDoc[]{});
67             assertEquals("getName", m.getName());
68             assertEquals(0, m.getParameterTypes().length);
69         } catch (ReflectionException re) {
70             fail(re.toString());
71         }
72
73         try {
74             ReflectionTools.getMethod(Bean.class, "bad", new Class JavaDoc[]{});
75             fail("Should have failed");
76         } catch (ReflectionException re) {
77             assertEquals(NoSuchMethodException JavaDoc.class, re.getCause().getClass());
78         }
79
80         SecurityManager JavaDoc old = System.getSecurityManager();
81         try {
82             SecurityManager JavaDoc sm = new SecurityManager JavaDoc() {
83                 public void checkMemberAccess(Class JavaDoc clazz, int which) {
84                     throw new SecurityException JavaDoc();
85                 }
86
87                 public void checkPermission(Permission JavaDoc p) {
88                     // No-op
89
}
90             };
91
92             System.setSecurityManager(sm);
93             ReflectionTools.getMethod(Bean.class, "getName", new Class JavaDoc[]{});
94             fail("Should have failed");
95         } catch (ReflectionException re) {
96             // Expected
97
assertEquals(SecurityException JavaDoc.class, re.getCause().getClass());
98         } finally {
99             System.setSecurityManager(old);
100         }
101
102         old = System.getSecurityManager();
103         try {
104             SecurityManager JavaDoc sm = new SecurityManager JavaDoc() {
105                 public void checkMemberAccess(Class JavaDoc clazz, int which) {
106                     throw new SecurityException JavaDoc();
107                 }
108
109                 public void checkPermission(Permission JavaDoc p) {
110                     // No-op
111
}
112             };
113
114             System.setSecurityManager(sm);
115             ReflectionTools.getMethod(Bean.class, "setName", new Class JavaDoc[]{String JavaDoc.class});
116             fail("Should have failed");
117         } catch (ReflectionException re) {
118             // Expected
119
assertEquals(SecurityException JavaDoc.class, re.getCause().getClass());
120         } finally {
121             System.setSecurityManager(old);
122         }
123     }
124
125     public void testGetMethods() {
126         try {
127             Method JavaDoc[] m = ReflectionTools.getMethods(Bean.class);
128             assertEquals(14, m.length);
129             assertEquals(Bean.class, m[0].getDeclaringClass());
130         } catch (ReflectionException re) {
131             fail(re.toString());
132         }
133
134         SecurityManager JavaDoc old = System.getSecurityManager();
135         try {
136             SecurityManager JavaDoc sm = new SecurityManager JavaDoc() {
137                 public void checkMemberAccess(Class JavaDoc clazz, int which) {
138                     throw new SecurityException JavaDoc();
139                 }
140
141                 public void checkPermission(Permission JavaDoc p) {
142                     // No-op
143
}
144             };
145
146             System.setSecurityManager(sm);
147             ReflectionTools.getMethods(Bean.class);
148             fail("Should have failed");
149         } catch (ReflectionException re) {
150             // Expected
151
assertEquals(SecurityException JavaDoc.class, re.getCause().getClass());
152         } finally {
153             System.setSecurityManager(old);
154         }
155     }
156
157     public void testInvokeBadClass() {
158
159         try {
160             Method JavaDoc getter = ReflectionTools.getMethod(Bean.class, "getName", new Class JavaDoc[]{});
161             ReflectionTools.invokeMethod(getter, new FileToolsTest("foo"), new Object JavaDoc[0]);
162             fail("Should have failed");
163         } catch (ReflectionException re) {
164             System.err.println(re.toString());
165         }
166     }
167
168     public void testInvokeBadParamters() {
169         try {
170             Method JavaDoc getter = ReflectionTools.getMethod(Bean.class, "getName", new Class JavaDoc[]{});
171             ReflectionTools.invokeMethod(getter, new Bean(), new Object JavaDoc[]{"foo"});
172             fail("Should have failed");
173         } catch (ReflectionException re) {
174             System.err.println(re.toString());
175         }
176     }
177
178     public void testInvokeException() {
179         try {
180             Method JavaDoc getter = ReflectionTools.getMethod(Bean.class,
181                 "throwCheckedMethod", new Class JavaDoc[]{});
182             ReflectionTools.invokeMethod(getter, new Bean(), new Object JavaDoc[0]);
183             fail("Should have failed");
184         } catch (ReflectionException re) {
185             assertEquals(BaseException.class, re.getTarget().getClass());
186         }
187
188         try {
189             Method JavaDoc getter = ReflectionTools.getMethod(Bean.class,
190                 "throwUncheckedMethod", new Class JavaDoc[]{});
191             ReflectionTools.invokeMethod(getter, new Bean(), new Object JavaDoc[0]);
192             fail("Should have failed");
193         } catch (ReflectionException re) {
194             fail("Shouldn't have thrown this");
195         } catch (NullPointerException JavaDoc npe) {
196             // Expected
197
}
198
199         try {
200             Method JavaDoc getter = ReflectionTools.getMethod(Bean.class,
201                 "assertMethod", new Class JavaDoc[]{});
202             ReflectionTools.invokeMethod(getter, new Bean(), new Object JavaDoc[0]);
203             fail("Should have failed");
204         } catch (ReflectionException re) {
205             fail("Shouldn't have thrown this");
206         } catch (Error JavaDoc e) {
207             assertTrue(e instanceof AssertionError JavaDoc);
208         }
209
210         try {
211             Method JavaDoc hidden = Bean.class.getDeclaredMethod("hidden", new Class JavaDoc[0]);
212             ReflectionTools.invokeMethod(hidden, new Bean(), new Object JavaDoc[0]);
213             fail("Should have failed");
214         } catch (ReflectionException re) {
215             assertEquals(IllegalAccessException JavaDoc.class, re.getCause().getClass());
216         } catch (Exception JavaDoc e) {
217             fail(e.toString());
218         }
219     }
220
221     public void testFindMethod() {
222
223         Method JavaDoc method = ReflectionTools.findMethod(ReflectionToolsTest.class,
224             "overLoadedMethod", new Class JavaDoc[]{ArrayList JavaDoc.class}, 0);
225         assertNotNull("Should not be null", method);
226         assertEquals("Should be abstract list version", method.getParameterTypes()[0],
227             AbstractList JavaDoc.class);
228
229         Method JavaDoc method1 = ReflectionTools.findMethod(ReflectionToolsTest.class,
230             "overLoadedMethod", new Class JavaDoc[] {HashMap JavaDoc.class}, 0);
231         assertNotNull("Should not be null", method1);
232         assertEquals("Should be Object version", method1.getParameterTypes()[0],
233             Object JavaDoc.class);
234
235         Method JavaDoc method2 = ReflectionTools.findMethod(ReflectionToolsTest.class,
236             "overLoadedMethod2", new Class JavaDoc[] {HashMap JavaDoc.class}, 0);
237         assertNull("Should be null", method2);
238
239         Class JavaDoc [] params = new Class JavaDoc[] {HashMap JavaDoc.class};
240         /*Method method3 = */ReflectionTools.findMethod(ReflectionToolsTest.class,
241             "overLoadedMethod", params, 0);
242         assertEquals("Should be Object version", method1.getParameterTypes()[0],
243             Object JavaDoc.class);
244         assertEquals("Should not have changed array", params.length, 1);
245         assertEquals("Should not have changed array values", params[0], HashMap JavaDoc.class);
246     }
247
248     public void testInstantiate() {
249         try {
250             Object JavaDoc obj = ReflectionTools.instantiate(Bean.class);
251             assertNotNull(obj);
252             assertTrue(obj instanceof Bean);
253         } catch (ReflectionException re) {
254             fail(re.toString());
255         }
256
257         try {
258             ReflectionTools.instantiate(PrivateBean.class);
259             fail("Should have failed");
260         } catch (ReflectionException re) {
261             assertEquals(IllegalAccessException JavaDoc.class, re.getCause().getClass());
262         }
263
264         try {
265             ReflectionTools.instantiate(ExceptionBean.class);
266             fail("Should have failed");
267         } catch (ReflectionException re) {
268             assertEquals(InstantiationException JavaDoc.class, re.getCause().getClass());
269         }
270
271         try {
272             Object JavaDoc obj = ReflectionTools.instantiate("com.inversoft.util.test.Bean");
273             assertNotNull(obj);
274             assertTrue(obj instanceof Bean);
275         } catch (ReflectionException re) {
276             fail(re.toString());
277         }
278
279         try {
280             ReflectionTools.instantiate("bad.class");
281             fail("Should have failed");
282         } catch (ReflectionException re) {
283             assertEquals(ClassNotFoundException JavaDoc.class, re.getCause().getClass());
284         }
285
286         SecurityManager JavaDoc old = System.getSecurityManager();
287         try {
288             SecurityManager JavaDoc sm = new SecurityManager JavaDoc() {
289                 public void checkMemberAccess(Class JavaDoc clazz, int which) {
290                     throw new SecurityException JavaDoc();
291                 }
292
293                 public void checkPermission(Permission JavaDoc p) {
294                     // No-op
295
}
296             };
297
298             System.setSecurityManager(sm);
299             ReflectionTools.instantiate(Bean.class);
300             fail("Should have failed");
301         } catch (ReflectionException re) {
302             // Expected
303
assertEquals(SecurityException JavaDoc.class, re.getCause().getClass());
304         } finally {
305             System.setSecurityManager(old);
306         }
307     }
308
309     public void testConvertToWrapper() {
310         assertEquals(Boolean JavaDoc.class, ReflectionTools.convertToWrapper(Boolean.TYPE));
311         assertEquals(Byte JavaDoc.class, ReflectionTools.convertToWrapper(Byte.TYPE));
312         assertEquals(Character JavaDoc.class, ReflectionTools.convertToWrapper(Character.TYPE));
313         assertEquals(Short JavaDoc.class, ReflectionTools.convertToWrapper(Short.TYPE));
314         assertEquals(Integer JavaDoc.class, ReflectionTools.convertToWrapper(Integer.TYPE));
315         assertEquals(Long JavaDoc.class, ReflectionTools.convertToWrapper(Long.TYPE));
316         assertEquals(Float JavaDoc.class, ReflectionTools.convertToWrapper(Float.TYPE));
317         assertEquals(Double JavaDoc.class, ReflectionTools.convertToWrapper(Double.TYPE));
318         assertNull(ReflectionTools.convertToWrapper(String JavaDoc.class));
319     }
320
321     public void overLoadedMethod(AbstractList JavaDoc param) {
322         // Do nothing
323
}
324
325     public void overLoadedMethod(Object JavaDoc param) {
326         // Do nothing
327
}
328
329     public void overLoadedMethod2(ArrayList JavaDoc param) {
330         // Do nothing
331
}
332 }
Popular Tags