~ubuntu-branches/ubuntu/oneiric/libjboss-remoting-java/oneiric

« back to all changes in this revision

Viewing changes to src/tests/org/jboss/test/remoting/transport/bisocket/SecondaryServerSocketTestCase.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: package-import@ubuntu.com-20110909140103-hqokx61534tas9rg
Tags: 2.5.3.SP1-1
* Newer but not newest upstream release. Do not build samples.
* Change debian/watch to upstream's svn repo.
* Add patch to fix compile error caused by tomcat update.
  (Closes: #628303)
* Switch to source format 3.0.
* Switch to debhelper level 7.
* Remove useless Depends.
* Update Standards-Version: 3.9.2.
* Update README.source.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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.
 
6
*
 
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.
 
11
*
 
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.
 
16
*
 
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.
 
21
*/
 
22
package org.jboss.test.remoting.transport.bisocket;
 
23
 
 
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;
 
30
import java.util.Map;
 
31
import java.util.Set;
 
32
 
 
33
import javax.management.MBeanServer;
 
34
import javax.xml.parsers.DocumentBuilderFactory;
 
35
 
 
36
import junit.framework.TestCase;
 
37
 
 
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;
 
54
 
 
55
 
 
56
/**
 
57
 * Unit tests for JBREM-755.
 
58
 * 
 
59
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
 
60
 * @version $Revision: 3283 $
 
61
 * <p>
 
62
 * Copyright Aug 22, 2007
 
63
 * </p>
 
64
 */
 
65
public class SecondaryServerSocketTestCase extends TestCase
 
66
{
 
67
   public static int port;
 
68
   
 
69
   private static Logger log = Logger.getLogger(SecondaryServerSocketTestCase.class);
 
70
   private static boolean firstTime = true;
 
71
   
 
72
   // remoting server connector
 
73
   private Connector connector;
 
74
   private InvokerLocator serverLocator;
 
75
   private TestInvocationHandler invocationHandler;
 
76
 
 
77
   
 
78
   /**
 
79
    * Sets up target remoting server.
 
80
    */
 
81
   public void setUp() throws Exception
 
82
   {
 
83
      if (firstTime)
 
84
      {
 
85
         firstTime = false;
 
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);  
 
92
      }
 
93
   }
 
94
 
 
95
   
 
96
   public void tearDown()
 
97
   {
 
98
   }
 
99
   
 
100
   
 
101
   /**
 
102
    * Verify that secondary port is correctly set when specifed in InvokerLocator.
 
103
    */
 
104
   public void testSetSecondaryPortByInvokerLocator() throws Throwable
 
105
   {
 
106
      log.info("entering " + getName());
 
107
      
 
108
      // Start server.
 
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);
 
120
      connector.create();
 
121
      invocationHandler = new TestInvocationHandler();
 
122
      connector.addInvocationHandler("sample", invocationHandler);
 
123
      connector.start();
 
124
      
 
125
      // Create client.
 
126
      HashMap clientConfig = new HashMap();
 
127
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
 
128
      addExtraClientConfig(clientConfig);
 
129
      Client client = new Client(serverLocator, clientConfig);
 
130
      client.connect();
 
131
      assertEquals("abc", client.invoke("abc"));
 
132
      log.info("client is connected");
 
133
      
 
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());
 
147
 
 
148
      client.disconnect();
 
149
      connector.stop();
 
150
   }
 
151
   
 
152
   
 
153
   /**
 
154
    * Verify that secondary bind port is correctly set when specifed in config map.
 
155
    */
 
156
   public void testSetSecondaryBindPortByConfigMap() throws Throwable
 
157
   {
 
158
      log.info("entering " + getName());
 
159
      
 
160
      // Start server.
 
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);
 
172
      connector.create();
 
173
      invocationHandler = new TestInvocationHandler();
 
174
      connector.addInvocationHandler("sample", invocationHandler);
 
175
      connector.start();
 
176
      
 
177
      // Create client.
 
