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

« back to all changes in this revision

Viewing changes to src/tests/org/jboss/test/remoting/transport/bisocket/multihome/BisocketMultihomeTestCase.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
/*
 
3
* JBoss, Home of Professional Open Source
 
4
* Copyright 2005, JBoss Inc., and individual contributors as indicated
 
5
* by the @authors tag. See the copyright.txt in the distribution for a
 
6
* full listing of individual contributors.
 
7
*
 
8
* This is free software; you can redistribute it and/or modify it
 
9
* under the terms of the GNU Lesser General Public License as
 
10
* published by the Free Software Foundation; either version 2.1 of
 
11
* the License, or (at your option) any later version.
 
12
*
 
13
* This software is distributed in the hope that it will be useful,
 
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
16
* Lesser General Public License for more details.
 
17
*
 
18
* You should have received a copy of the GNU Lesser General Public
 
19
* License along with this software; if not, write to the Free
 
20
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 
21
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 
22
*/
 
23
package org.jboss.test.remoting.transport.bisocket.multihome;
 
24
 
 
25
import java.lang.reflect.Field;
 
26
import java.net.InetAddress;
 
27
import java.net.NetworkInterface;
 
28
import java.net.ServerSocket;
 
29
import java.util.ArrayList;
 
30
import java.util.Enumeration;
 
31
import java.util.HashMap;
 
32
import java.util.HashSet;
 
33
import java.util.Iterator;
 
34
import java.util.List;
 
35
import java.util.Map;
 
36
import java.util.Set;
 
37
 
 
38
import org.jboss.remoting.Home;
 
39
import org.jboss.remoting.InvokerLocator;
 
40
import org.jboss.remoting.transport.AddressUtil;
 
41
import org.jboss.remoting.transport.Connector;
 
42
import org.jboss.remoting.transport.PortUtil;
 
43
import org.jboss.remoting.transport.bisocket.Bisocket;
 
44
import org.jboss.remoting.transport.bisocket.BisocketServerInvoker;
 
45
import org.jboss.test.remoting.multihome.MultihomeTestParent;
 
46
import org.jboss.test.remoting.multihome.TestInvocationHandler;
 
47
 
 
48
 
 
49
/**
 
50
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
 
51
 * @version $Revision: 1.1 $
 
52
 * <p>
 
53
 * Copyright Jan 8, 2008
 
54
 * </p>
 
55
 */
 
56
public class BisocketMultihomeTestCase extends MultihomeTestParent
 
