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.
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.
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.
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.
22
package org.jboss.test.remoting.transport.socket.configuration;
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;
32
import javax.management.MBeanServer;
34
import junit.framework.TestCase;
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;
55
* Unit test for JBREM-703.
57
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
58
* @version $Revision: 1.1 $
60
* Copyright Feb 28, 2008
63
public class SocketServerSocketConfigurationTestCase extends TestCase
65
private static Logger log = Logger.getLogger(SocketServerSocketConfigurationTestCase.class);
67
protected static boolean firstTime = true;
69
protected String host;
71
protected String locatorURI;
72
protected InvokerLocator serverLocator;
73
protected Connector connector;
74
protected TestInvocationHandler invocationHandler;
77
public void setUp() throws Exception
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);
92
public void tearDown()
97
public void testConfigureByConfigMap() throws Throwable
99
log.info("entering " + getName());
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);
111
log.info("client is connected");
114
assertEquals("abc", client.invoke("abc"));
115
log.info("connection is good");
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);
131
log.info(getName() + " PASSES");
135
public void testConfigureBySetter() throws Throwable
137
log.info("entering " + getName());
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);
149
log.info("client is connected");
152
assertEquals("abc", client.invoke("abc"));
153
log.info("connection is good");
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);
169
log.info(getName() + " PASSES");
173
protected void doSocketTest(Socket s) throws SocketException
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");
184
protected void suggestEquals(int i1, int i2, String s)
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");
194
protected String getTransport()
200
protected void addExtraClientConfig(Map config) {}
201
protected void addExtraServerConfig(Map config) {}
204
protected void setupServer(boolean useConfigMap) throws Exception
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");
215
addServerSocketConfig(config);
217
addExtraServerConfig(config);
218
connector = new Connector(serverLocator, config);
222
SocketServerInvoker invoker = (SocketServerInvoker) connector.getServerInvoker();
223
configureServerInvoker(invoker);
225
invocationHandler = new TestInvocationHandler();
226
connector.addInvocationHandler("test", invocationHandler);
231
protected void addServerSocketConfig(Map config)
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");
243
protected void configureServerInvoker(SocketServerInvoker invoker)
245
invoker.setReceiveBufferSize(12345);
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);
257
protected void shutdownServer() throws Exception
259
if (connector != null)
264
static class TestInvocationHandler implements ServerInvocationHandler
266
public void addListener(InvokerCallbackHandler callbackHandler) {}
267
public Object invoke(final InvocationRequest invocation) throws Throwable
269
return invocation.getParameter();
271
public void removeListener(InvokerCallbackHandler callbackHandler) {}
272
public void setMBeanServer(MBeanServer server) {}
273
public void setInvoker(ServerInvoker invoker) {}
b'\\ No newline at end of file'