KickJava   Java API By Example, From Geeks To Geeks.

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

34 public class InvokerClientTest extends TestCase implements InvokerConstants
35 {
36    private String JavaDoc sessionId = new UID JavaDoc().toString();
37    private Client client;
38    private Connector connector;
39    private InvokerLocator locator;
40
41    private static final Logger log = Logger.getLogger(InvokerClientTest.class);
42
43    public void init()
44    {
45       try
46       {
47          InvokerLocator locator = new InvokerLocator(transport + "://localhost:" + port);
48          client = new Client(locator, "mock");
49          client.connect();
50       }
51       catch(Exception JavaDoc e)
52       {
53          log.error(e.getMessage(), e);
54       }
55    }
56
57    /**
58     * This will be used to create callback server
59     *
60     * @param port
61     * @return
62     * @throws Exception
63     */

64    private InvokerLocator initServer(int port) throws Exception JavaDoc
65    {
66       if(port < 0)
67       {
68          port = TestUtil.getRandomPort();
69       }
70       log.debug("port = " + port);
71
72       InvokerRegistry.registerInvoker("mock", MockClientInvoker.class, MockServerInvoker.class);
73       connector = new Connector();
74       InvokerLocator locator = new InvokerLocator(transport + "://localhost:" + port);
75       StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
76       buf.append("<?xml version=\"1.0\"?>\n");
77       buf.append("<handlers>\n");
78       buf.append(" <handler subsystem=\"mock\">org.jboss.test.remoting.transport.mock.MockServerInvocationHandler</handler>\n");
79       buf.append("</handlers>\n");
80       Document JavaDoc xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream JavaDoc(buf.toString().getBytes()));
81       connector.setInvokerLocator(locator.getLocatorURI());
82       connector.setConfiguration(xml.getDocumentElement());
83       //connector.create();
84
connector.start();
85       return locator;
86    }
87
88
89    public void setUp() throws Exception JavaDoc
90    {
91       locator = initServer(-1);
92       init();
93    }
94
95    public void tearDown() throws Exception JavaDoc
96    {
97       if(connector != null)
98       {
99          connector.stop();
100          connector.destroy();
101          connector = null;
102       }
103       locator = null;
104       if(client != null)
105       {
106          client.disconnect();
107          client = null;
108       }
109    }
110
111    /**
112     * Test simple invocation and adding of listener with push callback (meaning server
113     * will send callback message when it gets it) to a local callback server
114     *
115     * @throws Throwable
116     */

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

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

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

238    }
239
240    /**
241     * Tests simple invocation and pull callbacks. Meaning will add a listener and
242     * will then have to get the callbacks from the server.
243     *
244     * @throws Throwable
245     */

246    public void testPullCallback() throws Throwable JavaDoc
247    {
248       log.debug("running testPullCallback()");
249
250       // should be null by default, since don't have connector started, but setting anyway
251
//client.setClientLocator(null);
252

253       MockInvokerCallbackHandler handler = new MockInvokerCallbackHandler(sessionId);
254
255       // simple invoke, should return bar
256
Object JavaDoc ret = makeInvocation("bar", "foo");
257       assertTrue("Result of runPullCallbackTest() invocation of bar.", "foo".equals(ret));
258       if("foo".equals(ret))
259       {
260          log.debug("PASS");
261       }
262       else
263       {
264          log.debug("FAILED - testPullCallback1");
265       }
266
267       client.addListener(handler);
268       // invoke which should cause callback on server side
269
ret = makeInvocation("test", "test");
270       // allow time for callback
271
Thread.sleep(5000);
272       ret = client.getCallbacks(handler);
273       log.debug("getCallbacks returned " + ret);
274       log.debug("should have something.");
275       assertTrue("Result of runPullCallbackTest() getCallbacks() after add listener.",
276                  ret != null);
277       if(ret != null)
278       {
279          log.debug("PASS");
280       }
281       else
282       {
283          log.debug("FAILED - testPullCallback2");
284       }
285
286       // can now call directly on client
287
//ret = makeInvocation("removeListener", null);
288
client.removeListener(handler);
289       ret = makeInvocation("getCallbacks", null);
290       log.debug("getCallbacks returned " + ret);
291       log.debug("should have been empty.");
292       assertTrue("Result of runPullCallbackTest() getCallbacks() after remove listener.",
293                  ret == null);
294       if(ret == null)
295       {
296          log.debug("PASS");
297       }
298       else
299       {
300          log.debug("FAILED - testPullCallback3");
301       }
302
303    }
304
305    /**
306     * Tests complex invocation to get object containing array of complex objects.
307     *
308     * @throws Throwable
309     */

310    public void testArrayReturn() throws Throwable JavaDoc
311    {
312       // simple invoke, should return bar
313
Object JavaDoc ret = makeInvocation("testComplexReturn", null);
314       ComplexReturn complexRet = (ComplexReturn) ret;
315       MockTest[] mockTests = complexRet.getMockTests();
316       assertTrue("ComplexReturn's array should contain 2 items",
317                  2 == mockTests.length);
318       if(2 == mockTests.length)
319       {
320          log.debug("PASS");
321       }
322       else
323       {
324          log.debug("FAILED - testArrayReturn1");
325       }
326
327       for(int x = 0; x < mockTests.length; x++)
328       {
329          System.err.println(mockTests[x]);
330          MockTest test = mockTests[x];
331          assertNotNull("MockTest should not be null", test);
332          if(test != null)
333          {
334             log.debug("PASS");
335          }
336          else
337          {
338             log.debug("FAILED - testArrayReturn2");
339          }
340
341       }
342
343 // assertTrue("Result of runPullCallbackTest() invocation of bar.",
344
// "foo".equals(ret));
345
}
346
347    private Object JavaDoc makeInvocation(String JavaDoc method, String JavaDoc param) throws Throwable JavaDoc
348    {
349       Object JavaDoc ret = client.invoke(new NameBasedInvocation(method,
350                                                          new Object JavaDoc[]{param},
351                                                          new String JavaDoc[]{String JavaDoc.class.getName()}),
352                                  null);
353
354       return ret;
355    }
356 }
357
Popular Tags