KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > MethodCallTest


1 // $Id: MethodCallTest.java,v 1.1 2007/07/04 07:29:33 belaban Exp $
2

3 package org.jgroups.tests;
4
5 import junit.framework.Test;
6 import junit.framework.TestCase;
7 import junit.framework.TestSuite;
8 import org.jgroups.blocks.MethodCall;
9 import org.jgroups.util.Util;
10
11 import java.io.ByteArrayInputStream JavaDoc;
12 import java.io.ByteArrayOutputStream JavaDoc;
13 import java.io.ObjectInputStream JavaDoc;
14 import java.io.ObjectOutputStream JavaDoc;
15 import java.lang.reflect.Method JavaDoc;
16
17
18 /**
19  * @author Bela Ban belaban@yahoo.com
20  * @author <a HREF="mailto:ovidiu@jboss.org">Ovidiu Feodorov</a>
21  * @version $Revision: 1.1 $
22  **/

23
24 public class MethodCallTest extends TestCase {
25
26     Class JavaDoc cl=MethodCallTest.class;
27
28     public MethodCallTest(String JavaDoc name) {
29         super(name);
30     }
31
32
33
34
35     public boolean foo(int a, String JavaDoc b) {
36         System.out.println("test(" + a + ", " + b + ')');
37         return true;
38     }
39
40
41     public void bar(String JavaDoc[] a, String JavaDoc b) {
42         if(a != null) {
43             for(int i=0; i < a.length; i++) {
44                 String JavaDoc s=a[i];
45                 System.out.print(s + ' ');
46             }
47         }
48         else
49             System.out.println("a=null");
50         if(b != null)
51             System.out.println("b=" + b);
52         else
53             System.out.println("b=null");
54     }
55
56
57     public void foobar() {
58         System.out.println("foobar()");
59     }
60
61
62     public void testOld() {
63         try {
64             MethodCall mc=new MethodCall("foo", new Object JavaDoc[]{new Integer JavaDoc(22), "Bela"});
65             assertEquals(mc.invoke(this), Boolean.TRUE);
66         }
67         catch(Throwable JavaDoc t) {
68             fail(t.toString());
69         }
70     }
71
72     public void testOld2() {
73         try {
74             MethodCall mc=new MethodCall("bar", new Object JavaDoc[]{new String JavaDoc[]{"one", "two", "three"}, "Bela"});
75             mc.invoke(this);
76         }
77         catch(Throwable JavaDoc t) {
78             fail(t.toString());
79         }
80     }
81
82     public void testWithNull() {
83         try {
84             MethodCall mc=new MethodCall("foobar", null, (Class JavaDoc[])null);
85             System.out.println("mc: " + mc);
86             mc.invoke(this);
87         }
88         catch(Throwable JavaDoc t) {
89             fail(t.toString());
90         }
91     }
92
93     public void testOldWithNull() {
94         try {
95             MethodCall mc=new MethodCall("bar", new Object JavaDoc[]{new String JavaDoc[]{"one", "two", "three"}, null});
96             mc.invoke(this);
97         }
98         catch(Throwable JavaDoc t) {
99             fail(t.toString());
100         }
101     }
102
103     public void testOldWithNull2() {
104         try {
105             MethodCall mc=new MethodCall("bar", new Object JavaDoc[]{null, "Bela"});
106             mc.invoke(this);
107         }
108         catch(Throwable JavaDoc t) {
109             fail(t.toString());
110         }
111     }
112
113     public void testOldWithNull3() {
114         try {
115             MethodCall mc=new MethodCall("foobar", null);
116             mc.invoke(this);
117         }
118         catch(Throwable JavaDoc t) {
119             fail(t.toString());
120         }
121     }
122
123     public void testOldWithNull4() {
124         try {
125             MethodCall mc=new MethodCall("foobar", new Object JavaDoc[0]);
126             mc.invoke(this);
127         }
128         catch(Throwable JavaDoc t) {
129             fail(t.toString());
130         }
131     }
132
133
134
135
136     public void testMethod() {
137         Method JavaDoc m;
138         try {
139             m=cl.getMethod("foo", new Class JavaDoc[]{int.class, String JavaDoc.class});
140             MethodCall mc=new MethodCall(m, new Object JavaDoc[]{new Integer JavaDoc(22), "Bela"});
141             assertEquals(mc.invoke(this), Boolean.TRUE);
142         }
143         catch(Throwable JavaDoc t) {
144             fail(t.toString());
145         }
146     }
147
148     public void testTypes() {
149         MethodCall mc;
150         mc=new MethodCall("foo", new Object JavaDoc[]{new Integer JavaDoc(35), "Bela"}, new Class JavaDoc[]{int.class, String JavaDoc.class});
151         try {
152             assertEquals(mc.invoke(this), Boolean.TRUE);
153         }
154         catch(Throwable JavaDoc t) {
155             fail(t.toString());
156         }
157     }
158
159
160     public void testTypesWithArray() {
161         MethodCall mc;
162         mc=new MethodCall("bar", new Object JavaDoc[]{new String JavaDoc[]{"one", "two", "three"}, "Bela"},
163                           new Class JavaDoc[]{String JavaDoc[].class, String JavaDoc.class});
164         try {
165             mc.invoke(this);
166         }
167         catch(Throwable JavaDoc t) {
168             fail(t.toString());
169         }
170     }
171
172     public void testTypesWithNullArgument() {
173         MethodCall mc;
174         mc=new MethodCall("bar", new Object JavaDoc[]{new String JavaDoc[]{"one", "two", "three"}, null},
175                           new Class JavaDoc[]{String JavaDoc[].class, String JavaDoc.class});
176         try {
177             mc.invoke(this);
178         }
179         catch(Throwable JavaDoc t) {
180             fail(t.toString());
181         }
182     }
183
184     public void testTypesWithNullArgument2() {
185         MethodCall mc;
186         mc=new MethodCall("bar", new Object JavaDoc[]{new String JavaDoc[]{"one", "two", "three"}, new Object JavaDoc[]{}},
187                           new Class JavaDoc[]{String JavaDoc[].class, String JavaDoc.class});
188         try {
189             mc.invoke(this);
190         }
191         catch(IllegalArgumentException JavaDoc ex) {
192             assertTrue("this was expected", true);
193         }
194         catch(Throwable JavaDoc t) {
195             fail(t.toString());
196         }
197     }
198
199     public void testTypesWithNullArgument3() {
200         MethodCall mc;
201         mc=new MethodCall("foobar", new Object JavaDoc[]{}, new Class JavaDoc[]{});
202         try {
203             mc.invoke(this);
204         }
205         catch(IllegalArgumentException JavaDoc ex) {
206             assertTrue("this was expected", true);
207         }
208         catch(Throwable JavaDoc t) {
209             fail(t.toString());
210         }
211     }
212
213     public void testTypesWithNullArgument4() {
214         MethodCall mc;
215         mc=new MethodCall("foobar", (Object JavaDoc[])null, (Class JavaDoc[])null);
216         try {
217             mc.invoke(this);
218         }
219         catch(IllegalArgumentException JavaDoc ex) {
220             assertTrue("this was expected", true);
221         }
222         catch(Throwable JavaDoc t) {
223             fail(t.toString());
224         }
225     }
226
227     public void testTypesWithNullArgument5() {
228         MethodCall mc;
229         mc=new MethodCall("foobar", new Object JavaDoc[0], new Class JavaDoc[0]);
230         try {
231             mc.invoke(this);
232         }
233         catch(IllegalArgumentException JavaDoc ex) {
234             assertTrue("this was expected", true);
235         }
236         catch(Throwable JavaDoc t) {
237             fail(t.toString());
238         }
239     }
240
241
242     public void testSignature() {
243         MethodCall mc;
244         mc=new MethodCall("foo", new Object JavaDoc[]{new Integer JavaDoc(35), "Bela"},
245                           new String JavaDoc[]{int.class.getName(), String JavaDoc.class.getName()});
246         try {
247             assertEquals(mc.invoke(this), Boolean.TRUE);
248         }
249         catch(Throwable JavaDoc t) {
250             fail(t.toString());
251         }
252     }
253
254
255     public void testBufferSize() throws Exception JavaDoc {
256         int a=10;
257         String JavaDoc b="Bela";
258         MethodCall m=new MethodCall("foo", new Object JavaDoc[]{new Integer JavaDoc(a),b}, new Class JavaDoc[]{int.class, String JavaDoc.class});
259         ByteArrayOutputStream JavaDoc msg_data=new ByteArrayOutputStream JavaDoc();
260         ObjectOutputStream JavaDoc msg_out=new ObjectOutputStream JavaDoc(msg_data);
261         m.writeExternal(msg_out);
262         msg_out.flush();
263         msg_out.close();
264         byte[] data=msg_data.toByteArray();
265         ByteArrayInputStream JavaDoc msg_in_data=new ByteArrayInputStream JavaDoc(data);
266         ObjectInputStream JavaDoc msg_in=new ObjectInputStream JavaDoc(msg_in_data);
267         MethodCall m2=new MethodCall();
268         m2.readExternal(msg_in);
269         System.out.println(m2.getName());
270         System.out.println(m2.getArgs().length);
271     }
272
273     //
274
// OLD
275
//
276

277     public void testOLD() throws Throwable JavaDoc {
278
279         MethodCall methodCall = new MethodCall("someMethod", new Object JavaDoc[] {"abc"});
280
281         Target target = new Target();
282         Object JavaDoc result = methodCall.invoke(target);
283         assertEquals("ABC", result);
284     }
285
286     public void testInheritanceOLD() throws Throwable JavaDoc {
287
288         MethodCall methodCall = new MethodCall("someMethod", new Object JavaDoc[] {"abc"});
289
290         TargetSubclass target = new TargetSubclass();
291         Object JavaDoc result = methodCall.invoke(target);
292         assertEquals("ABC", result);
293     }
294
295     //
296
// METHOD
297
//
298

299     public void testMETHOD() throws Throwable JavaDoc {
300
301         Method JavaDoc method = Target.class.getMethod("someMethod", new Class JavaDoc[] { String JavaDoc.class });
302         MethodCall methodCall = new MethodCall(method, new Object JavaDoc[] {"abc"});
303
304         Target target = new Target();
305         Object JavaDoc result = methodCall.invoke(target);
306         assertEquals("ABC", result);
307     }
308
309     public void testInheritanceMETHOD() throws Throwable JavaDoc {
310
311         Method JavaDoc method = Target.class.getMethod("someMethod", new Class JavaDoc[] { String JavaDoc.class });
312         MethodCall methodCall = new MethodCall(method, new Object JavaDoc[] {"abc"});
313
314         TargetSubclass target = new TargetSubclass();
315         Object JavaDoc result = methodCall.invoke(target);
316         assertEquals("ABC", result);
317     }
318
319     //
320
// TYPES
321
//
322

323     public void testTYPES() throws Throwable JavaDoc {
324
325         MethodCall methodCall = new MethodCall("someMethod",
326                                                new Object JavaDoc[] { "abc" },
327                                                new Class JavaDoc[] { String JavaDoc.class });
328
329         Target target = new Target();
330         Object JavaDoc result = methodCall.invoke(target);
331         assertEquals("ABC", result);
332     }
333
334     public void testInheritanceTYPES() throws Throwable JavaDoc {
335
336         MethodCall methodCall = new MethodCall("someMethod",
337                                                new Object JavaDoc[] { "abc" },
338                                                new Class JavaDoc[] { String JavaDoc.class });
339
340         TargetSubclass target = new TargetSubclass();
341         Object JavaDoc result = methodCall.invoke(target);
342         assertEquals("ABC", result);
343     }
344
345     /**
346      * This tests whether overriden methods are correctly identified and invoked.
347      */

348     public void testOverriddenForTYPES() throws Throwable JavaDoc {
349
350         MethodCall methodCall = new MethodCall("overriddenMethod",
351                                                new Object JavaDoc[] { "abc" },
352                                                new Class JavaDoc[] { String JavaDoc.class });
353
354         TargetSubclass target = new TargetSubclass();
355         Object JavaDoc result = methodCall.invoke(target);
356         assertEquals("TargetSubclassABC", result);
357
358     }
359
360     public void testNoArgumentMethodForTYPES() throws Throwable JavaDoc {
361
362         MethodCall methodCall = new MethodCall("noArgumentMethod", new Object JavaDoc[0], new Class JavaDoc[0]);
363
364         TargetSubclass target = new TargetSubclass();
365         Object JavaDoc result = methodCall.invoke(target);
366         assertEquals("noArgumentMethodResult", result);
367
368     }
369
370
371     //
372
// SIGNATURE
373
//
374

375     public void testSIGNATURE() throws Throwable JavaDoc {
376
377         MethodCall methodCall = new MethodCall("someMethod",
378                                                new Object JavaDoc[] { "abc" },
379                                                new String JavaDoc[] { "java.lang.String" });
380
381         Target target = new Target();
382         Object JavaDoc result = methodCall.invoke(target);
383         assertEquals("ABC", result);
384     }
385
386     public void testInheritanceSIGNATURE() throws Throwable JavaDoc {
387
388         MethodCall methodCall = new MethodCall("someMethod",
389                                                new Object JavaDoc[] { "abc" },
390                                                new String JavaDoc[] { "java.lang.String" });
391
392         TargetSubclass target = new TargetSubclass();
393         Object JavaDoc result = methodCall.invoke(target);
394         assertEquals("ABC", result);
395     }
396
397
398     public void testMarshalling() throws Exception JavaDoc {
399         MethodCall methodCall = new MethodCall("someMethod",
400                                                new Object JavaDoc[] { "abc" },
401                                                new String JavaDoc[] { "java.lang.String" });
402         methodCall.put("name", "Bela");
403         methodCall.put("id", new Integer JavaDoc(322649));
404
405         System.out.println("methodCall: " + methodCall);
406
407         MethodCall m=marshalAndUnmarshal(methodCall);
408         System.out.println("m: " + m);
409         assertEquals(m.get("name"), "Bela");
410         assertEquals(m.get("id"), new Integer JavaDoc(322649));
411     }
412
413
414     private MethodCall marshalAndUnmarshal(MethodCall m) throws Exception JavaDoc {
415         byte[] buf=Util.objectToByteBuffer(m);
416         MethodCall retval=(MethodCall)Util.objectFromByteBuffer(buf);
417         return retval;
418     }
419
420
421     public static Test suite() {
422         TestSuite s=new TestSuite(MethodCallTest.class);
423         return s;
424     }
425
426     public static void main(String JavaDoc[] args) {
427         junit.textui.TestRunner.run(suite());
428     }
429
430     public class Target {
431
432         public String JavaDoc someMethod(String JavaDoc arg) {
433             return arg.toUpperCase();
434         }
435
436         public String JavaDoc overriddenMethod(String JavaDoc arg) {
437             return "Target" + arg.toUpperCase();
438         }
439
440         public String JavaDoc noArgumentMethod() {
441             return "noArgumentMethodResult";
442         }
443     }
444
445     public class TargetSubclass extends Target {
446
447         public String JavaDoc overriddenMethod(String JavaDoc arg) {
448             return "TargetSubclass" + arg.toUpperCase();
449         }
450
451     }
452
453
454 }
455
Popular Tags