1 /****************************************************************************************************
2 * Copyright (c) Jonas Bonér, Alexandre Vasseur. All rights reserved. *
3 * http://aspectwerkz.codehaus.org *
4 * ---------------------------------------------------------------------------------- * The software
5 * in this package is published under the terms of the LGPL license * a copy of which has been
6 * included with this distribution in the license.txt file. *
7 **************************************************************************************************/
8 package test.clapp;
9
10 import junit.framework.TestCase;
11 import org.codehaus.aspectwerkz.compiler.VerifierClassLoader;
12
13 import java.lang.reflect.Method;
14 import java.net.URL;
15
16 public class CustomClassLoaderTest extends TestCase {
17 private static String targetPath = CustomClassLoaderTest.class.getClassLoader().getResource(
18 "test/clapp/Target.class").toString();
19
20 static {
21 targetPath = targetPath.substring(0, targetPath.indexOf("test/clapp/Target.class"));
22 }
23
24 /***
25 * Note: this test cannot be runned thru the WeavingClassLoader for debugging since it uses
26 * custom class loader hierarchy. See testWeavingClassLoader() commented method
27 */
28 public void testCustomClassLoaderWeaving() {
29 try {
30 VerifierClassLoader cl = new VerifierClassLoader(new URL[] {
31 new URL(targetPath)
32 }, ClassLoader.getSystemClassLoader());
33 Class target = cl.loadClass("test.clapp.Target");
34 assertEquals(target.getClassLoader().hashCode(), cl.hashCode());
35 Method m = target.getMethod("callme", new Class[] {});
36 String res = (String) m.invoke(target.newInstance(), new Object[] {});
37 assertEquals("before call after", res);
38 } catch (Throwable t) {
39 t.printStackTrace();
40 fail(t.getMessage());
41 }
42 }
43
44
45
46
47
48
49
50
51
52
53
54
55
56 public static void main(String[] a) {
57 CustomClassLoaderTest me = new CustomClassLoaderTest();
58 me.testCustomClassLoaderWeaving();
59
60
61
62
63
64
65
66
67
68
69
70 }
71 }