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

« back to all changes in this revision

Viewing changes to src/tests/org/jboss/test/remoting/transport/socket/configuration/SocketSocketConfigurationTestCase.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.LinkedList;
 
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.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.MicroSocketClientInvoker;
 
50
import org.jboss.remoting.transport.socket.SocketWrapper;
 
51
 
 
52
 
 
53
/**
 
54
 * Unit test for JBREM-703.
 
55
 * 
 
56
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
 
57
 * @version $Revision: 1.1 $
 
58
 * <p>
 
59
 * Copyright Feb 28, 2008
 
60
 * </p>
 
61
 */
 
62
public class SocketSocketConfigurationTestCase extends TestCase
 
63
{
 
64
   private static Logger log = Logger.getLogger(SocketSocketConfigurationTestCase.class);
 
65
   
 
66
   protected static boolean firstTime = true;
 
67
   
 
68
   protected String host;
 
69
   protected int port;
 
70
   protected String locatorURI;
 
71
   protected InvokerLocator serverLocator;
 
72
   protected Connector connector;
 
73
   protected TestInvocationHandler invocationHandler;
 
74
 
 
75
   
 
76
   public void setUp() throws Exception
 
77
   {
 
78
      if (firstTime)
 
79
      {
 
80
         firstTime = false;
 
81
         Logger.getLogger("org.jboss.remoting").setLevel(XLevel.INFO);
 
82
         Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
 
83
         String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
 
84
         PatternLayout layout = new PatternLayout(pattern);
 
85
         ConsoleAppender consoleAppender = new ConsoleAppender(layout);
 
86
         Logger.getRootLogger().addAppender(consoleAppender);  
 
87
      }
 
88
   }
 
89
 
 
90
   
 
91
   public void tearDown()
 
92
   {
 
93
   }
 
94
   
 
95
   
 
96
   public void testConfigureByConfigMap() throws Throwable
 
97
   {
 
98
      log.info("entering " + getName());
 
99
      
 
100
      // Start server.
 
101
      setupServer();
 
102
      
 
103
      // Create client.
 
104
      InvokerLocator clientLocator = new InvokerLocator(locatorURI);
 
105
      HashMap clientConfig = new HashMap();
 
106
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
 
107
      setupClient(clientConfig);
 
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
      assertTrue(client.getInvoker() instanceof MicroSocketClientInvoker);
 
118
      MicroSocketClientInvoker invoker = (MicroSocketClientInvoker) client.getInvoker();
 
119
      Field field = MicroSocketClientInvoker.class.getDeclaredField("pool");
 
120
      field.setAccessible(true);
 
121
      LinkedList pool = (LinkedList) field.get(invoker);
 
122
      assertTrue(pool.size() > 0);
 
123
      SocketWrapper socketWrapper = (SocketWrapper) pool.get(0);
 
124
      Socket socket = socketWrapper.getSocket();
 
125
      doSocketTest(socket);
 
126
      
 
127
      client.disconnect();
 
128
      shutdownServer();
 
129
      log.info(getName() + " PASSES");
 
130
   }
 
131
   
 
132
   
 
133
   protected void setupClient(Map config)
 
134
   {
 
135
      config.put("keepAlive", "true");
 
136
      config.put("oOBInline", "true");
 
137
      config.put("receiveBufferSize", "2345");
 
138
      config.put("sendBufferSize", "3456");
 
139
      config.put("soLinger", "true");
 
140
      config.put("soLingerDuration", "4567"); 
 
141
      config.put("trafficClass", "0");
 
142
   }
 
143
   
 
144
   
 
145
   protected void doSocketTest(Socket s) throws SocketException
 
146
   {
 
147
      assertTrue(s.getKeepAlive());
 
148
      assertTrue(s.getOOBInline());
 
149
      suggestEquals(2345, s.getReceiveBufferSize(), "receiveBufferSize");
 
150
      suggestEquals(3456, s.getSendBufferSize(), "sendBufferSize");
 
151
      assertEquals(4567, s.getSoLinger());
 
152
      suggestEquals(0, s.getTrafficClass(), "trafficClass");
 
153
   }
 
154
   
 
155
   
 
156
   protected void suggestEquals(int i1, int i2, String s)
 
157
   {
 
158
      if (i1 != i2)
 
159
      {
 
160
         log.warn(s + " has not been set: expected " + i1 + ", got " + i2);
 
161
         log.warn("note that setting \"" + s + "\" is just a suggestion to the underlying network code");
 
162
      }
 
163
   }
 
164
   
 
165
   
 
166
   protected String getTransport()
 
167
   {
 
168
      return "socket";
 
169
   }
 
170
   
 
171
   
 
172
   protected void addExtraClientConfig(Map config) {}
 
173
   protected void addExtraServerConfig(Map config) {}
 
174
   
 
175
 
 
176
   protected void setupServer() throws Exception
 
177
   {
 
178
      host = InetAddress.getLocalHost().getHostAddress();
 
179
      port = PortUtil.findFreePort(host);
 
180
      locatorURI = getTransport() + "://" + host + ":" + port; 
 
181
      serverLocator = new InvokerLocator(locatorURI);
 
182
      log.info("Starting remoting server with locator uri of: " + locatorURI);
 
183
      HashMap config = new HashMap();
 
184
      config.put(InvokerLocator.FORCE_REMOTE, "true");
 
185
      addExtraServerConfig(config);
 
186
      connector = new Connector(serverLocator, config);
 
187
      connector.create();
 
188
      invocationHandler = new TestInvocationHandler();
 
189
      connector.addInvocationHandler("test", invocationHandler);
 
190
      connector.start();
 
191
   }
 
192
   
 
193
   
 
194
   protected void shutdownServer() throws Exception
 
195
   {
 
196
      if (connector != null)
 
197
         connector.stop();
 
198
   }
 
199
   
 
200
   
 
201
   static class TestInvocationHandler implements ServerInvocationHandler
 
202
   {
 
203
      public void addListener(InvokerCallbackHandler callbackHandler) {}
 
204
      public Object invoke(final InvocationRequest invocation) throws Throwable
 
205
      {
 
206
         return invocation.getParameter();
 
207
      }
 
208
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
 
209
      public void setMBeanServer(MBeanServer server) {}
 
210
      public void setInvoker(ServerInvoker invoker) {}
 
211
   }
 
212
}
 
 
b'\\ No newline at end of file'