001: package org.jgroups.blocks;
002:
003: import junit.framework.Test;
004: import junit.framework.TestCase;
005: import junit.framework.TestSuite;
006: import org.jgroups.ChannelException;
007: import org.jgroups.JChannel;
008: import org.jgroups.TimeoutException;
009:
010: /**
011: * @author Bela Ban
012: * @version $Id: RpcDispatcherUnicastMethodExceptionTest.java,v 1.3 2006/04/30 13:45:54 belaban Exp $
013: */
014: public class RpcDispatcherUnicastMethodExceptionTest extends TestCase {
015: RpcDispatcher disp;
016: JChannel channel;
017:
018: protected void setUp() throws Exception {
019: super .setUp();
020: channel = new JChannel();
021: disp = new RpcDispatcher(channel, null, null, this );
022: channel.connect("demo");
023: }
024:
025: protected void tearDown() throws Exception {
026: super .tearDown();
027: disp.stop();
028: channel.close();
029: }
030:
031: public Object foo() {
032: System.out.println("-- foo()");
033: return "foo(): OK";
034: }
035:
036: public Object bar() throws Exception {
037: System.out.println("-- bar()");
038: throw new TimeoutException("this is an exception");
039: }
040:
041: public Object foobar() {
042: System.out.println("-- foobar()");
043: throw new IllegalArgumentException("bla bla bla from foobar");
044: }
045:
046: public Object foofoobar() {
047: System.out.println("-- foofoobar()");
048: throw new AssertionError("bla bla bla from foofoobar");
049: }
050:
051: public void fooWithThrowable() throws Throwable {
052: System.out.println("-- fooWithThrowable()");
053: throw new Throwable("this is an exception");
054: }
055:
056: public void testMethodWithoutException() throws Throwable {
057: Object retval = disp.callRemoteMethod(
058: channel.getLocalAddress(), "foo", null, (Class[]) null,
059: GroupRequest.GET_ALL, 5000);
060: System.out.println("retval: " + retval);
061: assertNotNull(retval);
062: }
063:
064: public void testMethodWithException() throws ChannelException {
065: try {
066: Object retval = disp.callRemoteMethod(channel
067: .getLocalAddress(), "bar", null, (Class[]) null,
068: GroupRequest.GET_ALL, 5000);
069: System.out.println("retval: " + retval);
070: fail("we should not get here; bar() should throw an exception");
071: } catch (Throwable e) {
072: System.out.println("caught exception (" + e
073: + ") - as expected");
074: assertTrue(e instanceof TimeoutException);
075: }
076: }
077:
078: public void testMethodWithException2() throws ChannelException {
079: try {
080: Object retval = disp.callRemoteMethod(channel
081: .getLocalAddress(), "foobar", null, (Class[]) null,
082: GroupRequest.GET_ALL, 5000);
083: System.out.println("retval: " + retval);
084: fail("we should not get here; foobar() should throw an exception");
085: } catch (Throwable e) {
086: System.out.println("caught exception (" + e
087: + ") - as expected");
088: assertTrue(e instanceof IllegalArgumentException);
089: }
090: }
091:
092: public void testMethodWithError() throws ChannelException {
093: try {
094: Object retval = disp.callRemoteMethod(channel
095: .getLocalAddress(), "foofoobar", null,
096: (Class[]) null, GroupRequest.GET_ALL, 5000);
097: System.out.println("retval: " + retval);
098: fail("we should not get here; foofoobar() should throw an exception");
099: } catch (Throwable e) {
100: System.out.println("caught exception (" + e
101: + ") - as expected");
102: assertTrue(e instanceof AssertionError);
103: }
104: }
105:
106: public void testMethodWithThrowable() throws ChannelException {
107: try {
108: Object retval = disp.callRemoteMethod(channel
109: .getLocalAddress(), "fooWithThrowable", null,
110: (Class[]) null, GroupRequest.GET_ALL, 5000);
111: System.out.println("retval: " + retval);
112: fail("we should not get here; foofoobar() should throw an exception");
113: } catch (Throwable e) {
114: System.out.println("caught exception (" + e
115: + ") - as expected");
116: assertTrue(e instanceof Throwable);
117: }
118: }
119:
120: public static Test suite() {
121: return new TestSuite(
122: RpcDispatcherUnicastMethodExceptionTest.class);
123: }
124:
125: public static void main(String[] args) {
126: junit.textui.TestRunner
127: .run(RpcDispatcherUnicastMethodExceptionTest.suite());
128: }
129: }
|