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