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 by default a custom LogFactoryImpl is loaded from the
32 * tccl classloader.
33 */
34
35 public class TcclEnabledTestCase extends TestCase {
36
37
38
39
40 /***
41 * Return the tests included in this test suite.
42 */
43 public static Test suite() throws Exception {
44 Class thisClass = TcclEnabledTestCase.class;
45
46
47
48
49
50 PathableClassLoader dummy = new PathableClassLoader(null);
51 dummy.useSystemLoader("junit.");
52 dummy.addLogicalLib("testclasses");
53 dummy.addLogicalLib("commons-logging");
54
55 String thisClassPath = thisClass.getName().replace('.', '/') + ".class";
56 URL baseUrl = dummy.findResource(thisClassPath);
57
58
59
60
61
62
63
64
65 PathableClassLoader emptyLoader = new PathableClassLoader(null);
66
67 PathableClassLoader parentLoader = new PathableClassLoader(null);
68 parentLoader.useSystemLoader("junit.");
69 parentLoader.addLogicalLib("commons-logging");
70 parentLoader.addLogicalLib("testclasses");
71
72
73 parentLoader.useExplicitLoader(
74 "org.apache.commons.logging.tccl.custom.", emptyLoader);
75
76 URL propsEnableUrl = new URL(baseUrl, "props_enable_tccl/");
77 parentLoader.addURL(propsEnableUrl);
78
79 PathableClassLoader tcclLoader = new PathableClassLoader(parentLoader);
80 tcclLoader.addLogicalLib("testclasses");
81
82 Class testClass = parentLoader.loadClass(thisClass.getName());
83 return new PathableTestSuite(testClass, tcclLoader);
84 }
85
86 /***
87 * Set up instance variables required by this test case.
88 */
89 public void setUp() throws Exception {
90 LogFactory.releaseAll();
91 }
92
93 /***
94 * Tear down instance variables required by this test case.
95 */
96 public void tearDown() {
97 LogFactory.releaseAll();
98 }
99
100
101
102 /***
103 * Verify that MyLogFactoryImpl is only loadable via the tccl.
104 */
105 public void testLoader() throws Exception {
106
107 ClassLoader thisClassLoader = this.getClass().getClassLoader();
108 ClassLoader tcclLoader = Thread.currentThread().getContextClassLoader();
109
110
111 assertNotSame("tccl not same as test classloader", thisClassLoader, tcclLoader);
112
113
114 try {
115 Class clazz = thisClassLoader.loadClass(
116 "org.apache.commons.logging.tccl.custom.MyLogFactoryImpl");
117 fail("Unexpectedly able to load MyLogFactoryImpl via test class classloader");
118 } catch(ClassNotFoundException ex) {
119
120 }
121
122
123 try {
124 Class clazz = tcclLoader.loadClass(
125 "org.apache.commons.logging.tccl.custom.MyLogFactoryImpl");
126 } catch(ClassNotFoundException ex) {
127 fail("Unexpectedly unable to load MyLogFactoryImpl via tccl classloader");
128 }
129 }
130
131 /***
132 * Verify that the custom LogFactory implementation which is only accessable
133 * via the TCCL has successfully been loaded as specified in the config file.
134 * This proves that the TCCL was used to load that class.
135 */
136 public void testTcclLoading() throws Exception {
137 LogFactory instance = LogFactory.getFactory();
138
139 assertEquals(
140 "Correct LogFactory loaded",
141 "org.apache.commons.logging.tccl.custom.MyLogFactoryImpl",
142 instance.getClass().getName());
143 }
144 }