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

« back to all changes in this revision

Viewing changes to src/tests/org/jboss/test/remoting/socketfactory/SocketFactoryClassNameTestRoot.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 2008, 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.socketfactory;
 
23
 
 
24
import java.io.IOException;
 
25
import java.net.InetAddress;
 
26
import java.net.Socket;
 
27
import java.net.UnknownHostException;
 
28
import java.util.HashMap;
 
29
import java.util.Map;
 
30
 
 
31
import javax.management.MBeanServer;
 
32
import javax.net.SocketFactory;
 
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.AbstractInvoker;
 
41
import org.jboss.remoting.Client;
 
42
import org.jboss.remoting.InvocationRequest;
 
43
import org.jboss.remoting.InvokerLocator;
 
44
import org.jboss.remoting.Remoting;
 
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
 
 
51
 
 
52
/**
 
53
 * Unit test for JBREM-1014.
 
54
 * 
 
55
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
 
56
 * @version $Revision: 1.1 $
 
57
 * <p>
 
58
 * Copyright Jul 18, 2008
 
59
 * </p>
 
60
 */
 
61
public abstract class SocketFactoryClassNameTestRoot extends TestCase
 
62
{
 
63
   private static Logger log = Logger.getLogger(SocketFactoryClassNameTestRoot.class);
 
64
   
 
65
   private static boolean firstTime = true;
 
66
   
 
67
   protected String host;
 
68
   protected int port;
 
69
   protected String locatorURI;
 
70
   protected InvokerLocator serverLocator;
 
71
   protected Connector connector;
 
72
   protected TestInvocationHandler invocationHandler;
 
73
 
 
74
   
 
75
   public void setUp() throws Exception
 
76
   {
 
77
      if (firstTime)
 
78
      {
 
79
         firstTime = false;
 
80
         Logger.getLogger("org.jboss.remoting").setLevel(Level.INFO);
 
81
         Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
 
82
         String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
 
83
         PatternLayout layout = new PatternLayout(pattern);
 
84
         ConsoleAppender consoleAppender = new ConsoleAppender(layout);
 
85
         Logger.getRootLogger().addAppender(consoleAppender);  
 
86
      }
 
87
   }
 
88
 
 
89
   
 
90
   public void tearDown()
 
91
   {
 
92
   }
 
93
   
 
94
   
 
95
   public void testSocketFactoryClassNameInLocator() throws Throwable
 
96
   {
 
97
      log.info("entering " + getName());
 
98
      
 
99
      // Start server.
 
100
      setupServer(false);
 
101
      
 
102
      // Create client.
 
103
      String clientLocatorURI = locatorURI;
 
104
      clientLocatorURI += "&" + Remoting.SOCKET_FACTORY_CLASS_NAME + "=" + getSocketFactoryClass().getName();
 
105
      InvokerLocator clientLocator = new InvokerLocator(clientLocatorURI);
 
106
      HashMap clientConfig = new HashMap();
 
107
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
 
108
      addExtraClientConfig(clientConfig);
 
109
      Client client = new Client(clientLocator, clientConfig);
 
110
      client.connect();
 
111
      log.info("client is connected to " + clientLocatorURI);
 
112
      
 
113
      // Test connections.
 
114
      assertEquals("abc", client.invoke("abc"));
 
115
      log.info("connection is good");
 
116
      
 
117
      // Verify client invoker is using configured SocketFactory.
 
118
      AbstractInvoker invoker = (AbstractInvoker) client.getInvoker();
 
119
      SocketFactory socketFactory = invoker.getSocketFactory();
 
120
      log.info("SocketFactory: " + socketFactory);
 
121
      assertTrue(getSocketFactoryClass().isInstance(socketFactory));
 
122
 
 
123
      client.disconnect();
 
124
      shutdownServer();
 
125
      log.info(getName() + " PASSES");
 
126
   }
 
127
   
 
128
   
 
129
   public void testSocketFactoryClassNameInConfigMap() throws Throwable
 
130
   {
 
131
      log.info("entering " + getName());
 
132
      
 
133
      // Start server.
 
134
      setupServer(false);
 
135
      
 
136
      // Create client.
 
137
      InvokerLocator clientLocator = new InvokerLocator(locatorURI);
 
138
      HashMap clientConfig = new HashMap();
 
139
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
 
140
      clientConfig.put(Remoting.SOCKET_FACTORY_CLASS_NAME, getSocketFactoryClass().getName());
 
141
      addExtraClientConfig(clientConfig);
 
142
      Client client = new Client(clientLocator, clientConfig);
 
143
      client.connect();
 
144
      log.info("client is connected");
 
145
      
 
146
      // Test connections.
 
147
      assertEquals("abc", client.invoke("abc"));
 
148
      log.info("connection is good");
 
149
      
 
150
      // Verify client invoker is using configured SocketFactory.
 
151
      AbstractInvoker invoker = (AbstractInvoker) client.getInvoker();
 
152
      SocketFactory socketFactory = invoker.getSocketFactory();
 
153
      log.info("SocketFactory: " + socketFactory);
 
154
      assertTrue(getSocketFactoryClass().isInstance(socketFactory));
 
155
      
 
156
      client.disconnect();
 
157
      shutdownServer();
 
158
      log.info(getName() + " PASSES");
 
159
   }
 
160
   
 
161
   
 
162
   public void testSocketFactoryClassNameInLocatorWithUseAllParams() throws Throwable
 
163
   {
 
164
      log.info("entering " + getName());
 
165
      
 
166
      // Start server.
 
167
      setupServer(true);
 
168
      
 
169
      // Create client.
 
170
      String clientLocatorURI = locatorURI;
 
171
      clientLocatorURI += "&" + Remoting.SOCKET_FACTORY_CLASS_NAME + "=" + getSocketFactoryClass().getName();
 
172
      InvokerLocator clientLocator = new InvokerLocator(clientLocatorURI);
 
173
      HashMap clientConfig = new HashMap();
 
174
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
 
175
      addExtraClientConfig(clientConfig);
 
176
      Client client = new Client(clientLocator, clientConfig);
 
177
      client.connect();
 
178
      log.info("client is connected to " + clientLocatorURI);
 
179
      
 
180
      // Test connections.
 
181
      assertEquals("abc", client.invoke("abc"));
 
182
      log.info("connection is good");
 
183
      
 
184
      // Verify client invoker is using configured SocketFactory.
 
185
      AbstractInvoker invoker = (AbstractInvoker) client.getInvoker();
 
186
      SocketFactory socketFactory = invoker.getSocketFactory();
 
187
      log.info("SocketFactory: " + socketFactory);
 
188
      assertTrue(getSocketFactoryClass().isInstance(socketFactory));
 
189
 
 
190
      client.disconnect();
 
191
      shutdownServer();
 
192
      log.info(getName() + " PASSES");
 
193
   }
 
194
   
 
195
   
 
196
   public void testSocketFactoryClassNameInConfigMapWithUseAllParams() throws Throwable
 
197
   {
 
198
      log.info("entering " + getName());
 
199
      
 
200
      // Start server.
 
201
      setupServer(true);
 
202
      
 
203
      // Create client.
 
204
      InvokerLocator clientLocator = new InvokerLocator(locatorURI);
 
205
      HashMap clientConfig = new HashMap();
 
206
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
 
207
      clientConfig.put(Remoting.SOCKET_FACTORY_CLASS_NAME, getSocketFactoryClass().getName());
 
208
      addExtraClientConfig(clientConfig);
 
209
      Client client = new Client(clientLocator, clientConfig);
 
210
      client.connect();
 
211
      log.info("client is connected");
 
212
      
 
213
      // Test connections.
 
214
      assertEquals("abc", client.invoke("abc"));
 
215
      log.info("connection is good");
 
216
      
 
217
      // Verify client invoker is using configured SocketFactory.
 
218
      AbstractInvoker invoker = (AbstractInvoker) client.getInvoker();
 
219
      SocketFactory socketFactory = invoker.getSocketFactory();
 
220
      log.info("SocketFactory: " + socketFactory);
 
221
      assertTrue(getSocketFactoryClass().isInstance(socketFactory));
 
222
      
 
223
      client.disconnect();
 
224
      shutdownServer();
 
225
      log.info(getName() + " PASSES");
 
226
   }
 
227
   
 
228
   
 
229
   protected abstract String getTransport();
 
230
 
 
231
 
 
232
   protected Class getSocketFactoryClass()
 
233
   {
 
234
      return TestSocketFactory.class;
 
235
   }
 
236
   
 
237
   
 
238
   protected void addExtraClientConfig(Map config) {}
 
239
   protected void addExtraServerConfig(Map config) {}
 
240
   
 
241
 
 
242
   protected void setupServer(boolean useAllParams) throws Exception
 
243
   {
 
244
      host = InetAddress.getLocalHost().getHostAddress();
 
245
      port = PortUtil.findFreePort(host);
 
246
      locatorURI = getTransport() + "://" + host + ":" + port + "/?x=x";
 
247
      String metadata = System.getProperty("remoting.metadata");
 
248
      if (metadata != null)
 
249
      {
 
250
         locatorURI += "&" + metadata;
 
251
      }
 
252
      if (useAllParams)
 
253
      {
 
254
         locatorURI += "&" + Remoting.USE_ALL_SOCKET_FACTORY_PARAMS + "=true";
 
255
      }
 
256
      serverLocator = new InvokerLocator(locatorURI);
 
257
      log.info("Starting remoting server with locator uri of: " + locatorURI);
 
258
      HashMap config = new HashMap();
 
259
      config.put(InvokerLocator.FORCE_REMOTE, "true");
 
260
      addExtraServerConfig(config);
 
261
      connector = new Connector(serverLocator, config);
 
262
      connector.create();
 
263
      invocationHandler = new TestInvocationHandler();
 
264
      connector.addInvocationHandler("test", invocationHandler);
 
265
      connector.start();
 
266
   }
 
267
   
 
268
   
 
269
   protected void shutdownServer() throws Exception
 
270
   {
 
271
      if (connector != null)
 
272
         connector.stop();
 
273
   }
 
274
   
 
275
   
 
276
   static class TestInvocationHandler implements ServerInvocationHandler
 
277
   {
 
278
      public void addListener(InvokerCallbackHandler callbackHandler) {}
 
279
      public Object invoke(final InvocationRequest invocation) throws Throwable
 
280
      {
 
281
         return invocation.getParameter();
 
282
      }
 
283
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
 
284
      public void setMBeanServer(MBeanServer server) {}
 
285
      public void setInvoker(ServerInvoker invoker) {}
 
286
   }
 
287
 
 
288
   
 
289
   public static class TestSocketFactory extends SocketFactory
 
290
   {
 
291
      SocketFactory sf = SocketFactory.getDefault();
 
292
      
 
293
      public TestSocketFactory()
 
294
      {
 
295
      }
 
296
 
 
297
      public Socket createSocket() throws IOException, UnknownHostException
 
298
      {
 
299
         return sf.createSocket();
 
300
      }
 
301
      
 
302
      public Socket createSocket(String arg0, int arg1) throws IOException, UnknownHostException
 
303
      {
 
304
         return sf.createSocket(arg0, arg1);
 
305
      }
 
306
 
 
307
      public Socket createSocket(InetAddress arg0, int arg1) throws IOException
 
308
      {
 
309
         return sf.createSocket(arg0, arg1);
 
310
      }
 
311
 
 
312
      public Socket createSocket(String arg0, int arg1, InetAddress arg2, int arg3) throws IOException,
 
313
            UnknownHostException
 
314
      {
 
315
         return sf.createSocket(arg0, arg1, arg2, arg3);
 
316
      }
 
317
 
 
318
      public Socket createSocket(InetAddress arg0, int arg1, InetAddress arg2, int arg3) throws IOException
 
319
      {
 
320
         return sf.createSocket(arg0, arg1, arg2, arg3);
 
321
      } 
 
322
   }
 
323
}
 
 
b'\\ No newline at end of file'