KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > remoting > exception > server > ServerExceptionTestCase


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.remoting.exception.server;
8
9 import junit.framework.TestCase;
10 import org.jboss.logging.Logger;
11 import org.jboss.remoting.CannotConnectException;
12 import org.jboss.remoting.Client;
13 import org.jboss.remoting.InvocationRequest;
14 import org.jboss.remoting.InvokerCallbackHandler;
15 import org.jboss.remoting.InvokerLocator;
16 import org.jboss.remoting.ServerInvocationHandler;
17 import org.jboss.remoting.ServerInvoker;
18 import org.jboss.remoting.invocation.NameBasedInvocation;
19 import org.jboss.remoting.transport.Connector;
20
21 import javax.management.MBeanServer JavaDoc;
22
23 /**
24  * @author <a HREF="mailto:tom.elrod@jboss.com">Tom Elrod</a>
25  */

26 public class ServerExceptionTestCase extends TestCase
27 {
28    private static final Logger log = Logger.getLogger(ServerExceptionTestCase.class);
29
30    public void setupServer(String JavaDoc locatorURI) throws Exception JavaDoc
31    {
32       InvokerLocator locator = new InvokerLocator(locatorURI);
33       System.out.println("Starting remoting server with locator uri of: " + locatorURI);
34       Connector connector = new Connector();
35       connector.setInvokerLocator(locator.getLocatorURI());
36       connector.start();
37
38       TestInvocationHandler invocationHandler = new TestInvocationHandler();
39       // first parameter is sub-system name. can be any String value.
40
connector.addInvocationHandler("test", invocationHandler);
41    }
42
43    public void testServerException()
44    {
45
46       try
47       {
48          log.debug("running testServerException()");
49
50          InvokerLocator locator = new InvokerLocator("socket://localhost:8823");
51          setupServer(locator.getOriginalURI());
52          Client client = new Client(locator, "test");
53          client.connect();
54
55          log.debug("client.getInvoker().getLocator()" + client.getInvoker().getLocator());
56
57          try
58          {
59             Object JavaDoc ret = client.invoke(new NameBasedInvocation("throwServerException",
60                   new Object JavaDoc[]{"nonserialized"},
61                   new String JavaDoc[]{String JavaDoc.class.getName()}),
62                   null);
63          }
64          catch(NonSerializeTestException nonEx)
65          {
66             log.debug("Expected to get NonSerializable exception and got it.", nonEx);
67             assertTrue(true);
68          }
69          try
70          {
71             Object JavaDoc ret = client.invoke(new NameBasedInvocation("throwServerException",
72                   new Object JavaDoc[]{"serialized"},
73                   new String JavaDoc[]{String JavaDoc.class.getName()}),
74                   null);
75          }
76          catch(SerializedTestException ex)
77          {
78             log.debug("Expected to get Serializable exception and got it.", ex);
79             assertTrue(true);
80          }
81
82       }
83       catch (CannotConnectException cce)
84       {
85          log.debug("Got CannotConnectException.", cce);
86          assertTrue("Did not expect CannotConnectException.", false);
87       }
88       catch (Throwable JavaDoc tr)
89       {
90          tr.printStackTrace();
91          assertTrue("Did not catch server exception as expected.", false);
92       }
93    }
94
95    /**
96     * Simple invocation handler implementation.
97     */

98    public static class TestInvocationHandler implements ServerInvocationHandler
99    {
100       /**
101        * called to handle a specific invocation
102        *
103        * @param invocation
104        * @return
105        * @throws Throwable
106        */

107       public Object JavaDoc invoke(InvocationRequest invocation) throws Throwable JavaDoc
108       {
109          String JavaDoc param = (String JavaDoc)((NameBasedInvocation)invocation.getParameter()).getParameters()[0];
110
111          if(param.equals("serialized"))
112          {
113             throw new SerializedTestException("This is serialized server test exception");
114          }
115          else
116          {
117             throw new NonSerializeTestException("This is a non serialized server test exception");
118          }
119       }
120
121       /**
122        * Adds a callback handler that will listen for callbacks from
123        * the server invoker handler.
124        *
125        * @param callbackHandler
126        */

127       public void addListener(InvokerCallbackHandler callbackHandler)
128       {
129          // NO OP as do not handling callback listeners in this example
130
}
131
132       /**
133        * Removes the callback handler that was listening for callbacks
134        * from the server invoker handler.
135        *
136        * @param callbackHandler
137        */

138       public void removeListener(InvokerCallbackHandler callbackHandler)
139       {
140          // NO OP as do not handling callback listeners in this example
141
}
142
143       /**
144        * set the mbean server that the handler can reference
145        *
146        * @param server
147        */

148       public void setMBeanServer(MBeanServer JavaDoc server)
149       {
150          // NO OP as do not need reference to MBeanServer for this handler
151
}
152
153       /**
154        * set the invoker that owns this handler
155        *
156        * @param invoker
157        */

158       public void setInvoker(ServerInvoker invoker)
159       {
160          // NO OP as do not need reference back to the server invoker
161
}
162
163    }
164
165 }
Popular Tags