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