1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
31
32
33
34
35
36
37
38
39
40 public class FirstPriorityConfigTestCase extends TestCase {
41
42
43
44
45 public static Test suite() throws Exception {
46 final Class thisClass = FirstPriorityConfigTestCase.class;
47
48
49
50
51
52 final PathableClassLoader dummy = new PathableClassLoader(null);
53 dummy.useExplicitLoader("junit.", Test.class.getClassLoader());
54 dummy.addLogicalLib("testclasses");
55 dummy.addLogicalLib("commons-logging");
56
57 final String thisClassPath = thisClass.getName().replace('.', '/') + ".class";
58 final URL baseUrl = dummy.findResource(thisClassPath);
59
60
61
62
63 final PathableClassLoader containerLoader = new PathableClassLoader(null);
64 containerLoader.useExplicitLoader("junit.", Test.class.getClassLoader());
65 containerLoader.addLogicalLib("commons-logging");
66
67 final PathableClassLoader webappLoader = new PathableClassLoader(containerLoader);
68 webappLoader.addLogicalLib("testclasses");
69
70 final URL pri20URL = new URL(baseUrl, "priority20/");
71 webappLoader.addURL(pri20URL);
72
73 final URL pri10URL = new URL(baseUrl, "priority10/");
74 webappLoader.addURL(pri10URL);
75
76
77
78 final Class testClass = webappLoader.loadClass(thisClass.getName());
79 return new PathableTestSuite(testClass, webappLoader);
80 }
81
82
83
84
85 @Override
86 public void setUp() throws Exception {
87 LogFactory.releaseAll();
88 }
89
90
91
92
93 @Override
94 public void tearDown() {
95 LogFactory.releaseAll();
96 }
97
98
99
100
101
102 public void testPriority() throws Exception {
103 final LogFactory instance = LogFactory.getFactory();
104
105 final ClassLoader thisClassLoader = this.getClass().getClassLoader();
106 final ClassLoader lfClassLoader = instance.getClass().getClassLoader();
107 final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
108
109
110 assertEquals(thisClassLoader, contextClassLoader);
111
112
113 assertEquals(lfClassLoader, thisClassLoader.getParent());
114 assertEquals(PathableClassLoader.class.getName(),
115 lfClassLoader.getClass().getName());
116
117 final String id = (String) instance.getAttribute("configId");
118 assertEquals("Correct config file loaded", "priority20", id );
119 }
120 }