1   /*
2    * Copyright 2006 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */ 
16  
17  package org.apache.commons.logging.tccl.logfactory;
18  
19  
20  import java.net.URL;
21  
22  import junit.framework.Test;
23  import junit.framework.TestCase;
24  
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.commons.logging.PathableClassLoader;
27  import org.apache.commons.logging.PathableTestSuite;
28  
29  
30  /***
31   * Verify that by default a custom LogFactoryImpl is loaded from the
32   * tccl classloader.
33   */
34  
35  public class TcclEnabledTestCase extends TestCase {
36  
37      // ------------------------------------------- JUnit Infrastructure Methods
38  
39  
40      /***
41       * Return the tests included in this test suite.
42       */
43      public static Test suite() throws Exception {
44          Class thisClass = TcclEnabledTestCase.class;
45  
46          // Determine the URL to this .class file, so that we can then
47          // append the priority dirs to it. For tidiness, load this
48          // class through a dummy loader though this is not absolutely
49          // necessary...
50          PathableClassLoader dummy = new PathableClassLoader(null);
51          dummy.useSystemLoader("junit.");
52          dummy.addLogicalLib("testclasses");
53          dummy.addLogicalLib("commons-logging");
54          
55          String thisClassPath = thisClass.getName().replace('.', '/') + ".class";
56          URL baseUrl = dummy.findResource(thisClassPath);
57  
58          // Now set up the desired classloader hierarchy. Everything goes into
59          // the parent classpath, but we exclude the custom LogFactoryImpl
60          // class.
61          //
62          // We then create a tccl classloader that can see the custom
63          // LogFactory class. Therefore if that class can be found, then the
64          // TCCL must have been used to load it.
65          PathableClassLoader emptyLoader = new PathableClassLoader(null);
66          
67          PathableClassLoader parentLoader = new PathableClassLoader(null);
68          parentLoader.useSystemLoader("junit.");
69          parentLoader.addLogicalLib("commons-logging");
70          parentLoader.addLogicalLib("testclasses");
71          // hack to ensure that the testcase classloader can't see
72          // the cust MyLogFactoryImpl
73          parentLoader.useExplicitLoader(
74              "org.apache.commons.logging.tccl.custom.", emptyLoader);
75          
76          URL propsEnableUrl = new URL(baseUrl, "props_enable_tccl/");
77          parentLoader.addURL(propsEnableUrl);
78  
79          PathableClassLoader tcclLoader = new PathableClassLoader(parentLoader);
80          tcclLoader.addLogicalLib("testclasses");
81  
82          Class testClass = parentLoader.loadClass(thisClass.getName());
83          return new PathableTestSuite(testClass, tcclLoader);
84      }
85  
86      /***
87       * Set up instance variables required by this test case.
88       */
89      public void setUp() throws Exception {
90          LogFactory.releaseAll();
91      }
92  
93      /***
94       * Tear down instance variables required by this test case.
95       */
96      public void tearDown() {
97          LogFactory.releaseAll();
98      }
99  
100     // ----------------------------------------------------------- Test Methods
101 
102     /***
103      * Verify that MyLogFactoryImpl is only loadable via the tccl.
104      */
105     public void testLoader() throws Exception {
106         
107         ClassLoader thisClassLoader = this.getClass().getClassLoader();
108         ClassLoader tcclLoader = Thread.currentThread().getContextClassLoader();
109 
110         // the tccl loader should NOT be the same as the loader that loaded this test class.
111         assertNotSame("tccl not same as test classloader", thisClassLoader, tcclLoader);
112 
113         // MyLogFactoryImpl should not be loadable via parent loader
114         try {
115             Class clazz = thisClassLoader.loadClass(
116                 "org.apache.commons.logging.tccl.custom.MyLogFactoryImpl");
117             fail("Unexpectedly able to load MyLogFactoryImpl via test class classloader");
118         } catch(ClassNotFoundException ex) {
119             // ok, expected
120         }
121         
122         // MyLogFactoryImpl should be loadable via tccl loader
123         try {
124             Class clazz = tcclLoader.loadClass(
125                 "org.apache.commons.logging.tccl.custom.MyLogFactoryImpl");
126         } catch(ClassNotFoundException ex) {
127             fail("Unexpectedly unable to load MyLogFactoryImpl via tccl classloader");
128         }
129     }
130 
131     /***
132      * Verify that the custom LogFactory implementation which is only accessable
133      * via the TCCL has successfully been loaded as specified in the config file.
134      * This proves that the TCCL was used to load that class.
135      */
136     public void testTcclLoading() throws Exception {
137         LogFactory instance = LogFactory.getFactory();
138         
139         assertEquals(
140             "Correct LogFactory loaded", 
141             "org.apache.commons.logging.tccl.custom.MyLogFactoryImpl",
142             instance.getClass().getName());
143     }
144 }