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