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.config;
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: * Tests that verify that the process of configuring logging on startup
030: * works correctly by selecting the file with the highest priority.
031: * <p>
032: * This test sets up a classpath where:
033: * <ul>
034: * <li> first file found has priority=20
035: * <li> second file found has priority=10
036: * </ul>
037: * The result should be that the first file is used.
038: */
039: public class FirstPriorityConfigTestCase extends TestCase {
040:
041: // ------------------------------------------- JUnit Infrastructure Methods
042:
043: /**
044: * Return the tests included in this test suite.
045: */
046: public static Test suite() throws Exception {
047: Class this Class = FirstPriorityConfigTestCase.class;
048:
049: // Determine the URL to this .class file, so that we can then
050: // append the priority dirs to it. For tidiness, load this
051: // class through a dummy loader though this is not absolutely
052: // necessary...
053: PathableClassLoader dummy = new PathableClassLoader(null);
054: dummy.useSystemLoader("junit.");
055: dummy.addLogicalLib("testclasses");
056: dummy.addLogicalLib("commons-logging");
057:
058: String this ClassPath = this Class.getName().replace('.', '/')
059: + ".class";
060: URL baseUrl = dummy.findResource(this ClassPath);
061:
062: // Now set up the desired classloader hierarchy. We'll put a config
063: // file of priority=10 in the container path, and ones of both
064: // "no priority" and priority=20 in the webapp path.
065: //
066: // A second properties file with priority=20 is also added,
067: // so we can check that the first one in the classpath is
068: // used.
069: PathableClassLoader containerLoader = new PathableClassLoader(
070: null);
071: containerLoader.useSystemLoader("junit.");
072: containerLoader.addLogicalLib("commons-logging");
073:
074: PathableClassLoader webappLoader = new PathableClassLoader(
075: containerLoader);
076: webappLoader.addLogicalLib("testclasses");
077:
078: URL pri20URL = new URL(baseUrl, "priority20/");
079: webappLoader.addURL(pri20URL);
080:
081: URL pri10URL = new URL(baseUrl, "priority10/");
082: webappLoader.addURL(pri10URL);
083:
084: // load the test class via webapp loader, and use the webapp loader
085: // as the tccl loader too.
086: Class testClass = webappLoader.loadClass(this Class.getName());
087: return new PathableTestSuite(testClass, webappLoader);
088: }
089:
090: /**
091: * Set up instance variables required by this test case.
092: */
093: public void setUp() throws Exception {
094: LogFactory.releaseAll();
095: }
096:
097: /**
098: * Tear down instance variables required by this test case.
099: */
100: public void tearDown() {
101: LogFactory.releaseAll();
102: }
103:
104: // ----------------------------------------------------------- Test Methods
105:
106: /**
107: * Verify that the config file being used is the one containing
108: * the desired configId value.
109: */
110: public void testPriority() throws Exception {
111: LogFactory instance = LogFactory.getFactory();
112: String id = (String) instance.getAttribute("configId");
113: assertEquals("Correct config file loaded", "priority20", id);
114: }
115: }
|