1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.logging.tccl.log;
19
20
21 import java.net.URL;
22
23 import junit.framework.Test;
24 import junit.framework.TestCase;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.commons.logging.PathableClassLoader;
29 import org.apache.commons.logging.PathableTestSuite;
30
31
32
33
34
35
36
37 public class TcclEnabledTestCase extends TestCase {
38
39 public static final String MY_LOG_PKG =
40 "org.apache.commons.logging.tccl.custom";
41
42 public static final String MY_LOG_IMPL =
43 MY_LOG_PKG + ".MyLog";
44
45
46
47
48
49
50 public static Test suite() throws Exception {
51 Class thisClass = TcclEnabledTestCase.class;
52
53
54
55
56
57 PathableClassLoader dummy = new PathableClassLoader(null);
58 dummy.useExplicitLoader("junit.", Test.class.getClassLoader());
59 dummy.addLogicalLib("testclasses");
60 dummy.addLogicalLib("commons-logging");
61
62 String thisClassPath = thisClass.getName().replace('.', '/') + ".class";
63 URL baseUrl = dummy.findResource(thisClassPath);
64
65
66
67
68
69
70
71 PathableClassLoader emptyLoader = new PathableClassLoader(null);
72
73 PathableClassLoader parentLoader = new PathableClassLoader(null);
74 parentLoader.useExplicitLoader("junit.", Test.class.getClassLoader());
75 parentLoader.addLogicalLib("commons-logging");
76 parentLoader.addLogicalLib("testclasses");
77
78
79 parentLoader.useExplicitLoader(MY_LOG_PKG + ".", emptyLoader);
80
81 URL propsEnableUrl = new URL(baseUrl, "props_enable_tccl/");
82 parentLoader.addURL(propsEnableUrl);
83
84 PathableClassLoader tcclLoader = new PathableClassLoader(parentLoader);
85 tcclLoader.addLogicalLib("testclasses");
86
87 Class testClass = parentLoader.loadClass(thisClass.getName());
88 return new PathableTestSuite(testClass, tcclLoader);
89 }
90
91
92
93
94 public void setUp() throws Exception {
95 LogFactory.releaseAll();
96 }
97
98
99
100
101 public void tearDown() {
102 LogFactory.releaseAll();
103 }
104
105
106
107
108
109
110 public void testLoader() throws Exception {
111
112 ClassLoader thisClassLoader = this.getClass().getClassLoader();
113 ClassLoader tcclLoader = Thread.currentThread().getContextClassLoader();
114
115
116 assertNotSame("tccl not same as test classloader", thisClassLoader, tcclLoader);
117
118
119 try {
120 Class clazz = thisClassLoader.loadClass(MY_LOG_IMPL);
121 fail("Unexpectedly able to load MyLog via test class classloader");
122 assertNotNull(clazz);
123 } catch(ClassNotFoundException ex) {
124
125 }
126
127
128 try {
129 Class clazz = tcclLoader.loadClass(MY_LOG_IMPL);
130 assertNotNull(clazz);
131 } catch(ClassNotFoundException ex) {
132 fail("Unexpectedly unable to load MyLog via tccl classloader");
133 }
134 }
135
136
137
138
139
140
141 public void testTcclLoading() throws Exception {
142 LogFactory instance = LogFactory.getFactory();
143
144 assertEquals(
145 "Correct LogFactory loaded",
146 "org.apache.commons.logging.impl.LogFactoryImpl",
147 instance.getClass().getName());
148
149 Log log = instance.getInstance("test");
150 assertEquals(
151 "Correct Log loaded",
152 MY_LOG_IMPL,
153 log.getClass().getName());
154 }
155 }