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