1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
53
54
55
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
65
66
67
68
69
70
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
85
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
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 }