KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > remoting > transport > socket > InvokerClientTest


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.socket;
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.Random JavaDoc;
29
30
31 /**
32  * This is the actual concrete test for the invoker client. Uses socket transport by default.
33  *
34  * @author <a HREF="mailto:telrod@e2technologies.net">Tom Elrod</a>
35  */

36 public class InvokerClientTest extends AbstractInvokerTest
37 {
38    private static final Logger log = Logger.getLogger(InvokerClientTest.class);
39
40    private String JavaDoc sessionId = new UID JavaDoc().toString();
41
42    private Client client;
43
44    private static final String JavaDoc NAME = "InvokerClientTest.class";
45
46    public InvokerClientTest(String JavaDoc name)
47    {
48       super(NAME);
49    }
50
51    public InvokerClientTest(int numberOfInstances)
52    {
53       super(NAME, numberOfInstances);
54    }
55
56    public InvokerClientTest(String JavaDoc transport, int port)
57    {
58       super(NAME, transport, port);
59    }
60
61    public InvokerClientTest(String JavaDoc transport, int port, int numberOfInstances)
62    {
63       super(NAME, transport, port, numberOfInstances);
64    }
65
66    public void init()
67    {
68       try
69       {
70          InvokerLocator locator = new InvokerLocator(getTransport() + "://localhost:" + port);
71          client = new Client(locator, "mock");
72          client.connect();
73       }
74       catch(Exception JavaDoc e)
75       {
76          log.error(e.getMessage(), e);
77       }
78    }
79
80    //TODO: Same method in InvokerServerTest so could move up into AbstractInvokerTest
81
private InvokerLocator initServer(int port) throws Exception JavaDoc
82    {
83       if(port < 0)
84       {
85          port = Math.abs(new Random JavaDoc().nextInt(2000) + 2000);
86       }
87       log.debug("port = " + port);
88
89       InvokerRegistry.registerInvoker("mock", MockClientInvoker.class, MockServerInvoker.class);
90       Connector connector = new Connector();
91       InvokerLocator locator = new InvokerLocator(transport + "://localhost:" + port);
92       StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
93       buf.append("<?xml version=\"1.0\"?>\n");
94       buf.append("<handlers>\n");
95       buf.append(" <handler subsystem=\"mock\">org.jboss.remoting.transport.mock.MockServerInvocationHandler</handler>\n");
96       buf.append("</handlers>\n");
97       Document JavaDoc xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream JavaDoc(buf.toString().getBytes()));
98       connector.setInvokerLocator(locator.getLocatorURI());
99       connector.setConfiguration(xml.getDocumentElement());
100       //connector.create();
101
connector.start();
102       return locator;
103    }
104
105
106    public void runInvokers() throws Throwable JavaDoc
107    {
108       startup(getNumberOfInstances());
109       try
110       {
111          testPullCallback();
112          testLocalPushCallback();
113       }
114       finally
115       {
116          shutdown();
117       }
118    }
119
120    /**
121     * Test simple invocation and adding of listener with push callback (meaning server
122     * will send callback message when it gets it) to a local callback server
123     *
124     * @throws Throwable
125     */

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

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

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

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

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

289          MockInvokerCallbackHandler handler = new MockInvokerCallbackHandler(sessionId);
290
291          // simple invoke, should return bar
292
Object JavaDoc ret = makeInvocation("bar", "foo");
293          assertTrue("Result of runPullCallbackTest() invocation of bar.", "foo".equals(ret));
294          if("foo".equals(ret))
295          {
296             log.debug("PASS");
297          }
298          else
299          {
300             log.debug("FAILED");
301          }
302
303          client.addListener(handler);
304          // invoke which should cause callback on server side
305
ret = makeInvocation("test", "test");
306          // allow time for callback
307
Thread.sleep(2000);
308          ret = client.getCallbacks();
309          log.debug("getCallbacks returned " + ret);
310          log.debug("should have something.");
311          assertTrue("Result of runPullCallbackTest() getCallbacks() after add listener.",
312                     ret != null);
313          if(ret != null)
314          {
315             log.debug("PASS");
316          }
317          else
318          {
319             log.debug("FAILED");
320          }
321
322          // can now call directly on client
323
//ret = makeInvocation("removeListener", null);
324
client.removeListener(handler);
325          ret = makeInvocation("getCallbacks", null);
326          log.debug("getCallbacks returned " + ret);
327          log.debug("should have been empty.");
328          assertTrue("Result of runPullCallbackTest() getCallbacks() after remove listener.",
329                     ret == null);
330          if(ret == null)
331          {
332             log.debug("PASS");
333          }
334          else
335          {
336             log.debug("FAILED");
337          }
338
339       }
340       finally
341       {
342          if(client != null)
343          {
344             client.disconnect();
345             client = null;
346          }
347       }
348    }
349
350    /**
351     * Tests complex invocation to get object containing array of complex objects.
352     *
353     * @throws Throwable
354     */

