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

« back to all changes in this revision

Viewing changes to tests/org/jboss/test/remoting/clientaddress/ClientAddressTestParent.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.clientaddress;
23
 
 
24
 
import java.io.InputStream;
25
 
import java.io.OutputStream;
26
 
import java.net.InetAddress;
27
 
import java.net.ServerSocket;
28
 
import java.net.Socket;
29
 
import java.util.HashMap;
30
 
import java.util.Map;
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.remoting.Client;
41
 
import org.jboss.remoting.InvocationRequest;
42
 
import org.jboss.remoting.InvokerLocator;
43
 
import org.jboss.remoting.Remoting;
44
 
import org.jboss.remoting.ServerInvocationHandler;
45
 
import org.jboss.remoting.ServerInvoker;
46
 
import org.jboss.remoting.callback.Callback;
47
 
import org.jboss.remoting.callback.HandleCallbackException;
48
 
import org.jboss.remoting.callback.InvokerCallbackHandler;
49
 
import org.jboss.remoting.transport.Connector;
50
 
import org.jboss.remoting.transport.PortUtil;
51
 
 
52
 
 
53
 
public abstract class ClientAddressTestParent extends TestCase
54
 
{
55
 
   private static Logger log = Logger.getLogger(ClientAddressTestParent.class);
56
 
   
57
 
   private static final String GET_ADDRESS = "getAddress";
58
 
   private static final String OPEN_CONNECTION = "openConnection";
59
 
   private static final String COPY = "copy:";
60
 
   private static final String SEND_CALLBACK = "sendCallback";
61
 
   private static final int ANSWER = 17;
62
 
   
63
 
   private static boolean firstTime = true;
64
 
   
65
 
   protected Connector connector;
66
 
   protected InvokerLocator serverLocator;
67
 
   protected String locatorURI;
68
 
   protected String host;
69
 
   protected int port;
70
 
   protected TestInvocationHandler invocationHandler;
71
 
   protected int callbackPort;
72
 
 
73
 
   
74
 
   public void setUp() throws Exception
75
 
   {
76
 
      if (firstTime)
77
 
      {
78
 
         firstTime = false;
79
 
         Logger.getLogger("org.jboss.remoting").setLevel(Level.INFO);
80
 
         Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
81
 
         String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
82
 
         PatternLayout layout = new PatternLayout(pattern);
83
 
         ConsoleAppender consoleAppender = new ConsoleAppender(layout);
84
 
         Logger.getRootLogger().addAppender(consoleAppender);  
85
 
      }
86
 
   }
87
 
 
88
 
   
89
 
   public void tearDown()
90
 
   {
91
 
   }
92
 
   
93
 
   
94
 
   /**
95
 
    * Verifies that a valid InetAddress for the client is passed to the
96
 
    * org.jboss.remoting.ServerInvocationHandler in the
97
 
    * org.jboss.remoting.InvocationRequest requestPayload.
98
 
    * @throws Throwable
99
 
    */
100
 
   public void testClientAddress() throws Throwable
101
 
   {
102
 
      log.info("entering " + getName());
103
 
      
104
 
      // Start server.
105
 
      setupServer();
106
 
      
107
 
      // Create client.
108
 
      InvokerLocator clientLocator1 = new InvokerLocator(locatorURI);
109
 
      HashMap clientConfig = new HashMap();
110
 
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
111
 
      addExtraClientConfig(clientConfig);
112
 
      Client client = new Client(clientLocator1, clientConfig);
113
 
      client.connect();
114
 
      log.info("client is connected");
115
 
      
116
 
      // Test client address facility.
117
 
      final InetAddress localAddressFromServer = (InetAddress) client.invoke(GET_ADDRESS);
118
 
      log.info("local address according to server: " + localAddressFromServer);
119
 
      final int randomPort = PortUtil.findFreePort(localAddressFromServer.getHostAddress());
120
 
      callbackPort = randomPort;
121
 
      
122
 
      class TestThread extends Thread
123
 
      {
124
 
         public int result = -1;
125
 
         
126
 
         public void run()
127
 
         {
128
 
            try
129
 
            {
130
 
               ServerSocket ss = new ServerSocket(randomPort, 200, localAddressFromServer);
131
 
               log.info("created ServerSocket bound to: " + ss.getLocalSocketAddress());
132
 
               Socket s = ss.accept();
133
 
               log.info("accepted socket: " + s);
134
 
               InputStream is = s.getInputStream();
135
 
               result = is.read();
136
 
               log.info("read: " + result);
137
 
               s.close();
138
 
               ss.close();
139
 
            }
140
 
            catch (Exception e)
141
 
            {
142
 
               log.error(e);
143
 
            }
144
 
         }
145
 
      };
146
 
      
147
 
      TestThread t = new TestThread();
148
 
      t.start();
149
 
      Thread.sleep(5000);
150
 
      HashMap metadata = new HashMap();
151
 
      metadata.put("callbackPort", new Integer(callbackPort));
152
 
      client.invoke(OPEN_CONNECTION, metadata);
153
 
      Thread.sleep(5000);
154
 
      assertEquals(ANSWER, t.result);
155
 
      
156
 
      client.disconnect();
157
 
      shutdownServer();
158
 
      log.info(getName() + " PASSES");
159
 
   }
160
 
   
161
 
   
162
 
   /**
163
 
    * Verifies that the address returned by
164
 
    * org.jboss.remoting.Client.getAddressSeenByServer() is valid.
165
 
    */
166
 
   public void testGetAddressSeenByServer() throws Throwable
167
 
   {
168
 
      log.info("entering " + getName());
169
 
      
170
 
      // Start server.
171
 
      setupServer();
172
 
      
173
 
      // Create client.
174
 
      InvokerLocator clientLocator = new InvokerLocator(locatorURI);
175
 
      HashMap clientConfig = new HashMap();
176
 
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
177
 
      addExtraClientConfig(clientConfig);
178
 
      Client client = new Client(clientLocator, clientConfig);
179
 
      client.connect();
180
 
      log.info("client is connected: " + locatorURI);
181
 
      
182
 
      // Test connections.
183
 
      assertEquals("abc", client.invoke("copy:abc"));
184
 
      log.info("connection is good");
185
 
      
186
 
      // Get address as seen by server and create callback Connector with that address.
187
 
      InetAddress callbackAddress = client.getAddressSeenByServer();
188
 
      log.info("client address seen by server: " + callbackAddress);
189
 
      String callbackHost = callbackAddress.getHostAddress();
190
 
      int callbackPort = PortUtil.findFreePort(callbackHost);
191
 
      String callbackLocatorURI = getCallbackTransport() + "://" + callbackHost + ":" + callbackPort;
192
 
      callbackLocatorURI += "/?timeout=10000";
193
 
      InvokerLocator callbackLocator = new InvokerLocator(callbackLocatorURI);
194
 
      log.info("callback locator: " + callbackLocator);
195
 
      HashMap callbackConfig = new HashMap();
196
 
      addExtraCallbackConfig(callbackConfig);
197
 
      Connector callbackConnector = new Connector(callbackLocator, callbackConfig);
198
 
      callbackConnector.start();
199
 
      TestCallbackHandler callbackHandler = new TestCallbackHandler();
200
 
      client.addListener(callbackHandler, callbackLocator);
201
 
      
202
 
      // Tell server to send a callback, and verify it was received.
203
 
      client.invoke(SEND_CALLBACK);
204
 
      assertEquals(1, callbackHandler.counter);
205
 
      
206
 
      client.removeListener(callbackHandler);
207
 
      callbackConnector.stop();
208
 
      client.disconnect();
209
 
      shutdownServer();
210
 
      log.info(getName() + " PASSES");
211
 
   }
212
 
   
213
 
   
214
 
   protected abstract String getTransport();
215
 
   
216
 
   
217
 
   protected String getCallbackTransport()
218
 
   {
219
 
      return getTransport();
220
 
   }
221
 
   
222
 
   
223
 
   protected void setupServer() throws Exception
224
 
   {
225
 
      host = InetAddress.getLocalHost().getHostAddress();
226
 
      port = PortUtil.findFreePort(host);
227
 
      locatorURI = getTransport() + "://" + host + ":" + port; 
228
 
      serverLocator = new InvokerLocator(locatorURI);
229
 
      log.info("Starting remoting server with locator uri of: " + locatorURI);
230
 
      HashMap config = new HashMap();
231
 
      config.put(InvokerLocator.FORCE_REMOTE, "true");
232
 
      addExtraServerConfig(config);
233
 
      connector = new Connector(serverLocator, config);
234
 
      connector.create();
235
 
      invocationHandler = new TestInvocationHandler();
236
 
      connector.addInvocationHandler("test", invocationHandler);
237
 
      connector.start();
238
 
   }
239
 
   
240
 
   
241
 
   protected void shutdownServer()
242
 
   {
243
 
      if (connector != null)
244
 
         connector.stop();
245
 
   }
246
 
   
247
 
   
248
 
   protected String reconstructLocator(InetAddress address)
249
 
   {
250
 
      return getTransport() + "://" + address.getHostAddress() + ":" + port;
251
 
 
252
 
   }
253
 
   
254
 
   protected void addExtraClientConfig(Map config) {}
255
 
   protected void addExtraServerConfig(Map config) {}
256
 
   protected void addExtraCallbackConfig(Map config) {}
257
 
   
258
 
 
259
 
   class TestInvocationHandler implements ServerInvocationHandler
260
 
   {
261
 
      private InvokerCallbackHandler callbackHandler;
262
 
      
263
 
      public void addListener(InvokerCallbackHandler callbackHandler)
264
 
      {
265
 
         this.callbackHandler = callbackHandler;
266
 
      }
267
 
      
268
 
      public Object invoke(final InvocationRequest invocation) throws Throwable
269
 
      {
270
 
         Object o = invocation.getParameter();
271
 
         if (! (o instanceof String))
272
 
            throw new Exception("command should be a String: " + o);
273
 
         
274
 
         String command = (String) o;
275
 
         log.info("command: " + command);
276
 
         
277
 
         if (GET_ADDRESS.equals(command))
278
 
         {
279
 
            return invocation.getRequestPayload().get(Remoting.CLIENT_ADDRESS);
280
 
         }
281
 
         else if (OPEN_CONNECTION.equals(command))
282
 
         {
283
 
            InetAddress addr = (InetAddress) invocation.getRequestPayload().get(Remoting.CLIENT_ADDRESS);
284
 
            log.info("creating socket connected to: " + addr);
285
 
            Integer callbackPortInt = (Integer) invocation.getRequestPayload().get("callbackPort");
286
 
            int callbackPort = callbackPortInt.intValue();
287
 
            Socket s = new Socket(addr, callbackPort);
288
 
            log.info("created socket connected to: " + addr);
289
 
            OutputStream os = s.getOutputStream();
290
 
            os.write(ANSWER);
291
 
            log.info("wrote answer");
292
 
            s.close();
293
 
            return null;
294
 
         }
295
 
         else if (SEND_CALLBACK.equals(command))
296
 
         {
297
 
            callbackHandler.handleCallback(new Callback("callback"));
298
 
            return null;
299
 
         }
300
 
         else if (command.startsWith(COPY))
301
 
         {
302
 
            return command.substring(5);
303
 
         }
304
 
         else
305
 
         {
306
 
            throw new Exception("unrecognized command: " + command);
307
 
         }
308
 
      }
309
 
      
310
 
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
311
 
      public void setMBeanServer(MBeanServer server) {}
312
 
      public void setInvoker(ServerInvoker invoker) {}
313
 
   }
314
 
   
315
 
   
316
 
   class TestCallbackHandler implements InvokerCallbackHandler
317
 
   {
318
 
      public int counter;
319
 
      
320
 
      public void handleCallback(Callback callback) throws HandleCallbackException
321
 
      {
322
 
         counter++;
323
 
         log.info("received callback");
324
 
      }      
325
 
   }
326
 
}
 
 
b'\\ No newline at end of file'