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;
29
import java.util.LinkedList;
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.MicroSocketClientInvoker;
50
import org.jboss.remoting.transport.socket.SocketWrapper;
54
* Unit test for JBREM-703.
56
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
57
* @version $Revision: 1.1 $
59
* Copyright Feb 28, 2008
62
public class SocketSocketConfigurationTestCase extends TestCase
64
private static Logger log = Logger.getLogger(SocketSocketConfigurationTestCase.class);
66
protected static boolean firstTime = true;
68
protected String host;
70
protected String locatorURI;
71
protected InvokerLocator serverLocator;
72
protected Connector connector;
73
protected TestInvocationHandler invocationHandler;
76
public void setUp() throws Exception
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);
91
public void tearDown()
96
public void testConfigureByConfigMap() throws Throwable
98
log.info("entering " + getName());
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);
111
log.info("client is connected");
114
assertEquals("abc", client.invoke("abc"));
115
log.info("connection is good");
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);
129
log.info(getName() + " PASSES");
133
protected void setupClient(Map config)
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");
145
protected void doSocketTest(Socket s) throws SocketException
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");
156
protected void suggestEquals(int i1, int i2, String s)
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");
166
protected String getTransport()
172
protected void addExtraClientConfig(Map config) {}
173
protected void addExtraServerConfig(Map config) {}
176
protected void setupServer() throws Exception
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);
188
invocationHandler = new TestInvocationHandler();
189
connector.addInvocationHandler("test", invocationHandler);
194
protected void shutdownServer() throws Exception
196
if (connector != null)
201
static class TestInvocationHandler implements ServerInvocationHandler
203
public void addListener(InvokerCallbackHandler callbackHandler) {}
204
public Object invoke(final InvocationRequest invocation) throws Throwable
206
return invocation.getParameter();
208
public void removeListener(InvokerCallbackHandler callbackHandler) {}
209
public void setMBeanServer(MBeanServer server) {}
210
public void setInvoker(ServerInvoker invoker) {}
b'\\ No newline at end of file'