View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.logging;
18  
19  import java.lang.reflect.InvocationTargetException;
20  
21  import junit.framework.TestCase;
22  
23  /**
24   * test to emulate container and application isolated from container
25   */
26  public class LoadTestCase extends TestCase {
27  
28      /**
29       * A custom class loader which "duplicates" logging classes available
30       * in the parent class loader into itself.
31       * <p>
32       * When asked to load a class that is in one of the LOG_PCKG packages,
33       * it loads the class itself (child-first). This class doesn't need
34       * to be set up with a classpath, as it simply uses the same classpath
35       * as the class loader that loaded it.
36       */
37      static class AppClassLoader extends ClassLoader {
38  
39          java.util.Map classes = new java.util.HashMap();
40  
41          AppClassLoader(final ClassLoader parent) {
42              super(parent);
43          }
44  
45          private Class<?> def(final String name) throws ClassNotFoundException {
46  
47              Class<?> result = (Class<?>) classes.get(name);
48              if (result != null) {
49                  return result;
50              }
51  
52              try {
53  
54                  final ClassLoader cl = this.getClass().getClassLoader();
55                  final String classFileName = name.replace('.', '/') + ".class";
56                  final java.io.InputStream is = cl.getResourceAsStream(classFileName);
57                  final java.io.ByteArrayOutputStream out = new java.io.ByteArrayOutputStream();
58  
59                  while (is.available() > 0) {
60                      out.write(is.read());
61                  }
62  
63                  final byte[] data = out.toByteArray();
64  
65                  result = super.defineClass(name, data, 0, data.length);
66                  classes.put(name, result);
67  
68                  return result;
69  
70              } catch (final java.io.IOException ioe) {
71  
72                  throw new ClassNotFoundException(name + " caused by " + ioe.getMessage());
73              }
74  
75          }
76  
77          // not very trivial to emulate we must implement "findClass",
78          // but it will delegate to JUnit class loader first
79          @Override
80          public Class loadClass(final String name) throws ClassNotFoundException {
81              // isolates all logging classes, application in the same class loader too.
82              // filters exceptions to simplify handling in test
83              for (final String element : LOG_PCKG) {
84                  if (name.startsWith(element) && !name.contains("Exception")) {
85                      return def(name);
86                  }
87              }
88              return super.loadClass(name);
89          }
90  
91      }
92  
93      //TODO: need some way to add service provider packages
94      static private String[] LOG_PCKG = {"org.apache.commons.logging",
95                                          "org.apache.commons.logging.impl"};
96  
97      private ClassLoader origContextClassLoader;
98  
99      private void execute(final Class cls) throws Exception {
100         cls.getConstructor().newInstance();
101     }
102 
103     /**
104      * Load class UserClass via a temporary class loader which is a child of
105      * the class loader used to load this test class.
106      */
107     private Class reload() throws Exception {
108         Class testObjCls = null;
109         final AppClassLoader appLoader = new AppClassLoader(this.getClass().getClassLoader());
110         try {
111 
112             testObjCls = appLoader.loadClass(UserClass.class.getName());
113 
114         } catch (final ClassNotFoundException cnfe) {
115             throw cnfe;
116         } catch (final Throwable t) {
117             t.printStackTrace();
118             fail("AppClassLoader failed ");
119         }
120 
121         assertSame("app isolated", testObjCls.getClassLoader(), appLoader);
122 
123         return testObjCls;
124 
125     }
126 
127     /**
128      * Call the static setAllowFlawedContext method on the specified class
129      * (expected to be a UserClass loaded via a custom class loader), passing
130      * it the specified state parameter.
131      */
132     private void setAllowFlawedContext(final Class<?> c, final String state) throws Exception {
133         final Class<?>[] params = {String.class};
134         final java.lang.reflect.Method m = c.getDeclaredMethod("setAllowFlawedContext", params);
135         m.invoke(null, state);
136     }
137 
138     @Override
139     public void setUp() {
140         // save state before test starts so we can restore it when test ends
141         origContextClassLoader = Thread.currentThread().getContextClassLoader();
142     }
143 
144     @Override
145     public void tearDown() {
146         // restore original state so a test can't stuff up later tests.
147         Thread.currentThread().setContextClassLoader(origContextClassLoader);
148     }
149 
150     /**
151      * Test what happens when we play various class loader tricks like those
152      * that happen in web and j2ee containers.
153      * <p>
154      * Note that this test assumes that commons-logging.jar and log4j.jar
155      * are available via the system classpath.
156      */
157     public void testInContainer() throws Exception {
158 
159         //problem can be in this step (broken app container or missconfiguration)
160         //1.  Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader());
161         //2.  Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
162         // we expect this :
163         // 1. Thread.currentThread().setContextClassLoader(appLoader);
164         // 2. Thread.currentThread().setContextClassLoader(null);
165 
166         // Context class loader is same as class calling into log
167         Class<?> cls = reload();
168         Thread.currentThread().setContextClassLoader(cls.getClassLoader());
169         execute(cls);
170 
171         // Context class loader is the "bootclass loader". This is technically
172         // bad, but LogFactoryImpl.ALLOW_FLAWED_CONTEXT defaults to true so
173         // this test should pass.
174         cls = reload();
175         Thread.currentThread().setContextClassLoader(null);
176         execute(cls);
177 
178         // Context class loader is the "bootclass loader". This is same as above
179         // except that ALLOW_FLAWED_CONTEXT is set to false; an error should
180         // now be reported.
181         cls = reload();
182         Thread.currentThread().setContextClassLoader(null);
183         try {
184             setAllowFlawedContext(cls, "false");
185             execute(cls);
186             fail("Logging config succeeded when context class loader was null!");
187         } catch (final InvocationTargetException ex) {
188             final Throwable targetException = ex.getTargetException();
189             // LogConfigurationException is expected; the boot class loader doesn't *have* JCL available
190             if (!(targetException instanceof LogConfigurationException)) {
191                 throw ex;
192             }
193         }
194 
195         // Context class loader is the system class loader.
196         //
197         // This is expected to cause problems, as LogFactoryImpl will attempt
198         // to use the system class loader to load the Log4JLogger class, which
199         // will then be unable to cast that object to the Log interface loaded
200         // via the child class loader. However as ALLOW_FLAWED_CONTEXT defaults
201         // to true this test should pass.
202         cls = reload();
203         Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader());
204         execute(cls);
205 
206         // Context class loader is the system class loader. This is the same
207         // as above except that ALLOW_FLAWED_CONTEXT is set to false; an error
208         // should now be reported.
209         cls = reload();
210         Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader());
211         try {
212             setAllowFlawedContext(cls, "false");
213             execute(cls);
214             fail("Error: somehow downcast a Logger loaded via system class loader"
215                     + " to the Log interface loaded via a custom class loader");
216         } catch (final InvocationTargetException ex) {
217             final Throwable targetException = ex.getTargetException();
218             // LogConfigurationException is expected
219             if (!(targetException instanceof LogConfigurationException)) {
220                 throw ex;
221             }
222         }
223     }
224 }