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.classloader.parentfirst;
24
import java.io.Serializable;
25
import java.net.InetAddress;
26
import java.security.AccessController;
27
import java.security.PrivilegedAction;
28
import java.util.HashMap;
31
import javax.management.MBeanServer;
33
import junit.framework.TestCase;
35
import org.apache.log4j.ConsoleAppender;
36
import org.apache.log4j.Level;
37
import org.apache.log4j.Logger;
38
import org.apache.log4j.PatternLayout;
39
import org.jboss.logging.XLevel;
40
import org.jboss.remoting.Client;
41
import org.jboss.remoting.InvocationRequest;
42
import org.jboss.remoting.InvokerLocator;
43
import org.jboss.remoting.Remoting;
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;
52
* Unit test for JBREM-1019.
54
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
55
* @version $Revision: 1.1 $
57
* Copyright Aug 3, 2008
60
public class ParentFirstClassloaderTestCase extends TestCase
62
private static Logger log = Logger.getLogger(ParentFirstClassloaderTestCase.class);
64
private static boolean firstTime = true;
66
protected String host;
68
protected String locatorURI;
69
protected InvokerLocator serverLocator;
70
protected Connector connector;
71
protected TestInvocationHandler invocationHandler;
74
public void setUp() throws Exception
79
Logger.getLogger("org.jboss.remoting").setLevel(XLevel.INFO);
80
Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
81
String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
82
PatternLayout layout = new PatternLayout(pattern);
83
ConsoleAppender consoleAppender = new ConsoleAppender(layout);
84
Logger.getRootLogger().addAppender(consoleAppender);
89
public void tearDown()
95
* Verify that by default the context classloader is not called first.
97
public void testDefaultConfig() 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
addExtraClientConfig(clientConfig);
109
Client client = new Client(clientLocator, clientConfig);
111
log.info("client is connected");
113
TestClassLoader tcl = new TestClassLoader();
114
Thread.currentThread().setContextClassLoader(tcl);
117
client.invoke("abc");
118
log.info("connection is good");
120
// Verify that TestClassLoader has not been queried.
121
assertFalse(tcl.visited);
122
log.info("context classloader has not been queried");
126
log.info(getName() + " PASSES");
131
* Verify that by the context classloader is called first if
132
* org.jboss.remoting.Remoting.CLASSLOADING_PARENT_FIRST_DELEGATION is set to "true"
133
* in the InvokerLocator.
135
public void testByInvokerLocator() throws Throwable
137
log.info("entering " + getName());
143
String clientLocatorURI = locatorURI;
144
clientLocatorURI += "/?" + Remoting.CLASSLOADING_PARENT_FIRST_DELEGATION + "=false";
145
InvokerLocator clientLocator = new InvokerLocator(clientLocatorURI);
146
HashMap clientConfig = new HashMap();
147
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
148
addExtraClientConfig(clientConfig);
149
Client client = new Client(clientLocator, clientConfig);
151
log.info("client is connected");
153
TestClassLoader tcl = new TestClassLoader();
154
setContextClassLoader(tcl);
157
client.invoke("abc");
158
log.info("connection is good");
160
// Verify that TestClassLoader has been queried.
161
assertTrue(tcl.visited);
162
log.info("context classloader has been queried");
166
log.info(getName() + " PASSES");
171
* Verify that by the context classloader is called first if
172
* org.jboss.remoting.Remoting.CLASSLOADING_PARENT_FIRST_DELEGATION is set to "true"
175
public void testByConfigMap() throws Throwable
177
log.info("entering " + getName());
183
InvokerLocator clientLocator = new InvokerLocator(locatorURI);
184
HashMap clientConfig = new HashMap();
185
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
186
clientConfig.put(Remoting.CLASSLOADING_PARENT_FIRST_DELEGATION, "false");
187
addExtraClientConfig(clientConfig);
188
Client client = new Client(clientLocator, clientConfig);
190
log.info("client is connected");
192
TestClassLoader tcl = new TestClassLoader();
193
setContextClassLoader(tcl);
196
client.invoke("abc");
197
log.info("connection is good");
199
// Verify that TestClassLoader has been queried.
200
assertTrue(tcl.visited);
201
log.info("context classloader has been queried");
205
log.info(getName() + " PASSES");
210
* Verify that by the context classloader is called first if the system property
211
* org.jboss.remoting.Remoting.CLASSLOADING_PARENT_FIRST_DELEGATION_PROP is set to "true"
214
public void testBySystemProperty() throws Throwable
216
log.info("entering " + getName());
222
InvokerLocator clientLocator = new InvokerLocator(locatorURI);
223
HashMap clientConfig = new HashMap();
224
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
225
addExtraClientConfig(clientConfig);
226
System.setProperty(Remoting.CLASSLOADING_PARENT_FIRST_DELEGATION_PROP, "false");
227
Client client = new Client(clientLocator, clientConfig);
229
log.info("client is connected");
231
TestClassLoader tcl = new TestClassLoader();
232
setContextClassLoader(tcl);
235
client.invoke("abc");
236
log.info("connection is good");
238
// Verify that TestClassLoader has been queried.
239
assertTrue(tcl.visited);
240
log.info("context classloader has been queried");
244
log.info(getName() + " PASSES");
247
protected String getTransport()
253
protected void addExtraClientConfig(Map config) {}
254
protected void addExtraServerConfig(Map config) {}
257
protected void setupServer() throws Exception
259
host = InetAddress.getLocalHost().getHostAddress();
260
port = PortUtil.findFreePort(host);
261
locatorURI = getTransport() + "://" + host + ":" + port;
262
serverLocator = new InvokerLocator(locatorURI);
263
log.info("Starting remoting server with locator uri of: " + locatorURI);
264
HashMap config = new HashMap();
265
config.put(InvokerLocator.FORCE_REMOTE, "true");
266
addExtraServerConfig(config);
267
connector = new Connector(serverLocator, config);
269
invocationHandler = new TestInvocationHandler();
270
connector.addInvocationHandler("test", invocationHandler);
275
protected void shutdownServer() throws Exception
277
if (connector != null)
282
protected void setContextClassLoader(final ClassLoader classLoader)
284
AccessController.doPrivileged( new PrivilegedAction()
288
Thread.currentThread().setContextClassLoader(classLoader);
295
static class TestInvocationHandler implements ServerInvocationHandler
297
public void addListener(InvokerCallbackHandler callbackHandler) {}
298
public Object invoke(final InvocationRequest invocation) throws Throwable
300
return new TestClass();
302
public void removeListener(InvokerCallbackHandler callbackHandler) {}
303
public void setMBeanServer(MBeanServer server) {}
304
public void setInvoker(ServerInvoker invoker) {}
308
static class TestClassLoader extends ClassLoader
312
public Class loadClass(String name) throws ClassNotFoundException
315
log.info("TestClassLoader.loadClass(): " + name);
316
throw new ClassNotFoundException(name);
321
static class TestClass implements Serializable
323
/** The serialVersionUID */
324
private static final long serialVersionUID = 1L;
b'\\ No newline at end of file'