1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.jxpath.util;
18
19 import java.io.ByteArrayOutputStream;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.lang.reflect.InvocationTargetException;
23 import java.lang.reflect.Method;
24 import java.net.URL;
25
26 import org.apache.commons.jxpath.JXPathContext;
27 import org.apache.commons.jxpath.JXPathException;
28
29 import junit.framework.TestCase;
30
31
32
33
34
35
36 public class ClassLoaderUtilTest extends TestCase {
37
38
39
40
41 private static final String TEST_CASE_CLASS_NAME = "org.apache.commons.jxpath.util.ClassLoaderUtilTest";
42 private static final String EXAMPLE_CLASS_NAME = "org.apache.commons.jxpath.util.ClassLoadingExampleClass";
43
44 private ClassLoader orginalContextClassLoader;
45
46
47
48
49 public void setUp() {
50 this.orginalContextClassLoader = Thread.currentThread().getContextClassLoader();
51 }
52
53
54
55
56 public void tearDown() {
57 Thread.currentThread().setContextClassLoader(this.orginalContextClassLoader);
58 }
59
60
61
62
63
64 public void testClassLoadFailWithoutContextClassLoader() {
65 Thread.currentThread().setContextClassLoader(null);
66 ClassLoader cl = new TestClassLoader(getClass().getClassLoader());
67 executeTestMethodUnderClassLoader(cl, "callExampleMessageMethodAndAssertClassNotFoundJXPathException");
68 }
69
70
71
72
73
74
75 public void testClassLoadSuccessWithContextClassLoader() {
76 Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
77 ClassLoader cl = new TestClassLoader(getClass().getClassLoader());
78 executeTestMethodUnderClassLoader(cl, "callExampleMessageMethodAndAssertSuccess");
79 }
80
81
82
83
84
85
86 public void testCurrentClassLoaderFallback() {
87 ClassLoader cl = new TestClassLoader(getClass().getClassLoader());
88 Thread.currentThread().setContextClassLoader(cl);
89 callExampleMessageMethodAndAssertSuccess();
90 }
91
92
93
94
95
96 public void testClassLoadSuccessWithoutContextClassLoader() {
97 Thread.currentThread().setContextClassLoader(null);
98 callExampleMessageMethodAndAssertSuccess();
99 }
100
101
102
103
104
105 public static void callExampleMessageMethodAndAssertClassNotFoundJXPathException() {
106 JXPathContext context = JXPathContext.newContext(new Object());
107 try {
108 context.selectSingleNode(EXAMPLE_CLASS_NAME+".getMessage()");
109 fail("We should not be able to load "+EXAMPLE_CLASS_NAME+".");
110 } catch( Exception e ) {
111 assertTrue( e instanceof JXPathException );
112 }
113 }
114
115
116
117
118
119 public static void callExampleMessageMethodAndAssertSuccess() {
120 JXPathContext context = JXPathContext.newContext(new Object());
121 Object value;
122 try {
123 value = context.selectSingleNode(EXAMPLE_CLASS_NAME+".getMessage()");
124 assertEquals("an example class", value);
125 } catch( Exception e ) {
126 fail(e.getMessage());
127 }
128 }
129
130
131
132
133
134
135
136
137
138 private void executeTestMethodUnderClassLoader(ClassLoader cl, String methodName) {
139 Class testClass = null;
140 try {
141 testClass = cl.loadClass(TEST_CASE_CLASS_NAME);
142 } catch (ClassNotFoundException e) {
143 fail(e.getMessage());
144 }
145 Method testMethod = null;
146 try {
147 testMethod = testClass.getMethod(methodName, null);
148 } catch (SecurityException e) {
149 fail(e.getMessage());
150 } catch (NoSuchMethodException e) {
151 fail(e.getMessage());
152 }
153
154 try {
155 testMethod.invoke(null, null);
156 } catch (IllegalArgumentException e) {
157 fail(e.getMessage());
158 } catch (IllegalAccessException e) {
159 fail(e.getMessage());
160 } catch (InvocationTargetException e) {
161 if( e.getCause() instanceof RuntimeException ) {
162
163 throw (RuntimeException) e.getCause();
164 }
165 }
166 }
167
168
169
170
171
172
173
174
175
176
177
178 private static class TestClassLoader extends ClassLoader {
179 private Class testCaseClass = null;
180
181 public TestClassLoader(ClassLoader classLoader) {
182 super(classLoader);
183 }
184
185 public synchronized Class loadClass(String name, boolean resolved) throws ClassNotFoundException {
186 if( EXAMPLE_CLASS_NAME.equals(name) ) {
187 throw new ClassNotFoundException();
188 }
189 else if( TEST_CASE_CLASS_NAME.equals(name) ) {
190 if( testCaseClass == null ) {
191 URL clazzUrl = this.getParent().getResource("org/apache/commons/jxpath/util/ClassLoaderUtilTest.class");
192
193 ByteArrayOutputStream out = new ByteArrayOutputStream();
194 InputStream in = null;
195 try {
196 in = clazzUrl.openStream();
197 byte[] buffer = new byte[2048];
198 for( int read = in.read(buffer); read > -1; read = in.read(buffer) ) {
199 out.write(buffer, 0, read);
200 }
201 } catch( IOException e ) {
202 throw new ClassNotFoundException("Could not read class from resource "+clazzUrl+".", e);
203 } finally {
204 try { in.close(); } catch( Exception e ) { }
205 try { out.close(); } catch( Exception e ) { }
206 }
207
208 byte[] clazzBytes = out.toByteArray();
209 this.testCaseClass = this.defineClass(TEST_CASE_CLASS_NAME, clazzBytes, 0, clazzBytes.length);
210 }
211 return this.testCaseClass;
212 }
213 return this.getParent().loadClass(name);
214 }
215 }
216 }