2
* JBoss, Home of Professional Open Source
3
* Copyright 2005, JBoss Inc., and individual contributors as indicated
4
* by the @authors tag. See the copyright.txt in the distribution for a
5
* full listing of individual contributors.
7
* This is free software; you can redistribute it and/or modify it
8
* under the terms of the GNU Lesser General Public License as
9
* published by the Free Software Foundation; either version 2.1 of
10
* the License, or (at your option) any later version.
12
* This software is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this software; if not, write to the Free
19
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23
package org.jboss.test.remoting.transport;
25
import junit.framework.TestCase;
26
import org.apache.log4j.Level;
27
import org.jboss.logging.Logger;
28
import org.jboss.remoting.Client;
29
import org.jboss.remoting.InvokerLocator;
30
import org.jboss.remoting.ServerInvocationHandler;
31
import org.jboss.remoting.invocation.NameBasedInvocation;
32
import org.jboss.remoting.transport.Connector;
33
import org.jboss.test.remoting.ComplexReturn;
34
import org.jboss.test.remoting.TestUtil;
35
import org.jboss.test.remoting.performance.synchronous.PerformanceTestCase;
36
import org.jboss.test.remoting.transport.mock.MockInvokerCallbackHandler;
37
import org.jboss.test.remoting.transport.mock.MockServerInvocationHandler;
38
import org.jboss.test.remoting.transport.mock.MockTest;
40
import java.net.BindException;
41
import java.rmi.server.UID;
45
* This is the actual concrete test for the invoker client. Uses socket transport by default.
47
* @author <a href="mailto:telrod@e2technologies.net">Tom Elrod</a>
49
public abstract class InvokerClientTest extends TestCase
51
private String sessionId = new UID().toString();
52
private Client client;
53
protected Connector connector;
54
protected InvokerLocator locator;
56
protected int port = 9091; //default port
57
protected int callbackPort = -1;
58
protected String metadata = null;
60
public static final Logger log = Logger.getLogger(InvokerClientTest.class);
62
public abstract String getTransport();
64
public int getCallbackPort()
73
String host = System.getProperty("jrunit.bind_addr", "localhost");
74
String locatorURI = getTransport() + "://" + host+ ":" + port;
77
locatorURI = locatorURI + "/?" + metadata;
79
log.info("connecting to: " + locatorURI);
80
InvokerLocator locator = new InvokerLocator(locatorURI);
82
client = new Client(locator, "mock");
87
log.error(e.getMessage(), e);
92
* This will be used to create callback server
98
protected InvokerLocator initServer(int port) throws Exception
102
port = TestUtil.getRandomPort();
104
log.debug("port = " + port);
106
// InvokerRegistry.registerInvoker("mock", MockClientInvoker.class, MockServerInvoker.class);
107
connector = new Connector();
109
String locatorURI = getTransport() + "://localhost:" + port;
112
locatorURI = locatorURI + "/?" + metadata;
114
InvokerLocator locator = new InvokerLocator(locatorURI);
116
connector.setInvokerLocator(locator.getLocatorURI());
118
connector.addInvocationHandler(getSubsystem(), getServerInvocationHandler());
124
protected String getSubsystem()
129
protected ServerInvocationHandler getServerInvocationHandler()
131
return new MockServerInvocationHandler();
135
public void setUp() throws Exception
137
org.apache.log4j.BasicConfigurator.configure();
138
org.apache.log4j.Category.getRoot().setLevel(Level.INFO);
140
String newMetadata = System.getProperty(PerformanceTestCase.REMOTING_METADATA);
141
if(newMetadata != null && newMetadata.length() > 0)
143
metadata = newMetadata;
144
log.info("Using metadata: " + metadata);
147
newMetadata = System.getProperty(PerformanceTestCase.REMOTING_METADATA_CALLBACK);
148
if(newMetadata != null && newMetadata.length() > 0)
152
metadata = newMetadata;
156
metadata += newMetadata;
159
log.info("Using metadata: " + metadata);
162
// this is a retry hack because in some cases, can get duplicate callback server ports
163
// when trying to find a free one.
165
for(int x = 0; x < retryLimit; x++)
169
locator = initServer(getCallbackPort());
171
catch(BindException e)
173
if(x + 1 == retryLimit)
189
public void tearDown() throws Exception
191
if(connector != null)
206
* Test simple invocation and adding of listener with push callback (meaning server
207
* will send callback message when it gets it) to a local callback server
211
public void testLocalPushCallback() throws Throwable
213
log.debug("running testLocalPushCallback()");
215
sessionId = new UID().toString();
217
sessionId = client.getSessionId();
218
MockInvokerCallbackHandler handler = new MockInvokerCallbackHandler(sessionId);
220
log.debug("client.getInvoker().getLocator()" + client.getInvoker().getLocator());
222
// simple invoke, should return bar
223
Object ret = makeInvocation("foo", "bar");
224
assertTrue("Result of testLocalPushCallback() invocation of foo.", "bar".equals(ret));
225
if("bar".equals(ret))
231
log.debug("FAILED - testLocalPushCallback1");
234
client.addListener(handler, locator);
235
// invoke which should cause callback
236
ret = makeInvocation("test", "test");
237
// allow time for callback
239
log.debug("done sleeping.");
240
int callbacksPerformed = handler.isCallbackReceived();
241
log.debug("callbacksPerformed after adding listener is " + callbacksPerformed);
242
assertTrue("Result of testLocalPushCallback() failed since did not get callback.",
243
(callbacksPerformed == 1));
244
if(callbacksPerformed == 1)
250
log.debug("FAILED - testLocalPushCallback2");
252
// Can now call direct on client
253
client.removeListener(handler);
254
// shouldn't get callback now since removed listener
255
ret = makeInvocation("test", "test");
256
// allow time for callback
258
log.debug("done sleeping.");
259
callbacksPerformed = handler.isCallbackReceived();
260
log.debug("callbackPerformed after removing listener is " + callbacksPerformed);
261
assertTrue("Result of testLocalPushCallback() failed since did get callback " +
262
"but have been removed as listener.",
263
(callbacksPerformed == 1));
264
if(callbacksPerformed == 1)
270
log.debug("FAILED - testLocalPushCallback3");
276
* Test simple invocation and adding of listener with push callback (meaning server
277
* will send callback message when it gets it) to a remote callback server
281
public void testRemotePushCallback() throws Throwable
283
log.debug("running testRemotePushCallback()");
285
sessionId = new UID().toString();
286
//InvokerLocator locator = client.getInvoker().getLocator();
287
sessionId = client.getSessionId();
288
MockInvokerCallbackHandler handler = new MockInvokerCallbackHandler(sessionId);
290
log.debug("client.getInvoker().getLocator()" + client.getInvoker().getLocator());
292
// simple invoke, should return bar
293
Object ret = makeInvocation("foo", "bar");
294
assertTrue("Result of testRemotePushCallback() invocation of foo.", "bar".equals(ret));
295
if("bar".equals(ret))
301
log.debug("FAILED - testRemotePushCallback1");
304
client.addListener(handler, locator);
305
// invoke which should cause callback
306
ret = makeInvocation("test", "test");
307
// allow time for callback
309
log.debug("done sleeping.");
310
// TODO: No way to currently check the remote callback handler
311
// to see if it got callback -TME
313
int callbacksPerformed = handler.isCallbackReceived();
314
log.debug("callbacksPerformed after adding listener is " + callbacksPerformed);
315
assertTrue("Result of testRemotePushCallback() failed since did not get callback.",
316
(callbacksPerformed == 1));
318
// Can now call direct on client
319
client.removeListener(handler);
320
// shouldn't get callback now since removed listener
321
ret = makeInvocation("test", "test");
322
// allow time for callback
324
log.debug("done sleeping.");
326
callbacksPerformed = handler.isCallbackReceived();
327
log.debug("callbackPerformed after removing listener is " + callbacksPerformed);
328
assertTrue("Result of testRemotePushCallback() failed since did get callback " +
329
"but have been removed as listener.",
330
(callbacksPerformed == 1));
335
* Tests simple invocation and pull callbacks. Meaning will add a listener and
336
* will then have to get the callbacks from the server.
340
public void testPullCallback() throws Throwable
342
log.debug("running testPullCallback()");
344
// should be null by default, since don't have connector started, but setting anyway
345
//client.setClientLocator(null);
347
MockInvokerCallbackHandler handler = new MockInvokerCallbackHandler(sessionId);
349
// simple invoke, should return bar
350
Object ret = makeInvocation("bar", "foo");
351
assertTrue("Result of runPullCallbackTest() invocation of bar.", "foo".equals(ret));
352
if("foo".equals(ret))
358
log.debug("FAILED - testPullCallback1");
361
client.addListener(handler);
362
// invoke which should cause callback on server side
363
ret = makeInvocation("test", "test");
364
// allow time for callback
366
ret = client.getCallbacks(handler);
367
log.debug("getCallbacks returned " + ret);
368
log.debug("should have something.");
369
assertTrue("Result of runPullCallbackTest() getCallbacks() after add listener.",
377
log.debug("FAILED - testPullCallback2");
380
// can now call directly on client
381
//ret = makeInvocation("removeListener", null);
382
client.removeListener(handler);
383
ret = makeInvocation("getCallbacks", null);
384
log.debug("getCallbacks returned " + ret);
385
log.debug("should have been empty.");
386
assertTrue("Result of runPullCallbackTest() getCallbacks() after remove listener.",
394
log.debug("FAILED - testPullCallback3");
400
* Tests complex invocation to get object containing array of complex objects.
404
public void testArrayReturn() throws Throwable
406
// simple invoke, should return bar
407
Object ret = makeInvocation("testComplexReturn", null);
408
ComplexReturn complexRet = (ComplexReturn) ret;
409
MockTest[] mockTests = complexRet.getMockTests();
410
assertTrue("ComplexReturn's array should contain 2 items",
411
2 == mockTests.length);
412
if(2 == mockTests.length)
418
log.debug("FAILED - testArrayReturn1");
421
for(int x = 0; x < mockTests.length; x++)
423
System.err.println(mockTests[x]);
424
MockTest test = mockTests[x];
425
assertNotNull("MockTest should not be null", test);
432
log.debug("FAILED - testArrayReturn2");
437
// assertTrue("Result of runPullCallbackTest() invocation of bar.",
438
// "foo".equals(ret));
441
public void testThrownException() throws Throwable
445
makeInvocation("testException", null);
446
assertTrue("Did not get exception thrown as expected.", false);
448
catch(Exception throwable)
450
assertTrue("Got exception thrown as expected.", true);
454
protected Client getClient()
459
protected Object makeInvocation(String method, String param) throws Throwable
461
return client.invoke(new NameBasedInvocation(method,
463
new String[]{String.class.getName()}),