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 (in parent loader) has priority=10 (parentFirst=true)
36   * <li> second file found has no priority set
37   * <li> third file found has priority=20
38   * <li> fourth file found also has priority=20
39   * </ul>
40   * The result should be that the third file is used.
41   * <p>
42   * Note that parentFirst=true is used in this test because method
43   * {@code PathableClassLoader.getResources} always behaves as if
44   * parentFirst=true; see the PathableClassLoader Javadoc for details.
45   */
46  
47  public class PriorityConfigTestCase extends TestCase {
48  
49      /**
50       * Return the tests included in this test suite.
51       */
52      public static Test suite() throws Exception {
53          final Class thisClass = PriorityConfigTestCase.class;
54  
55          // Determine the URL to this .class file, so that we can then
56          // append the priority dirs to it. For tidiness, load this
57          // class through a dummy loader though this is not absolutely
58          // necessary...
59          final PathableClassLoader dummy = new PathableClassLoader(null);
60          dummy.useExplicitLoader("junit.", Test.class.getClassLoader());
61          dummy.addLogicalLib("testclasses");
62          dummy.addLogicalLib("commons-logging");
63  
64          final String thisClassPath = thisClass.getName().replace('.', '/') + ".class";
65          final URL baseUrl = dummy.findResource(thisClassPath);
66  
67          // Now set up the desired class loader hierarchy. We'll put a config
68          // file of priority=10 in the container path, and ones of both
69          // "no priority" and priority=20 in the webapp path.
70          //
71          // A second properties file with priority=20 is also added,
72          // so we can check that the first one in the classpath is
73          // used.
74          final PathableClassLoader containerLoader = new PathableClassLoader(null);
75          containerLoader.useExplicitLoader("junit.", Test.class.getClassLoader());
76          containerLoader.addLogicalLib("commons-logging");
77  
78          final URL pri10URL = new URL(baseUrl, "priority10/");
79          containerLoader.addURL(pri10URL);
80  
81          final PathableClassLoader webappLoader = new PathableClassLoader(containerLoader);
82          webappLoader.setParentFirst(true);
83          webappLoader.addLogicalLib("testclasses");
84  
85          final URL noPriorityURL = new URL(baseUrl, "nopriority/");
86          webappLoader.addURL(noPriorityURL);
87  
88          final URL pri20URL = new URL(baseUrl, "priority20/");
89          webappLoader.addURL(pri20URL);
90  
91          final URL pri20aURL = new URL(baseUrl, "priority20a/");
92          webappLoader.addURL(pri20aURL);
93  
94          // load the test class via webapp loader, and use the webapp loader
95          // as the tccl loader too.
96          final Class testClass = webappLoader.loadClass(thisClass.getName());
97          return new PathableTestSuite(testClass, webappLoader);
98      }
99  
100     /**
101      * Sets up instance variables required by this test case.
102      */
103     @Override
104     public void setUp() throws Exception {
105         LogFactory.releaseAll();
106     }
107 
108     /**
109      * Tear down instance variables required by this test case.
110      */
111     @Override
112     public void tearDown() {
113         LogFactory.releaseAll();
114     }
115 
116     /**
117      * Verify that the config file being used is the one containing
118      * the desired configId value.
119      */
120     public void testPriority() throws Exception {
121         final LogFactory instance = LogFactory.getFactory();
122         final String id = (String) instance.getAttribute("configId");
123         assertEquals("Correct config file loaded", "priority20", id );
124     }
125 }