KickJava   Java API By Example, From Geeks To Geeks.

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

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

157    public void testLocalPushCallback() throws Throwable JavaDoc
158    {
159       try
160       {
161          log.debug("running testLocalPushCallback()");
162
163          sessionId = new UID JavaDoc().toString();
164          InvokerLocator locator = initServer(rmiMetaData, -1);
165          init(rmiMetaData);
166
167          sessionId = client.getSessionId();
168          MockInvokerCallbackHandler handler = new MockInvokerCallbackHandler(sessionId);
169
170          log.debug("client.getInvoker().getLocator()" + client.getInvoker().getLocator());
171
172          // simple invoke, should return bar
173
Object JavaDoc ret = makeInvocation("foo", "bar");
174          assertTrue("Result of testLocalPushCallback() invocation of foo.", "bar".equals(ret));
175          client.addListener(handler, locator);
176          // invoke which should cause callback
177
ret = makeInvocation("test", "test");
178          // allow time for callback
179
Thread.sleep(3000);
180          log.debug("done sleeping.");
181          int callbacksPerformed = handler.isCallbackReceived();
182          log.debug("callbacksPerformed after adding listener is " + callbacksPerformed);
183          assertTrue("Result of testLocalPushCallback() failed since did not get callback.",
184                     (callbacksPerformed == 1));
185          // Can now call direct on client
186
client.removeListener(handler);
187          // shouldn't get callback now since removed listener
188
ret = makeInvocation("test", "test");
189          // allow time for callback
190
Thread.sleep(2000);
191          log.debug("done sleeping.");
192          callbacksPerformed = handler.isCallbackReceived();
193          log.debug("callbackPerformed after removing listener is " + callbacksPerformed);
194          assertTrue("Result of testLocalPushCallback() failed since did get callback " +
195                     "but have been removed as listener.",
196                     (callbacksPerformed == 1));
197       }
198       finally
199       {
200          if(client != null)
201          {
202             client.disconnect();
203          }
204       }
205    }
206
207    /**
208     * Test simple invocation and adding of listener with push callback (meaning server
209     * will send callback message when it gets it) to a remote callback server
210     *
211     * @throws Throwable
212     */

213    public void testRemotePushCallback() throws Throwable JavaDoc
214    {
215       try
216       {
217          log.debug("running testRemotePushCallback()");
218
219          sessionId = new UID JavaDoc().toString();
220          initServer(rmiMetaData, -1);
221          init(rmiMetaData);
222          InvokerLocator locator = client.getInvoker().getLocator();
223          sessionId = client.getSessionId();
224          MockInvokerCallbackHandler handler = new MockInvokerCallbackHandler(sessionId);
225
226          log.debug("client.getInvoker().getLocator()" + client.getInvoker().getLocator());
227
228          // simple invoke, should return bar
229
Object JavaDoc ret = makeInvocation("foo", "bar");
230          assertTrue("Result of testRemotePushCallback() invocation of foo.", "bar".equals(ret));
231          client.addListener(handler, locator);
232          // invoke which should cause callback
233
ret = makeInvocation("test", "test");
234          // allow time for callback
235
Thread.sleep(3000);
236          log.debug("done sleeping.");
237          // TODO: No way to currently check the remote callback handler
238
// to see if it got callback -TME
239
/*
240          int callbacksPerformed = handler.isCallbackReceived();
241          log.debug("callbacksPerformed after adding listener is " + callbacksPerformed);
242          assertTrue("Result of testRemotePushCallback() failed since did not get callback.",
243                     (callbacksPerformed == 1));
244          */

245          // Can now call direct on client
246
client.removeListener(handler);
247          // shouldn't get callback now since removed listener
248
ret = makeInvocation("test", "test");
249          // allow time for callback
250
Thread.sleep(2000);
251          log.debug("done sleeping.");
252          /*
253          callbacksPerformed = handler.isCallbackReceived();
254          log.debug("callbackPerformed after removing listener is " + callbacksPerformed);
255          assertTrue("Result of testRemotePushCallback() failed since did get callback " +
256                     "but have been removed as listener.",
257                     (callbacksPerformed == 1));
258          */

259       }
260       finally
261       {
262          if(client != null)
263          {
264             client.disconnect();
265          }
266       }
267    }
268
269    /**
270     * Tests simple invocation and pull callbacks. Meaning will add a listener and
271     * will then have to get the callbacks from the server.
272     *
273     * @throws Throwable
274     */

