~ubuntu-branches/ubuntu/raring/libjboss-remoting-java/raring

« back to all changes in this revision

Viewing changes to tests/org/jboss/test/remoting/classloader/parentfirst/ParentFirstClassloaderTestCase.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: package-import@ubuntu.com-20110909140103-hqokx61534tas9rg
Tags: 2.5.3.SP1-1
* Newer but not newest upstream release. Do not build samples.
* Change debian/watch to upstream's svn repo.
* Add patch to fix compile error caused by tomcat update.
  (Closes: #628303)
* Switch to source format 3.0.
* Switch to debhelper level 7.
* Remove useless Depends.
* Update Standards-Version: 3.9.2.
* Update README.source.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
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.
6
 
*
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.
11
 
*
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.
16
 
*
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.
21
 
*/
22
 
package org.jboss.test.remoting.classloader.parentfirst;
23
 
 
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;
29
 
import java.util.Map;
30
 
 
31
 
import javax.management.MBeanServer;
32
 
 
33
 
import junit.framework.TestCase;
34
 
 
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;
49
 
 
50
 
 
51
 
/**
52
 
 * Unit test for JBREM-1019.
53
 
 * 
54
 
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
55
 
 * @version $Revision: 1.1 $
56
 
 * <p>
57
 
 * Copyright Aug 3, 2008
58
 
 * </p>
59
 
 */
60
 
public class ParentFirstClassloaderTestCase extends TestCase
61
 
{
62
 
   private static Logger log = Logger.getLogger(ParentFirstClassloaderTestCase.class);
63
 
   
64
 
   private static boolean firstTime = true;
65
 
   
66
 
   protected String host;
67
 
   protected int port;
68
 
   protected String locatorURI;
69
 
   protected InvokerLocator serverLocator;
70
 
   protected Connector connector;
71
 
   protected TestInvocationHandler invocationHandler;
72
 
 
73
 
   
74
 
   public void setUp() throws Exception
75
 
   {
76
 
      if (firstTime)
77
 
      {
78
 
         firstTime = false;
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);  
85
 
      }
86
 
   }
87
 
 
88
 
   
89
 
   public void tearDown()
90
 
   {
91
 
   }
92
 
   
93
 
 
94
 
   /**
95
 
    * Verify that by default the context classloader is not called first.
96
 
    */
97
 
   public void testDefaultConfig() throws Throwable
98
 
   {
99
 
      log.info("entering " + getName());
100
 
      
101
 
      // Start server.
102
 
      setupServer();
103
 
      
104
 
      // Create client.
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);
110
 
      client.connect();
111
 
      log.info("client is connected");
112
 
      
113
 
      TestClassLoader tcl = new TestClassLoader();
114
 
      Thread.currentThread().setContextClassLoader(tcl);
115
 
      
116
 
      // Test connection.
117
 
      client.invoke("abc");
118
 
      log.info("connection is good");
119
 
      
120
 
      // Verify that TestClassLoader has not been queried.
121
 
      assertFalse(tcl.visited);
122
 
      log.info("context classloader has not been queried");
123
 
      
124
 
      client.disconnect();
125
 
      shutdownServer();
126
 
      log.info(getName() + " PASSES");
127
 
   }
128
 
   
129
 
   
130
 
   /**
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.
134
 
    */
135
 
   public void testByInvokerLocator() throws Throwable
136
 
   {
137
 
      log.info("entering " + getName());
138
 
      
139
 
      // Start server.
140
 
      setupServer();
141
 
      
142
 
      // Create client.
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);
150
 
      client.connect();
151
 
      log.info("client is connected");
152
 
      
153
 
      TestClassLoader tcl = new TestClassLoader();
154
 
      setContextClassLoader(tcl);
155
 
      
156
 
      // Test connection.
157
 
      client.invoke("abc");
158
 
      log.info("connection is good");
159
 
      
160
 
      // Verify that TestClassLoader has been queried.
161
 
      assertTrue(tcl.visited);
162
 
      log.info("context classloader has been queried");
163
 
      
164
 
      client.disconnect();
165
 
      shutdownServer();
166
 
      log.info(getName() + " PASSES");
167
 
   }
168
 
   
169
 
   
170
 
   /**
171
 
    * Verify that by the context classloader is called first if 
172
 
    * org.jboss.remoting.Remoting.CLASSLOADING_PARENT_FIRST_DELEGATION is set to "true"
173
 
    * in the config map.
174
 
    */
