KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > remoting > transport > rmi > RMIInvokerClientTest


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.transport.rmi;
8
9 import java.io.ByteArrayInputStream JavaDoc;
10 import java.rmi.server.UID JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.Map JavaDoc;
14 import java.util.Set JavaDoc;
15 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
16 import org.jboss.logging.Logger;
17 import org.jboss.remoting.Client;
18 import org.jboss.remoting.InvokerLocator;
19 import org.jboss.remoting.InvokerRegistry;
20 import org.jboss.remoting.invocation.NameBasedInvocation;
21 import org.jboss.remoting.transport.Connector;
22 import org.jboss.remoting.transport.rmi.RMIServerInvoker;
23 import org.jboss.test.remoting.ComplexReturn;
24 import org.jboss.test.remoting.TestUtil;
25 import org.jboss.test.remoting.transport.mock.MockClientInvoker;
26 import org.jboss.test.remoting.transport.mock.MockInvokerCallbackHandler;
27 import org.jboss.test.remoting.transport.mock.MockServerInvoker;
28 import org.jboss.test.remoting.transport.mock.MockTest;
29 import org.w3c.dom.Document JavaDoc;
30
31 import junit.framework.TestCase;
32
33 /**
34  * This is the actual concrete test for the invoker client.
35  *
36  * @author <a HREF="mailto:telrod@e2technologies.net">Tom Elrod</a>
37  */

38 public class RMIInvokerClientTest extends TestCase
39 {
40
41    private String JavaDoc sessionId = new UID JavaDoc().toString();
42    private Client client;
43    private Connector connector;
44    private InvokerLocator locator;
45    private int port = RMIServerInvoker.DEFAULT_REGISTRY_PORT - 1;
46    private String JavaDoc transport = "rmi";
47
48    private static final Logger log = Logger.getLogger(RMIInvokerClientTest.class);
49
50    public void init(Map JavaDoc metadata)
51    {
52       try
53       {
54          InvokerLocator locator = new InvokerLocator(buildLocatorURI(metadata, this.port));
55          //InvokerLocator locator = new InvokerLocator(getTransport() + "://localhost:" + port);
56
client = new Client(locator, "mock");
57          client.connect();
58       }
59       catch(Exception JavaDoc e)
60       {
61          log.error(e.getMessage(), e);
62       }
63    }
64
65    private String JavaDoc buildLocatorURI(Map JavaDoc metadata, int port)
66    {
67       if(metadata == null || metadata.size() == 0)
68       {
69          return transport + "://localhost:" + port;
70       }
71       else
72       {
73          StringBuffer JavaDoc uriBuffer = new StringBuffer JavaDoc(transport + "://localhost:" + port + "/?");
74
75          Set JavaDoc keys = metadata.keySet();
76          Iterator JavaDoc itr = keys.iterator();
77          while(itr.hasNext())
78          {
79             String JavaDoc key = (String JavaDoc) itr.next();
80             String JavaDoc value = (String JavaDoc) metadata.get(key);
81             uriBuffer.append(key + "=" + value + ",");
82          }
83          return uriBuffer.substring(0, uriBuffer.length() - 1);
84       }
85    }
86
87    private InvokerLocator initServer(Map JavaDoc metadata, int serverPort) throws Exception JavaDoc
88    {
89       if(serverPort < 0)
90       {
91          serverPort = TestUtil.getRandomPort();
92       }
93       log.debug("port = " + serverPort);
94
95       InvokerRegistry.registerInvoker("mock", MockClientInvoker.class, MockServerInvoker.class);
96       connector = new Connector();
97
98       InvokerLocator locator = new InvokerLocator(buildLocatorURI(metadata, serverPort));
99
100       //InvokerLocator locator = new InvokerLocator(transport + "://localhost:" + port);
101
StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
102       buf.append("<?xml version=\"1.0\"?>\n");
103       buf.append("<handlers>\n");
104       buf.append(" <handler subsystem=\"mock\">org.jboss.test.remoting.transport.mock.MockServerInvocationHandler</handler>\n");
105       buf.append("</handlers>\n");
106       Document JavaDoc xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream JavaDoc(buf.toString().getBytes()));
107       connector.setInvokerLocator(locator.getLocatorURI());
108       connector.setConfiguration(xml.getDocumentElement());
109       //connector.create();
110
connector.start();
111       return locator;
112    }
113
114
115    public void setUp() throws Exception JavaDoc
116    {
117       Map JavaDoc metadata = new HashMap JavaDoc();
118       metadata.put(RMIServerInvoker.REGISTRY_PORT_KEY, String.valueOf(port + 1));
119       locator = initServer(metadata, -1);
120       init(metadata);
121    }
122
123    public void tearDown() throws Exception JavaDoc
124    {
125       if(connector != null)
126       {
127          connector.stop();
128          connector.destroy();
129          connector = null;
130       }
131       locator = null;
132       if(client != null)
133       {
134          client.disconnect();
135          client = null;
136       }
137    }
138
139    /**
140     * Test simple invocation and adding of listener with push callback (meaning server
141     * will send callback message when it gets it) to a local callback server
142     *
143     * @throws Throwable
144     */

