KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > logging > tccl > logfactory > TcclDisabledTestCase


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 JavaDoc;
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 a commons-logging.properties file can prevent a custom
32  * LogFactoryImpl being loaded from the tccl classloader.
33  */

34
35 public class TcclDisabledTestCase extends TestCase {
36
37     public static final String JavaDoc MY_LOG_FACTORY_PKG =
38         "org.apache.commons.logging.tccl.custom";
39
40     public static final String JavaDoc MY_LOG_FACTORY_IMPL =
41         MY_LOG_FACTORY_PKG + ".MyLogFactoryImpl";
42
43     // ------------------------------------------- JUnit Infrastructure Methods
44

45
46     /**
47      * Return the tests included in this test suite.
48      */

49     public static Test suite() throws Exception JavaDoc {
50         Class JavaDoc thisClass = TcclDisabledTestCase.class;
51
52         // Determine the URL to this .class file, so that we can then
53
// append the priority dirs to it. For tidiness, load this
54
// class through a dummy loader though this is not absolutely
55
// necessary...
56
PathableClassLoader dummy = new PathableClassLoader(null);
57         dummy.useSystemLoader("junit.");
58         dummy.addLogicalLib("testclasses");
59         dummy.addLogicalLib("commons-logging");
60         
61         String JavaDoc thisClassPath = thisClass.getName().replace('.', '/') + ".class";
62         URL JavaDoc baseUrl = dummy.findResource(thisClassPath);
63
64         // Now set up the desired classloader hierarchy. Everything goes into
65
// the parent classpath, but we exclude the custom LogFactoryImpl
66
// class.
67
//
68
// We then create a tccl classloader that can see the custom
69
// LogFactory class. Therefore if that class can be found, then the
70
// TCCL must have been used to load it.
71
PathableClassLoader emptyLoader = new PathableClassLoader(null);
72         
73         PathableClassLoader parentLoader = new PathableClassLoader(null);
74         parentLoader.useSystemLoader("junit.");
75         parentLoader.addLogicalLib("commons-logging");
76         parentLoader.addLogicalLib("testclasses");
77         // hack to ensure that the testcase classloader can't see
78
// the custom MyLogFactoryImpl
79
parentLoader.useExplicitLoader(
80             MY_LOG_FACTORY_PKG + ".", emptyLoader);
81         
82         URL JavaDoc propsEnableUrl = new URL JavaDoc(baseUrl, "props_disable_tccl/");
83         parentLoader.addURL(propsEnableUrl);
84
85         PathableClassLoader tcclLoader = new PathableClassLoader(parentLoader);
86         tcclLoader.addLogicalLib("testclasses");
87
88         Class JavaDoc testClass = parentLoader.loadClass(thisClass.getName());
89         return new PathableTestSuite(testClass, tcclLoader);
90     }
91
92     /**
93      * Set up instance variables required by this test case.
94      */

95     public void setUp() throws Exception JavaDoc {
96         LogFactory.releaseAll();
97     }
98
99     /**
100      * Tear down instance variables required by this test case.
101      */

102     public void tearDown() {
103         LogFactory.releaseAll();
104     }
105
106     // ----------------------------------------------------------- Test Methods
107

108     /**
109      * Verify that MyLogFactoryImpl is only loadable via the tccl.
110      */

111     public void testLoader() throws Exception JavaDoc {
112         
113         ClassLoader JavaDoc thisClassLoader = this.getClass().getClassLoader();
114         ClassLoader JavaDoc tcclLoader = Thread.currentThread().getContextClassLoader();
115
116         // the tccl loader should NOT be the same as the loader that loaded this test class.
117
assertNotSame("tccl not same as test classloader", thisClassLoader, tcclLoader);
118
119         // MyLogFactoryImpl should not be loadable via parent loader
120
try {
121             Class JavaDoc clazz = thisClassLoader.loadClass(MY_LOG_FACTORY_IMPL);
122             fail("Unexpectedly able to load MyLogFactoryImpl via test class classloader");
123         } catch(ClassNotFoundException JavaDoc ex) {
124             // ok, expected
125
}
126         
127         // MyLogFactoryImpl should be loadable via tccl loader
128
try {
129             Class JavaDoc clazz = tcclLoader.loadClass(MY_LOG_FACTORY_IMPL);
130         } catch(ClassNotFoundException JavaDoc ex) {
131             fail("Unexpectedly unable to load MyLogFactoryImpl via tccl classloader");
132         }
133     }
134
135     /**
136      * Verify that the custom LogFactory implementation which is only accessable
137      * via the TCCL has NOT been loaded. Because this is only accessable via the
138      * TCCL, and we've use a commons-logging.properties that disables TCCL loading,
139      * we should see the default LogFactoryImpl rather than the custom one.
140      */

141     public void testTcclLoading() throws Exception JavaDoc {
142         try {
143             LogFactory instance = LogFactory.getFactory();
144             fail("Unexpectedly succeeded in loading custom factory, though TCCL disabled.");
145         } catch(org.apache.commons.logging.LogConfigurationException ex) {
146             // ok, custom MyLogFactoryImpl as specified in props_disable_tccl
147
// could not be found.
148
int index = ex.getMessage().indexOf(MY_LOG_FACTORY_IMPL);
149             assertTrue("MylogFactoryImpl not found", index >= 0);
150         }
151     }
152 }
153
Popular Tags