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 (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
51
52
53 /***
54 * Return the tests included in this test suite.
55 */
56 public static Test suite() throws Exception {
57 Class thisClass = PriorityConfigTestCase.class;
58
59
60
61
62
63 PathableClassLoader dummy = new PathableClassLoader(null);
64 dummy.useSystemLoader("junit.");
65 dummy.addLogicalLib("testclasses");
66 dummy.addLogicalLib("commons-logging");
67
68 String thisClassPath = thisClass.getName().replace('.', '/') + ".class";
69 URL baseUrl = dummy.findResource(thisClassPath);
70
71
72
73
74
75
76
77
78 PathableClassLoader containerLoader = new PathableClassLoader(null);
79 containerLoader.useSystemLoader("junit.");
80 containerLoader.addLogicalLib("commons-logging");
81
82 URL pri10URL = new URL(baseUrl, "priority10/");
83 containerLoader.addURL(pri10URL);
84
85 PathableClassLoader webappLoader = new PathableClassLoader(containerLoader);
86 webappLoader.setParentFirst(true);
87 webappLoader.addLogicalLib("testclasses");
88
89 URL noPriorityURL = new URL(baseUrl, "nopriority/");
90 webappLoader.addURL(noPriorityURL);
91
92 URL pri20URL = new URL(baseUrl, "priority20/");
93 webappLoader.addURL(pri20URL);
94
95 URL pri20aURL = new URL(baseUrl, "priority20a/");
96 webappLoader.addURL(pri20aURL);
97
98
99
100 Class 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 {
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
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 {
125 LogFactory instance = LogFactory.getFactory();
126 String id = (String) instance.getAttribute("configId");
127 assertEquals("Correct config file loaded", "priority20", id );
128 }
129 }