145    public void testLocalPushCallback() throws Throwable JavaDoc
146    {
147       log.debug("running testLocalPushCallback()");
148
149       sessionId = new UID JavaDoc().toString();
150
151       sessionId = client.getSessionId();
152       MockInvokerCallbackHandler handler = new MockInvokerCallbackHandler(sessionId);
153
154       log.debug("client.getInvoker().getLocator()" + client.getInvoker().getLocator());
155
156       // simple invoke, should return bar
157
Object JavaDoc ret = makeInvocation("foo", "bar");
158       assertTrue("Result of testLocalPushCallback() invocation of foo.", "bar".equals(ret));
159       client.addListener(handler, locator);
160       // invoke which should cause callback
161
ret = makeInvocation("test", "test");
162       // allow time for callback
163
Thread.sleep(3000);
164       log.debug("done sleeping.");
165       int callbacksPerformed = handler.isCallbackReceived();
166       log.debug("callbacksPerformed after adding listener is " + callbacksPerformed);
167       assertTrue("Result of testLocalPushCallback() failed since did not get callback.",
168                  (callbacksPerformed == 1));
169       // Can now call direct on client
170
client.removeListener(handler);
171       // shouldn't get callback now since removed listener
172
ret = makeInvocation("test", "test");
173       // allow time for callback
174
Thread.sleep(2000);
175       log.debug("done sleeping.");
176       callbacksPerformed = handler.isCallbackReceived();
177       log.debug("callbackPerformed after removing listener is " + callbacksPerformed);
178       assertTrue("Result of testLocalPushCallback() failed since did get callback " +
179                  "but have been removed as listener.",
180                  (callbacksPerformed == 1));
181    }
182
183    /**
184     * Test simple invocation and adding of listener with push callback (meaning server
185     * will send callback message when it gets it) to a remote callback server
186     *
187     * @throws Throwable
188     */

