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.config;
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 * Tests that verify that the process of configuring logging on startup
033 * works correctly by selecting the file with the highest priority.
034 * <p>
035 * This test sets up a classpath where:
036 * <ul>
037 * <li> first file found has priority=20
038 * <li> second file found has priority=10
039 * </ul>
040 * The result should be that the first file is used.
041 */
042 public class FirstPriorityConfigTestCase extends TestCase {
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 = FirstPriorityConfigTestCase.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. We'll put JCL
066 // in the container path, the testcase in a webapp path, and
067 // both config files into the webapp path too.
068 PathableClassLoader containerLoader = new PathableClassLoader(null);
069 containerLoader.useExplicitLoader("junit.", Test.class.getClassLoader());
070 containerLoader.addLogicalLib("commons-logging");
071
072 PathableClassLoader webappLoader = new PathableClassLoader(containerLoader);
073 webappLoader.addLogicalLib("testclasses");
074
075 URL pri20URL = new URL(baseUrl, "priority20/");
076 webappLoader.addURL(pri20URL);
077
078 URL pri10URL = new URL(baseUrl, "priority10/");
079 webappLoader.addURL(pri10URL);
080
081 // load the test class via webapp loader, and use the webapp loader
082 // as the tccl loader too.
083 Class testClass = webappLoader.loadClass(thisClass.getName());
084 return new PathableTestSuite(testClass, webappLoader);
085 }
086
087 /**
088 * Set up instance variables required by this test case.
089 */
090 public void setUp() throws Exception {
091 LogFactory.releaseAll();
092 }
093
094 /**
095 * Tear down instance variables required by this test case.
096 */
097 public void tearDown() {
098 LogFactory.releaseAll();
099 }
100
101 // ----------------------------------------------------------- Test Methods
102
103 /**
104 * Verify that the config file being used is the one containing
105 * the desired configId value.
106 */
107 public void testPriority() throws Exception {
108 LogFactory instance = LogFactory.getFactory();
109
110 ClassLoader thisClassLoader = this.getClass().getClassLoader();
111 ClassLoader lfClassLoader = instance.getClass().getClassLoader();
112 ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
113
114 // context classloader should be thisClassLoader
115 assertEquals(thisClassLoader, contextClassLoader);
116
117 // lfClassLoader should be parent of this classloader
118 assertEquals(lfClassLoader, thisClassLoader.getParent());
119 assertEquals(PathableClassLoader.class.getName(),
120 lfClassLoader.getClass().getName());
121
122 String id = (String) instance.getAttribute("configId");
123 assertEquals("Correct config file loaded", "priority20", id );
124 }
125 }