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

« back to all changes in this revision

Viewing changes to tests/org/jboss/test/remoting/transport/socket/configuration/SocketServerSocketConfigurationTestCase.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.socket.configuration;
23
 
 
24
 
import java.lang.reflect.Field;
25
 
import java.net.InetAddress;
26
 
import java.net.Socket;
27
 
import java.net.SocketException;
28
 
import java.util.HashMap;
29
 
import java.util.Map;
30
 
import java.util.Set;
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.InvocationRequest;
43
 
import org.jboss.remoting.InvokerLocator;
44
 
import org.jboss.remoting.ServerInvocationHandler;
45
 
import org.jboss.remoting.ServerInvoker;
46
 
import org.jboss.remoting.callback.InvokerCallbackHandler;
47
 
import org.jboss.remoting.transport.Connector;
48
 
import org.jboss.remoting.transport.PortUtil;
49
 
import org.jboss.remoting.transport.socket.LRUPool;
50
 
import org.jboss.remoting.transport.socket.ServerThread;
51
 
import org.jboss.remoting.transport.socket.SocketServerInvoker;
52
 
 
53
 
 
54
 
/**
55
 
 * Unit test for JBREM-703.
56
 
 * 
57
 
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
58
 
 * @version $Revision: 1.1 $
59
 
 * <p>
60
 
 * Copyright Feb 28, 2008
61
 
 * </p>
62
 
 */
63
 
public class SocketServerSocketConfigurationTestCase extends TestCase
64
 
