View Javadoc
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  import java.net.URL;
21  
22  import org.apache.commons.logging.LogFactory;
23  import org.apache.commons.logging.PathableClassLoader;
24  import org.apache.commons.logging.PathableTestSuite;
25  
26  import junit.framework.Test;
27  import junit.framework.TestCase;
28  
29  /**
30   * Tests that verify that the process of configuring logging on startup
31   * works correctly by selecting the file with the highest priority.
32   * <p>
33   * This test sets up a classpath where:
34   * <ul>
35   * <li> first file found has priority=20
36   * <li> second file found has priority=10
37   * </ul>
38   * The result should be that the first file is used.
39   */
40  public class FirstPriorityConfigTestCase extends TestCase {
41  
42      /**
43       * Return the tests included in this test suite.
44       */
45      public static Test suite() throws Exception {
46          final Class thisClass = FirstPriorityConfigTestCase.class;
47  
48          // Determine the URL to this .class file, so that we can then
49          // append the priority dirs to it. For tidiness, load this
50          // class through a dummy loader though this is not absolutely
51          // necessary...
52          final PathableClassLoader dummy = new PathableClassLoader(null);
53          dummy.useExplicitLoader("junit.", Test.class.getClassLoader());
54          dummy.addLogicalLib("testclasses");
55          dummy.addLogicalLib("commons-logging");
56  
57          final String thisClassPath = thisClass.getName().replace('.', '/') + ".class";
58          final URL baseUrl = dummy.findResource(thisClassPath);
59  
60          // Now set up the desired class loader hierarchy. We'll put JCL
61          // in the container path, the test in a webapp path, and
62          // both config files into the webapp path too.
63          final PathableClassLoader containerLoader = new PathableClassLoader(null);
64          containerLoader.useExplicitLoader("junit.", Test.class.getClassLoader());
65          containerLoader.addLogicalLib("commons-logging");
66  
67          final PathableClassLoader webappLoader = new PathableClassLoader(containerLoader);
68          webappLoader.addLogicalLib("testclasses");
69  
70          final URL pri20URL = new URL(baseUrl, "priority20/");
71          webappLoader.addURL(pri20URL);
72  
73          final URL pri10URL = new URL(baseUrl, "priority10/");
74          webappLoader.addURL(pri10URL);
75  
76          // load the test class via webapp loader, and use the webapp loader
77          // as the tccl loader too.
78          final Class testClass = webappLoader.loadClass(thisClass.getName());
79          return new PathableTestSuite(testClass, webappLoader);
80      }
81  
82      /**
83       * Sets up instance variables required by this test case.
84       */
85      @Override
86      public void setUp() throws Exception {
87          LogFactory.releaseAll();
88      }
89  
90      /**
91       * Tear down instance variables required by this test case.
92       */
93      @Override
94      public void tearDown() {
95          LogFactory.releaseAll();
96      }
97  
98      /**
99       * Verify that the config file being used is the one containing
100      * the desired configId value.
101      */
102     public void testPriority() throws Exception {
103         final LogFactory instance = LogFactory.getFactory();
104 
105         final ClassLoader thisClassLoader = this.getClass().getClassLoader();
106         final ClassLoader lfClassLoader = instance.getClass().getClassLoader();
107         final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
108 
109         // context class loader should be thisClassLoader
110         assertEquals(thisClassLoader, contextClassLoader);
111 
112         // lfClassLoader should be parent of this class loader
113         assertEquals(lfClassLoader, thisClassLoader.getParent());
114         assertEquals(PathableClassLoader.class.getName(),
115                 lfClassLoader.getClass().getName());
116 
117         final String id = (String) instance.getAttribute("configId");
118         assertEquals("Correct config file loaded", "priority20", id );
119     }
120 }