KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > blocks > RpcDispatcherUnicastMethodExceptionTest


1 package org.jgroups.blocks;
2
3 import junit.framework.Test;
4 import junit.framework.TestSuite;
5
6 import org.jgroups.Channel;
7 import org.jgroups.ChannelException;
8 import org.jgroups.TimeoutException;
9 import org.jgroups.tests.ChannelTestBase;
10
11 /**
12  * @author Bela Ban
13  * @version $Id: RpcDispatcherUnicastMethodExceptionTest.java,v 1.4 2007/01/22 23:21:26 vlada Exp $
14  */

15 public class RpcDispatcherUnicastMethodExceptionTest extends ChannelTestBase {
16     RpcDispatcher disp;
17     Channel channel;
18
19     protected void setUp() throws Exception JavaDoc {
20         super.setUp();
21         channel=createChannel("A");
22         disp=new RpcDispatcher(channel, null, null, this);
23         channel.connect("demo");
24     }
25
26     protected void tearDown() throws Exception JavaDoc {
27         super.tearDown();
28         disp.stop();
29         channel.close();
30     }
31
32
33     public Object JavaDoc foo() {
34         System.out.println("-- foo()");
35         return "foo(): OK";
36     }
37
38     public Object JavaDoc bar() throws Exception JavaDoc {
39         System.out.println("-- bar()");
40         throw new TimeoutException("this is an exception");
41     }
42
43     public Object JavaDoc foobar() {
44         System.out.println("-- foobar()");
45         throw new IllegalArgumentException JavaDoc("bla bla bla from foobar");
46     }
47
48     public Object JavaDoc foofoobar() {
49         System.out.println("-- foofoobar()");
50         throw new AssertionError JavaDoc("bla bla bla from foofoobar");
51     }
52
53     public void fooWithThrowable() throws Throwable JavaDoc {
54         System.out.println("-- fooWithThrowable()");
55         throw new Throwable JavaDoc("this is an exception");
56     }
57
58
59
60     public void testMethodWithoutException() throws Throwable JavaDoc {
61         Object JavaDoc retval=disp.callRemoteMethod(channel.getLocalAddress(), "foo", null, (Class JavaDoc[])null, GroupRequest.GET_ALL, 5000);
62         System.out.println("retval: " + retval);
63         assertNotNull(retval);
64     }
65
66
67
68     public void testMethodWithException() throws ChannelException {
69         try {
70             Object JavaDoc retval=disp.callRemoteMethod(channel.getLocalAddress(), "bar", null, (Class JavaDoc[])null, GroupRequest.GET_ALL, 5000);
71             System.out.println("retval: " + retval);
72             fail("we should not get here; bar() should throw an exception");
73         }
74         catch(Throwable JavaDoc e) {
75             System.out.println("caught exception (" + e + ") - as expected");
76             assertTrue(e instanceof TimeoutException);
77         }
78     }
79
80     public void testMethodWithException2() throws ChannelException {
81         try {
82             Object JavaDoc retval=disp.callRemoteMethod(channel.getLocalAddress(), "foobar", null, (Class JavaDoc[])null, GroupRequest.GET_ALL, 5000);
83             System.out.println("retval: " + retval);
84             fail("we should not get here; foobar() should throw an exception");
85         }
86         catch(Throwable JavaDoc e) {
87             System.out.println("caught exception (" + e + ") - as expected");
88             assertTrue(e instanceof IllegalArgumentException JavaDoc);
89         }
90     }
91
92     public void testMethodWithError() throws ChannelException {
93         try {
94             Object JavaDoc retval=disp.callRemoteMethod(channel.getLocalAddress(), "foofoobar", null, (Class JavaDoc[])null, GroupRequest.GET_ALL, 5000);
95             System.out.println("retval: " + retval);
96             fail("we should not get here; foofoobar() should throw an exception");
97         }
98         catch(Throwable JavaDoc e) {
99             System.out.println("caught exception (" + e + ") - as expected");
100             assertTrue(e instanceof AssertionError JavaDoc);
101         }
102     }
103
104     public void testMethodWithThrowable() throws ChannelException {
105         try {
106             Object JavaDoc retval=disp.callRemoteMethod(channel.getLocalAddress(), "fooWithThrowable", null, (Class JavaDoc[])null, GroupRequest.GET_ALL, 5000);
107             System.out.println("retval: " + retval);
108             fail("we should not get here; foofoobar() should throw an exception");
109         }
110         catch(Throwable JavaDoc e) {
111             System.out.println("caught exception (" + e + ") - as expected");
112             assertTrue(e instanceof Throwable JavaDoc);
113         }
114     }
115
116
117     public static Test suite() {
118         return new TestSuite(RpcDispatcherUnicastMethodExceptionTest.class);
119     }
120
121
122     public static void main(String JavaDoc[] args) {
123         junit.textui.TestRunner.run(RpcDispatcherUnicastMethodExceptionTest.suite());
124     }
125 }
126
Popular Tags