001: /**************************************************************************************
002: * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved. *
003: * http://aspectwerkz.codehaus.org *
004: * ---------------------------------------------------------------------------------- *
005: * The software in this package is published under the terms of the LGPL license *
006: * a copy of which has been included with this distribution in the license.txt file. *
007: **************************************************************************************/package test;
008:
009: import junit.framework.TestCase;
010: import org.codehaus.aspectwerkz.reflect.ClassInfo;
011: import org.codehaus.aspectwerkz.reflect.impl.java.JavaClassInfo;
012:
013: /**
014: * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
015: * @TODO: this test is deprecated - need a better way of handling dynamic stuff
016: */
017: public class DynamicDeploymentTest extends TestCase implements Loggable {
018: private static final String ASPECT_NAME = "test.aspect.DynamicDeploymentTestAspect";
019:
020: private static final String NEW_ASPECT_NAME = "test.aspect.DynamicallyCreatedAspect";
021:
022: private String m_logString = "";
023:
024: private ClassInfo m_classMetaData = JavaClassInfo
025: .getClassInfo(DynamicDeploymentTest.class);
026:
027: public DynamicDeploymentTest(String name) {
028: super (name);
029: }
030:
031: // FIXME XXX implement dynamic deployment and comment out tests
032:
033: // public void testReorderAdvicesAtRuntime1() {
034: // m_logString = "";
035: // reorderAdvicesTestMethod();
036: // assertEquals("before1 before2 invocation after2 after1 ", m_logString);
037: //
038: // // get the pointcut by name (can also be retrieved by method meta-data)
039: // Pointcut pointcut = SystemLoader.getCflowStack(this.getClass()).getAspectManager("tests")
040: // .getPointcutManager(ASPECT_NAME).getPointcut("pc1 || pc2 || pc3");
041: //
042: // // get the advices
043: // List advices = pointcut.getAroundAdviceIndexTuples();
044: // NameIndexTuple tuple1 = (NameIndexTuple) advices.get(0);
045: // NameIndexTuple tuple2 = (NameIndexTuple) advices.get(1);
046: //
047: // // reorder the advices
048: // advices.set(0, tuple2);
049: // advices.set(1, tuple1);
050: //
051: // // set the reordered advices
052: // pointcut.setAroundAdviceIndexTuples(advices);
053: // }
054: //
055: // public void testAddAdviceAtRuntime() {
056: // m_logString = "";
057: // addAdviceTestMethod();
058: // assertEquals("before1 invocation after1 ", m_logString);
059: // MethodInfo methodMetaData = null;
060: // try {
061: // methodMetaData = JavaMethodInfo.getMethodInfo(getClass().getMethodInfo(
062: // "addAdviceTestMethod",
063: // new Class[] {}));
064: // } catch (NoSuchMethodException e) {
065: // e.printStackTrace(); //To change body of catch statement use File | Settings | File
066: // // Templates.
067: // }
068: // Pointcut methodPointcut = (Pointcut) SystemLoader.getCflowStack(this.getClass())
069: // .getAspectManager("tests").getPointcutManager(ASPECT_NAME).getPointcuts(
070: // new ExpressionContext(PointcutType.EXECUTION, methodMetaData, null)).get(0);
071: // methodPointcut.addAroundAdvice("test.aspect.DynamicDeploymentTestAspect.advice2");
072: // m_logString = "";
073: // addAdviceTestMethod();
074: // assertEquals("before1 before2 invocation after2 after1 ", m_logString);
075: //
076: // // remove it for other tests
077: // methodPointcut.removeAroundAdvice("test.aspect.DynamicDeploymentTestAspect.advice2");
078: // }
079: //
080: // public void testRemoveAdviceAtRuntime() {
081: // m_logString = "";
082: // removeAdviceTestMethod();
083: // assertEquals("before1 before2 invocation after2 after1 ", m_logString);
084: // MethodInfo methodMetaData = null;
085: // try {
086: // methodMetaData = JavaMethodInfo.getMethodInfo(getClass().getMethodInfo(
087: // "removeAdviceTestMethod",
088: // new Class[] {}));
089: // } catch (NoSuchMethodException e) {
090: // e.printStackTrace(); //To change body of catch statement use File | Settings | File
091: // // Templates.
092: // }
093: // Pointcut methodPointcut = (Pointcut) SystemLoader.getCflowStack(this).getAspectManager("tests")
094: // .getPointcutManager(ASPECT_NAME).getPointcuts(
095: // new ExpressionContext(PointcutType.EXECUTION, methodMetaData, null)).get(0);
096: // List advices = methodPointcut.getAroundAdviceIndexTuples();
097: // NameIndexTuple adviceTuple = (NameIndexTuple) advices.remove(0);
098: // methodPointcut.setAroundAdviceIndexTuples(advices);
099: // m_logString = "";
100: // removeAdviceTestMethod();
101: // assertEquals("before2 invocation after2 ", m_logString);
102: //
103: // // restore it for other tests
104: // advices.add(0, adviceTuple);
105: // methodPointcut.setAroundAdviceIndexTuples(advices);
106: // }
107: //
108: // public void testCreateAspectAtRuntime() {
109: // try {
110: // // check that we have a pointcut at the createAspectTestMethod method
111: // m_logString = "";
112: // createAspectTestMethod();
113: // assertEquals("before2 invocation after2 ", m_logString);
114: //
115: // // create a new advice
116: // SystemLoader.getCflowStack(this).getAspectManager("tests").createAspect(
117: // NEW_ASPECT_NAME,
118: // NEW_ASPECT_NAME,
119: // DeploymentModel.PER_INSTANCE,
120: // null);
121: //
122: // // test the some stuff for the aspect
123: // assertNotNull(SystemLoader.getCflowStack(this).getAspectManager("tests")
124: // .getPointcutManager(NEW_ASPECT_NAME));
125: // assertEquals(DeploymentModel.PER_INSTANCE, SystemLoader.getCflowStack(this)
126: // .getAspectManager("tests").getPointcutManager(NEW_ASPECT_NAME)
127: // .getDeploymentModel());
128: // assertEquals(NEW_ASPECT_NAME, SystemLoader.getCflowStack(this).getAspectManager("tests")
129: // .getPointcutManager(NEW_ASPECT_NAME).getName());
130: // MethodInfo methodMetaData = null;
131: // try {
132: // methodMetaData = JavaMethodInfo.getMethodInfo(getClass().getMethodInfo(
133: // "createAspectTestMethod",
134: // new Class[] {}));
135: // } catch (NoSuchMethodException e) {
136: // e.printStackTrace(); //To change body of catch statement use File | Settings | File
137: // // Templates.
138: // }
139: //
140: // // get an existing pointcut
141: // Pointcut methodPointcut = (Pointcut) SystemLoader.getCflowStack(this).getAspectManager(
142: // "tests").getPointcutManager(ASPECT_NAME).getPointcuts(
143: // new ExpressionContext(PointcutType.EXECUTION, methodMetaData, null)).get(0);
144: //
145: // // add the new advice to the pointcut
146: // methodPointcut.addAroundAdvice("test.aspects.DynamicallyCreatedAspect.advice1");
147: //
148: // // check that it is executed
149: // m_logString = "";
150: // createAspectTestMethod();
151: // assertEquals("before2 beforeNew invocation afterNew after2 ", m_logString);
152: //
153: // //remove it for other tests
154: // methodPointcut.removeAroundAdvice("test.aspects.DynamicallyCreatedAspect.advice1");
155: // } catch (Exception e) {
156: // e.printStackTrace();
157: // fail(e.getMessage());
158: // }
159: // }
160:
161: public static void main(String[] args) {
162: junit.textui.TestRunner.run(suite());
163: }
164:
165: public static junit.framework.Test suite() {
166: return new junit.framework.TestSuite(
167: 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: }
|