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

« back to all changes in this revision

Viewing changes to src/tests/org/jboss/test/remoting/multihome/MultihomeTestParent.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.multihome;
 
23
 
 
24
import java.net.InetAddress;
 
25
import java.net.NetworkInterface;
 
26
import java.util.HashMap;
 
27
import java.util.Iterator;
 
28
import java.util.List;
 
29
import java.util.Map;
 
30
import java.util.Enumeration;
 
31
 
 
32
import javax.management.MBeanServer;
 
33
 
 
34
import junit.framework.TestCase;
 
35
 
 
36
import org.apache.log4j.ConsoleAppender;
 
37
import org.apache.log4j.Level;
 
38
import org.apache.log4j.Logger;
 
39
import org.apache.log4j.PatternLayout;
 
40
import org.jboss.logging.XLevel;
 
41
import org.jboss.remoting.Client;
 
42
import org.jboss.remoting.Home;
 
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.Callback;
 
48
import org.jboss.remoting.callback.HandleCallbackException;
 
49
import org.jboss.remoting.callback.InvokerCallbackHandler;
 
50
import org.jboss.remoting.transport.AddressUtil;
 
51
import org.jboss.remoting.transport.Connector;
 
52
import org.jboss.remoting.transport.PortUtil;
 
53
 
 
54
 
 
55
public abstract class MultihomeTestParent extends TestCase
 
