KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > logging > config > PriorityConfigTestCase


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 JavaDoc;
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 (in parent loader) has priority=10 (parentFirst=true)
37  * <li> second file found has no priority set
38  * <li> third file found has priority=20
39  * <li> fourth file found also has priority=20
40  * </ul>
41  * The result should be that the third file is used.
42  * <p>
43  * Note that parentFirst=true is used in this test because method
44  * <code>PathableClassLoader.getResources</code> always behaves as if
45  * parentFirst=true; see the PathableClassLoader javadoc for details.
46  */

47
48 public class PriorityConfigTestCase extends TestCase {
49
50     // ------------------------------------------- JUnit Infrastructure Methods
51

52
53     /**
54      * Return the tests included in this test suite.
55      */

56     public static Test suite() throws Exception JavaDoc {
57         Class JavaDoc thisClass = PriorityConfigTestCase.class;
58
59         // Determine the URL to this .class file, so that we can then
60
// append the priority dirs to it. For tidiness, load this
61
// class through a dummy loader though this is not absolutely
62
// necessary...
63
PathableClassLoader dummy = new PathableClassLoader(null);
64         dummy.useSystemLoader("junit.");
65         dummy.addLogicalLib("testclasses");
66         dummy.addLogicalLib("commons-logging");
67         
68         String JavaDoc thisClassPath = thisClass.getName().replace('.', '/') + ".class";
69         URL JavaDoc baseUrl = dummy.findResource(thisClassPath);
70
71         // Now set up the desired classloader hierarchy. We'll put a config
72
// file of priority=10 in the container path, and ones of both
73
// "no priority" and priority=20 in the webapp path.
74
//
75
// A second properties file with priority=20 is also added,
76
// so we can check that the first one in the classpath is
77
// used.
78
PathableClassLoader containerLoader = new PathableClassLoader(null);
79         containerLoader.useSystemLoader("junit.");
80         containerLoader.addLogicalLib("commons-logging");
81         
82         URL JavaDoc pri10URL = new URL JavaDoc(baseUrl, "priority10/");
83         containerLoader.addURL(pri10URL);
84
85         PathableClassLoader webappLoader = new PathableClassLoader(containerLoader);
86         webappLoader.setParentFirst(true);
87         webappLoader.addLogicalLib("testclasses");
88
89         URL JavaDoc noPriorityURL = new URL JavaDoc(baseUrl, "nopriority/");
90         webappLoader.addURL(noPriorityURL);
91         
92         URL JavaDoc pri20URL = new URL JavaDoc(baseUrl, "priority20/");
93         webappLoader.addURL(pri20URL);
94         
95         URL JavaDoc pri20aURL = new URL JavaDoc(baseUrl, "priority20a/");
96         webappLoader.addURL(pri20aURL);
97         
98         // load the test class via webapp loader, and use the webapp loader
99
// as the tccl loader too.
100
Class JavaDoc testClass = webappLoader.loadClass(thisClass.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 JavaDoc {
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 JavaDoc {
125         LogFactory instance = LogFactory.getFactory();
126         String JavaDoc id = (String JavaDoc) instance.getAttribute("configId");
127         assertEquals("Correct config file loaded", "priority20", id );
128     }
129 }
130
Popular Tags