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   *
17   * TODO rewrite test.aopc.* with ASM or using an already builded jar with the small appp deployed
18   * several time in difft CL to test system defs and namespaces.
19   *
20   * Note: does not work behing WeavingCL. Use a real online mode <p/>
21   * java -Xrunaspectwerkz -Xdebug -Xbootclasspath/a:lib\aspectwerkz-core-1.0.jar ...
22   * <p/>
23   * The CallablePrototype class is renamed and defined as a deployed application class in a child classloader
24   * with its own META-INF/aop.xml file.
25   *
26   * @author <a href="mailto:alex@gnilux.com">Alexandre Vasseur </a>
27   */
28  public class AspectSystemTest extends TestCase {
29      public void testDoubleHierarchyMethodExecution() {
30          // VM system classpath level classes
31          Callable cvm = new CallablePrototype();
32          cvm.methodAround();
33          cvm.debug();
34          assertEquals("methodAround ", cvm.getLogString());
35  
36          // appserver like classloader, with its own aop.xml file
37          // the aop.xml file contains one aspect in the VM system classpath
38          ClassLoader myCL = new URLClassLoader(
39                  new URL[]{ClassCreator.getPathFor(Callable.class.getResource("META-INF/aop.xml"))},
40                  ClassLoader.getSystemClassLoader()
41          );
42          Callable cas = (Callable) ClassCreator.createInstance(
43                  "test.aopc.CallableAppServer",
44                  CallablePrototype.class,
45                  myCL
46          );
47          cas.methodAround();
48          cas.debug();
49          assertEquals(
50                  "system/asCL/test.aopc.BaseAspect.beforeAround "
51                  + "methodAround "
52                  + "system/asCL/test.aopc.BaseAspect.afterAround ",
53                  cas.getLogString()
54          );
55  
56          // deployed app A
57          // the aop.xml file is REusing VM system classpath aspect and is defining one of its own as well, with 2 systems
58          // we are defining the aspect on the fly in an intermediate CL without aop.xml
59          ClassLoader mySubCLAAspect = new URLClassLoader(new URL[]{}, myCL);
60          ClassCreator.createClass("test.aopc.a.Aspect", BaseAspect.class, mySubCLAAspect);
61          ClassLoader mySubCLA = new URLClassLoader(
62                  new URL[]{ClassCreator.getPathFor(Callable.class.getResource("a/META-INF/aop.xml"))}, mySubCLAAspect
63          );
64          //ClassCreator.createClass("test.aopc.a.Aspect", BaseAspect.class, mySubCLA);
65          Callable ca = (Callable) ClassCreator.createInstance("test.aopc.a.Callee", CallablePrototype.class, mySubCLA);
66          ca.methodAround();
67          ca.debug();
68          assertEquals(
69                  "system/asCL/test.aopc.BaseAspect.beforeAround "
70                  + "system/subCL/a1/subCLAspect.beforeAround "
71                  + "system/subCL/a2/subCLAspect.beforeAround "
72                  + "methodAround "
73                  + "system/subCL/a2/subCLAspect.afterAround "
74                  + "system/subCL/a1/subCLAspect.afterAround "
75                  + "system/asCL/test.aopc.BaseAspect.afterAround ", ca.getLogString()
76          );
77  
78          // deployed app B
79          // no aop.xml
80          ClassLoader mySubCLB = new URLClassLoader(new URL[]{}, myCL);
81          Callable cb = (Callable) ClassCreator.createInstance("test.aopc.b.Callee", CallablePrototype.class, mySubCLB);
82          cb.methodAround();
83          cb.debug();
84          assertEquals(
85                  "system/asCL/test.aopc.BaseAspect.beforeAround "
86                  + "methodAround "
87                  + "system/asCL/test.aopc.BaseAspect.afterAround ",
88                  cb.getLogString()
89          );
90      }
91  
92      // ------------------------------------------------
93      public static void main(String[] args) {
94          junit.textui.TestRunner.run(suite());
95      }
96  
97      public static junit.framework.Test suite() {
98          return new junit.framework.TestSuite(AspectSystemTest.class);
99      }
100 }