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  
82              // isolates all logging classes, application in the same class loader too.
83              // filters exceptions to simplify handling in test
84              for (final String element : LOG_PCKG) {
85                  if (name.startsWith(element) && name.indexOf("Exception") == -1) {
86                      return def(name);
87                  }
88              }
89              return super.loadClass(name);
90          }
91  
92      }
93  
94      //TODO: need some way to add service provider packages
95      static private String[] LOG_PCKG = {"org.apache.commons.logging",
96                                          "org.apache.commons.logging.impl"};
97  
98      private ClassLoader origContextClassLoader;
99  
100     private void execute(final Class cls) throws Exception {
101         cls.getConstructor().newInstance();
102     }
103 
104     /**
105      * Load class UserClass via a temporary class loader which is a child of
106      * the class loader used to load this test class.
107      */
108     private Class reload() throws Exception {
109         Class testObjCls = null;
110         final AppClassLoader appLoader = new AppClassLoader(this.getClass().getClassLoader());
111         try {
112 
113             testObjCls = appLoader.loadClass(UserClass.class.getName());
114 
115         } catch (final ClassNotFoundException cnfe) {
116             throw cnfe;
117         } catch (final Throwable t) {
118             t.printStackTrace();
119             fail("AppClassLoader failed ");
120         }
121 
122         assertSame("app isolated", testObjCls.getClassLoader(), appLoader);
123 
124         return testObjCls;
125 
126     }
127 
128     /**
129      * Call the static setAllowFlawedContext method on the specified class
130      * (expected to be a UserClass loaded via a custom class loader), passing
131      * it the specified state parameter.
132      */
133     private void setAllowFlawedContext(final Class<?> c, final String state) throws Exception {
134         final Class<?>[] params = {String.class};
135         final java.lang.reflect.Method m = c.getDeclaredMethod("setAllowFlawedContext", params);
136         m.invoke(null, state);
137     }
138 
139     @Override
140     public void setUp() {
141         // save state before test starts so we can restore it when test ends
142         origContextClassLoader = Thread.currentThread().getContextClassLoader();
143     }
144 
145     @Override
146     public void tearDown() {
147         // restore original state so a test can't stuff up later tests.
148         Thread.currentThread().setContextClassLoader(origContextClassLoader);
149     }
150 
151     /**
152      * Test what happens when we play various class loader tricks like those
153      * that happen in web and j2ee containers.
154      * <p>
155      * Note that this test assumes that commons-logging.jar and log4j.jar
156      * are available via the system classpath.
157      */
158     public void testInContainer() throws Exception {
159 
160         //problem can be in this step (broken app container or missconfiguration)
161         //1.  Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader());
162         //2.  Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
163         // we expect this :
164         // 1. Thread.currentThread().setContextClassLoader(appLoader);
165         // 2. Thread.currentThread().setContextClassLoader(null);
166 
167         // Context class loader is same as class calling into log
168         Class<?> cls = reload();
169         Thread.currentThread().setContextClassLoader(cls.getClassLoader());
170         execute(cls);
171 
172         // Context class loader is the "bootclass loader". This is technically
173         // bad, but LogFactoryImpl.ALLOW_FLAWED_CONTEXT defaults to true so
174         // this test should pass.
175         cls = reload();
176         Thread.currentThread().setContextClassLoader(null);
177         execute(cls);
178 
179         // Context class loader is the "bootclass loader". This is same as above
180         // except that ALLOW_FLAWED_CONTEXT is set to false; an error should
181         // now be reported.
182         cls = reload();
183         Thread.currentThread().setContextClassLoader(null);
184         try {
185             setAllowFlawedContext(cls, "false");
186             execute(cls);
187             fail("Logging config succeeded when context class loader was null!");
188         } catch (final InvocationTargetException ex) {
189             final Throwable targetException = ex.getTargetException();
190             // LogConfigurationException is expected; the boot class loader doesn't *have* JCL available
191             if (!(targetException instanceof LogConfigurationException)) {
192                 throw ex;
193             }
194         }
195 
196         // Context class loader is the system class loader.
197         //
198         // This is expected to cause problems, as LogFactoryImpl will attempt
199         // to use the system class loader to load the Log4JLogger class, which
200         // will then be unable to cast that object to the Log interface loaded
201         // via the child class loader. However as ALLOW_FLAWED_CONTEXT defaults
202         // to true this test should pass.
203         cls = reload();
204         Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader());
205         execute(cls);
206 
207         // Context class loader is the system class loader. This is the same
208         // as above except that ALLOW_FLAWED_CONTEXT is set to false; an error
209         // should now be reported.
210         cls = reload();
211         Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader());
212         try {
213             setAllowFlawedContext(cls, "false");
214             execute(cls);
215             fail("Error: somehow downcast a Logger loaded via system class loader"
216                     + " to the Log interface loaded via a custom class loader");
217         } catch (final InvocationTargetException ex) {
218             final Throwable targetException = ex.getTargetException();
219             // LogConfigurationException is expected
220             if (!(targetException instanceof LogConfigurationException)) {
221                 throw ex;
222             }
223         }
224     }
225 }