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       * // uncomment this to test outside of online mode // hack clinit to fix taregtPath = "foo"; //
46       * put Target.class in C:\temp and remove it from the IDE compiled classes public void
47       * testWeavingClassLoader() { try { targetPath = (new
48       * java.io.File("C://temp//")).toURL().toString(); WeavingClassLoader wcl = new
49       * WeavingClassLoader( new URL[]{new URL(targetPath)}, ClassLoader.getSystemClassLoader());
50       * Class target = wcl.loadClass("test.xmldef.clapp.Target");
51       * assertEquals(target.getClassLoader().hashCode(), wcl.hashCode()); Method m =
52       * target.getAdvice("callme", new Class[]{}); String res = (String)
53       * m.invoke(target.newInstance(), new Object[]{}); assertEquals("before call after", res); }
54       * catch (Throwable t) { t.printStackTrace(); fail(t.getMessage()); } }
55       */
56      public static void main(String[] a) {
57          CustomClassLoaderTest me = new CustomClassLoaderTest();
58          me.testCustomClassLoaderWeaving();
59  
60          // uncomment this to run test outside of online mode
61          //me.testWeavingClassLoader();
62  
63          /*
64           * // uncomment this to run test outside of junitperf Thread t1 = new Thread(new Runnable() {
65           * public void run() { CustomClassLoaderTest me = new CustomClassLoaderTest();
66           * me.testWeavingClassLoader(); } }); Thread t2 = new Thread(new Runnable() { public void
67           * run() { CustomClassLoaderTest me = new CustomClassLoaderTest();
68           * me.testWeavingClassLoader(); } }); t1.start(); t2.start();
69           */
70      }
71  }