1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.logging.tccl.logfactory;
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.LogFactory;
27 import org.apache.commons.logging.PathableClassLoader;
28 import org.apache.commons.logging.PathableTestSuite;
29
30
31
32
33
34
35
36 public class TcclDisabledTestCase extends TestCase {
37
38 public static final String MY_LOG_FACTORY_PKG =
39 "org.apache.commons.logging.tccl.custom";
40
41 public static final String MY_LOG_FACTORY_IMPL =
42 MY_LOG_FACTORY_PKG + ".MyLogFactoryImpl";
43
44
45
46
47
48
49
50 public static Test suite() throws Exception {
51 Class thisClass = TcclDisabledTestCase.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
72 PathableClassLoader emptyLoader = new PathableClassLoader(null);
73
74 PathableClassLoader parentLoader = new PathableClassLoader(null);
75 parentLoader.useExplicitLoader("junit.", Test.class.getClassLoader());
76 parentLoader.addLogicalLib("commons-logging");
77 parentLoader.addLogicalLib("testclasses");
78
79
80 parentLoader.useExplicitLoader(
81 MY_LOG_FACTORY_PKG + ".", emptyLoader);
82
83 URL propsEnableUrl = new URL(baseUrl, "props_disable_tccl/");
84 parentLoader.addURL(propsEnableUrl);
85
86 PathableClassLoader tcclLoader = new PathableClassLoader(parentLoader);
87 tcclLoader.addLogicalLib("testclasses");
88
89 Class testClass = parentLoader.loadClass(thisClass.getName());
90 return new PathableTestSuite(testClass, tcclLoader);
91 }
92
93
94
95
96 public void setUp() throws Exception {
97 LogFactory.releaseAll();
98 }
99
100
101
102
103 public void tearDown() {
104 LogFactory.releaseAll();
105 }
106
107
108
109
110
111
112 public void testLoader() throws Exception {
113
114 ClassLoader thisClassLoader = this.getClass().getClassLoader();
115 ClassLoader tcclLoader = Thread.currentThread().getContextClassLoader();
116
117
118 assertNotSame("tccl not same as test classloader", thisClassLoader, tcclLoader);
119
120
121 try {
122 Class clazz = thisClassLoader.loadClass(MY_LOG_FACTORY_IMPL);
123 fail("Unexpectedly able to load MyLogFactoryImpl via test class classloader");
124 assertNotNull(clazz);
125 } catch(ClassNotFoundException ex) {
126
127 }
128
129
130 try {
131 Class clazz = tcclLoader.loadClass(MY_LOG_FACTORY_IMPL);
132 assertNotNull(clazz);
133 } catch(ClassNotFoundException ex) {
134 fail("Unexpectedly unable to load MyLogFactoryImpl via tccl classloader");
135 }
136 }
137
138
139
140
141
142
143
144 public void testTcclLoading() throws Exception {
145 try {
146 LogFactory instance = LogFactory.getFactory();
147 fail("Unexpectedly succeeded in loading custom factory, though TCCL disabled.");
148 assertNotNull(instance);
149 } catch(org.apache.commons.logging.LogConfigurationException ex) {
150
151
152 int index = ex.getMessage().indexOf(MY_LOG_FACTORY_IMPL);
153 assertTrue("MylogFactoryImpl not found", index >= 0);
154 }
155 }
156 }