KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > remoting > callback > push > InVMPushCallbackTestCase


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.test.remoting.callback.push;
8
9 import java.util.Random JavaDoc;
10 import javax.management.MBeanServer JavaDoc;
11 import org.jboss.remoting.Client;
12 import org.jboss.remoting.InvocationRequest;
13 import org.jboss.remoting.InvokerLocator;
14 import org.jboss.remoting.ServerInvocationHandler;
15 import org.jboss.remoting.ServerInvoker;
16 import org.jboss.remoting.callback.Callback;
17 import org.jboss.remoting.callback.HandleCallbackException;
18 import org.jboss.remoting.callback.InvokerCallbackHandler;
19 import org.jboss.remoting.transport.Connector;
20
21 import junit.framework.TestCase;
22
23 /**
24  * Tests a push callback in the situation when the client, target server and callback server are in
25  * the same VM. No need for DistributedTestCase.
26  *
27  * @author <a HREF="mailto:ovidiu@jboss.org">Ovidiu Feodorov</a>
28  * @version <tt>$Revision: 1.3 $</tt>
29  */

30 public class InVMPushCallbackTestCase extends TestCase
31 {
32    // Constants -----------------------------------------------------
33

34    // Static --------------------------------------------------------
35

36    // Attributes ----------------------------------------------------
37

38    protected InvokerLocator targetServerLocator;
39    protected InvokerLocator callbackServerLocator;
40
41    protected Connector targetServerConnector;
42    protected Connector callbackServerConnector;
43
44    protected ServerInvocationHandlerImpl targetServerInvocationHandler;
45
46    protected Client client;
47    protected InvokerCallbackHandlerImpl callbackHandler;
48
49    // Constructors --------------------------------------------------
50

51    public InVMPushCallbackTestCase(String JavaDoc name)
52    {
53       super(name);
54    }
55
56    // TestCase override ---------------------------------------------
57

58    public void setUp() throws Exception JavaDoc
59    {
60       super.setUp();
61
62       targetServerLocator = new InvokerLocator("socket://localhost:2323");
63       callbackServerLocator = new InvokerLocator("socket://localhost:3434");
64
65       targetServerConnector = new Connector();
66       targetServerConnector.setInvokerLocator(targetServerLocator.getLocatorURI());
67       targetServerConnector.start();
68       targetServerInvocationHandler = new ServerInvocationHandlerImpl();
69       targetServerConnector.addInvocationHandler("TARGET", targetServerInvocationHandler);
70
71       callbackServerConnector = new Connector();
72       callbackServerConnector.setInvokerLocator(callbackServerLocator.getLocatorURI());
73       callbackServerConnector.start();
74       callbackServerConnector.addInvocationHandler("IRRELEVANT", new ServerInvocationHandlerImpl());
75
76       client = new Client(targetServerLocator);
77       callbackHandler = new InvokerCallbackHandlerImpl();
78       try
79       {
80          client.addListener(callbackHandler, callbackServerLocator);
81       }
82       catch(Throwable JavaDoc t)
83       {
84          throw new Exception JavaDoc(t);
85       }
86       client.connect();
87    }
88
89    public void tearDown() throws Exception JavaDoc
90    {
91       callbackServerConnector.stop();
92       targetServerConnector.stop();
93       client.disconnect();
94
95       super.tearDown();
96    }
97
98    // Public --------------------------------------------------------
99

100
101    public void testPushCallback() throws Exception JavaDoc
102    {
103       // send callback, the callback handler must receive it
104
Long JavaDoc arg = new Long JavaDoc(new Random JavaDoc().nextLong());
105       targetServerInvocationHandler.sendCallback(arg);
106
107       assertEquals(arg, callbackHandler.getReceivedArgument());
108    }
109
110    // Package protected ---------------------------------------------
111

112    // Protected -----------------------------------------------------
113

114    // Private -------------------------------------------------------
115

116    // Inner classes -------------------------------------------------
117

118    private class ServerInvocationHandlerImpl implements ServerInvocationHandler
119    {
120
121       private MBeanServer JavaDoc server;
122       private ServerInvoker invoker;
123       private InvokerCallbackHandler theOnlyHandler;
124
125       // ServerInocationHandler implementation ---------------------
126

127       public void setMBeanServer(MBeanServer JavaDoc server)
128       {
129          this.server = server;
130       }
131
132       public void setInvoker(ServerInvoker invoker)
133       {
134          this.invoker = invoker;
135       }
136
137       public Object JavaDoc invoke(InvocationRequest invocation) throws Throwable JavaDoc
138       {
139          return null;
140       }
141
142       public void addListener(InvokerCallbackHandler callbackHandler)
143       {
144          theOnlyHandler = callbackHandler;
145       }
146
147       public void removeListener(InvokerCallbackHandler callbackHandler)
148       {
149          // noop
150
}
151
152       // Public ---------------------------------------------------
153

154       public void sendCallback(Object JavaDoc arg) throws Exception JavaDoc
155       {
156          Callback callback = new Callback(arg);
157          theOnlyHandler.handleCallback(callback);
158       }
159
160    }
161
162    private class InvokerCallbackHandlerImpl implements InvokerCallbackHandler
163    {
164
165       Object JavaDoc receivedArg;
166
167       // InvokerCallbackHandler implementation ---------------------
168

169       public synchronized void handleCallback(Callback callback)
170             throws HandleCallbackException
171       {
172          receivedArg = callback.getParameter();
173       }
174
175       // Public ----------------------------------------------------
176

177       public synchronized Object JavaDoc getReceivedArgument()
178       {
179          return receivedArg;
180       }
181    }
182
183 }
184
Popular Tags