KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > jwebunit > util > reflect > MethodInvokerTest


1 /*
2  * User: djoiner
3  * Date: Jul 19, 2002
4  * Time: 2:53:19 PM
5  */

6 package net.sourceforge.jwebunit.util.reflect;
7
8 import junit.framework.TestCase;
9
10 import java.lang.reflect.InvocationTargetException JavaDoc;
11
12
13 public class MethodInvokerTest extends TestCase {
14     private Receiver receiver;
15
16     public MethodInvokerTest(String JavaDoc s) {
17         super(s);
18     }
19
20     protected void setUp() throws Exception JavaDoc {
21         super.setUp();
22         receiver = new Receiver();
23     }
24
25     public void testNoArg() throws Exception JavaDoc {
26         MethodInvoker invoker = new MethodInvoker(receiver, "noArg");
27         invoker.invoke();
28         assertTrue(receiver.noArgCalled);
29     }
30
31     public void testOneArg() throws Exception JavaDoc {
32         MethodInvoker invoker = new MethodInvoker(receiver, "oneArg", "anArg");
33         invoker.invoke();
34         assertTrue(receiver.oneArgCalled);
35     }
36
37     public void testMultipleArgs() throws Exception JavaDoc {
38         MethodInvoker invoker = new MethodInvoker(receiver, "multiArg", new Object JavaDoc[]{"arg1", "arg2"});
39         invoker.invoke();
40         assertTrue(receiver.multiArgCalled);
41     }
42
43     public void testNoMethod() {
44         try {
45             MethodInvoker invoker = new MethodInvoker(receiver, "noMethod");
46             invoker.invoke();
47             fail();
48         } catch (NoSuchMethodException JavaDoc e) {
49         } catch (Exception JavaDoc e) {
50             fail();
51         }
52     }
53
54     public void testMethodThrowsException() throws Exception JavaDoc {
55         MethodInvoker invoker = new MethodInvoker(receiver, "throwRuntime");
56         try {
57             invoker.invoke();
58         } catch (InvocationTargetException JavaDoc e) {
59             assertEquals(e.getTargetException().getClass(), RuntimeException JavaDoc.class);
60         }
61     }
62
63     public void testPrimitiveArgs() throws Exception JavaDoc {
64         MethodInvoker invoker = new MethodInvoker(receiver, "primitiveArg", new Integer JavaDoc(1));
65         invoker.invoke();
66         assertTrue(receiver.primitiveArgCalled);
67     }
68
69     public void testAllPrimitives() throws Exception JavaDoc {
70         Object JavaDoc[] args = new Object JavaDoc[]{new Boolean JavaDoc(true), new Byte JavaDoc((byte) 1),
71                                      new Character JavaDoc('c'), new Double JavaDoc(1),
72                                      new Float JavaDoc(1), new Integer JavaDoc(1),
73                                      new Long JavaDoc(1), new Short JavaDoc((short) 1)};
74         MethodInvoker invoker = new MethodInvoker(receiver, "allPrimitives", args);
75         invoker.invoke();
76         assertTrue(receiver.allPrimitivesCalled);
77     }
78
79     class Receiver {
80         private boolean noArgCalled;
81         private boolean oneArgCalled;
82         private boolean multiArgCalled;
83         private boolean primitiveArgCalled;
84         private boolean allPrimitivesCalled;
85
86         public void noArg() {
87             noArgCalled = true;
88         }
89
90         public void oneArg(String JavaDoc arg) {
91             oneArgCalled = true;
92         }
93
94         public void multiArg(String JavaDoc arg1, String JavaDoc arg2) {
95             multiArgCalled = true;
96         }
97
98         public void primitiveArg(int i) {
99             primitiveArgCalled = true;
100         }
101
102         public void allPrimitives(boolean bool, byte b,
103                                   char c, double d,
104                                   float f, int i,
105                                   long l, short s) {
106             allPrimitivesCalled = true;
107         }
108
109         public void throwRuntime() {
110             throw new RuntimeException JavaDoc();
111         }
112     }
113 }
114
Popular Tags