1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.commons.logging.config;
19
20
21 import java.net.URL;
22
23 import junit.framework.Test;
24 import junit.framework.TestCase;
25
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.commons.logging.PathableClassLoader;
28 import org.apache.commons.logging.PathableTestSuite;
29
30
31 /**
32 * Tests that verify that the process of configuring logging on startup
33 * works correctly by selecting the file with the highest priority.
34 * <p>
35 * This test sets up a classpath where:
36 * <ul>
37 * <li> first file found has priority=20
38 * <li> second file found has priority=10
39 * </ul>
40 * The result should be that the first file is used.
41 */
42 public class FirstPriorityConfigTestCase extends TestCase {
43
44 // ------------------------------------------- JUnit Infrastructure Methods
45
46
47 /**
48 * Return the tests included in this test suite.
49 */
50 public static Test suite() throws Exception {
51 Class thisClass = FirstPriorityConfigTestCase.class;
52
53 // Determine the URL to this .class file, so that we can then
54 // append the priority dirs to it. For tidiness, load this
55 // class through a dummy loader though this is not absolutely
56 // necessary...
57 PathableClassLoader dummy = new PathableClassLoader(null);
58 dummy.useExplicitLoader("junit.", Test.class.getClassLoader());
59 dummy.addLogicalLib("testclasses");
60 dummy.addLogicalLib("commons-logging");
61
62 String thisClassPath = thisClass.getName().replace('.', '/') + ".class";
63 URL baseUrl = dummy.findResource(thisClassPath);
64
65 // Now set up the desired classloader hierarchy. We'll put JCL
66 // in the container path, the testcase in a webapp path, and
67 // both config files into the webapp path too.
68 PathableClassLoader containerLoader = new PathableClassLoader(null);
69 containerLoader.useExplicitLoader("junit.", Test.class.getClassLoader());
70 containerLoader.addLogicalLib("commons-logging");
71
72 PathableClassLoader webappLoader = new PathableClassLoader(containerLoader);
73 webappLoader.addLogicalLib("testclasses");
74
75 URL pri20URL = new URL(baseUrl, "priority20/");
76 webappLoader.addURL(pri20URL);
77
78 URL pri10URL = new URL(baseUrl, "priority10/");
79 webappLoader.addURL(pri10URL);
80
81 // load the test class via webapp loader, and use the webapp loader
82 // as the tccl loader too.
83 Class testClass = webappLoader.loadClass(thisClass.getName());
84 return new PathableTestSuite(testClass, webappLoader);
85 }
86
87 /**
88 * Set up instance variables required by this test case.
89 */
90 public void setUp() throws Exception {
91 LogFactory.releaseAll();
92 }
93
94 /**
95 * Tear down instance variables required by this test case.
96 */
97 public void tearDown() {
98 LogFactory.releaseAll();
99 }
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 }