2
* JBoss, Home of Professional Open Source
3
* Copyright 2009, 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.socketexception;
24
import java.io.IOException;
25
import java.net.InetAddress;
26
import java.net.Socket;
27
import java.net.SocketException;
28
import java.net.UnknownHostException;
29
import java.util.HashMap;
32
import javax.management.MBeanServer;
33
import javax.net.ServerSocketFactory;
34
import javax.net.SocketFactory;
36
import junit.framework.TestCase;
38
import org.apache.log4j.ConsoleAppender;
39
import org.apache.log4j.Level;
40
import org.apache.log4j.Logger;
41
import org.apache.log4j.PatternLayout;
42
import org.jboss.remoting.Client;
43
import org.jboss.remoting.InvocationRequest;
44
import org.jboss.remoting.InvokerLocator;
45
import org.jboss.remoting.Remoting;
46
import org.jboss.remoting.ServerInvocationHandler;
47
import org.jboss.remoting.ServerInvoker;
48
import org.jboss.remoting.callback.Callback;
49
import org.jboss.remoting.callback.HandleCallbackException;
50
import org.jboss.remoting.callback.InvokerCallbackHandler;
51
import org.jboss.remoting.transport.Connector;
52
import org.jboss.remoting.transport.PortUtil;
55
* Unit tests for JBREM-1152.
57
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
60
* Copyright Sep 9, 2009
63
public class SocketCreationExceptionTestCase extends TestCase
65
protected static String SEND_CALLBACK = "sendCallback";
66
private static Logger log = Logger.getLogger(SocketCreationExceptionTestCase.class);
67
private 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(Level.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 testInvocationWithSocketException() 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
clientConfig.put(Remoting.CUSTOM_SOCKET_FACTORY, new TestSocketFactory(new SocketException(getName()), 2));
109
addExtraClientConfig(clientConfig);
110
Client client = new Client(clientLocator, clientConfig);
112
log.info("client is connected");
115
assertEquals("abc", client.invoke("abc"));
116
log.info("connection is good");
120
log.info(getName() + " PASSES");
124
public void testInvocationWithIOException() throws Throwable
126
log.info("entering " + getName());
132
InvokerLocator clientLocator = new InvokerLocator(locatorURI);
133
HashMap clientConfig = new HashMap();
134
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
135
clientConfig.put(Remoting.CUSTOM_SOCKET_FACTORY, new TestSocketFactory(new IOException("Connection reset"), 2));
136
clientConfig.put("generalizeSocketException", "true");
137
addExtraClientConfig(clientConfig);
138
Client client = new Client(clientLocator, clientConfig);
140
log.info("client is connected");
143
assertEquals("abc", client.invoke("abc"));
144
log.info("connection is good");
148
log.info(getName() + " PASSES");
152
protected String getTransport()
158
protected void addExtraClientConfig(Map config) {}
159
protected void addExtraServerConfig(Map config) {}
162
protected void setupServer(ServerSocketFactory ssf) throws Exception
164
host = InetAddress.getLocalHost().getHostAddress();
165
port = PortUtil.findFreePort(host);
166
locatorURI = getTransport() + "://" + host + ":" + port;
167
String metadata = System.getProperty("remoting.metadata");
168
if (metadata != null)
170
locatorURI += "/?" + metadata;
172
serverLocator = new InvokerLocator(locatorURI);
173
log.info("Starting remoting server with locator uri of: " + locatorURI);
174
HashMap config = new HashMap();
175
config.put(InvokerLocator.FORCE_REMOTE, "true");
176
config.put("timeout", "5000");
179
config.put(Remoting.CUSTOM_SERVER_SOCKET_FACTORY, ssf);
181
addExtraServerConfig(config);
182
connector = new Connector(serverLocator, config);
184
invocationHandler = new TestInvocationHandler();
185
connector.addInvocationHandler("test", invocationHandler);
190
protected void shutdownServer() throws Exception
192
if (connector != null)
197
static class TestInvocationHandler implements ServerInvocationHandler
199
InvokerCallbackHandler callbackHandler;
201
public void addListener(InvokerCallbackHandler callbackHandler)
203
this.callbackHandler = callbackHandler;
205
public Object invoke(final InvocationRequest invocation) throws Throwable
207
Object parameter = invocation.getParameter();
208
if (SEND_CALLBACK.equals(parameter))
212
callbackHandler.handleCallback(new Callback("callback"));
214
catch (HandleCallbackException e)
216
log.error("unable to send callback", e);
221
public void removeListener(InvokerCallbackHandler callbackHandler) {}
222
public void setMBeanServer(MBeanServer server) {}
223
public void setInvoker(ServerInvoker invoker) {}
227
public static class TestSocketFactory extends SocketFactory
231
IOException exception;
233
public TestSocketFactory()
236
public TestSocketFactory(IOException exception, int limit)
238
this.exception = exception;
241
public Socket createSocket() throws IOException
244
log.info("counter: " + counter);
245
if (counter <= limit)
247
log.info("throwing exception");
250
log.info("returning socket");
253
public Socket createSocket(String arg0, int arg1) throws IOException, UnknownHostException
256
log.info("counter: " + counter);
257
if (counter <= limit)
261
log.info("returning socket");
262
return new Socket(arg0, arg1);
264
public Socket createSocket(InetAddress arg0, int arg1) throws IOException
267
log.info("counter: " + counter);
268
if (counter <= limit)
272
log.info("returning socket");
273
return new Socket(arg0, arg1);
275
public Socket createSocket(String arg0, int arg1, InetAddress arg2, int arg3) throws IOException, UnknownHostException
278
log.info("counter: " + counter);
279
if (counter <= limit)
283
log.info("returning socket");
284
return new Socket(arg0, arg1, arg2, arg3);
287
public Socket createSocket(InetAddress arg0, int arg1, InetAddress arg2, int arg3) throws IOException
290
log.info("counter: " + counter);
291
if (counter <= limit)
295
log.info("returning socket");
296
return new Socket(arg0, arg1, arg2, arg3);
b'\\ No newline at end of file'