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