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.local;
25
import junit.framework.Test;
26
import junit.framework.TestCase;
27
import junit.framework.TestSuite;
28
import org.apache.log4j.Level;
29
import org.jboss.logging.Logger;
30
import org.jboss.remoting.Client;
31
import org.jboss.remoting.InvokerLocator;
32
import org.jboss.remoting.invocation.NameBasedInvocation;
33
import org.jboss.remoting.transport.Connector;
34
import org.jboss.test.remoting.TestUtil;
35
import org.w3c.dom.Document;
37
import javax.xml.parsers.DocumentBuilderFactory;
38
import java.io.ByteArrayInputStream;
39
import java.rmi.server.UID;
42
* Test for the Local client invoker to verify will call directly on
43
* local server invoker.
45
* @author <a href="mailto:telrod@e2technologies.net">Tom Elrod</a>
47
public class LocalInvokerTestCase extends TestCase
49
private String sessionId = new UID().toString();
50
private Client client;
51
private String transport = "socket";
52
private int port = -1;
53
private InvokerLocator locator;
54
private static final String NAME = "LocalInvokerTest.class";
55
private static final Logger log = Logger.getLogger(LocalInvokerTestCase.class);
57
public LocalInvokerTestCase()
62
public LocalInvokerTestCase(String name)
67
public LocalInvokerTestCase(String transport, int port)
70
this.transport = transport;
78
client = new Client(locator, "mock");
83
log.error(e.getMessage(), e);
88
* Create and initalize local server
94
private InvokerLocator initServer(int port) throws Exception
98
port = TestUtil.getRandomPort();
100
log.debug("port = " + port);
102
// InvokerRegistry.registerInvoker("mock", MockClientInvoker.class, MockServerInvoker.class);
103
Connector connector = new Connector();
104
InvokerLocator locator = new InvokerLocator(transport + "://localhost:" + port);
105
StringBuffer buf = new StringBuffer();
106
buf.append("<?xml version=\"1.0\"?>\n");
107
buf.append("<handlers>\n");
108
buf.append(" <handler subsystem=\"mock\">org.jboss.test.remoting.transport.mock.MockServerInvocationHandler</handler>\n");
109
buf.append("</handlers>\n");
110
Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(buf.toString().getBytes()));
111
connector.setInvokerLocator(locator.getLocatorURI());
112
connector.setConfiguration(xml.getDocumentElement());
113
//connector.create();
116
this.locator = connector.getLocator();
121
public void testLocalInvoke() throws Throwable
125
log.debug("running testLocalInvoke()");
127
InvokerLocator locator = initServer(-1);
130
// MockInvokerCallbackHandler handler = new MockInvokerCallbackHandler(sessionId);
132
log.debug("client.getInvoker()" + client.getInvoker());
134
// simple invoke, should return bar
135
Object ret = makeInvocation("foo", "bar");
136
assertTrue("Result of testLocalInvoke() invocation of foo.", "bar".equals(ret));
138
client.addListener(handler, locator);
139
// invoke which should cause callback
140
ret = makeInvocation("test", "test");
141
// allow time for callback
143
log.debug("done sleeping.");
144
boolean callbackPerformed = handler.isCallbackReceived();
145
log.debug("callbackPerformed after adding listener is " + callbackPerformed);
146
assertTrue("Result of runPushCallbackTest() failed since did not get callback.",
148
handler.isCallbackReceived(false);
149
// Can now call direct on client
150
//ret = makeInvocation("removeListener", null);
151
client.removeListener(handler);
152
// shouldn't get callback now since removed listener
153
ret = makeInvocation("test", "test");
154
// allow time for callback
156
log.debug("done sleeping.");
157
callbackPerformed = handler.isCallbackReceived();
158
log.debug("callbackPerformed after removing listener is " + callbackPerformed);
159
assertTrue("Result of runPushCallbackTest() failed since did get callback " +
160
"but have been removed as listener.",
174
* Tests simple invocation and pull callbacks. Meaning will add a listener and
175
* will then have to get the callbacks from the server.
179
public void testPullCallback() throws Throwable
183
log.debug("running testPullCallback()");
186
// should be null by default, since don't have connector started, but setting anyway
187
//client.setClientLocator(null);
189
MockInvokerCallbackHandler handler = new MockInvokerCallbackHandler(sessionId);
191
// simple invoke, should return bar
192
Object ret = makeInvocation("bar", "foo");
193
assertTrue("Result of runPullCallbackTest() invocation of bar.", "foo".equals(ret));
194
client.addListener(handler);
195
// invoke which should cause callback on server side
196
ret = makeInvocation("test", "test");
197
// allow time for callback
199
ret = client.getCallbacks();
200
log.debug("getCallbacks returned " + ret);
201
log.debug("should have something.");
202
assertTrue("Result of runPullCallbackTest() getCallbacks() after add listener.",
204
// can now call directly on client
205
//ret = makeInvocation("removeListener", null);
206
client.removeListener(handler);
207
ret = makeInvocation("getCallbacks", null);
208
log.debug("getCallbacks returned " + ret);
209
log.debug("should have been empty.");
210
assertTrue("Result of runPullCallbackTest() getCallbacks() after remove listener.",
225
* Tests complex invocation to get object containing array of complex objects.
230
public void testArrayReturn() throws Throwable
236
// simple invoke, should return bar
237
Object ret = makeInvocation("testComplexReturn", null);
238
ComplexReturn complexRet = (ComplexReturn)ret;
239
MockTest[] mockTests = complexRet.getMockTests();
240
assertTrue("ComplexReturn's array should contain 2 items",
241
2 == mockTests.length);
242
for(int x = 0; x < mockTests.length; x++)
244
System.err.println(mockTests[x]);
245
MockTest test = mockTests[x];
246
assertNotNull("MockTest should not be null", test);
249
// assertTrue("Result of runPullCallbackTest() invocation of bar.",
250
// "foo".equals(ret));
262
private Object makeInvocation(String method, String param) throws Throwable
264
Object ret = client.invoke(new NameBasedInvocation(method,
266
new String[]{String.class.getName()}),
272
public static Test suite()
274
return new TestSuite(LocalInvokerTestCase.class);
278
public static void main(String[] args)
280
org.apache.log4j.BasicConfigurator.configure();
281
org.apache.log4j.Category.getRoot().setLevel(Level.DEBUG);
283
LocalInvokerTestCase client = null;
286
String transport = args[0];
287
int port = Integer.parseInt(args[1]);
288
client = new LocalInvokerTestCase(transport, port);
292
client = new LocalInvokerTestCase(LocalInvokerTestCase.class.getName());
293
System.out.println("Using default transport " +
294
"and default port." +
295
"\nCan enter transport and port.");
301
//client.testLocalInvoke();
302
//client.runInvokers();
303
// MultipleTestRunner runner = new MultipleTestRunner();
304
// runner.doRun(client, true);
305
junit.textui.TestRunner.run(suite());