001: /*
002: * Copyright 2006 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.commons.logging.tccl.logfactory;
018:
019: import java.net.URL;
020:
021: import junit.framework.Test;
022: import junit.framework.TestCase;
023:
024: import org.apache.commons.logging.LogFactory;
025: import org.apache.commons.logging.PathableClassLoader;
026: import org.apache.commons.logging.PathableTestSuite;
027:
028: /**
029: * Verify that by default a custom LogFactoryImpl is loaded from the
030: * tccl classloader.
031: */
032:
033: public class TcclEnabledTestCase extends TestCase {
034:
035: // ------------------------------------------- JUnit Infrastructure Methods
036:
037: /**
038: * Return the tests included in this test suite.
039: */
040: public static Test suite() throws Exception {
041: Class this Class = TcclEnabledTestCase.class;
042:
043: // Determine the URL to this .class file, so that we can then
044: // append the priority dirs to it. For tidiness, load this
045: // class through a dummy loader though this is not absolutely
046: // necessary...
047: PathableClassLoader dummy = new PathableClassLoader(null);
048: dummy.useSystemLoader("junit.");
049: dummy.addLogicalLib("testclasses");
050: dummy.addLogicalLib("commons-logging");
051:
052: String this ClassPath = this Class.getName().replace('.', '/')
053: + ".class";
054: URL baseUrl = dummy.findResource(this ClassPath);
055:
056: // Now set up the desired classloader hierarchy. Everything goes into
057: // the parent classpath, but we exclude the custom LogFactoryImpl
058: // class.
059: //
060: // We then create a tccl classloader that can see the custom
061: // LogFactory class. Therefore if that class can be found, then the
062: // TCCL must have been used to load it.
063: PathableClassLoader emptyLoader = new PathableClassLoader(null);
064:
065: PathableClassLoader parentLoader = new PathableClassLoader(null);
066: parentLoader.useSystemLoader("junit.");
067: parentLoader.addLogicalLib("commons-logging");
068: parentLoader.addLogicalLib("testclasses");
069: // hack to ensure that the testcase classloader can't see
070: // the cust MyLogFactoryImpl
071: parentLoader.useExplicitLoader(
072: "org.apache.commons.logging.tccl.custom.", emptyLoader);
073:
074: URL propsEnableUrl = new URL(baseUrl, "props_enable_tccl/");
075: parentLoader.addURL(propsEnableUrl);
076:
077: PathableClassLoader tcclLoader = new PathableClassLoader(
078: parentLoader);
079: tcclLoader.addLogicalLib("testclasses");
080:
081: Class testClass = parentLoader.loadClass(this Class.getName());
082: return new PathableTestSuite(testClass, tcclLoader);
083: }
084:
085: /**
086: * Set up instance variables required by this test case.
087: */
088: public void setUp() throws Exception {
089: LogFactory.releaseAll();
090: }
091:
092: /**
093: * Tear down instance variables required by this test case.
094: */
095: public void tearDown() {
096: LogFactory.releaseAll();
097: }
098:
099: // ----------------------------------------------------------- Test Methods
100:
101: /**
102: * Verify that MyLogFactoryImpl is only loadable via the tccl.
103: */
104: public void testLoader() throws Exception {
105:
106: ClassLoader this ClassLoader = this .getClass().getClassLoader();
107: ClassLoader tcclLoader = Thread.currentThread()
108: .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",
112: this ClassLoader, tcclLoader);
113:
114: // MyLogFactoryImpl should not be loadable via parent loader
115: try {
116: Class clazz = this ClassLoader
117: .loadClass("org.apache.commons.logging.tccl.custom.MyLogFactoryImpl");
118: fail("Unexpectedly able to load MyLogFactoryImpl via test class classloader");
119: } catch (ClassNotFoundException ex) {
120: // ok, expected
121: }
122:
123: // MyLogFactoryImpl should be loadable via tccl loader
124: try {
125: Class clazz = tcclLoader
126: .loadClass("org.apache.commons.logging.tccl.custom.MyLogFactoryImpl");
127: } catch (ClassNotFoundException ex) {
128: fail("Unexpectedly unable to load MyLogFactoryImpl via tccl classloader");
129: }
130: }
131:
132: /**
133: * Verify that the custom LogFactory implementation which is only accessable
134: * via the TCCL has successfully been loaded as specified in the config file.
135: * This proves that the TCCL was used to load that class.
136: */
137: public void testTcclLoading() throws Exception {
138: LogFactory instance = LogFactory.getFactory();
139:
140: assertEquals(
141: "Correct LogFactory loaded",
142: "org.apache.commons.logging.tccl.custom.MyLogFactoryImpl",
143: instance.getClass().getName());
144: }
145: }
|