275    public void testPullCallback() throws Throwable JavaDoc
276    {
277       try
278       {
279          log.debug("running testPullCallback()");
280
281          init(rmiMetaData);
282          // should be null by default, since don't have connector started, but setting anyway
283
//client.setClientLocator(null);
284

285          MockInvokerCallbackHandler handler = new MockInvokerCallbackHandler(sessionId);
286
287          // simple invoke, should return bar
288
Object JavaDoc ret = makeInvocation("bar", "foo");
289          assertTrue("Result of runPullCallbackTest() invocation of bar.", "foo".equals(ret));
290          client.addListener(handler);
291          // invoke which should cause callback on server side
292
ret = makeInvocation("test", "test");
293          // allow time for callback
294
Thread.sleep(2000);
295          ret = client.getCallbacks();
296          log.debug("getCallbacks returned " + ret);
297          log.debug("should have something.");
298          assertTrue("Result of runPullCallbackTest() getCallbacks() after add listener.",
299                     ret != null);
300          // can now call directly on client
301
//ret = makeInvocation("removeListener", null);
302
client.removeListener(handler);
303          ret = makeInvocation("getCallbacks", null);
304          log.debug("getCallbacks returned " + ret);
305          log.debug("should have been empty.");
306          assertTrue("Result of runPullCallbackTest() getCallbacks() after remove listener.",
307                     ret == null);
308       }
309       finally
310       {
311          if(client != null)
312          {
313             client.disconnect();
314             client = null;
315          }
316       }
317    }
318
319    /**
320     * Tests complex invocation to get object containing array of complex objects.
321     *
322     * @throws Throwable
323     */

324    public void testArrayReturn() throws Throwable JavaDoc
325    {
326       try
327       {
328          init(rmiMetaData);
329
330          // simple invoke, should return bar
331
Object JavaDoc ret = makeInvocation("testComplexReturn", null);
332          ComplexReturn complexRet = (ComplexReturn) ret;
333          MockTest[] mockTests = complexRet.getMockTests();
334          assertTrue("ComplexReturn's array should contain 2 items",
335                     2 == mockTests.length);
336          for(int x = 0; x < mockTests.length; x++)
337          {
338             System.err.println(mockTests[x]);
339             MockTest test = mockTests[x];
340             assertNotNull("MockTest should not be null", test);
341          }
342
343 // assertTrue("Result of runPullCallbackTest() invocation of bar.",
344
// "foo".equals(ret));
345
}
346       finally
347       {
348          if(client != null)
349          {
350             client.disconnect();
351             client = null;
352          }
353       }
354    }
355
356    private Object JavaDoc makeInvocation(String JavaDoc method, String JavaDoc param) throws Throwable JavaDoc
357    {
358       Object JavaDoc ret = client.invoke(new NameBasedInvocation(method,
359                                                          new Object JavaDoc[]{param},
360                                                          new String JavaDoc[]{String JavaDoc.class.getName()}),
361                                  null);
362
363       return ret;
364    }
365
366    public static void main(String JavaDoc[] args)
367    {
368
369       org.apache.log4j.BasicConfigurator.configure();
370       org.apache.log4j.Category.getRoot().setLevel(Level.INFO);
371       org.apache.log4j.Category.getInstance("org.jgroups").setLevel(Level.INFO);
372       org.apache.log4j.Category.getInstance("org.jboss.remoting").setLevel(Level.DEBUG);
373       org.apache.log4j.Category.getInstance("test").setLevel(Level.DEBUG);
374
375       Map JavaDoc metadata = new HashMap JavaDoc();
376       int port = RMIServerInvoker.DEFAULT_REGISTRY_PORT - 1;
377       String JavaDoc transport = "rmi";
378
379       RMIInvokerClientTest client = null;
380       if(args.length == 1)
381       {
382          port = Integer.parseInt(args[0]);
383          client = new RMIInvokerClientTest(transport, port);
384       }
385       else if(args.length == 2)
386       {
387          int instances = Integer.parseInt(args[0]);
388          port = Integer.parseInt(args[1]);
389          client = new RMIInvokerClientTest(transport, port, instances);
390       }
391       else
392       {
393          client = new RMIInvokerClientTest(transport, port);
394          System.out.println("Using default transport (" + client.getTransport() +
395                             ") and default port (" + client.getPort() + ") and " +
396                             "default number of instances (" + client.getNumberOfInstances() + ")" +
397                             "\nCan enter port and instances via command line.");
398       }
399
400       metadata.put(RMIServerInvoker.REGISTRY_PORT_KEY, String.valueOf(port + 1));
401       client.rmiMetaData = metadata;
402
403       try
404       {
405
406          //regular class run
407
//client.runInvokers();
408
MultipleTestRunner runner = new MultipleTestRunner();
409          runner.doRun(client, true);
410       }
411       catch(Throwable JavaDoc e)
412       {
413          e.printStackTrace();
414          System.exit(1);
415       }
416       System.exit(0);
417    }
418 }
419
Popular Tags