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