2
* JBoss, Home of Professional Open Source
3
* Copyright 2008, 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.socketfactory;
24
import java.io.IOException;
25
import java.net.InetAddress;
26
import java.net.Socket;
27
import java.net.UnknownHostException;
28
import java.util.HashMap;
31
import javax.management.MBeanServer;
32
import javax.net.SocketFactory;
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.remoting.AbstractInvoker;
41
import org.jboss.remoting.Client;
42
import org.jboss.remoting.InvocationRequest;
43
import org.jboss.remoting.InvokerLocator;
44
import org.jboss.remoting.Remoting;
45
import org.jboss.remoting.ServerInvocationHandler;
46
import org.jboss.remoting.ServerInvoker;
47
import org.jboss.remoting.callback.InvokerCallbackHandler;
48
import org.jboss.remoting.transport.Connector;
49
import org.jboss.remoting.transport.PortUtil;
53
* Unit test for JBREM-1014.
55
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
56
* @version $Revision: 1.1 $
58
* Copyright Jul 18, 2008
61
public abstract class SocketFactoryClassNameTestRoot extends TestCase
63
private static Logger log = Logger.getLogger(SocketFactoryClassNameTestRoot.class);
65
private static boolean firstTime = true;
67
protected String host;
69
protected String locatorURI;
70
protected InvokerLocator serverLocator;
71
protected Connector connector;
72
protected TestInvocationHandler invocationHandler;
75
public void setUp() throws Exception
80
Logger.getLogger("org.jboss.remoting").setLevel(Level.INFO);
81
Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
82
String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
83
PatternLayout layout = new PatternLayout(pattern);
84
ConsoleAppender consoleAppender = new ConsoleAppender(layout);
85
Logger.getRootLogger().addAppender(consoleAppender);
90
public void tearDown()
95
public void testSocketFactoryClassNameInLocator() throws Throwable
97
log.info("entering " + getName());
103
String clientLocatorURI = locatorURI;
104
clientLocatorURI += "&" + Remoting.SOCKET_FACTORY_CLASS_NAME + "=" + getSocketFactoryClass().getName();
105
InvokerLocator clientLocator = new InvokerLocator(clientLocatorURI);
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 to " + clientLocatorURI);
114
assertEquals("abc", client.invoke("abc"));
115
log.info("connection is good");
117
// Verify client invoker is using configured SocketFactory.
118
AbstractInvoker invoker = (AbstractInvoker) client.getInvoker();
119
SocketFactory socketFactory = invoker.getSocketFactory();
120
log.info("SocketFactory: " + socketFactory);
121
assertTrue(getSocketFactoryClass().isInstance(socketFactory));
125
log.info(getName() + " PASSES");
129
public void testSocketFactoryClassNameInConfigMap() throws Throwable
131
log.info("entering " + getName());
137
InvokerLocator clientLocator = new InvokerLocator(locatorURI);
138
HashMap clientConfig = new HashMap();
139
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
140
clientConfig.put(Remoting.SOCKET_FACTORY_CLASS_NAME, getSocketFactoryClass().getName());
141
addExtraClientConfig(clientConfig);
142
Client client = new Client(clientLocator, clientConfig);
144
log.info("client is connected");
147
assertEquals("abc", client.invoke("abc"));
148
log.info("connection is good");
150
// Verify client invoker is using configured SocketFactory.
151
AbstractInvoker invoker = (AbstractInvoker) client.getInvoker();
152
SocketFactory socketFactory = invoker.getSocketFactory();
153
log.info("SocketFactory: " + socketFactory);
154
assertTrue(getSocketFactoryClass().isInstance(socketFactory));
158
log.info(getName() + " PASSES");
162
public void testSocketFactoryClassNameInLocatorWithUseAllParams() throws Throwable
164
log.info("entering " + getName());
170
String clientLocatorURI = locatorURI;
171
clientLocatorURI += "&" + Remoting.SOCKET_FACTORY_CLASS_NAME + "=" + getSocketFactoryClass().getName();
172
InvokerLocator clientLocator = new InvokerLocator(clientLocatorURI);
173
HashMap clientConfig = new HashMap();
174
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
175
addExtraClientConfig(clientConfig);
176
Client client = new Client(clientLocator, clientConfig);
178
log.info("client is connected to " + clientLocatorURI);
181
assertEquals("abc", client.invoke("abc"));
182
log.info("connection is good");
184
// Verify client invoker is using configured SocketFactory.
185
AbstractInvoker invoker = (AbstractInvoker) client.getInvoker();
186
SocketFactory socketFactory = invoker.getSocketFactory();
187
log.info("SocketFactory: " + socketFactory);
188
assertTrue(getSocketFactoryClass().isInstance(socketFactory));
192
log.info(getName() + " PASSES");
196
public void testSocketFactoryClassNameInConfigMapWithUseAllParams() throws Throwable
198
log.info("entering " + getName());
204
InvokerLocator clientLocator = new InvokerLocator(locatorURI);
205
HashMap clientConfig = new HashMap();
206
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
207
clientConfig.put(Remoting.SOCKET_FACTORY_CLASS_NAME, getSocketFactoryClass().getName());
208
addExtraClientConfig(clientConfig);
209
Client client = new Client(clientLocator, clientConfig);
211
log.info("client is connected");
214
assertEquals("abc", client.invoke("abc"));
215
log.info("connection is good");
217
// Verify client invoker is using configured SocketFactory.
218
AbstractInvoker invoker = (AbstractInvoker) client.getInvoker();
219
SocketFactory socketFactory = invoker.getSocketFactory();
220
log.info("SocketFactory: " + socketFactory);
221
assertTrue(getSocketFactoryClass().isInstance(socketFactory));
225
log.info(getName() + " PASSES");
229
protected abstract String getTransport();
232
protected Class getSocketFactoryClass()
234
return TestSocketFactory.class;
238
protected void addExtraClientConfig(Map config) {}
239
protected void addExtraServerConfig(Map config) {}
242
protected void setupServer(boolean useAllParams) throws Exception
244
host = InetAddress.getLocalHost().getHostAddress();
245
port = PortUtil.findFreePort(host);
246
locatorURI = getTransport() + "://" + host + ":" + port + "/?x=x";
247
String metadata = System.getProperty("remoting.metadata");
248
if (metadata != null)
250
locatorURI += "&" + metadata;
254
locatorURI += "&" + Remoting.USE_ALL_SOCKET_FACTORY_PARAMS + "=true";
256
serverLocator = new InvokerLocator(locatorURI);
257
log.info("Starting remoting server with locator uri of: " + locatorURI);
258
HashMap config = new HashMap();
259
config.put(InvokerLocator.FORCE_REMOTE, "true");
260
addExtraServerConfig(config);
261
connector = new Connector(serverLocator, config);
263
invocationHandler = new TestInvocationHandler();
264
connector.addInvocationHandler("test", invocationHandler);
269
protected void shutdownServer() throws Exception
271
if (connector != null)
276
static class TestInvocationHandler implements ServerInvocationHandler
278
public void addListener(InvokerCallbackHandler callbackHandler) {}
279
public Object invoke(final InvocationRequest invocation) throws Throwable
281
return invocation.getParameter();
283
public void removeListener(InvokerCallbackHandler callbackHandler) {}
284
public void setMBeanServer(MBeanServer server) {}
285
public void setInvoker(ServerInvoker invoker) {}
289
public static class TestSocketFactory extends SocketFactory
291
SocketFactory sf = SocketFactory.getDefault();
293
public TestSocketFactory()
297
public Socket createSocket() throws IOException, UnknownHostException
299
return sf.createSocket();
302
public Socket createSocket(String arg0, int arg1) throws IOException, UnknownHostException
304
return sf.createSocket(arg0, arg1);
307
public Socket createSocket(InetAddress arg0, int arg1) throws IOException
309
return sf.createSocket(arg0, arg1);
312
public Socket createSocket(String arg0, int arg1, InetAddress arg2, int arg3) throws IOException,
315
return sf.createSocket(arg0, arg1, arg2, arg3);
318
public Socket createSocket(InetAddress arg0, int arg1, InetAddress arg2, int arg3) throws IOException
320
return sf.createSocket(arg0, arg1, arg2, arg3);
b'\\ No newline at end of file'