175
 
   public void testByConfigMap() throws Throwable
176
 
   {
177
 
      log.info("entering " + getName());
178
 
      
179
 
      // Start server.
180
 
      setupServer();
181
 
      
182
 
      // Create client.
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);
189
 
      client.connect();
190
 
      log.info("client is connected");
191
 
      
192
 
      TestClassLoader tcl = new TestClassLoader();
193
 
      setContextClassLoader(tcl);
194
 
      
195
 
      // Test connection.
196
 
      client.invoke("abc");
197
 
      log.info("connection is good");
198
 
      
199
 
      // Verify that TestClassLoader has been queried.
200
 
      assertTrue(tcl.visited);
201
 
      log.info("context classloader has been queried");
202
 
      
203
 
      client.disconnect();
204
 
      shutdownServer();
205
 
      log.info(getName() + " PASSES");
206
 
   }
207
 
   
208
 
   
209
 
   /**
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"
212
 
    * in the config map.
213
 
    */
214
 
   public void testBySystemProperty() throws Throwable
215
 
   {
216
 
      log.info("entering " + getName());
217
 
      
218
 
      // Start server.
219
 
      setupServer();
220
 
      
221
 
      // Create client.
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);
228
 
      client.connect();
229
 
      log.info("client is connected");
230
 
      
231
 
      TestClassLoader tcl = new TestClassLoader();
232
 
      setContextClassLoader(tcl);
233
 
      
234
 
      // Test connection.
235
 
      client.invoke("abc");
236
 
      log.info("connection is good");
237
 
      
238
 
      // Verify that TestClassLoader has been queried.
239
 
      assertTrue(tcl.visited);
240
 
      log.info("context classloader has been queried");
241
 
      
242
 
      client.disconnect();
243
 
      shutdownServer();
244
 
      log.info(getName() + " PASSES");
245
 
   }
246
 
   
247
 
   protected String getTransport()
248
 
   {
249
 
      return "socket";
250
 
   }
251
 
   
252
 
   
253
 
   protected void addExtraClientConfig(Map config) {}
254
 
   protected void addExtraServerConfig(Map config) {}
255
 
   
256
 
 
257
 
   protected void setupServer() throws Exception
258
 
   {
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);
268
 
      connector.create();
269
 
      invocationHandler = new TestInvocationHandler();
270
 
      connector.addInvocationHandler("test", invocationHandler);
271
 
      connector.start();
272
 
   }
273
 
   
274
 
   
275
 
   protected void shutdownServer() throws Exception
276
 
   {
277
 
      if (connector != null)
278
 
         connector.stop();
279
 
   }
280
 
   
281
 
   
282
 
   protected void setContextClassLoader(final ClassLoader classLoader)
283
 
   {
284
 
      AccessController.doPrivileged( new PrivilegedAction()
285
 
      {
286
 
         public Object run()
287
 
         {
288
 
            Thread.currentThread().setContextClassLoader(classLoader);
289
 
            return null;
290
 
         }
291
 
      });
292
 
   }
293
 
   
294
 
   
295
 
   static class TestInvocationHandler implements ServerInvocationHandler
296
 
   {
297
 
      public void addListener(InvokerCallbackHandler callbackHandler) {}
298
 
      public Object invoke(final InvocationRequest invocation) throws Throwable
299
 
      {
300
 
         return new TestClass();
301
 
      }
302
 
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
303
 
      public void setMBeanServer(MBeanServer server) {}
304
 
      public void setInvoker(ServerInvoker invoker) {}
305
 
   }
306
 
 
307
 
   
308
 
   static class TestClassLoader extends ClassLoader
309
 
   {
310
 
      boolean visited;
311
 
      
312
 
      public Class loadClass(String name) throws ClassNotFoundException
313
 
      {
314
 
         visited = true;
315
 
         log.info("TestClassLoader.loadClass(): " + name);
316
 
         throw new ClassNotFoundException(name);
317
 
      }
318
 
   }
319
 
   
320
 
   
321
 
   static class TestClass implements Serializable
322
 
   {
323
 
      /** The serialVersionUID */
324
 
      private static final long serialVersionUID = 1L;
325
 
   }
326
 
}
 
 
b'\\ No newline at end of file'