{
65
 
   private static Logger log = Logger.getLogger(SocketServerSocketConfigurationTestCase.class);
66
 
   
67
 
   protected static boolean firstTime = true;
68
 
   
69
 
   protected String host;
70
 
   protected int port;
71
 
   protected String locatorURI;
72
 
   protected InvokerLocator serverLocator;
73
 
   protected Connector connector;
74
 
   protected TestInvocationHandler invocationHandler;
75
 
 
76
 
   
77
 
   public void setUp() throws Exception
78
 
   {
79
 
      if (firstTime)
80
 
      {
81
 
         firstTime = false;
82
 
         Logger.getLogger("org.jboss.remoting").setLevel(XLevel.INFO);
83
 
         Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
84
 
         String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
85
 
         PatternLayout layout = new PatternLayout(pattern);
86
 
         ConsoleAppender consoleAppender = new ConsoleAppender(layout);
87
 
         Logger.getRootLogger().addAppender(consoleAppender);  
88
 
      }
89
 
   }
90
 
 
91
 
   
92
 
   public void tearDown()
93
 
   {
94
 
   }
95
 
   
96
 
   
97
 
   public void testConfigureByConfigMap() throws Throwable
98
 
   {
99
 
      log.info("entering " + getName());
100
 
      
101
 
      // Start server.
102
 
      setupServer(true);
103
 
      
104
 
      // Create client.
105
 
      InvokerLocator clientLocator = new InvokerLocator(locatorURI);
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");
112
 
      
113
 
      // Test connections.
114
 
      assertEquals("abc", client.invoke("abc"));
115
 
      log.info("connection is good");
116
 
      
117
 
      SocketServerInvoker invoker = (SocketServerInvoker) connector.getServerInvoker();
118
 
      Field field = SocketServerInvoker.class.getDeclaredField("clientpool");
119
 
      field.setAccessible(true);
120
 
      LRUPool pool = (LRUPool) field.get(invoker);
121
 
      Set keys = pool.getContents();
122
 
      assertEquals(1, keys.size());
123
 
      ServerThread t = (ServerThread) keys.iterator().next();
124
 
      field = ServerThread.class.getDeclaredField("socket");
125
 
      field.setAccessible(true);
126
 
      Socket socket = (Socket) field.get(t);
127
 
      doSocketTest(socket);
128
 
      
129
 
      client.disconnect();
130
 
      shutdownServer();
131
 
      log.info(getName() + " PASSES");
132
 
   }
133
 
   
134
 
   
135
 
   public void testConfigureBySetter() throws Throwable
136
 
   {
137
 
      log.info("entering " + getName());
138
 
      
139
 
      // Start server.
140
 
      setupServer(false);
141
 
      
142
 
      // Create client.
143
 
      InvokerLocator clientLocator = new InvokerLocator(locatorURI);
144
 
      HashMap clientConfig = new HashMap();
145
 
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
146
 
      addExtraClientConfig(clientConfig);
147
 
      Client client = new Client(clientLocator, clientConfig);
148
 
      client.connect();
149
 
      log.info("client is connected");
150
 
      
151
 
      // Test connections.
152
 
      assertEquals("abc", client.invoke("abc"));
153
 
      log.info("connection is good");
154
 
      
155
 
      SocketServerInvoker invoker = (SocketServerInvoker) connector.getServerInvoker();
156
 
      Field field = SocketServerInvoker.class.getDeclaredField("clientpool");
157
 
      field.setAccessible(true);
158
 
      LRUPool pool = (LRUPool) field.get(invoker);
159
 
      Set keys = pool.getContents();
160
 
      assertEquals(1, keys.size());
161
 
      ServerThread t = (ServerThread) keys.iterator().next();
162
 
      field = ServerThread.class.getDeclaredField("socket");
163
 
      field.setAccessible(true);
164
 
      Socket socket = (Socket) field.get(t);
165
 
      doSocketTest(socket);
166
 
      
167
 
      client.disconnect();
168
 
      shutdownServer();
169
 
      log.info(getName() + " PASSES");
170
 
   }
171
 
 
172
 
   
173
 
   protected void doSocketTest(Socket s) throws SocketException
174
 
   {
175
 
      assertTrue(s.getKeepAlive());
176
 
      assertTrue(s.getOOBInline());
177
 
      suggestEquals(2345, s.getReceiveBufferSize(), "receiveBufferSize");
178
 
      suggestEquals(3456, s.getSendBufferSize(), "sendBufferSize");
179
 
      assertEquals(4567, s.getSoLinger());
180
 
      suggestEquals(0, s.getTrafficClass(), "trafficClass");
181
 
   }
182
 
   
183
 
   
184
 
   protected void suggestEquals(int i1, int i2, String s)
185
 
   {
186
 
      if (i1 != i2)
187
 
      {
188
 
         log.warn(s + " has not been set: expected " + i1 + ", got " + i2);
189
 
         log.warn("note that setting \"" + s + "\" is just a suggestion to the underlying network code");
190
 
      }
191
 
   }
192
 
   
193
 
   
194
 
   protected String getTransport()
195
 
   {
196
 
      return "socket";
197
 
   }
198
 
   
199
 
   
200
 
   protected void addExtraClientConfig(Map config) {}
201
 
   protected void addExtraServerConfig(Map config) {}
202
 
   
203
 
 
204
 
   protected void setupServer(boolean useConfigMap) throws Exception
205
 
   {
206
 
      host = InetAddress.getLocalHost().getHostAddress();
207
 
      port = PortUtil.findFreePort(host);
208
 
      locatorURI = getTransport() + "://" + host + ":" + port; 
209
 
      serverLocator = new InvokerLocator(locatorURI);
210
 
      log.info("Starting remoting server with locator uri of: " + locatorURI);
211
 
      HashMap config = new HashMap();
212
 
      config.put(InvokerLocator.FORCE_REMOTE, "true");
213
 
      if (useConfigMap)
214
 
      {
215
 
         addServerSocketConfig(config);
216
 
      }
217
 
      addExtraServerConfig(config);
218
 
      connector = new Connector(serverLocator, config);
219
 
      connector.create();
220
 
      if (!useConfigMap)
221
 
      {
222
 
         SocketServerInvoker invoker = (SocketServerInvoker) connector.getServerInvoker();
223
 
         configureServerInvoker(invoker);
224
 
      }
225
 
      invocationHandler = new TestInvocationHandler();
226
 
      connector.addInvocationHandler("test", invocationHandler);
227
 
      connector.start();
228
 
   }
229
 
   
230
 
   
231
 
   protected void addServerSocketConfig(Map config)
232
 
   {
233
 
      config.put("keepAlive", "true");
234
 
      config.put("oOBInline", "true");
235
 
      config.put("receiveBufferSize", "2345");
236
 
      config.put("sendBufferSize", "3456");
237
 
      config.put("soLinger", "true");
238
 
      config.put("soLingerDuration", "4567"); 
239
 
      config.put("trafficClass", "0");
240
 
   }
241
 
   
242
 
   
243
 
   protected void configureServerInvoker(SocketServerInvoker invoker)
244
 
   {
245
 
      invoker.setReceiveBufferSize(12345);
246
 
      
247
 
      invoker.setKeepAlive(true);
248
 
      invoker.setOOBInline(true);
249
 
      invoker.setReceiveBufferSize(2345);
250
 
      invoker.setSendBufferSize(3456);
251
 
      invoker.setSoLinger(true);
252
 
      invoker.setSoLingerDuration(4567); 
253
 
      invoker.setTrafficClass(0);
254
 
   }
255
 
   
256
 
   
257
 
   protected void shutdownServer() throws Exception
258
 
   {
259
 
      if (connector != null)
260
 
         connector.stop();
261
 
   }
262
 
   
263
 
   
264
 
   static class TestInvocationHandler implements ServerInvocationHandler
265
 
   {
266
 
      public void addListener(InvokerCallbackHandler callbackHandler) {}
267
 
      public Object invoke(final InvocationRequest invocation) throws Throwable
268
 
      {
269
 
         return invocation.getParameter();
270
 
      }
271
 
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
272
 
      public void setMBeanServer(MBeanServer server) {}
273
 
      public void setInvoker(ServerInvoker invoker) {}
274
 
   }
275
 
}
 
 
b'\\ No newline at end of file'