178
      HashMap clientConfig = new HashMap();
 
179
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
 
180
      addExtraClientConfig(clientConfig);
 
181
      Client client = new Client(serverLocator, clientConfig);
 
182
      client.connect();
 
183
      assertEquals("abc", client.invoke("abc"));
 
184
      log.info("client is connected");
 
185
      
 
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());
 
199
 
 
200
      client.disconnect();
 
201
      connector.stop();
 
202
   }
 
203
   
 
204
   
 
205
   /**
 
206
    * Verify that secondary bind port is correctly set by
 
207
    * BisocketServerInvoker.setSecondaryBindPort().
 
208
    */
 
209
   public void testSetSecondaryBindPortBySetter() throws Throwable
 
210
   {
 
211
      log.info("entering " + getName());
 
212
      
 
213
      // Start server.
 
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);
 
224
      connector.create();
 
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);
 
230
      connector.start();
 
231
      
 
232
      // Create client.
 
233
      HashMap clientConfig = new HashMap();
 
234
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
 
235
      addExtraClientConfig(clientConfig);
 
236
      Client client = new Client(serverLocator, clientConfig);
 
237
      client.connect();
 
238
      assertEquals("abc", client.invoke("abc"));
 
239
      log.info("client is connected");
 
240
      
 
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());
 
253
 
 
254
      client.disconnect();
 
255
      connector.stop();
 
256
   }
 
257
   
 
258
   
 
259
   /**
 
260
    * Verify that secondary bind port is correctly set by when specified
 
261
    * in XML document.
 
262
    */
 
263
   public void testSetSecondaryBindPortByXMLConfig() throws Throwable
 
264
   {
 
265
      log.info("entering " + getName());
 
266
      
 
267
      // Start server.
 
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);
 
275
      
 
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());
 
289
      connector.create();
 
290
      invocationHandler = new TestInvocationHandler();
 
291
      connector.addInvocationHandler("sample", invocationHandler);
 
292
      connector.start();
 
293
      
 
294
      String locatorURI = connector.getInvokerLocator();
 
295
      log.info("Started remoting server with locator uri of: " + locatorURI);
 
296
      serverLocator = new InvokerLocator(locatorURI);
 
297
      
 
298
      // Create client.
 
299
      HashMap clientConfig = new HashMap();
 
300
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
 
301
      addExtraClientConfig(clientConfig);
 
302
      Client client = new Client(serverLocator, clientConfig);
 
303
      client.connect();
 
304
      assertEquals("abc", client.invoke("abc"));
 
305
      log.info("client is connected");
 
306
      
 
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());
 
320
 
 
321
      client.disconnect();
 
322
      connector.stop();
 
323
   }
 
324
   
 
325
   
 
326
   /**
 
327
    * Verify that secondary connect port is correctly set when specifed in config map.
 
328
    */
 
329
   public void testSetSecondaryConnectPortByConfigMap() throws Throwable
 
330
   {
 
331
      log.info("entering " + getName());
 
332
      
 
333
      // Start server.
 
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);
 
345
      connector.create();
 
346
      invocationHandler = new TestInvocationHandler();
 
347
      connector.addInvocationHandler("sample", invocationHandler);
 
348
      connector.start();
 
349
      
 
350
      // Create client.
 
351
      HashMap clientConfig = new HashMap();
 
352
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
 
353
      addExtraClientConfig(clientConfig);
 
354
      Client client = new Client(serverLocator, clientConfig);
 
355
      client.connect();
 
356
      assertEquals("abc", client.invoke("abc"));
 
357
      log.info("client is connected");
 
358
      
 
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());
 
367
 
 
368
      client.disconnect();
 
369
      connector.stop();
 
370
   }
 
371
   
 
372
   
 
373
   /**
 
374
    * Verifies that host in secondary InvokerLocator can be set by
 
375
    * clientConnectAddress parameter.
 
376
    */
 
377
   public void testClientConnectAddressFromXML() throws Throwable
 