56
{
 
57
   protected static Logger log = Logger.getLogger(MultihomeTestParent.class);
 
58
   
 
59
   private static boolean firstTime = true;
 
60
   
 
61
   protected String host;
 
62
   protected int port;
 
63
   protected String locatorURI;
 
64
   protected InvokerLocator serverLocator;
 
65
   protected Connector connector;
 
66
   protected TestInvocationHandler invocationHandler;
 
67
 
 
68
   
 
69
   public void setUp() throws Exception
 
70
   {
 
71
      if (firstTime)
 
72
      {
 
73
         firstTime = false;
 
74
         Logger.getLogger("org.jboss.remoting").setLevel(XLevel.INFO);
 
75
         Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
 
76
         String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
 
77
         PatternLayout layout = new PatternLayout(pattern);
 
78
         ConsoleAppender consoleAppender = new ConsoleAppender(layout);
 
79
         Logger.getRootLogger().addAppender(consoleAppender);  
 
80
      }
 
81
   }
 
82
 
 
83
   
 
84
   public void tearDown()
 
85
   {
 
86
   }
 
87
   
 
88
   
 
89
   public void testConnectToEachHome() throws Throwable
 
90
   {
 
91
      log.info("entering " + getName());
 
92
 
 
93
      // Start server.
 
94
      setupServer();
 
95
 
 
96
      Iterator it = serverLocator.getConnectHomeList().iterator();
 
97
      while (it.hasNext())
 
98
      {
 
99
         // Create client.
 
100
         Home h = (Home) it.next();
 
101
         String path = serverLocator.getPath();
 
102
         Map parameters = serverLocator.getParameters();
 
103
         InvokerLocator clientLocator = new InvokerLocator(getTransport(), h.host, h.port, path, parameters);
 
104
         log.info("locator: " + clientLocator);
 
105
         HashMap clientConfig = new HashMap();
 
106
         clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
 
107
         addExtraClientConfig(clientConfig);
 
108
         Client client = new Client(clientLocator, clientConfig);
 
109
         client.connect();
 
110
         log.info("client is connected to " + clientLocator);
 
111
 
 
112
         // Test invocations.
 
113
         assertEquals("abc", client.invoke("abc"));
 
114
         log.info("invocation successful");
 
115
         
 
116
         // Test callbacks.
 
117
         Map metadata = new HashMap();
 
118
         addExtraCallbackConfig(metadata);
 
119
         TestCallbackHandler callbackHandler = new TestCallbackHandler();
 
120
         client.addListener(callbackHandler, metadata, null, true);
 
121
         assertTrue(callbackHandler.ok);
 
122
         log.info("callback successful");
 
123
         client.removeListener(callbackHandler);
 
124
         client.disconnect();
 
125
      }
 
126
 
 
127
      shutdownServer();
 
128
      log.info(getName() + " PASSES");
 
129
   }
 
130
   
 
131
   
 
132
   public void testConnectToAnyHome() throws Throwable
 
133
   {
 
134
      log.info("entering " + getName());
 
135
      
 
136
      // Start server.
 
137
      setupServer();
 
138
      
 
139
      // Create client.
 
140
      InvokerLocator clientLocator = new InvokerLocator(locatorURI);
 
141
      HashMap clientConfig = new HashMap();
 
142
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
 
143
      addExtraClientConfig(clientConfig);
 
144
      Client client = new Client(clientLocator, clientConfig);
 
145
      client.connect();
 
146
      log.info("client is connected to " + clientLocator);
 
147
      
 
148
      // Test ivocations.
 
149
      assertEquals("abc", client.invoke("abc"));
 
150
      log.info("invocation successful");
 
151
      
 
152
      // Test callbacks.
 
153
      Map metadata = new HashMap();
 
154
      addExtraCallbackConfig(metadata);
 
155
      TestCallbackHandler callbackHandler = new TestCallbackHandler();
 
156
      client.addListener(callbackHandler, metadata, null, true);
 
157
      assertTrue(callbackHandler.ok);
 
158
      log.info("callback successful");
 
159
      
 
160
      client.removeListener(callbackHandler);
 
161
      client.disconnect();
 
162
      shutdownServer();
 
163
      log.info(getName() + " PASSES");
 
164
   }
 
165
   
 
166
   
 
167
   public void testInvocationHandlersAreShared() throws Throwable
 
168
   {
 
169
      log.info("entering " + getName());
 
170
 
 
171
      // Start server.
 
172
      setupServer();
 
173
      
 
174
      List homes = serverLocator.getConnectHomeList();
 
175
      for (int i = 0; i < homes.size(); i++)
 
176
      {
 
177
         // Create client.
 
178
         Home h = (Home) homes.get(i);
 
179
         String path = serverLocator.getPath();
 
180
         Map parameters = serverLocator.getParameters();
 
181
         InvokerLocator clientLocator = new InvokerLocator(getTransport(), h.host, h.port, path, parameters);
 
182
         log.info("locator: " + clientLocator);
 
183
         HashMap clientConfig = new HashMap();
 
184
         clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
 
185
         addExtraClientConfig(clientConfig);
 
186
         Client client = new Client(clientLocator, clientConfig);
 
187
         client.connect();
 
188
         log.info("client is connected to " + clientLocator);
 
189
         
 
190
         if (i == 0)
 
191
            client.invoke("reset");
 
192
         
 
193
         // Test invocations.
 
194
         assertEquals("count", client.invoke("count"));
 
195
         log.info("invocation successful");
 
196
         
 
197
         // Test callbacks.
 
198
         Map metadata = new HashMap();
 
199
         addExtraCallbackConfig(metadata);
 
200
         TestCallbackHandler callbackHandler = new TestCallbackHandler();
 
201
         client.addListener(callbackHandler, metadata, null, true);
 
202
         assertTrue(callbackHandler.ok);
 
203
         log.info("callback successful");
 
204
         
 
205
         // Verify that a single ServerInvocationHandler handles invocation
 
206
         // for all interfaces.
 
207
         Integer counter = (Integer) client.invoke("getCounter");
 
208
         assertEquals(i + 1, counter.intValue());
 
209
         
 
210
         client.removeListener(callbackHandler);
 
211
         client.disconnect();
 
212
      }
 
213
      
 
214
      shutdownServer();
 
215
      log.info(getName() + " PASSES");
 
216
   }
 
217
   
 
218
   
 
219
   protected abstract String getTransport();
 
220
 
 
221
   
 
222
   protected String getPath() {return "";}
 
223
   protected void addExtraClientConfig(Map config) {}
 
224
   protected void addExtraServerConfig(Map config) {}
 
225
   protected void addExtraCallbackConfig(Map config) {}
 
226
   
 
227
 
 
228
   protected void setupServer() throws Exception
 
229
   {
 
230
      StringBuffer sb = new StringBuffer();
 
231
      Enumeration e1 = NetworkInterface.getNetworkInterfaces();
 
232
      boolean first = true;
 
233
      int counter = 0;
 
234
      
 
235
      loop: while (e1.hasMoreElements())
 
236
      {
 
237
         NetworkInterface iface = (NetworkInterface) e1.nextElement();
 
238
         Enumeration e2 = iface.getInetAddresses();
 
239
         while (e2.hasMoreElements())
 
240
         {
 
241
            InetAddress address = (InetAddress) e2.nextElement();
 
242
            String host = address.getHostAddress();
 
243
            if (host.indexOf(':') != host.lastIndexOf(':'))
 
244
               host = "[" + host + "]";
 
245
            if (AddressUtil.checkAddress(host))
 
246
            {
 
247
               log.info("host is functional: " + host);
 
248
               int port = PortUtil.findFreePort(host);
 
249
               if (first)
 
250
                  first = false;
 
251
               else
 
252
                  sb.append('!');
 
253
               sb.append(host).append(':').append(port);
 
254
               if (++counter > 10) break loop;
 
255
            }
 
256
            else
 
257
            {
 
258
               log.info("skipping host: " + host);
 
259
            }
 
260
         }
 
261
      }
 
262
      
 
263
      locatorURI = getTransport() + "://" + InvokerLocator.MULTIHOME + getPath() + "/?";
 
264
      locatorURI += InvokerLocator.HOMES_KEY + "=" + sb.toString() + "&";
 
265
      locatorURI += InvokerLocator.CONNECT_HOMES_KEY + "=" + sb.toString();
 
266
      serverLocator = new InvokerLocator(locatorURI);
 
267
      log.info("Starting remoting server with locator uri of: " + locatorURI);
 
268
      HashMap config = new HashMap();
 
269
      config.put(InvokerLocator.FORCE_REMOTE, "true");
 
270
      addExtraServerConfig(config);
 
271
      connector = new Connector(serverLocator, config);
 
272
      connector.create();
 
273
      invocationHandler = new TestInvocationHandler();
 
274
      connector.addInvocationHandler("test", invocationHandler);
 
275
      connector.start();
 
276
   }
 
277
   
 
278
   
 
279
   protected void shutdownServer() throws Exception
 
280
   {
 
281
      if (connector != null)
 
282
         connector.stop();
 
283
   }
 
284
   
 
285
   
 
286
   static class TestCallbackHandler implements InvokerCallbackHandler
 
287
   {
 
288
      boolean ok;
 
289
      
 
290
      public void handleCallback(Callback callback) throws HandleCallbackException
 
291
      {
 
292
         log.info("received callback");
 
293
         ok = true;
 
294
      }  
 
295
   }
 
296
}
 
 
b'\\ No newline at end of file'