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