355    public void testArrayReturn() throws Throwable JavaDoc
356    {
357       try
358       {
359          init();
360
361          // simple invoke, should return bar
362
Object JavaDoc ret = makeInvocation("testComplexReturn", null);
363          ComplexReturn complexRet = (ComplexReturn) ret;
364          MockTest[] mockTests = complexRet.getMockTests();
365          assertTrue("ComplexReturn's array should contain 2 items",
366                     2 == mockTests.length);
367          if(1 == mockTests.length)
368          {
369             log.debug("PASS");
370          }
371          else
372          {
373             log.debug("FAILED");
374          }
375
376          for(int x = 0; x < mockTests.length; x++)
377          {
378             System.err.println(mockTests[x]);
379             MockTest test = mockTests[x];
380             assertNotNull("MockTest should not be null", test);
381             if(test != null)
382             {
383                log.debug("PASS");
384             }
385             else
386             {
387                log.debug("FAILED");
388             }
389
390          }
391
392 // assertTrue("Result of runPullCallbackTest() invocation of bar.",
393
// "foo".equals(ret));
394
}
395       finally
396       {
397          if(client != null)
398          {
399             client.disconnect();
400             client = null;
401          }
402       }
403    }
404
405    private Object JavaDoc makeInvocation(String JavaDoc method, String JavaDoc param) throws Throwable JavaDoc
406    {
407       Object JavaDoc ret = client.invoke(new NameBasedInvocation(method,
408                                                          new Object JavaDoc[]{param},
409                                                          new String JavaDoc[]{String JavaDoc.class.getName()}),
410                                  null);
411
412       return ret;
413    }
414
415    public static void main(String JavaDoc[] args)
416    {
417
418       org.apache.log4j.BasicConfigurator.configure();
419       org.apache.log4j.Category.getRoot().setLevel(Level.INFO);
420       org.apache.log4j.Category.getInstance("org.jgroups").setLevel(Level.INFO);
421       org.apache.log4j.Category.getInstance("org.jboss.remoting").setLevel(Level.DEBUG);
422       org.apache.log4j.Category.getInstance("test").setLevel(Level.DEBUG);
423
424       InvokerClientTest client = null;
425       if(args.length == 1)
426       {
427          int instances = Integer.parseInt(args[0]);
428          client = new InvokerClientTest(instances);
429       }
430       else if(args.length == 2)
431       {
432          String JavaDoc transport = args[0];
433          int port = Integer.parseInt(args[1]);
434          client = new InvokerClientTest(transport, port);
435       }
436       else if(args.length == 3)
437       {
438          String JavaDoc transport = args[0];
439          int port = Integer.parseInt(args[1]);
440          int instances = Integer.parseInt(args[2]);
441          client = new InvokerClientTest(transport, port, instances);
442       }
443       else
444       {
445          client = new InvokerClientTest(InvokerClientTest.class.getName());
446          System.out.println("Using default transport (" + client.getTransport() +
447                             ") and default port (" + client.getPort() + ") and " +
448                             "default number of instances (" + client.getNumberOfInstances() + ")" +
449                             "\nCan enter transport, port, and instances via command line.");
450       }
451
452       try
453       {
454          //regular class run
455
//client.runInvokers();
456
MultipleTestRunner runner = new MultipleTestRunner();
457          runner.doRun(client, true);
458       }
459       catch(Throwable JavaDoc e)
460       {
461          e.printStackTrace();
462          System.exit(1);
463       }
464       System.exit(0);
465    }
466 }
467
Popular Tags