2
* JBoss, Home of Professional Open Source.
3
* Copyright 2010, Red Hat Middleware LLC, and individual contributors
4
* as indicated by the @author tags. See the copyright.txt file in the
5
* distribution for a 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.
23
package org.jboss.test.remoting.transport.socket.timeout;
25
import java.io.IOException;
26
import java.lang.reflect.Method;
27
import java.net.InetAddress;
28
import java.security.AccessController;
29
import java.security.PrivilegedAction;
30
import java.util.HashMap;
33
import javax.management.MBeanServer;
35
import junit.framework.TestCase;
37
import org.apache.log4j.ConsoleAppender;
38
import org.apache.log4j.Level;
39
import org.apache.log4j.Logger;
40
import org.apache.log4j.PatternLayout;
41
import org.jboss.logging.XLevel;
42
import org.jboss.remoting.Client;
43
import org.jboss.remoting.InvocationRequest;
44
import org.jboss.remoting.InvokerLocator;
45
import org.jboss.remoting.InvokerRegistry;
46
import org.jboss.remoting.ServerInvocationHandler;
47
import org.jboss.remoting.ServerInvoker;
48
import org.jboss.remoting.callback.InvokerCallbackHandler;
49
import org.jboss.remoting.transport.ClientFactory;
50
import org.jboss.remoting.transport.ClientInvoker;
51
import org.jboss.remoting.transport.Connector;
52
import org.jboss.remoting.transport.PortUtil;
53
import org.jboss.remoting.transport.ServerFactory;
54
import org.jboss.remoting.transport.socket.ServerAddress;
55
import org.jboss.remoting.transport.socket.SocketClientInvoker;
56
import org.jboss.remoting.transport.socket.SocketServerInvoker;
60
* Unit tests for JBREM-1188.
62
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
63
* @version $Revision: 1.1 $
65
* Copyright Feb 16, 2010
67
public class SocketDefaultTimeoutTestCase extends TestCase
69
private static Logger log = Logger.getLogger(SocketDefaultTimeoutTestCase.class);
71
private static boolean firstTime = true;
73
protected String host;
75
protected String locatorURI;
76
protected InvokerLocator serverLocator;
77
protected Connector connector;
78
protected TestInvocationHandler invocationHandler;
81
public void setUp() throws Exception
86
Logger.getLogger("org.jboss.remoting").setLevel(XLevel.INFO);
87
Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
88
String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
89
PatternLayout layout = new PatternLayout(pattern);
90
ConsoleAppender consoleAppender = new ConsoleAppender(layout);
91
Logger.getRootLogger().addAppender(consoleAppender);
94
AccessController.doPrivileged( new PrivilegedAction()
98
InvokerRegistry.registerInvokerFactories(getTransport(), getClientFactoryClass(), TestServerFactory.class);
105
public void tearDown()
110
public void testDefaultTimeout() throws Throwable
112
log.info("entering " + getName());
118
InvokerLocator clientLocator = new InvokerLocator(locatorURI);
119
HashMap clientConfig = new HashMap();
120
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
121
addExtraClientConfig(clientConfig);
122
Client client = new Client(clientLocator, clientConfig);
124
log.info("client is connected");
125
Method getServerAddress = getClientInvokerClass().getMethod("getServerAddress", new Class[]{});
126
getServerAddress.setAccessible(true);
127
ClientInvoker clientInvoker = client.getInvoker();
128
ServerAddress address = (ServerAddress) getServerAddress.invoke(clientInvoker, new Object[]{});
129
log.info("timeout in use: " + address.timeout);
130
assertEquals(SocketClientInvoker.SO_TIMEOUT_DEFAULT, address.timeout);
134
log.info(getName() + " PASSES");
138
protected String getTransport()
144
protected void addExtraClientConfig(Map config) {}
145
protected void addExtraServerConfig(Map config) {}
148
protected void setupServer() throws Exception
150
host = InetAddress.getLocalHost().getHostAddress();
151
port = PortUtil.findFreePort(host);
152
locatorURI = getTransport() + "://" + host + ":" + port;
153
String metadata = System.getProperty("remoting.metadata");
154
if (metadata != null)
156
locatorURI += "/?" + metadata;
158
serverLocator = new InvokerLocator(locatorURI);
159
log.info("Starting remoting server with locator uri of: " + locatorURI);
160
HashMap config = new HashMap();
161
config.put(InvokerLocator.FORCE_REMOTE, "true");
162
addExtraServerConfig(config);
163
connector = new Connector(serverLocator, config);
165
invocationHandler = new TestInvocationHandler();
166
connector.addInvocationHandler("test", invocationHandler);
171
protected void shutdownServer() throws Exception
173
if (connector != null)
178
protected Class getClientFactoryClass()
180
return TestSocketClientFactory.class;
184
protected Class getClientInvokerClass()
186
return TestSocketClientInvoker.class;
190
static class TestInvocationHandler implements ServerInvocationHandler
192
public void addListener(InvokerCallbackHandler callbackHandler) {}
193
public Object invoke(final InvocationRequest invocation) throws Throwable
195
return invocation.getParameter();
197
public void removeListener(InvokerCallbackHandler callbackHandler) {}
198
public void setMBeanServer(MBeanServer server) {}
199
public void setInvoker(ServerInvoker invoker) {}
203
static public class TestSocketClientInvoker extends SocketClientInvoker
205
public TestSocketClientInvoker(InvokerLocator locator, Map configuration) throws IOException
207
super(locator, configuration);
209
public TestSocketClientInvoker(InvokerLocator locator)
213
public ServerAddress getServerAddress()
217
public String toString()
219
return "TestSocketClientInvoker";
224
public static class TestSocketClientFactory implements ClientFactory
226
public ClientInvoker createClientInvoker(InvokerLocator locator, Map config) throws IOException
228
ClientInvoker clientInvoker = new TestSocketClientInvoker(locator, config);
229
log.info("TestClientFaotory.createClientInvoker() returning " + clientInvoker);
230
return clientInvoker;
232
public boolean supportsSSL()
239
public static class TestServerFactory implements ServerFactory
241
public ServerInvoker createServerInvoker(InvokerLocator locator, Map config) throws IOException
243
log.info("TestServerFactory.createServerInvoker() called");
244
return new SocketServerInvoker(locator, config);
246
public boolean supportsSSL()
b'\\ No newline at end of file'