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.client;
24
import java.net.InetAddress;
25
import java.util.HashMap;
28
import javax.management.MBeanServer;
30
import junit.framework.TestCase;
32
import org.apache.log4j.ConsoleAppender;
33
import org.apache.log4j.Level;
34
import org.apache.log4j.Logger;
35
import org.apache.log4j.PatternLayout;
36
import org.jboss.remoting.Client;
37
import org.jboss.remoting.InvocationRequest;
38
import org.jboss.remoting.InvokerLocator;
39
import org.jboss.remoting.ServerInvocationHandler;
40
import org.jboss.remoting.ServerInvoker;
41
import org.jboss.remoting.callback.InvokerCallbackHandler;
42
import org.jboss.remoting.transport.Connector;
43
import org.jboss.remoting.transport.PortUtil;
46
* ClientSerializationTestCase verifies that org.jboss.remoting.Client can be
49
* See JBREM-708: http://jira.jboss.org/jira/browse/JBREM-708
51
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
52
* @version $Revision: 2225 $
54
* Copyright Feb 23, 2007
57
public class ClientSerializationTestCase extends TestCase
59
protected static Logger log = Logger.getLogger(ClientSerializationTestCase.class);
60
protected static boolean firstTime = true;
62
protected static final String TEST = "test";
63
protected static final String RETURN_CLIENT = "returnClient";
65
protected InvokerLocator locator;
66
protected Connector connector;
67
protected Client client;
70
public void setUp() throws Exception
75
Logger.getLogger("org.jboss.remoting").setLevel(Level.DEBUG);
76
Logger.getLogger("org.jboss.test.remoting").setLevel(Level.DEBUG);
77
String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
78
PatternLayout layout = new PatternLayout(pattern);
79
ConsoleAppender consoleAppender = new ConsoleAppender(layout);
80
Logger.getRootLogger().addAppender(consoleAppender);
85
public void tearDown() throws Exception
93
* Calling client.invoke(RETURN_CLIENT) will cause the invoker handler to return
94
* a Client. This method uses java serialization.
96
public void testJavaSerialization() throws Throwable
98
log.info("entering " + getName());
100
assertEquals(TEST, client.invoke(TEST));
101
Object response = client.invoke(RETURN_CLIENT);
102
assertTrue(response instanceof Client);
103
log.info("received Client");
104
Client returnedClient = (Client) response;
105
assertEquals(TEST, returnedClient.invoke(TEST));
106
log.info(getName() + " PASSES");
111
* Calling client.invoke(RETURN_CLIENT) will cause the invoker handler to return
112
* a Client. This method uses jboss serialization.
114
public void testJBossSerialization() throws Throwable
116
log.info("entering " + getName());
118
assertEquals(TEST, client.invoke(TEST));
119
Object response = client.invoke(RETURN_CLIENT);
120
assertTrue(response instanceof Client);
121
log.info("received Client");
122
Client returnedClient = (Client) response;
123
assertEquals(TEST, returnedClient.invoke(TEST));
124
log.info(getName() + " PASSES");
129
* Create a Connector and a Client.
131
protected void init(String serializationType) throws Exception
133
String host = InetAddress.getLocalHost().getHostAddress();
134
int port = PortUtil.findFreePort(host);
135
String locatorURI = getTransport() + "://" + host + ":" + port;
136
locatorURI += "/?serializationtype=" + serializationType;
137
log.info("server locator: " + locatorURI);
138
locator = new InvokerLocator(locatorURI);
139
HashMap serverConfig = new HashMap();
140
addServerConfig(serverConfig);
141
connector = new Connector(locator, serverConfig);
143
connector.addInvocationHandler("test", new TestHandler());
146
HashMap clientConfig = new HashMap();
147
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
148
addClientConfig(clientConfig);
149
client = new Client(locator, clientConfig);
154
protected String getTransport()
160
protected void addServerConfig(Map config)
165
protected void addClientConfig(Map config)
170
public class TestHandler implements ServerInvocationHandler
173
public void setMBeanServer(MBeanServer server) {}
174
public void setInvoker(ServerInvoker invoker) {}
176
public Object invoke(InvocationRequest invocation) throws Throwable
178
String command = (String) invocation.getParameter();
179
if (TEST.equals(command))
183
else if (RETURN_CLIENT.equals(command))
185
HashMap config = new HashMap();
186
config.put(InvokerLocator.FORCE_REMOTE, "true");
187
Client client = new Client(locator, config);
190
if (TEST.equals(client.invoke(TEST)))
193
throw new Exception("unable to create working client");
197
log.error("unrecognized command: " + command);
198
throw new Exception("unrecognized command: " + command);
202
public void addListener(InvokerCallbackHandler callbackHandler) {}
203
public void removeListener(InvokerCallbackHandler callbackHandler) {}