1   /***************************************************************************************
2    * Copyright (c) Jonas Bon?r, Alexandre Vasseur. All rights reserved.                 *
3    * http://aspectwerkz.codehaus.org                                                    *
4    * ---------------------------------------------------------------------------------- *
5    * The software in this package is published under the terms of the LGPL license      *
6    * a copy of which has been included with this distribution in the license.txt file.  *
7    **************************************************************************************/
8   package test.aopc;
9   
10  import junit.framework.TestCase;
11  
12  import java.net.URL;
13  import java.net.URLClassLoader;
14  
15  /***
16   * Note: does not work behing WeavingCL. Use a real online mode <p/>
17   * java -Xrunaspectwerkz -Xdebug -Xbootclasspath/a:lib\aspectwerkz-core-1.0.jar;lib\javassist-3.0RC1.jar ...
18   * <p/>
19   * The CallablePrototype class is renamed and defined as a deployed application class in a child classloader
20   * with its own META-INF/aop.xml file.
21   *
22   * @author <a href="mailto:alex@gnilux.com">Alexandre Vasseur </a>
23   */
24  public class AspectSystemTest extends TestCase {
25      public void testDoubleHierarchyMethodExecution() {
26          // VM system classpath level classes
27          Callable cvm = new CallablePrototype();
28          cvm.methodAround();
29          cvm.debug();
30          assertEquals("methodAround ", cvm.getLogString());
31  
32          // appserver like classloader, with its own aop.xml file
33          // the aop.xml file contains one aspect in the VM system classpath
34          ClassLoader myCL = new URLClassLoader(
35                  new URL[]{ClassCreator.getPathFor(Callable.class.getResource("META-INF/aop.xml"))},
36                  ClassLoader.getSystemClassLoader());
37          Callable cas = (Callable) ClassCreator.createInstance("test.aopc.CallableAppServer",
38                  CallablePrototype.class,
39                  myCL);
40          cas.methodAround();
41          cas.debug();
42          assertEquals("system/asCL/test.aopc.BaseAspect.beforeAround "
43                  + "methodAround "
44                  + "system/asCL/test.aopc.BaseAspect.afterAround ",
45                  cas.getLogString());
46  
47          // deployed app A
48          // the aop.xml file is REusing VM system classpath aspect and is defining one of its own as well, with 2 systems
49          // we are defining the aspect on the fly in an intermediate CL without aop.xml
50          ClassLoader mySubCLAAspect = new URLClassLoader(new URL[]{}, myCL);
51          ClassCreator.createClass("test.aopc.a.Aspect", BaseAspect.class, mySubCLAAspect);
52          ClassLoader mySubCLA = new URLClassLoader(
53                  new URL[]{ClassCreator.getPathFor(Callable.class.getResource("a/META-INF/aop.xml"))}, mySubCLAAspect);
54          //ClassCreator.createClass("test.aopc.a.Aspect", BaseAspect.class, mySubCLA);
55          Callable ca = (Callable) ClassCreator.createInstance("test.aopc.a.Callee", CallablePrototype.class, mySubCLA);
56          ca.methodAround();
57          ca.debug();
58          assertEquals("system/asCL/test.aopc.BaseAspect.beforeAround "
59                  + "system/subCL/a1/subCLAspect.beforeAround "
60                  + "system/subCL/a2/subCLAspect.beforeAround "
61                  + "methodAround "
62                  + "system/subCL/a2/subCLAspect.afterAround "
63                  + "system/subCL/a1/subCLAspect.afterAround "
64                  + "system/asCL/test.aopc.BaseAspect.afterAround ", ca.getLogString());
65  
66          // deployed app B
67          // no aop.xml
68          ClassLoader mySubCLB = new URLClassLoader(new URL[]{}, myCL);
69          Callable cb = (Callable) ClassCreator.createInstance("test.aopc.b.Callee", CallablePrototype.class, mySubCLB);
70          cb.methodAround();
71          cb.debug();
72          assertEquals("system/asCL/test.aopc.BaseAspect.beforeAround "
73                  + "methodAround "
74                  + "system/asCL/test.aopc.BaseAspect.afterAround ",
75                  cb.getLogString());
76      }
77  
78      // ------------------------------------------------
79      public static void main(String[] args) {
80          junit.textui.TestRunner.run(suite());
81      }
82  
83      public static junit.framework.Test suite() {
84          return new junit.framework.TestSuite(AspectSystemTest.class);
85      }
86  }