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 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
32
33 public class TcclDisabledTestCase extends TestCase {
34
35 public static final String MY_LOG_FACTORY_PKG =
36 "org.apache.commons.logging.tccl.custom";
37
38 public static final String MY_LOG_FACTORY_IMPL =
39 MY_LOG_FACTORY_PKG + ".MyLogFactoryImpl";
40
41
42
43
44 public static Test suite() throws Exception {
45 final Class<TcclDisabledTestCase> thisClass = TcclDisabledTestCase.class;
46
47
48
49
50
51 final PathableClassLoader dummy = new PathableClassLoader(null);
52 dummy.useExplicitLoader("junit.", Test.class.getClassLoader());
53 dummy.addLogicalLib("testclasses");
54 dummy.addLogicalLib("commons-logging");
55
56 final String thisClassPath = thisClass.getName().replace('.', '/') + ".class";
57 final URL baseUrl = dummy.findResource(thisClassPath);
58
59
60
61
62
63
64
65
66 final PathableClassLoader emptyLoader = new PathableClassLoader(null);
67
68 final PathableClassLoader parentLoader = new PathableClassLoader(null);
69 parentLoader.useExplicitLoader("junit.", Test.class.getClassLoader());
70 parentLoader.addLogicalLib("commons-logging");
71 parentLoader.addLogicalLib("testclasses");
72
73
74 parentLoader.useExplicitLoader(
75 MY_LOG_FACTORY_PKG + ".", emptyLoader);
76
77 final URL propsEnableUrl = new URL(baseUrl, "props_disable_tccl/");
78 parentLoader.addURL(propsEnableUrl);
79
80 final PathableClassLoader tcclLoader = new PathableClassLoader(parentLoader);
81 tcclLoader.addLogicalLib("testclasses");
82
83 final Class<?> testClass = parentLoader.loadClass(thisClass.getName());
84 return new PathableTestSuite(testClass, tcclLoader);
85 }
86
87
88
89
90 @Override
91 public void setUp() throws Exception {
92 LogFactory.releaseAll();
93 }
94
95
96
97
98 @Override
99 public void tearDown() {
100 LogFactory.releaseAll();
101 }
102
103
104
105
106 public void testLoader() throws Exception {
107
108 final ClassLoader thisClassLoader = this.getClass().getClassLoader();
109 final ClassLoader tcclLoader = Thread.currentThread().getContextClassLoader();
110
111
112 assertNotSame("TCCL not same as test class loader", thisClassLoader, tcclLoader);
113
114
115 try {
116 final Class<?> clazz = thisClassLoader.loadClass(MY_LOG_FACTORY_IMPL);
117 fail("Unexpectedly able to load MyLogFactoryImpl via test class class loader");
118 assertNotNull(clazz);
119 } catch (final ClassNotFoundException ex) {
120
121 }
122
123
124 try {
125 final Class<?> clazz = tcclLoader.loadClass(MY_LOG_FACTORY_IMPL);
126 assertNotNull(clazz);
127 } catch (final ClassNotFoundException ex) {
128 fail("Unexpectedly unable to load MyLogFactoryImpl via TCCL class loader");
129 }
130 }
131
132
133
134
135
136
137
138 public void testTcclLoading() throws Exception {
139 try {
140 final LogFactory instance = LogFactory.getFactory();
141 fail("Unexpectedly succeeded in loading custom factory, though TCCL disabled.");
142 assertNotNull(instance);
143 } catch (final org.apache.commons.logging.LogConfigurationException ex) {
144
145
146 assertTrue("MylogFactoryImpl not found", ex.getMessage().contains(MY_LOG_FACTORY_IMPL));
147 }
148 }
149 }