1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.logging;
18
19 import junit.framework.TestCase;
20
21
22
23
24
25
26 public class LoadTestCase extends TestCase{
27
28 static private String LOG_PCKG[] = {"org.apache.commons.logging",
29 "org.apache.commons.logging.impl"};
30
31
32
33
34
35
36
37
38
39
40 static class AppClassLoader extends ClassLoader{
41
42 java.util.Map classes = new java.util.HashMap();
43
44 AppClassLoader(ClassLoader parent){
45 super(parent);
46 }
47
48 private Class def(String name)throws ClassNotFoundException{
49
50 Class result = (Class)classes.get(name);
51 if(result != null){
52 return result;
53 }
54
55 try{
56
57 ClassLoader cl = this.getClass().getClassLoader();
58 String classFileName = name.replace('.','/') + ".class";
59 java.io.InputStream is = cl.getResourceAsStream(classFileName);
60 java.io.ByteArrayOutputStream out = new java.io.ByteArrayOutputStream();
61
62 while(is.available() > 0){
63 out.write(is.read());
64 }
65
66 byte data [] = out.toByteArray();
67
68 result = super.defineClass(name, data, 0, data.length );
69 classes.put(name,result);
70
71 return result;
72
73 }catch(java.io.IOException ioe){
74
75 throw new ClassNotFoundException( name + " caused by "
76 + ioe.getMessage() );
77 }
78
79
80 }
81
82
83
84 public Class loadClass(String name)throws ClassNotFoundException{
85
86
87
88 for(int i = 0; i < LOG_PCKG.length; i++ ){
89 if( name.startsWith( LOG_PCKG[i] ) &&
90 name.indexOf("Exception") == -1 ){
91 return def(name);
92 }
93 }
94 return super.loadClass(name);
95 }
96
97 }
98
99
100
101
102
103
104
105 private void setAllowFlawedContext(Class c, String state) throws Exception {
106 Class[] params = {String.class};
107 java.lang.reflect.Method m = c.getDeclaredMethod("setAllowFlawedContext", params);
108 m.invoke(null, new Object[] {state});
109 }
110
111
112
113
114
115
116
117
118 public void testInContainer()throws Exception{
119
120
121
122
123
124
125
126
127
128 Class cls = reload();
129 Thread.currentThread().setContextClassLoader(cls.getClassLoader());
130 execute(cls);
131
132
133
134
135 cls = reload();
136 Thread.currentThread().setContextClassLoader(null);
137 execute(cls);
138
139
140
141
142 cls = reload();
143 Thread.currentThread().setContextClassLoader(null);
144 try {
145 setAllowFlawedContext(cls, "false");
146 execute(cls);
147 fail("Logging config succeeded when context classloader was null!");
148 } catch(LogConfigurationException ex) {
149
150 }
151
152
153
154
155
156
157
158
159 cls = reload();
160 Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader());
161 execute(cls);
162
163
164
165
166 cls = reload();
167 Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader());
168 try {
169 setAllowFlawedContext(cls, "false");
170 execute(cls);
171 fail("Error: somehow downcast a Logger loaded via system classloader"
172 + " to the Log interface loaded via a custom classloader");
173 } catch(LogConfigurationException ex) {
174
175 }
176 }
177
178
179
180
181
182 private Class reload()throws Exception{
183
184 Class testObjCls = null;
185
186 AppClassLoader appLoader = new AppClassLoader(
187 this.getClass().getClassLoader());
188 try{
189
190 testObjCls = appLoader.loadClass(UserClass.class.getName());
191
192 }catch(ClassNotFoundException cnfe){
193 throw cnfe;
194 }catch(Throwable t){
195 t.printStackTrace();
196 fail("AppClassLoader failed ");
197 }
198
199 assertTrue( "app isolated" ,testObjCls.getClassLoader() == appLoader );
200
201
202 return testObjCls;
203
204
205 }
206
207
208 private void execute(Class cls)throws Exception{
209
210 cls.newInstance();
211
212 }
213
214
215 public static void main(String[] args){
216 String[] testCaseName = { LoadTestCase.class.getName() };
217 junit.textui.TestRunner.main(testCaseName);
218 }
219
220 public void setUp() {
221
222 origContextClassLoader = Thread.currentThread().getContextClassLoader();
223 }
224
225 public void tearDown() {
226
227 Thread.currentThread().setContextClassLoader(origContextClassLoader);
228 }
229
230 private ClassLoader origContextClassLoader;
231 }