57
{
 
58
   protected ArrayList interfaces = new ArrayList();
 
59
   
 
60
   protected String getTransport()
 
61
   {
 
62
      return "bisocket";
 
63
   }
 
64
   
 
65
   protected void addExtraCallbackConfig(Map config)
 
66
   {
 
67
      config.put(Bisocket.IS_CALLBACK_SERVER, "true");
 
68
   }
 
69
   
 
70
   public void testSecondaryBindPorts() throws Throwable
 
71
   {
 
72
      log.info("entering " + getName());
 
73
 
 
74
      // Start server.
 
75
      localCreateLocatorURI();
 
76
      InetAddress address = (InetAddress) interfaces.get(0);
 
77
      int port = PortUtil.findFreePort(address.getHostAddress());
 
78
      Set secondaryBindPorts = new HashSet();
 
79
      secondaryBindPorts.add(new Integer(port));
 
80
      StringBuffer sb = new StringBuffer(Integer.toString(port));
 
81
      for (int i = 1; i < interfaces.size(); i++)
 
82
      {
 
83
         address = (InetAddress) interfaces.get(i);
 
84
         port = PortUtil.findFreePort(address.getHostAddress());
 
85
         sb.append('!').append(port);
 
86
         secondaryBindPorts.add(new Integer(port));
 
87
      }
 
88
      String secondaryBindPortsString = sb.toString();
 
89
      log.info("secondaryBindPorts: " + secondaryBindPortsString);
 
90
      locatorURI += "&" + Bisocket.SECONDARY_BIND_PORTS + "=" + secondaryBindPortsString;
 
91
      HashMap config = new HashMap();
 
92
      addExtraServerConfig(config);
 
93
      connector = new Connector(locatorURI, config);
 
94
      connector.create();
 
95
      invocationHandler = new TestInvocationHandler();
 
96
      connector.addInvocationHandler("test", invocationHandler);
 
97
      connector.start();
 
98
      
 
99
      // Check bind ports in secondary locator.
 
100
      BisocketServerInvoker invoker = (BisocketServerInvoker) connector.getServerInvoker();
 
101
      Field field = BisocketServerInvoker.class.getDeclaredField("secondaryLocator");
 
102
      field.setAccessible(true);
 
103
      InvokerLocator secondaryLocator = (InvokerLocator) field.get(invoker);
 
104
      log.info("secondaryLocator: " + secondaryLocator);
 
105
      List homes = secondaryLocator.getHomeList();
 
106
      Home h = (Home) homes.get(0);
 
107
      StringBuffer sb2 = new StringBuffer(Integer.toString(h.port));
 
108
      for (int i = 1; i < homes.size(); i++)
 
109
      {
 
110
         h = (Home) homes.get(i);
 
111
         sb2.append('!').append(h.port);
 
112
      }
 
113
      log.info("sb2: " + sb2.toString());
 
114
      assertEquals(secondaryBindPortsString, sb2.toString());
 
115
      
 
116
      // Check bind ports in secondary server sockets.
 
117
      field = BisocketServerInvoker.class.getDeclaredField("secondaryServerSockets");
 
118
      field.setAccessible(true);
 
119
      Set secondaryServerSockets = (Set) field.get(invoker);
 
120
      assertEquals(secondaryBindPorts.size(), secondaryServerSockets.size());
 
121
      Set ports = new HashSet();
 
122
      Iterator it = secondaryServerSockets.iterator();
 
123
      while (it.hasNext())
 
124
      {
 
125
         ServerSocket ss = (ServerSocket) it.next();
 
126
         ports.add(new Integer(ss.getLocalPort()));
 
127
      }
 
128
      assertEquals(secondaryBindPorts, ports);
 
129
      
 
130
      shutdownServer();
 
131
      log.info(getName() + " PASSES");
 
132
   }
 
133
   
 
134
   
 
135
   public void testSecondaryConnectPorts() throws Throwable
 
136
   {
 
137
      log.info("entering " + getName());
 
138
 
 
139
      // Start server.
 
140
      localCreateLocatorURI();
 
141
      InetAddress address = (InetAddress) interfaces.get(0);
 
142
      int port = PortUtil.findFreePort(address.getHostAddress());
 
143
      Set secondaryBindPorts = new HashSet();
 
144
      secondaryBindPorts.add(new Integer(port));
 
145
      StringBuffer sb = new StringBuffer(Integer.toString(port));
 
146
      for (int i = 1; i < interfaces.size(); i++)
 
147
      {
 
148
         address = (InetAddress) interfaces.get(i);
 
149
         port = PortUtil.findFreePort(address.getHostAddress());
 
150
         sb.append('!').append(port);
 
151
         secondaryBindPorts.add(new Integer(port));
 
152
      }
 
153
      String secondaryBindPortsString = sb.toString();
 
154
      log.info("secondaryBindPorts: " + secondaryBindPortsString);
 
155
      locatorURI += "&" + Bisocket.SECONDARY_BIND_PORTS + "=" + secondaryBindPortsString;
 
156
      
 
157
      address = (InetAddress) interfaces.get(0);
 
158
      port = PortUtil.findFreePort(address.getHostAddress());
 
159
      StringBuffer sb2 = new StringBuffer(Integer.toString(port));
 
160
      for (int i = 1; i < interfaces.size(); i++)
 
161
      {
 
162
         address = (InetAddress) interfaces.get(i);
 
163
         port = PortUtil.findFreePort(address.getHostAddress());
 
164
         sb2.append('!').append(port);
 
165
      }
 
166
      String secondaryConnectPortsString = sb2.toString();
 
167
      log.info("secondaryConnectPorts: " + secondaryConnectPortsString);
 
168
      locatorURI += "&" + Bisocket.SECONDARY_CONNECT_PORTS + "=" + secondaryConnectPortsString;
 
169
      log.info("locatorURI: " + locatorURI);
 
170
      HashMap config = new HashMap();
 
171
      addExtraServerConfig(config);
 
172
      connector = new Connector(locatorURI, config);
 
173
      connector.create();
 
174
      invocationHandler = new TestInvocationHandler();
 
175
      connector.addInvocationHandler("test", invocationHandler);
 
176
      connector.start();
 
177
      
 
178
      // Check connect ports in secondary locator.
 
179
      BisocketServerInvoker invoker = (BisocketServerInvoker) connector.getServerInvoker();
 
180
      Field field = BisocketServerInvoker.class.getDeclaredField("secondaryLocator");
 
181
      field.setAccessible(true);
 
182
      InvokerLocator secondaryLocator = (InvokerLocator) field.get(invoker);
 
183
      log.info("secondaryLocator: " + secondaryLocator);
 
184
      List homes = secondaryLocator.getHomeList();
 
185
      Home h = (Home) homes.get(0);
 
186
      StringBuffer sb3 = new StringBuffer(Integer.toString(h.port));
 
187
      for (int i = 1; i < homes.size(); i++)
 
188
      {
 
189
         h = (Home) homes.get(i);
 
190
         sb3.append('!').append(h.port);
 
191
      }
 
192
      log.info("sb3: " + sb3.toString());
 
193
      assertEquals(secondaryConnectPortsString, sb3.toString());
 
194
      
 
195
      // Check bind ports in secondary server sockets.
 
196
      field = BisocketServerInvoker.class.getDeclaredField("secondaryServerSockets");
 
197
      field.setAccessible(true);
 
198
      Set secondaryServerSockets = (Set) field.get(invoker);
 
199
      assertEquals(secondaryBindPorts.size(), secondaryServerSockets.size());
 
200
      Set ports = new HashSet();
 
201
      Iterator it = secondaryServerSockets.iterator();
 
202
      while (it.hasNext())
 
203
      {
 
204
         ServerSocket ss = (ServerSocket) it.next();
 
205
         ports.add(new Integer(ss.getLocalPort()));
 
206
      }
 
207
      assertEquals(secondaryBindPorts, ports);
 
208
      
 
209
      shutdownServer();
 
210
      log.info(getName() + " PASSES");
 
211
   }
 
212
   
 
213
   
 
214
   public void testMismatchedSecondaryPorts() throws Throwable
 
215
   {
 
216
      log.info("entering " + getName());
 
217
 
 
218
      // Start server.
 
219
      localCreateLocatorURI();
 
220
      InetAddress address = (InetAddress) interfaces.get(0);
 
221
      int port = PortUtil.findFreePort(address.getHostAddress());
 
222
      Set secondaryBindPorts = new HashSet();
 
223
      secondaryBindPorts.add(new Integer(port));
 
224
      StringBuffer sb = new StringBuffer(Integer.toString(port));
 
225
      for (int i = 1; i < interfaces.size(); i++)
 
226
      {
 
227
         address = (InetAddress) interfaces.get(i);
 
228
         port = PortUtil.findFreePort(address.getHostAddress());
 
229
         sb.append('!').append(port);
 
230
         secondaryBindPorts.add(new Integer(port));
 
231
      }
 
232
      String secondaryBindPortsString = sb.toString();
 
233
      log.info("secondaryBindPorts: " + secondaryBindPortsString);
 
234
      locatorURI += "&" + Bisocket.SECONDARY_BIND_PORTS + "=" + secondaryBindPortsString;
 
235
      
 
236
      address = (InetAddress) interfaces.get(0);
 
237
      port = PortUtil.findFreePort(address.getHostAddress());
 
238
      StringBuffer sb2 = new StringBuffer(Integer.toString(port));
 
239
      for (int i = 1; i < interfaces.size(); i++)
 
240
      {
 
241
         address = (InetAddress) interfaces.get(i);
 
242
         port = PortUtil.findFreePort(address.getHostAddress());
 
243
         sb2.append('!').append(port);
 
244
      }
 
245
      
 
246
      // Add extra secondary connect port.
 
247
      sb2.append('!').append(9876);
 
248
      String secondaryConnectPortsString = sb2.toString();
 
249
      log.info("secondaryConnectPorts: " + secondaryConnectPortsString);
 
250
      locatorURI += "&" + Bisocket.SECONDARY_CONNECT_PORTS + "=" + secondaryConnectPortsString;
 
251
      log.info("locatorURI: " + locatorURI);
 
252
      HashMap config = new HashMap();
 
253
      addExtraServerConfig(config);
 
254
      connector = new Connector(locatorURI, config);
 
255
      connector.create();
 
256
      invocationHandler = new TestInvocationHandler();
 
257
      connector.addInvocationHandler("test", invocationHandler);
 
258
      connector.start();
 
259
      
 
260
      // Check bind ports in secondary locator.
 
261
      BisocketServerInvoker invoker = (BisocketServerInvoker) connector.getServerInvoker();
 
262
      Field field = BisocketServerInvoker.class.getDeclaredField("secondaryLocator");
 
263
      field.setAccessible(true);
 
264
      InvokerLocator secondaryLocator = (InvokerLocator) field.get(invoker);
 
265
      log.info("secondaryLocator: " + secondaryLocator);
 
266
      List homes = secondaryLocator.getHomeList();
 
267
      Home h = (Home) homes.get(0);
 
268
      StringBuffer sb3 = new StringBuffer(Integer.toString(h.port));
 
269
      for (int i = 1; i < homes.size(); i++)
 
270
      {
 
271
         h = (Home) homes.get(i);
 
272
         sb3.append('!').append(h.port);
 
273
      }
 
274
      log.info("sb3: " + sb3.toString());
 
275
      assertEquals(secondaryBindPortsString, sb3.toString());
 
276
      
 
277
      // Check bind ports in secondary server sockets.
 
278
      field = BisocketServerInvoker.class.getDeclaredField("secondaryServerSockets");
 
279
      field.setAccessible(true);
 
280
      Set secondaryServerSockets = (Set) field.get(invoker);
 
281
      assertEquals(secondaryBindPorts.size(), secondaryServerSockets.size());
 
282
      Set ports = new HashSet();
 
283
      Iterator it = secondaryServerSockets.iterator();
 
284
      while (it.hasNext())
 
285
      {
 
286
         ServerSocket ss = (ServerSocket) it.next();
 
287
         ports.add(new Integer(ss.getLocalPort()));
 
288
      }
 
289
      assertEquals(secondaryBindPorts, ports);
 
290
      
 
291
      shutdownServer();
 
292
      log.info(getName() + " PASSES");
 
293
   }
 
294
   
 
295
   
 
296
   protected void localCreateLocatorURI() throws Exception
 
297
   {
 
298
      StringBuffer sb = new StringBuffer();
 
299
      Enumeration e1 = NetworkInterface.getNetworkInterfaces();
 
300
      boolean first = true;
 
301
      int counter = 0;
 
302
      
 
303
      loop: while (e1.hasMoreElements())
 
304
      {
 
305
         NetworkInterface iface = (NetworkInterface) e1.nextElement();
 
306
         Enumeration e2 = iface.getInetAddresses();
 
307
         while (e2.hasMoreElements())
 
308
         {
 
309
            if (++counter > 5) break loop;
 
310
            InetAddress address = (InetAddress) e2.nextElement();
 
311
            String host = address.getHostAddress();
 
312
            
 
313
            if (AddressUtil.checkAddress(host))
 
314
            {
 
315
               log.info("host is functional: " + host);
 
316
               int port = PortUtil.findFreePort(host);
 
317
               if (first)
 
318
                  first = false;
 
319
               else
 
320
                  sb.append('!');
 
321
               sb.append(host).append(':').append(port);
 
322
               interfaces.add(address);
 
323
            }
 
324
            else
 
325
            {
 
326
               log.info("skipping host: " + host);
 
327
            }
 
328
         }
 
329
      }
 
330
      
 
331
      locatorURI = getTransport() + "://" + InvokerLocator.MULTIHOME + getPath() + "/?";
 
332
      locatorURI += InvokerLocator.HOMES_KEY + "=" + sb.toString();
 
333
   }
 
334
}
 
335