189    public void testRemotePushCallback() throws Throwable JavaDoc
190    {
191       log.debug("running testRemotePushCallback()");
192
193       sessionId = new UID JavaDoc().toString();
194       //InvokerLocator locator = client.getInvoker().getLocator();
195
sessionId = client.getSessionId();
196       MockInvokerCallbackHandler handler = new MockInvokerCallbackHandler(sessionId);
197
198       log.debug("client.getInvoker().getLocator()" + client.getInvoker().getLocator());
199
200       // simple invoke, should return bar
201
Object JavaDoc ret = makeInvocation("foo", "bar");
202       assertTrue("Result of testRemotePushCallback() invocation of foo.", "bar".equals(ret));
203       client.addListener(handler, locator);
204       // invoke which should cause callback
205
ret = makeInvocation("test", "test");
206       // allow time for callback
207
Thread.sleep(3000);
208       log.debug("done sleeping.");
209       // TODO: No way to currently check the remote callback handler
210
// to see if it got callback -TME
211
/*
212       int callbacksPerformed = handler.isCallbackReceived();
213       log.debug("callbacksPerformed after adding listener is " + callbacksPerformed);
214       assertTrue("Result of testRemotePushCallback() failed since did not get callback.",
215                  (callbacksPerformed == 1));
216       */

217       // Can now call direct on client
218
client.removeListener(handler);
219       // shouldn't get callback now since removed listener
220
ret = makeInvocation("test", "test");
221       // allow time for callback
222
Thread.sleep(2000);
223       log.debug("done sleeping.");
224       /*
225       callbacksPerformed = handler.isCallbackReceived();
226       log.debug("callbackPerformed after removing listener is " + callbacksPerformed);
227       assertTrue("Result of testRemotePushCallback() failed since did get callback " +
228                  "but have been removed as listener.",
229                  (callbacksPerformed == 1));
230       */

231    }
232
233    /**
234     * Tests simple invocation and pull callbacks. Meaning will add a listener and
235     * will then have to get the callbacks from the server.
236     *
237     * @throws Throwable
238     */

239    public void testPullCallback() throws Throwable JavaDoc
240    {
241       log.debug("running testPullCallback()");
242
243       // should be null by default, since don't have connector started, but setting anyway
244
//client.setClientLocator(null);
245

246       MockInvokerCallbackHandler handler = new MockInvokerCallbackHandler(sessionId);
247
248       // simple invoke, should return bar
249
Object JavaDoc ret = makeInvocation("bar", "foo");
250       assertTrue("Result of runPullCallbackTest() invocation of bar.", "foo".equals(ret));
251       client.addListener(handler);
252       // invoke which should cause callback on server side
253
ret = makeInvocation("test", "test");
254       // allow time for callback
255
Thread.sleep(2000);
256       ret = client.getCallbacks(handler);
257       log.debug("getCallbacks returned " + ret);
258       log.debug("should have something.");
259       assertTrue("Result of runPullCallbackTest() getCallbacks() after add listener.",
260                  ret != null);
261       // can now call directly on client
262
//ret = makeInvocation("removeListener", null);
263
client.removeListener(handler);
264       ret = makeInvocation("getCallbacks", null);
265       log.debug("getCallbacks returned " + ret);
266       log.debug("should have been empty.");
267       assertTrue("Result of runPullCallbackTest() getCallbacks() after remove listener.",
268                  ret == null);
269    }
270
271    /**
272     * Tests complex invocation to get object containing array of complex objects.
273     *
274     * @throws Throwable
275     */

276    public void testArrayReturn() throws Throwable JavaDoc
277    {
278       // simple invoke, should return bar
279
Object JavaDoc ret = makeInvocation("testComplexReturn", null);
280       ComplexReturn complexRet = (ComplexReturn) ret;
281       MockTest[] mockTests = complexRet.getMockTests();
282       assertTrue("ComplexReturn's array should contain 2 items",
283                  2 == mockTests.length);
284       for(int x = 0; x < mockTests.length; x++)
285       {
286          System.err.println(mockTests[x]);
287          MockTest test = mockTests[x];
288          assertNotNull("MockTest should not be null", test);
289       }
290
291 // assertTrue("Result of runPullCallbackTest() invocation of bar.",
292
// "foo".equals(ret));
293
}
294
295    private Object JavaDoc makeInvocation(String JavaDoc method, String JavaDoc param) throws Throwable JavaDoc
296    {
297       Object JavaDoc ret = client.invoke(new NameBasedInvocation(method,
298                                                          new Object JavaDoc[]{param},
299                                                          new String JavaDoc[]{String JavaDoc.class.getName()}),
300                                  null);
301
302       return ret;
303    }
304
305 }
306
Popular Tags