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.
22
package org.jboss.test.remoting.shutdown.local;
24
import java.lang.reflect.Field;
25
import java.net.InetAddress;
26
import java.util.HashMap;
29
import javax.management.MBeanServer;
31
import junit.framework.TestCase;
33
import org.apache.log4j.ConsoleAppender;
34
import org.apache.log4j.Level;
35
import org.apache.log4j.Logger;
36
import org.apache.log4j.PatternLayout;
37
import org.jboss.remoting.Client;
38
import org.jboss.remoting.ConnectionListener;
39
import org.jboss.remoting.InvocationRequest;
40
import org.jboss.remoting.InvokerLocator;
41
import org.jboss.remoting.MicroRemoteClientInvoker;
42
import org.jboss.remoting.ServerInvocationHandler;
43
import org.jboss.remoting.ServerInvoker;
44
import org.jboss.remoting.callback.Callback;
45
import org.jboss.remoting.callback.HandleCallbackException;
46
import org.jboss.remoting.callback.InvokerCallbackHandler;
47
import org.jboss.remoting.transport.Connector;
48
import org.jboss.remoting.transport.PortUtil;
51
* Provides unit tests that verifies that Client.disconnectLocal() and
52
* Client.removeListenerLocal() do not attempt to contact the server.
54
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
55
* @version $Revision: 2172 $
57
* Copyright Jan 18, 2007
60
public class LocalShutdownTestCase extends TestCase
62
private Logger log = Logger.getLogger(LocalShutdownTestCase.class);
63
protected static boolean firstTime = true;
66
public void setUp() throws Exception
71
Logger.getLogger("org.jboss.remoting").setLevel(Level.DEBUG);
72
Logger.getLogger("org.jboss.test.remoting").setLevel(Level.DEBUG);
73
String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
74
PatternLayout layout = new PatternLayout(pattern);
75
ConsoleAppender consoleAppender = new ConsoleAppender(layout);
76
Logger.getRootLogger().addAppender(consoleAppender);
80
public void testDummy()
84
// public void testDisconnectLocal() throws Throwable
86
// log.info("entering " + getName());
89
// String host = InetAddress.getLocalHost().getHostAddress();
90
// int port = PortUtil.findFreePort(host);
91
// String locatorURI = "socket://" + host + ":" + port;
92
// InvokerLocator locator = new InvokerLocator(locatorURI);
93
// HashMap serverConfig = new HashMap();
94
// Connector connector = new Connector(locator, serverConfig);
95
// connector.create();
96
// connector.addInvocationHandler("test", new TestHandler());
97
// connector.addConnectionListener(new TestListener());
100
// // Connect client, verify that a lease exists, then disconnect client
101
// // and verify that lease has been removed on server.
102
// HashMap clientConfig = new HashMap();
103
// clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
104
// clientConfig.put(Client.ENABLE_LEASE, "true");
105
// clientConfig.put(InvokerLocator.CLIENT_LEASE_PERIOD, "1000");
106
// Client client = new Client(locator, clientConfig);
108
// Integer i = (Integer) client.invoke(new Integer(7));
109
// assertEquals(8, i.intValue());
110
// Thread.sleep(1000);
111
// ServerInvoker serverInvoker = connector.getServerInvoker();
112
// Field field = ServerInvoker.class.getDeclaredField("clientLeases");
113
// field.setAccessible(true);
114
// Map clientLeases = (Map) field.get(serverInvoker);
115
// assertEquals(1, clientLeases.size());
116
// field = MicroRemoteClientInvoker.class.getDeclaredField("invokerSessionId");
117
// field.setAccessible(true);
118
// String invokerSessionId = (String) field.get(client.getInvoker());
119
// assertTrue(clientLeases.containsKey(invokerSessionId));
120
// client.disconnect();
121
// assertEquals(0, clientLeases.size());
123
// // Connect new client, verify that a lease exists, then locally disconnect
124
// // client and verify that lease still exists on server.
125
// client = new Client(locator, clientConfig);
127
// i = (Integer) client.invoke(new Integer(11));
128
// assertEquals(12, i.intValue());
129
// Thread.sleep(1000);
130
// assertEquals(1, clientLeases.size());
131
// invokerSessionId = (String) field.get(client.getInvoker());
132
// assertTrue(clientLeases.containsKey(invokerSessionId));
133
// client.disconnectLocal();
134
// assertEquals(1, clientLeases.size());
135
// assertTrue(clientLeases.containsKey(invokerSessionId));
138
// log.info(getName() + " PASSES");
142
// public void testRemoveListenerLocalPushCallbacks() throws Throwable
144
// log.info("entering " + getName());
147
// String host = InetAddress.getLocalHost().getHostAddress();
148
// int port = PortUtil.findFreePort(host);
149
// String locatorURI = "socket://" + host + ":" + port;
150
// InvokerLocator locator = new InvokerLocator(locatorURI);
151
// HashMap serverConfig = new HashMap();
152
// serverConfig.put(InvokerLocator.FORCE_REMOTE, "true");
153
// Connector connector = new Connector(locator, serverConfig);
154
// connector.create();
155
// connector.addInvocationHandler("test", new TestHandler());
156
// connector.start();
158
// // Connect client, add a callback handler, then call Client.removeListener()
159
// // and verify that callback handler has been removed on server.
160
// HashMap clientConfig = new HashMap();
161
// clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
162
// Client client = new Client(locator, clientConfig);
164
// Integer i = (Integer) client.invoke(new Integer(7));
165
// assertEquals(8, i.intValue());
166
// TestCallbackHandler callbackHandler = new TestCallbackHandler();
167
// client.addListener(callbackHandler, null, null, true);
168
// ServerInvoker serverInvoker = connector.getServerInvoker();
169
// Field field = ServerInvoker.class.getDeclaredField("callbackHandlers");
170
// field.setAccessible(true);
171
// Map callbackHandlers = (Map) field.get(serverInvoker);
172
// assertEquals(1, callbackHandlers.size());
173
// client.removeListener(callbackHandler);
174
// assertEquals(0, callbackHandlers.size());
175
// client.disconnect();
177
// // Connect client, add a callback handler, then call Client.removeListenerLocal()
178
// // and verify that callback handler still exists on server.
179
// client = new Client(locator, clientConfig);
181
// i = (Integer) client.invoke(new Integer(11));
182
// assertEquals(12, i.intValue());
183
// callbackHandler = new TestCallbackHandler();
184
// client.addListener(callbackHandler, null, null, true);
185
// assertEquals(1, callbackHandlers.size());
186
// client.removeListenerLocal(callbackHandler);
187
// assertEquals(1, callbackHandlers.size());
188
// client.disconnect();
191
// log.info(getName() + " PASSES");
195
// public void testRemoveListenerLocalPolledCallbacks() throws Throwable
197
// log.info("entering " + getName());
200
// String host = InetAddress.getLocalHost().getHostAddress();
201
// int port = PortUtil.findFreePort(host);
202
// String locatorURI = "socket://" + host + ":" + port;
203
// InvokerLocator locator = new InvokerLocator(locatorURI);
204
// HashMap serverConfig = new HashMap();
205
// serverConfig.put(InvokerLocator.FORCE_REMOTE, "true");
206
// Connector connector = new Connector(locator, serverConfig);
207
// connector.create();
208
// connector.addInvocationHandler("test", new TestHandler());
209
// connector.start();
211
// // Connect client, add a callback handler, then call Client.removeListener()
212
// // and verify that callback handler has been removed on server.
213
// HashMap clientConfig = new HashMap();
214
// clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
215
// Client client = new Client(locator, clientConfig);
217
// Integer i = (Integer) client.invoke(new Integer(7));
218
// assertEquals(8, i.intValue());
219
// TestCallbackHandler callbackHandler = new TestCallbackHandler();
220
// client.addListener(callbackHandler, null, null, false);
221
// ServerInvoker serverInvoker = connector.getServerInvoker();
222
// Field field = ServerInvoker.class.getDeclaredField("callbackHandlers");
223
// field.setAccessible(true);
224
// Map callbackHandlers = (Map) field.get(serverInvoker);
225
// assertEquals(1, callbackHandlers.size());
226
// client.removeListener(callbackHandler);
227
// assertEquals(0, callbackHandlers.size());
228
// client.disconnect();
230
// // Connect client, add a callback handler, then call Client.removeListenerLocal()
231
// // and verify that callback handler still exists on server.
232
// client = new Client(locator, clientConfig);
234
// i = (Integer) client.invoke(new Integer(11));
235
// assertEquals(12, i.intValue());
236
// callbackHandler = new TestCallbackHandler();
237
// client.addListener(callbackHandler, null, null, false);
238
// assertEquals(1, callbackHandlers.size());
239
// client.removeListenerLocal(callbackHandler);
240
// assertEquals(1, callbackHandlers.size());
241
// client.disconnect();
244
// log.info(getName() + " PASSES");
248
// public void testRemoveListenerLocalPullCallbacks() throws Throwable
250
// log.info("entering " + getName());
253
// String host = InetAddress.getLocalHost().getHostAddress();
254
// int port = PortUtil.findFreePort(host);
255
// String locatorURI = "socket://" + host + ":" + port;
256
// InvokerLocator locator = new InvokerLocator(locatorURI);
257
// HashMap serverConfig = new HashMap();
258
// serverConfig.put(InvokerLocator.FORCE_REMOTE, "true");
259
// Connector connector = new Connector(locator, serverConfig);
260
// connector.create();
261
// connector.addInvocationHandler("test", new TestHandler());
262
// connector.start();
264
// // Connect client, add a callback handler, then call Client.removeListener()
265
// // and verify that callback handler has been removed on server.
266
// HashMap clientConfig = new HashMap();
267
// clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
268
// Client client = new Client(locator, clientConfig);
270
// Integer i = (Integer) client.invoke(new Integer(7));
271
// assertEquals(8, i.intValue());
272
// TestCallbackHandler callbackHandler = new TestCallbackHandler();
273
// client.addListener(callbackHandler);
274
// ServerInvoker serverInvoker = connector.getServerInvoker();
275
// Field field = ServerInvoker.class.getDeclaredField("callbackHandlers");
276
// field.setAccessible(true);
277
// Map callbackHandlers = (Map) field.get(serverInvoker);
278
// assertEquals(1, callbackHandlers.size());
279
// client.removeListener(callbackHandler);
280
// assertEquals(0, callbackHandlers.size());
281
// client.disconnect();
283
// // Connect client, add a callback handler, then call Client.removeListenerLocal()
284
// // and verify that callback handler still exists on server.
285
// client = new Client(locator, clientConfig);
287
// i = (Integer) client.invoke(new Integer(11));
288
// assertEquals(12, i.intValue());
289
// callbackHandler = new TestCallbackHandler();
290
// client.addListener(callbackHandler);
291
// assertEquals(1, callbackHandlers.size());
292
// client.removeListenerLocal(callbackHandler);
293
// assertEquals(1, callbackHandlers.size());
294
// client.disconnect();
297
// log.info(getName() + " PASSES");
301
public class TestHandler implements ServerInvocationHandler
304
public void setMBeanServer(MBeanServer server) {}
305
public void setInvoker(ServerInvoker invoker) {}
307
public Object invoke(InvocationRequest invocation) throws Throwable
309
Integer i = (Integer) invocation.getParameter();
310
return new Integer(i.intValue() + 1);
313
public void addListener(InvokerCallbackHandler callbackHandler) {}
314
public void removeListener(InvokerCallbackHandler callbackHandler) {}
318
public class TestListener implements ConnectionListener
320
public void handleConnectionException(Throwable throwable, Client client) {}
324
public class TestCallbackHandler implements InvokerCallbackHandler
326
public void handleCallback(Callback callback) throws HandleCallbackException
328
log.info("received callback: " + callback);
b'\\ No newline at end of file'