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;
9   
10  import junit.framework.TestCase;
11  import org.codehaus.aspectwerkz.cflow.CflowCompiler;
12  import org.codehaus.aspectwerkz.cflow.AbstractCflowSystemAspect;
13  import org.codehaus.aspectwerkz.transform.inlining.AsmHelper;
14  import org.codehaus.aspectwerkz.reflect.ClassInfo;
15  import org.codehaus.aspectwerkz.reflect.MethodInfo;
16  import org.codehaus.aspectwerkz.reflect.impl.asm.AsmClassInfo;
17  import org.codehaus.aspectwerkz.exception.DefinitionException;
18  
19  import java.lang.reflect.Method;
20  
21  /***
22   * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
23   */
24  public class CflowCompilerTest extends TestCase {
25  
26      public void testCompiler() {
27          try {
28              Class cflowAspect = CflowCompiler.compileCflowAspectAndAttachToClassLoader(
29                      CflowCompilerTest.class.getClassLoader(),
30                      4
31              );
32  
33              assertEquals(cflowAspect.getName().replace('/', '.'), "org.codehaus.aspectwerkz.cflow.Cflow_4");
34              assertTrue(cflowAspect.getSuperclass().equals(AbstractCflowSystemAspect.class));
35              Method enter = cflowAspect.getDeclaredMethod("isInCflow", new Class[0]);
36          } catch (Throwable t) {
37              fail(t.toString());
38          }
39      }
40  
41      public void testCflow() throws Throwable {
42          Class cflowAspect = CflowCompiler.compileCflowAspectAndAttachToClassLoader(
43                  CflowCompilerTest.class.getClassLoader(),
44                  4
45          );
46  
47          // check NPE
48          Method staticMethod = cflowAspect.getDeclaredMethod("isInCflow", new Class[0]);
49          Boolean b = (Boolean) staticMethod.invoke(null, new Object[0]);
50          assertFalse(b.booleanValue());
51  
52          final AbstractCflowSystemAspect cflow = (AbstractCflowSystemAspect)
53                  cflowAspect.getDeclaredMethod("aspectOf", new Class[0]).invoke(null, new Object[0]);
54  
55          assertFalse(cflow.inCflow());
56          Thread t = new Thread() {
57              public void run() {
58                  System.out.println(Thread.currentThread());
59                  cflow.enter();
60                  assertTrue(cflow.inCflow());
61                  cflow.enter();
62                  assertTrue(cflow.inCflow());
63                  cflow.exit();
64                  // leave the cflow in "inCflow" state is in this thread
65              }
66          };
67          t.start();
68          System.out.println(Thread.currentThread());
69  
70          assertFalse(cflow.inCflow());
71      }
72  
73  
74  
75      public static void main(String[] args) {
76          junit.textui.TestRunner.run(suite());
77      }
78  
79      public static junit.framework.Test suite() {
80          return new junit.framework.TestSuite(CflowCompilerTest.class);
81      }
82  
83  }