378
   {
 
379
      log.info("entering " + getName());
 
380
      
 
381
      // Start server.
 
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);
 
389
      
 
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());
 
403
      connector.create();
 
404
      invocationHandler = new TestInvocationHandler();
 
405
      connector.addInvocationHandler("sample", invocationHandler);
 
406
      connector.start();
 
407
      
 
408
      String locatorURI = connector.getInvokerLocator();
 
409
      log.info("Started remoting server with locator uri of: " + locatorURI);
 
410
      serverLocator = new InvokerLocator(locatorURI);
 
411
      
 
412
      // Create client.
 
413
      HashMap clientConfig = new HashMap();
 
414
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
 
415
      addExtraClientConfig(clientConfig);
 
416
      Client client = new Client(serverLocator, clientConfig);
 
417
      client.connect();
 
418
      assertEquals("abc", client.invoke("abc"));
 
419
      log.info("client is connected");
 
420
      
 
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());
 
429
 
 
430
      client.disconnect();
 
431
      connector.stop();
 
432
   }
 
433
   
 
434
   
 
435
   /**
 
436
    * Verifies that host in secondary InvokerLocator can be set by InvokerLocator.
 
437
    */
 
438
   public void testClientConnectAddressFromInvokerLocator() throws Throwable
 
439
   {
 
440
      log.info("entering " + getName());
 
441
      
 
442
      // Start server.
 
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);
 
453
      connector.create();
 
454
      invocationHandler = new TestInvocationHandler();
 
455
      connector.addInvocationHandler("sample", invocationHandler);
 
456
      connector.start();
 
457
      
 
458
      // Create client.
 
459
      HashMap clientConfig = new HashMap();
 
460
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
 
461
      addExtraClientConfig(clientConfig);
 
462
      Client client = new Client(serverLocator, clientConfig);
 
463
      client.connect();
 
464
      assertEquals("abc", client.invoke("abc"));
 
465
      log.info("client is connected");
 
466
      
 
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());
 
475
      client.disconnect();
 
476
      connector.stop();
 
477
   }
 
478
   
 
479
   
 
480
   /**
 
481
    * Verifies that secondary ServerSocket is bound to the same address as the
 
482
    * primary ServerSocket.
 
483
    */
 
484
   public void testServerBindAddress() throws Throwable
 
485
   {
 
486
      log.info("entering " + getName());
 
487
      
 
488
      // Start server.
 
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);
 
500
      connector.create();
 
501
      invocationHandler = new TestInvocationHandler();
 
502
      connector.addInvocationHandler("sample", invocationHandler);
 
503
      connector.start();
 
504
      
 
505
      // Create client.
 
506
      HashMap clientConfig = new HashMap();
 
507
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
 
508
      addExtraClientConfig(clientConfig);
 
509
      Client client = new Client(serverLocator, clientConfig);
 
510
      client.connect();
 
511
      assertEquals("abc", client.invoke("abc"));
 
512
      log.info("client is connected");
 
513
      
 
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());
 
526
 
 
527
      client.disconnect();
 
528
      connector.stop();
 
529
   }
 
530
   
 
531
   
 
532
   protected String getTransport()
 
533
   {
 
534
      return "bisocket";
 
535
   }
 
536
   
 
537
   
 
538
   protected void addExtraClientConfig(Map config) {}
 
539
   protected void addExtraServerConfig(Map config) {}
 
540
   
 
541
 
 
542
   static class TestInvocationHandler implements ServerInvocationHandler
 
543
   {
 
544
      public void addListener(InvokerCallbackHandler callbackHandler) {}
 
545
      public Object invoke(final InvocationRequest invocation) throws Throwable
 
546
      {
 
547
         return invocation.getParameter();
 
548
      }
 
549
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
 
550
      public void setMBeanServer(MBeanServer server) {}
 
551
      public void setInvoker(ServerInvoker invoker) {}
 
552
   }
 
553
}
 
 
b'\\ No newline at end of file'