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.transport.bisocket;
24
import java.io.ByteArrayInputStream;
25
import java.lang.reflect.Field;
26
import java.net.InetAddress;
27
import java.net.ServerSocket;
28
import java.util.HashMap;
29
import java.util.List;
33
import javax.management.MBeanServer;
34
import javax.xml.parsers.DocumentBuilderFactory;
36
import junit.framework.TestCase;
38
import org.apache.log4j.ConsoleAppender;
39
import org.apache.log4j.Level;
40
import org.apache.log4j.Logger;
41
import org.apache.log4j.PatternLayout;
42
import org.jboss.remoting.Client;
43
import org.jboss.remoting.InvocationRequest;
44
import org.jboss.remoting.InvokerLocator;
45
import org.jboss.remoting.ServerInvocationHandler;
46
import org.jboss.remoting.ServerInvoker;
47
import org.jboss.remoting.callback.InvokerCallbackHandler;
48
import org.jboss.remoting.transport.Connector;
49
import org.jboss.remoting.transport.PortUtil;
50
import org.jboss.remoting.transport.bisocket.Bisocket;
51
import org.jboss.remoting.transport.bisocket.BisocketServerInvoker;
52
import org.jboss.remoting.transport.socket.SocketServerInvoker;
53
import org.w3c.dom.Document;
57
* Unit tests for JBREM-755.
59
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
60
* @version $Revision: 3283 $
62
* Copyright Aug 22, 2007
65
public class SecondaryServerSocketTestCase extends TestCase
67
public static int port;
69
private static Logger log = Logger.getLogger(SecondaryServerSocketTestCase.class);
70
private static boolean firstTime = true;
72
// remoting server connector
73
private Connector connector;
74
private InvokerLocator serverLocator;
75
private TestInvocationHandler invocationHandler;
79
* Sets up target remoting server.
81
public void setUp() throws Exception
86
Logger.getLogger("org.jboss.remoting").setLevel(Level.INFO);
87
Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
88
String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
89
PatternLayout layout = new PatternLayout(pattern);
90
ConsoleAppender consoleAppender = new ConsoleAppender(layout);
91
Logger.getRootLogger().addAppender(consoleAppender);
96
public void tearDown()
102
* Verify that secondary port is correctly set when specifed in InvokerLocator.
104
public void testSetSecondaryPortByInvokerLocator() throws Throwable
106
log.info("entering " + getName());
109
String host = InetAddress.getLocalHost().getHostAddress();
110
int PORT = PortUtil.findFreePort(host);
111
port = PortUtil.findFreePort(host);
112
String locatorURI = getTransport() + "://" + host + ":" + port;
113
locatorURI += "/?" + Bisocket.SECONDARY_BIND_PORT + "=" + PORT;
114
serverLocator = new InvokerLocator(locatorURI);
115
log.info("Starting remoting server with locator uri of: " + locatorURI);
116
HashMap config = new HashMap();
117
config.put(InvokerLocator.FORCE_REMOTE, "true");
118
addExtraServerConfig(config);
119
connector = new Connector(serverLocator, config);
121
invocationHandler = new TestInvocationHandler();
122
connector.addInvocationHandler("sample", invocationHandler);
126
HashMap clientConfig = new HashMap();
127
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
128
addExtraClientConfig(clientConfig);
129
Client client = new Client(serverLocator, clientConfig);
131
assertEquals("abc", client.invoke("abc"));
132
log.info("client is connected");
134
// Verify secondary ServerSocket port is set correctly.
135
ServerInvoker serverInvoker = connector.getServerInvoker();
136
assertTrue(serverInvoker instanceof BisocketServerInvoker);
137
Field field = BisocketServerInvoker.class.getDeclaredField("secondaryBindPorts");
138
field.setAccessible(true);
139
List secondaryBindPorts = (List) field.get(serverInvoker);
140
Integer secondaryBindPort = (Integer) secondaryBindPorts.get(0);
141
assertEquals(PORT, secondaryBindPort.intValue());
142
field = BisocketServerInvoker.class.getDeclaredField("secondaryServerSockets");
143
field.setAccessible(true);
144
Set secondaryServerSockets = (Set) field.get(serverInvoker);
145
ServerSocket serverSocket = (ServerSocket) secondaryServerSockets.iterator().next();
146
assertEquals(PORT, serverSocket.getLocalPort());
154
* Verify that secondary bind port is correctly set when specifed in config map.
156
public void testSetSecondaryBindPortByConfigMap() throws Throwable
158
log.info("entering " + getName());
161
String host = InetAddress.getLocalHost().getHostAddress();
162
int PORT = PortUtil.findFreePort(host);
163
port = PortUtil.findFreePort(host);
164
String locatorURI = getTransport() + "://" + host + ":" + port;
165
serverLocator = new InvokerLocator(locatorURI);
166
log.info("Starting remoting server with locator uri of: " + locatorURI);
167
HashMap config = new HashMap();
168
config.put(InvokerLocator.FORCE_REMOTE, "true");
169
config.put(Bisocket.SECONDARY_BIND_PORT, Integer.toString(PORT));
170
addExtraServerConfig(config);
171
connector = new Connector(serverLocator, config);
173
invocationHandler = new TestInvocationHandler();
174
connector.addInvocationHandler("sample", invocationHandler);
178
HashMap clientConfig = new HashMap();
179
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
180
addExtraClientConfig(clientConfig);
181
Client client = new Client(serverLocator, clientConfig);
183
assertEquals("abc", client.invoke("abc"));
184
log.info("client is connected");
186
// Verify secondary ServerSocket port is set correctly.
187
ServerInvoker serverInvoker = connector.getServerInvoker();
188
assertTrue(serverInvoker instanceof BisocketServerInvoker);
189
Field field = BisocketServerInvoker.class.getDeclaredField("secondaryBindPorts");
190
field.setAccessible(true);
191
List secondaryBindPorts = (List) field.get(serverInvoker);
192
Integer secondaryBindPort = (Integer) secondaryBindPorts.get(0);
193
assertEquals(PORT, secondaryBindPort.intValue());
194
field = BisocketServerInvoker.class.getDeclaredField("secondaryServerSockets");
195
field.setAccessible(true);
196
Set secondaryServerSockets = (Set) field.get(serverInvoker);
197
ServerSocket serverSocket = (ServerSocket) secondaryServerSockets.iterator().next();
198
assertEquals(PORT, serverSocket.getLocalPort());
206
* Verify that secondary bind port is correctly set by
207
* BisocketServerInvoker.setSecondaryBindPort().
209
public void testSetSecondaryBindPortBySetter() throws Throwable
211
log.info("entering " + getName());
214
String host = InetAddress.getLocalHost().getHostAddress();
215
int PORT = PortUtil.findFreePort(host);
216
port = PortUtil.findFreePort(host);
217
String locatorURI = getTransport() + "://" + host + ":" + port;
218
serverLocator = new InvokerLocator(locatorURI);
219
log.info("Starting remoting server with locator uri of: " + locatorURI);
220
HashMap config = new HashMap();
221
config.put(InvokerLocator.FORCE_REMOTE, "true");
222
addExtraServerConfig(config);
223
connector = new Connector(serverLocator, config);
225
assertTrue(connector.getServerInvoker() instanceof BisocketServerInvoker);
226
BisocketServerInvoker serverInvoker = (BisocketServerInvoker) connector.getServerInvoker();
227
serverInvoker.setSecondaryBindPort(PORT);
228
invocationHandler = new TestInvocationHandler();
229
connector.addInvocationHandler("sample", invocationHandler);
233
HashMap clientConfig = new HashMap();
234
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
235
addExtraClientConfig(clientConfig);
236
Client client = new Client(serverLocator, clientConfig);
238
assertEquals("abc", client.invoke("abc"));
239
log.info("client is connected");
241
// Verify secondary ServerSocket port is set correctly.
242
Field field = BisocketServerInvoker.class.getDeclaredField("secondaryBindPorts");
243
field.setAccessible(true);
244
List secondaryBindPorts = (List) field.get(serverInvoker);
245
log.info("secondaryBindPorts.size(): " + secondaryBindPorts.size());
246
Integer secondaryBindPort = (Integer) secondaryBindPorts.get(0);
247
assertEquals(PORT, secondaryBindPort.intValue());
248
field = BisocketServerInvoker.class.getDeclaredField("secondaryServerSockets");
249
field.setAccessible(true);
250
Set secondaryServerSockets = (Set) field.get(serverInvoker);
251
ServerSocket serverSocket = (ServerSocket) secondaryServerSockets.iterator().next();
252
assertEquals(PORT, serverSocket.getLocalPort());
260
* Verify that secondary bind port is correctly set by when specified
263
public void testSetSecondaryBindPortByXMLConfig() throws Throwable
265
log.info("entering " + getName());
268
String host = InetAddress.getLocalHost().getHostAddress();
269
int PORT = PortUtil.findFreePort(host);
270
port = PortUtil.findFreePort(host);
271
HashMap config = new HashMap();
272
config.put(InvokerLocator.FORCE_REMOTE, "true");
273
addExtraServerConfig(config);
274
connector = new Connector(config);
276
// Create and set xml configuration document.
277
StringBuffer buf = new StringBuffer();
278
buf.append("<?xml version=\"1.0\"?>\n");
279
buf.append("<config>");
280
buf.append(" <invoker transport=\"" + getTransport() + "\">");
281
buf.append(" <attribute name=\"serverBindAddress\">" + host + "</attribute>");
282
buf.append(" <attribute name=\"serverBindPort\">" + port + "</attribute>");
283
buf.append(" <attribute name=\"secondaryBindPort\">" + PORT + "</attribute>");
284
buf.append(" </invoker>");
285
buf.append("</config>");
286
ByteArrayInputStream bais = new ByteArrayInputStream(buf.toString().getBytes());
287
Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(bais);
288
connector.setConfiguration(xml.getDocumentElement());
290
invocationHandler = new TestInvocationHandler();
291
connector.addInvocationHandler("sample", invocationHandler);
294
String locatorURI = connector.getInvokerLocator();
295
log.info("Started remoting server with locator uri of: " + locatorURI);
296
serverLocator = new InvokerLocator(locatorURI);
299
HashMap clientConfig = new HashMap();
300
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
301
addExtraClientConfig(clientConfig);
302
Client client = new Client(serverLocator, clientConfig);
304
assertEquals("abc", client.invoke("abc"));
305
log.info("client is connected");
307
// Verify secondary ServerSocket port is set correctly.
308
ServerInvoker serverInvoker = connector.getServerInvoker();
309
assertTrue(serverInvoker instanceof BisocketServerInvoker);
310
Field field = BisocketServerInvoker.class.getDeclaredField("secondaryBindPorts");
311
field.setAccessible(true);
312
List secondaryBindPorts = (List) field.get(serverInvoker);
313
Integer secondaryBindPort = (Integer) secondaryBindPorts.get(0);
314
assertEquals(PORT, secondaryBindPort.intValue());
315
field = BisocketServerInvoker.class.getDeclaredField("secondaryServerSockets");
316
field.setAccessible(true);
317
Set secondaryServerSockets = (Set) field.get(serverInvoker);
318
ServerSocket serverSocket = (ServerSocket) secondaryServerSockets.iterator().next();
319
assertEquals(PORT, serverSocket.getLocalPort());
327
* Verify that secondary connect port is correctly set when specifed in config map.
329
public void testSetSecondaryConnectPortByConfigMap() throws Throwable
331
log.info("entering " + getName());
334
String host = InetAddress.getLocalHost().getHostAddress();
335
int PORT = PortUtil.findFreePort(host);
336
port = PortUtil.findFreePort(host);
337
String locatorURI = getTransport() + "://" + host + ":" + port;
338
serverLocator = new InvokerLocator(locatorURI);
339
log.info("Starting remoting server with locator uri of: " + locatorURI);
340
HashMap config = new HashMap();
341
config.put(InvokerLocator.FORCE_REMOTE, "true");
342
config.put(Bisocket.SECONDARY_CONNECT_PORT, Integer.toString(PORT));
343
addExtraServerConfig(config);
344
connector = new Connector(serverLocator, config);
346
invocationHandler = new TestInvocationHandler();
347
connector.addInvocationHandler("sample", invocationHandler);
351
HashMap clientConfig = new HashMap();
352
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
353
addExtraClientConfig(clientConfig);
354
Client client = new Client(serverLocator, clientConfig);
356
assertEquals("abc", client.invoke("abc"));
357
log.info("client is connected");
359
// Verify secondaryLocator is set correctly.
360
ServerInvoker serverInvoker = connector.getServerInvoker();
361
assertTrue(serverInvoker instanceof BisocketServerInvoker);
362
Field field = BisocketServerInvoker.class.getDeclaredField("secondaryLocator");
363
field.setAccessible(true);
364
InvokerLocator secondaryLocator = (InvokerLocator) field.get(serverInvoker);
365
log.info("secondaryLocator: " + secondaryLocator);
366
assertEquals(PORT, secondaryLocator.getPort());
374
* Verifies that host in secondary InvokerLocator can be set by
375
* clientConnectAddress parameter.
377
public void testClientConnectAddressFromXML() throws Throwable
379
log.info("entering " + getName());
382
String HOST = InetAddress.getLocalHost().getHostName();
383
String host = InetAddress.getLocalHost().getHostAddress();
384
port = PortUtil.findFreePort(host);
385
HashMap config = new HashMap();
386
config.put(InvokerLocator.FORCE_REMOTE, "true");
387
addExtraServerConfig(config);
388
connector = new Connector(config);
390
// Create and set xml configuration document.
391
StringBuffer buf = new StringBuffer();
392
buf.append("<?xml version=\"1.0\"?>\n");
393
buf.append("<config>");
394
buf.append(" <invoker transport=\"" + getTransport() + "\">");
395
buf.append(" <attribute name=\"serverBindAddress\">" + host + "</attribute>");
396
buf.append(" <attribute name=\"serverBindPort\">" + port + "</attribute>");
397
buf.append(" <attribute name=\"clientConnectAddress\">" + HOST + "</attribute>");
398
buf.append(" </invoker>");
399
buf.append("</config>");
400
ByteArrayInputStream bais = new ByteArrayInputStream(buf.toString().getBytes());
401
Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(bais);
402
connector.setConfiguration(xml.getDocumentElement());
404
invocationHandler = new TestInvocationHandler();
405
connector.addInvocationHandler("sample", invocationHandler);
408
String locatorURI = connector.getInvokerLocator();
409
log.info("Started remoting server with locator uri of: " + locatorURI);
410
serverLocator = new InvokerLocator(locatorURI);
413
HashMap clientConfig = new HashMap();
414
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
415
addExtraClientConfig(clientConfig);
416
Client client = new Client(serverLocator, clientConfig);
418
assertEquals("abc", client.invoke("abc"));
419
log.info("client is connected");
421
// Verify secondaryLocator is set correctly.
422
ServerInvoker serverInvoker = connector.getServerInvoker();
423
assertTrue(serverInvoker instanceof BisocketServerInvoker);
424
Field field = BisocketServerInvoker.class.getDeclaredField("secondaryLocator");
425
field.setAccessible(true);
426
InvokerLocator secondaryLocator = (InvokerLocator) field.get(serverInvoker);
427
log.info("secondaryLocator: " + secondaryLocator);
428
assertEquals(HOST, secondaryLocator.getHost());
436
* Verifies that host in secondary InvokerLocator can be set by InvokerLocator.
438
public void testClientConnectAddressFromInvokerLocator() throws Throwable
440
log.info("entering " + getName());
443
String HOST = InetAddress.getLocalHost().getHostName();
444
String host = InetAddress.getLocalHost().getHostAddress();
445
port = PortUtil.findFreePort(host);
446
String locatorURI = getTransport() + "://" + HOST + ":" + port;
447
serverLocator = new InvokerLocator(locatorURI);
448
log.info("Starting remoting server with locator uri of: " + locatorURI);
449
HashMap config = new HashMap();
450
config.put(InvokerLocator.FORCE_REMOTE, "true");
451
addExtraServerConfig(config);
452
connector = new Connector(serverLocator, config);
454
invocationHandler = new TestInvocationHandler();
455
connector.addInvocationHandler("sample", invocationHandler);
459
HashMap clientConfig = new HashMap();
460
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
461
addExtraClientConfig(clientConfig);
462
Client client = new Client(serverLocator, clientConfig);
464
assertEquals("abc", client.invoke("abc"));
465
log.info("client is connected");
467
// Verify secondaryLocator is set correctly.
468
ServerInvoker serverInvoker = connector.getServerInvoker();
469
assertTrue(serverInvoker instanceof BisocketServerInvoker);
470
Field field = BisocketServerInvoker.class.getDeclaredField("secondaryLocator");
471
field.setAccessible(true);
472
InvokerLocator secondaryLocator = (InvokerLocator) field.get(serverInvoker);
473
log.info("secondaryLocator: " + secondaryLocator);
474
assertEquals(HOST, secondaryLocator.getHost());
481
* Verifies that secondary ServerSocket is bound to the same address as the
482
* primary ServerSocket.
484
public void testServerBindAddress() throws Throwable
486
log.info("entering " + getName());
489
String HOST = InetAddress.getLocalHost().getHostName();
490
String host = InetAddress.getLocalHost().getHostAddress();
491
port = PortUtil.findFreePort(host);
492
String locatorURI = getTransport() + "://" + host + ":" + port;
493
serverLocator = new InvokerLocator(locatorURI);
494
log.info("Starting remoting server with locator uri of: " + locatorURI);
495
HashMap config = new HashMap();
496
config.put(InvokerLocator.FORCE_REMOTE, "true");
497
config.put(ServerInvoker.SERVER_BIND_ADDRESS_KEY, HOST);
498
addExtraServerConfig(config);
499
connector = new Connector(serverLocator, config);
501
invocationHandler = new TestInvocationHandler();
502
connector.addInvocationHandler("sample", invocationHandler);
506
HashMap clientConfig = new HashMap();
507
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
508
addExtraClientConfig(clientConfig);
509
Client client = new Client(serverLocator, clientConfig);
511
assertEquals("abc", client.invoke("abc"));
512
log.info("client is connected");
514
// Verify secondary ServerSocket port is set correctly.
515
ServerInvoker serverInvoker = connector.getServerInvoker();
516
assertTrue(serverInvoker instanceof BisocketServerInvoker);
517
Field field = SocketServerInvoker.class.getDeclaredField("serverSockets");
518
field.setAccessible(true);
519
List serverSockets = (List) field.get(serverInvoker);
520
ServerSocket primaryServerSocket = (ServerSocket) serverSockets.get(0);
521
field = BisocketServerInvoker.class.getDeclaredField("secondaryServerSockets");
522
field.setAccessible(true);
523
Set secondaryServerSockets = (Set) field.get(serverInvoker);
524
ServerSocket secondaryServerSocket = (ServerSocket) secondaryServerSockets.iterator().next();
525
assertEquals(primaryServerSocket.getInetAddress(), secondaryServerSocket.getInetAddress());
532
protected String getTransport()
538
protected void addExtraClientConfig(Map config) {}
539
protected void addExtraServerConfig(Map config) {}
542
static class TestInvocationHandler implements ServerInvocationHandler
544
public void addListener(InvokerCallbackHandler callbackHandler) {}
545
public Object invoke(final InvocationRequest invocation) throws Throwable
547
return invocation.getParameter();
549
public void removeListener(InvokerCallbackHandler callbackHandler) {}
550
public void setMBeanServer(MBeanServer server) {}
551
public void setInvoker(ServerInvoker invoker) {}
b'\\ No newline at end of file'