KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > factory > config > MethodInvokingFactoryBeanTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.beans.factory.config;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.List JavaDoc;
22
23 import junit.framework.TestCase;
24
25 import org.springframework.beans.TypeMismatchException;
26 import org.springframework.beans.support.ArgumentConvertingMethodInvoker;
27 import org.springframework.util.MethodInvoker;
28
29 /**
30  * @author Colin Sampaleanu
31  * @since 21.11.2003
32  */

33 public class MethodInvokingFactoryBeanTests extends TestCase {
34
35     public void testParameterValidation() throws Exception JavaDoc {
36         String JavaDoc validationError = "improper validation of input properties";
37
38         // assert that only static OR non static are set, but not both or none
39
MethodInvokingFactoryBean mcfb = new MethodInvokingFactoryBean();
40         try {
41             mcfb.afterPropertiesSet();
42             fail(validationError);
43         }
44         catch (IllegalArgumentException JavaDoc ex) {
45             // expected
46
}
47
48         mcfb = new MethodInvokingFactoryBean();
49         mcfb.setTargetObject(this);
50         mcfb.setTargetMethod("whatever");
51         try {
52             mcfb.afterPropertiesSet();
53             fail(validationError);
54         }
55         catch (NoSuchMethodException JavaDoc ex) {
56             // expected
57
}
58
59         // bogus static method
60
mcfb = new MethodInvokingFactoryBean();
61         mcfb.setTargetClass(TestClass1.class);
62         mcfb.setTargetMethod("some.bogus.Method.name");
63         try {
64             mcfb.afterPropertiesSet();
65             fail(validationError);
66         }
67         catch (NoSuchMethodException JavaDoc ex) {
68             // expected
69
}
70
71         // bogus static method
72
mcfb = new MethodInvokingFactoryBean();
73         mcfb.setTargetClass(TestClass1.class);
74         mcfb.setTargetMethod("method1");
75         try {
76             mcfb.afterPropertiesSet();
77             fail(validationError);
78         }
79         catch (IllegalArgumentException JavaDoc ex) {
80             // expected
81
}
82
83         // missing method
84
mcfb = new MethodInvokingFactoryBean();
85         mcfb.setTargetObject(this);
86         try {
87             mcfb.afterPropertiesSet();
88             fail(validationError);
89         }
90         catch (IllegalArgumentException JavaDoc ex) {
91             // expected
92
}
93
94         // bogus method
95
mcfb = new MethodInvokingFactoryBean();
96         mcfb.setTargetObject(this);
97         mcfb.setTargetMethod("bogus");
98         try {
99             mcfb.afterPropertiesSet();
100             fail(validationError);
101         }
102         catch (NoSuchMethodException JavaDoc ex) {
103             // expected
104
}
105
106         // static method
107
TestClass1._staticField1 = 0;
108         mcfb = new MethodInvokingFactoryBean();
109         mcfb.setTargetClass(TestClass1.class);
110         mcfb.setTargetMethod("staticMethod1");
111         mcfb.afterPropertiesSet();
112
113         // non-static method
114
TestClass1 tc1 = new TestClass1();
115         mcfb = new MethodInvokingFactoryBean();
116         mcfb.setTargetObject(tc1);
117         mcfb.setTargetMethod("method1");
118         mcfb.afterPropertiesSet();
119     }
120
121     public void testGetObjectType() throws Exception JavaDoc {
122         TestClass1 tc1 = new TestClass1();
123         MethodInvokingFactoryBean mcfb = new MethodInvokingFactoryBean();
124         mcfb = new MethodInvokingFactoryBean();
125         mcfb.setTargetObject(tc1);
126         mcfb.setTargetMethod("method1");
127         mcfb.afterPropertiesSet();
128         assertTrue(int.class.equals(mcfb.getObjectType()));
129
130         mcfb = new MethodInvokingFactoryBean();
131         mcfb.setTargetClass(TestClass1.class);
132         mcfb.setTargetMethod("voidRetvalMethod");
133         mcfb.afterPropertiesSet();
134         Class JavaDoc objType = mcfb.getObjectType();
135         assertTrue(objType.equals(MethodInvokingFactoryBean.VoidType.class));
136
137         // verify that we can call a method with args that are subtypes of the
138
// target method arg types
139
TestClass1._staticField1 = 0;
140         mcfb = new MethodInvokingFactoryBean();
141         mcfb.setTargetClass(TestClass1.class);
142         mcfb.setTargetMethod("supertypes");
143         mcfb.setArguments(new Object JavaDoc[]{new ArrayList JavaDoc(), new ArrayList JavaDoc(), "hello"});
144         mcfb.afterPropertiesSet();
145         mcfb.getObjectType();
146
147         // fail on improper argument types at afterPropertiesSet
148
mcfb = new MethodInvokingFactoryBean();
149         mcfb.setTargetClass(TestClass1.class);
150         mcfb.setTargetMethod("supertypes");
151         mcfb.setArguments(new Object JavaDoc[]{"1", "2", "3"});
152         try {
153             mcfb.afterPropertiesSet();
154         }
155         catch (TypeMismatchException ex) {
156             // expected
157
}
158     }
159
160     public void testGetObject() throws Exception JavaDoc {
161         // singleton, non-static
162
TestClass1 tc1 = new TestClass1();
163         MethodInvokingFactoryBean mcfb = new MethodInvokingFactoryBean();
164         mcfb.setTargetObject(tc1);
165         mcfb.setTargetMethod("method1");
166         mcfb.afterPropertiesSet();
167         Integer JavaDoc i = (Integer JavaDoc) mcfb.getObject();
168         assertEquals(1, i.intValue());
169         i = (Integer JavaDoc) mcfb.getObject();
170         assertEquals(1, i.intValue());
171
172         // non-singleton, non-static
173
tc1 = new TestClass1();
174         mcfb = new MethodInvokingFactoryBean();
175         mcfb.setTargetObject(tc1);
176         mcfb.setTargetMethod("method1");
177         mcfb.setSingleton(false);
178         mcfb.afterPropertiesSet();
179         i = (Integer JavaDoc) mcfb.getObject();
180         assertEquals(1, i.intValue());
181         i = (Integer JavaDoc) mcfb.getObject();
182         assertEquals(2, i.intValue());
183
184         // singleton, static
185
TestClass1._staticField1 = 0;
186         mcfb = new MethodInvokingFactoryBean();
187         mcfb.setTargetClass(TestClass1.class);
188         mcfb.setTargetMethod("staticMethod1");
189         mcfb.afterPropertiesSet();
190         i = (Integer JavaDoc) mcfb.getObject();
191         assertEquals(1, i.intValue());
192         i = (Integer JavaDoc) mcfb.getObject();
193         assertEquals(1, i.intValue());
194
195         // non-singleton, static
196
TestClass1._staticField1 = 0;
197         mcfb = new MethodInvokingFactoryBean();
198         mcfb.setStaticMethod("org.springframework.beans.factory.config.MethodInvokingFactoryBeanTests$TestClass1.staticMethod1");
199         mcfb.setSingleton(false);
200         mcfb.afterPropertiesSet();
201         i = (Integer JavaDoc) mcfb.getObject();
202         assertEquals(1, i.intValue());
203         i = (Integer JavaDoc) mcfb.getObject();
204         assertEquals(2, i.intValue());
205
206         // void return value
207
mcfb = new MethodInvokingFactoryBean();
208         mcfb.setTargetClass(TestClass1.class);
209         mcfb.setTargetMethod("voidRetvalMethod");
210         mcfb.afterPropertiesSet();
211         assertTrue(MethodInvokingFactoryBean.VOID.equals(mcfb.getObject()));
212
213         // now see if we can match methods with arguments that have supertype arguments
214
mcfb = new MethodInvokingFactoryBean();
215         mcfb.setTargetClass(TestClass1.class);
216         mcfb.setTargetMethod("supertypes");
217         mcfb.setArguments(new Object JavaDoc[] {new ArrayList JavaDoc(), new ArrayList JavaDoc(), "hello"});
218         // should pass
219
mcfb.afterPropertiesSet();
220     }
221
222     public void testArgumentConversion() throws Exception JavaDoc {
223         MethodInvokingFactoryBean mcfb = new MethodInvokingFactoryBean();
224         mcfb.setTargetClass(TestClass1.class);
225         mcfb.setTargetMethod("supertypes");
226         mcfb.setArguments(new Object JavaDoc[] {new ArrayList JavaDoc(), new ArrayList JavaDoc(), "hello", "bogus"});
227         try {
228             mcfb.afterPropertiesSet();
229             fail("Matched method with wrong number of args");
230         }
231         catch (NoSuchMethodException JavaDoc ex) {
232             // expected
233
}
234
235         mcfb = new MethodInvokingFactoryBean();
236         mcfb.setTargetClass(TestClass1.class);
237         mcfb.setTargetMethod("supertypes");
238         mcfb.setArguments(new Object JavaDoc[] {new Integer JavaDoc(1), new Integer JavaDoc(2), new Integer JavaDoc(3)});
239         try {
240             mcfb.afterPropertiesSet();
241             Object JavaDoc x = mcfb.getObject();
242             fail("Should have failed on getObject with mismatched argument types");
243         }
244         catch (NoSuchMethodException JavaDoc ex) {
245             // expected
246
}
247
248         mcfb = new MethodInvokingFactoryBean();
249         mcfb.setTargetClass(TestClass1.class);
250         mcfb.setTargetMethod("supertypes2");
251         mcfb.setArguments(new Object JavaDoc[] {new ArrayList JavaDoc(), new ArrayList JavaDoc(), "hello", "bogus"});
252         mcfb.afterPropertiesSet();
253         assertEquals("hello", mcfb.getObject());
254
255         mcfb = new MethodInvokingFactoryBean();
256         mcfb.setTargetClass(TestClass1.class);
257         mcfb.setTargetMethod("supertypes2");
258         mcfb.setArguments(new Object JavaDoc[] {new ArrayList JavaDoc(), new ArrayList JavaDoc(), "hello", new ArrayList JavaDoc()});
259         try {
260             mcfb.afterPropertiesSet();
261             fail("Matched method when shouldn't have matched");
262         }
263         catch (NoSuchMethodException JavaDoc ex) {
264             // expected
265
}
266     }
267
268     public void testInvokeWithNullArgument() throws Exception JavaDoc {
269         MethodInvoker methodInvoker = new MethodInvoker();
270         methodInvoker.setTargetClass(TestClass1.class);
271         methodInvoker.setTargetMethod("nullArgument");
272         methodInvoker.setArguments(new Object JavaDoc[] {null});
273         methodInvoker.prepare();
274         methodInvoker.invoke();
275     }
276
277     public void testInvokeWithIntArgument() throws Exception JavaDoc {
278         ArgumentConvertingMethodInvoker methodInvoker = new ArgumentConvertingMethodInvoker();
279         methodInvoker.setTargetClass(TestClass1.class);
280         methodInvoker.setTargetMethod("intArgument");
281         methodInvoker.setArguments(new Object JavaDoc[] {new Integer JavaDoc(5)});
282         methodInvoker.prepare();
283         methodInvoker.invoke();
284
285         methodInvoker = new ArgumentConvertingMethodInvoker();
286         methodInvoker.setTargetClass(TestClass1.class);
287         methodInvoker.setTargetMethod("intArgument");
288         methodInvoker.setArguments(new Object JavaDoc[] {"5"});
289         methodInvoker.prepare();
290         methodInvoker.invoke();
291     }
292
293     public void testPlainMethodInvoker() throws Exception JavaDoc {
294         // sanity check: singleton, non-static should work
295
TestClass1 tc1 = new TestClass1();
296         MethodInvoker mi = new MethodInvoker();
297         mi.setTargetObject(tc1);
298         mi.setTargetMethod("method1");
299         mi.prepare();
300         Integer JavaDoc i = (Integer JavaDoc) mi.invoke();
301         assertEquals(1, i.intValue());
302
303         // sanity check: check that argument count matching works
304
mi = new MethodInvoker();
305         mi.setTargetClass(TestClass1.class);
306         mi.setTargetMethod("supertypes");
307         mi.setArguments(new Object JavaDoc[] {new ArrayList JavaDoc(), new ArrayList JavaDoc(), "hello"});
308         mi.prepare();
309         assertEquals("hello", mi.invoke());
310
311         // sanity check: check that argument conversion doesn't work with plain MethodInvoker
312
mi = new MethodInvoker();
313         mi.setTargetClass(TestClass1.class);
314         mi.setTargetMethod("supertypes2");
315         mi.setArguments(new Object JavaDoc[] {new ArrayList JavaDoc(), new ArrayList JavaDoc(), "hello", "bogus"});
316         try {
317             mi.prepare();
318             fail("Shouldn't have matched without argument conversion");
319         }
320         catch (NoSuchMethodException JavaDoc ex) {
321             // expected
322
}
323     }
324
325
326     // a test class to work with
327
public static class TestClass1 {
328
329         public static int _staticField1;
330
331         public int _field1 = 0;
332
333         public int method1() {
334             return ++_field1;
335         }
336
337         public static int staticMethod1() {
338             return ++_staticField1;
339         }
340
341         public static void voidRetvalMethod() {
342         }
343
344         public static void nullArgument(Object JavaDoc arg) {
345         }
346
347         public static void intArgument(int arg) {
348         }
349
350         public static String JavaDoc supertypes(Collection JavaDoc c, List JavaDoc l, String JavaDoc s) {
351             return s;
352         }
353
354         public static String JavaDoc supertypes2(Collection JavaDoc c, List JavaDoc l, String JavaDoc s, Integer JavaDoc i) {
355             return s;
356         }
357
358         public static String JavaDoc supertypes2(Collection JavaDoc c, List JavaDoc l, String JavaDoc s, String JavaDoc s2) {
359             return s;
360         }
361     }
362
363 }
364
Popular Tags