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.DeploymentModel;
12  import org.codehaus.aspectwerkz.NameIndexTuple;
13  import org.codehaus.aspectwerkz.SystemLoader;
14  import org.codehaus.aspectwerkz.aspect.management.Pointcut;
15  import org.codehaus.aspectwerkz.expression.ExpressionContext;
16  import org.codehaus.aspectwerkz.expression.PointcutType;
17  import org.codehaus.aspectwerkz.reflect.ClassInfo;
18  import org.codehaus.aspectwerkz.reflect.MethodInfo;
19  import org.codehaus.aspectwerkz.reflect.impl.java.JavaClassInfo;
20  import org.codehaus.aspectwerkz.reflect.impl.java.JavaMethodInfo;
21  
22  import java.util.List;
23  
24  /***
25   * @author <a href="mailto:jboner@codehaus.org">Jonas Bonér </a>
26   * @TODO: this test is deprecated - need a better way of handling dynamic stuff
27   */
28  public class DynamicDeploymentTest extends TestCase implements Loggable {
29      private static final String ASPECT_NAME = "test.aspect.DynamicDeploymentTestAspect";
30  
31      private static final String NEW_ASPECT_NAME = "test.aspect.DynamicallyCreatedAspect";
32  
33      private String m_logString = "";
34  
35      private ClassInfo m_classMetaData = JavaClassInfo.getClassInfo(DynamicDeploymentTest.class);
36  
37      public DynamicDeploymentTest(String name) {
38          super(name);
39      }
40  
41      public void testReorderAdvicesAtRuntime1() {
42          m_logString = "";
43          reorderAdvicesTestMethod();
44          assertEquals("before1 before2 invocation after2 after1 ", m_logString);
45  
46          // get the pointcut by name (can also be retrieved by method meta-data)
47          Pointcut pointcut = SystemLoader.getSystem(this.getClass()).getAspectManager("tests")
48                  .getPointcutManager(ASPECT_NAME).getPointcut("pc1 || pc2 || pc3");
49  
50          // get the advices
51          List advices = pointcut.getAroundAdviceIndexTuples();
52          NameIndexTuple tuple1 = (NameIndexTuple) advices.get(0);
53          NameIndexTuple tuple2 = (NameIndexTuple) advices.get(1);
54  
55          // reorder the advices
56          advices.set(0, tuple2);
57          advices.set(1, tuple1);
58  
59          // set the reordered advices
60          pointcut.setAroundAdviceIndexTuples(advices);
61      }
62  
63      public void testAddAdviceAtRuntime() {
64          m_logString = "";
65          addAdviceTestMethod();
66          assertEquals("before1 invocation after1 ", m_logString);
67          MethodInfo methodMetaData = null;
68          try {
69              methodMetaData = JavaMethodInfo.getMethodInfo(getClass().getMethod(
70                  "addAdviceTestMethod",
71                  new Class[] {}));
72          } catch (NoSuchMethodException e) {
73              e.printStackTrace(); //To change body of catch statement use File | Settings | File
74                                   // Templates.
75          }
76          Pointcut methodPointcut = (Pointcut) SystemLoader.getSystem(this.getClass())
77                  .getAspectManager("tests").getPointcutManager(ASPECT_NAME).getPointcuts(
78                      new ExpressionContext(PointcutType.EXECUTION, methodMetaData, null)).get(0);
79          methodPointcut.addAroundAdvice("test.aspect.DynamicDeploymentTestAspect.advice2");
80          m_logString = "";
81          addAdviceTestMethod();
82          assertEquals("before1 before2 invocation after2 after1 ", m_logString);
83  
84          // remove it for other tests
85          methodPointcut.removeAroundAdvice("test.aspect.DynamicDeploymentTestAspect.advice2");
86      }
87  
88      public void testRemoveAdviceAtRuntime() {
89          m_logString = "";
90          removeAdviceTestMethod();
91          assertEquals("before1 before2 invocation after2 after1 ", m_logString);
92          MethodInfo methodMetaData = null;
93          try {
94              methodMetaData = JavaMethodInfo.getMethodInfo(getClass().getMethod(
95                  "removeAdviceTestMethod",
96                  new Class[] {}));
97          } catch (NoSuchMethodException e) {
98              e.printStackTrace(); //To change body of catch statement use File | Settings | File
99                                   // Templates.
100         }
101         Pointcut methodPointcut = (Pointcut) SystemLoader.getSystem(this).getAspectManager("tests")
102                 .getPointcutManager(ASPECT_NAME).getPointcuts(
103                     new ExpressionContext(PointcutType.EXECUTION, methodMetaData, null)).get(0);
104         List advices = methodPointcut.getAroundAdviceIndexTuples();
105         NameIndexTuple adviceTuple = (NameIndexTuple) advices.remove(0);
106         methodPointcut.setAroundAdviceIndexTuples(advices);
107         m_logString = "";
108         removeAdviceTestMethod();
109         assertEquals("before2 invocation after2 ", m_logString);
110 
111         // restore it for other tests
112         advices.add(0, adviceTuple);
113         methodPointcut.setAroundAdviceIndexTuples(advices);
114     }
115 
116     public void testCreateAspectAtRuntime() {
117         try {
118             // check that we have a pointcut at the createAspectTestMethod method
119             m_logString = "";
120             createAspectTestMethod();
121             assertEquals("before2 invocation after2 ", m_logString);
122 
123             // create a new advice
124             SystemLoader.getSystem(this).getAspectManager("tests").createAspect(
125                 NEW_ASPECT_NAME,
126                 NEW_ASPECT_NAME,
127                 DeploymentModel.PER_INSTANCE,
128                 null);
129 
130             // test the some stuff for the aspect
131             assertNotNull(SystemLoader.getSystem(this).getAspectManager("tests")
132                     .getPointcutManager(NEW_ASPECT_NAME));
133             assertEquals(DeploymentModel.PER_INSTANCE, SystemLoader.getSystem(this)
134                     .getAspectManager("tests").getPointcutManager(NEW_ASPECT_NAME)
135                     .getDeploymentModel());
136             assertEquals(NEW_ASPECT_NAME, SystemLoader.getSystem(this).getAspectManager("tests")
137                     .getPointcutManager(NEW_ASPECT_NAME).getName());
138             MethodInfo methodMetaData = null;
139             try {
140                 methodMetaData = JavaMethodInfo.getMethodInfo(getClass().getMethod(
141                     "createAspectTestMethod",
142                     new Class[] {}));
143             } catch (NoSuchMethodException e) {
144                 e.printStackTrace(); //To change body of catch statement use File | Settings | File
145                                      // Templates.
146             }
147 
148             // get an existing pointcut
149             Pointcut methodPointcut = (Pointcut) SystemLoader.getSystem(this).getAspectManager(
150                 "tests").getPointcutManager(ASPECT_NAME).getPointcuts(
151                 new ExpressionContext(PointcutType.EXECUTION, methodMetaData, null)).get(0);
152 
153             // add the new advice to the pointcut
154             methodPointcut.addAroundAdvice("test.aspects.DynamicallyCreatedAspect.advice1");
155 
156             // check that it is executed
157             m_logString = "";
158             createAspectTestMethod();
159             assertEquals("before2 beforeNew invocation afterNew after2 ", m_logString);
160 
161             //remove it for other tests
162             methodPointcut.removeAroundAdvice("test.aspects.DynamicallyCreatedAspect.advice1");
163         } catch (Exception e) {
164             e.printStackTrace();
165             fail(e.getMessage());
166         }
167     }
168 
169     public static void main(String[] args) {
170         junit.textui.TestRunner.run(suite());
171     }
172 
173     public static junit.framework.Test suite() {
174         return new junit.framework.TestSuite(DynamicDeploymentTest.class);
175     }
176 
177     public void log(final String wasHere) {
178         m_logString += wasHere;
179     }
180 
181     public void reorderAdvicesTestMethod() {
182         log("invocation ");
183     }
184 
185     public void removeAdviceTestMethod() {
186         log("invocation ");
187     }
188 
189     public void addAdviceTestMethod() {
190         log("invocation ");
191     }
192 
193     public void createAspectTestMethod() {
194         log("invocation ");
195     }
196 }