1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.logging.tccl.logfactory;
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 * Verify that a commons-logging.properties file can prevent a custom
32 * LogFactoryImpl being loaded from the tccl classloader.
33 */
34
35 public class TcclDisabledTestCase extends TestCase {
36
37 public static final String MY_LOG_FACTORY_PKG =
38 "org.apache.commons.logging.tccl.custom";
39
40 public static final String MY_LOG_FACTORY_IMPL =
41 MY_LOG_FACTORY_PKG + ".MyLogFactoryImpl";
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 = TcclDisabledTestCase.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 emptyLoader = new PathableClassLoader(null);
72
73 PathableClassLoader parentLoader = new PathableClassLoader(null);
74 parentLoader.useSystemLoader("junit.");
75 parentLoader.addLogicalLib("commons-logging");
76 parentLoader.addLogicalLib("testclasses");
77
78
79 parentLoader.useExplicitLoader(
80 MY_LOG_FACTORY_PKG + ".", emptyLoader);
81
82 URL propsEnableUrl = new URL(baseUrl, "props_disable_tccl/");
83 parentLoader.addURL(propsEnableUrl);
84
85 PathableClassLoader tcclLoader = new PathableClassLoader(parentLoader);
86 tcclLoader.addLogicalLib("testclasses");
87
88 Class testClass = parentLoader.loadClass(thisClass.getName());
89 return new PathableTestSuite(testClass, tcclLoader);
90 }
91
92 /***
93 * Set up instance variables required by this test case.
94 */
95 public void setUp() throws Exception {
96 LogFactory.releaseAll();
97 }
98
99 /***
100 * Tear down instance variables required by this test case.
101 */
102 public void tearDown() {
103 LogFactory.releaseAll();
104 }
105
106
107
108 /***
109 * Verify that MyLogFactoryImpl is only loadable via the tccl.
110 */
111 public void testLoader() throws Exception {
112
113 ClassLoader thisClassLoader = this.getClass().getClassLoader();
114 ClassLoader tcclLoader = Thread.currentThread().getContextClassLoader();
115
116
117 assertNotSame("tccl not same as test classloader", thisClassLoader, tcclLoader);
118
119
120 try {
121 Class clazz = thisClassLoader.loadClass(MY_LOG_FACTORY_IMPL);
122 fail("Unexpectedly able to load MyLogFactoryImpl via test class classloader");
123 } catch(ClassNotFoundException ex) {
124
125 }
126
127
128 try {
129 Class clazz = tcclLoader.loadClass(MY_LOG_FACTORY_IMPL);
130 } catch(ClassNotFoundException ex) {
131 fail("Unexpectedly unable to load MyLogFactoryImpl via tccl classloader");
132 }
133 }
134
135 /***
136 * Verify that the custom LogFactory implementation which is only accessable
137 * via the TCCL has NOT been loaded. Because this is only accessable via the
138 * TCCL, and we've use a commons-logging.properties that disables TCCL loading,
139 * we should see the default LogFactoryImpl rather than the custom one.
140 */
141 public void testTcclLoading() throws Exception {
142 try {
143 LogFactory instance = LogFactory.getFactory();
144 fail("Unexpectedly succeeded in loading custom factory, though TCCL disabled.");
145 } catch(org.apache.commons.logging.LogConfigurationException ex) {
146
147
148 int index = ex.getMessage().indexOf(MY_LOG_FACTORY_IMPL);
149 assertTrue("MylogFactoryImpl not found", index >= 0);
150 }
151 }
152 }