01: /**************************************************************************************
02: * Copyright (c) Jonas Bonér, Alexandre Vasseur. All rights reserved. *
03: * http://aspectwerkz.codehaus.org *
04: * ---------------------------------------------------------------------------------- *
05: * The software in this package is published under the terms of the LGPL license *
06: * a copy of which has been included with this distribution in the license.txt file. *
07: **************************************************************************************/package test;
08:
09: import junit.framework.TestCase;
10: import org.codehaus.aspectwerkz.annotation.Around;
11: import org.codehaus.aspectwerkz.joinpoint.JoinPoint;
12:
13: /**
14: * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
15: */
16: public class CustomProceedChangeTargetTest extends TestCase {
17:
18: static int s_instance = 0;
19: int m_me;
20:
21: public CustomProceedChangeTargetTest() {
22: m_me = ++s_instance;
23: }
24:
25: public void testPassOtherTarget() {
26: s_instance = 0;
27: CustomProceedChangeTargetTest one = new CustomProceedChangeTargetTest();//1
28:
29: // as an around
30: int meOfOne = one.getMe(1);//advised, new instance[2] + 1 -> 3
31: assertFalse(meOfOne == one.m_me);
32: assertTrue(meOfOne == 3);
33:
34: String meOfOneAsString = one.getMeAsString(1);//advised, new instance[3] + 1 -> 4
35: assertFalse(meOfOneAsString.equals("" + (one.m_me + 1)));
36: assertTrue("4".equals(meOfOneAsString));
37: }
38:
39: public int getMe(int i) {
40: return m_me + i;
41: }
42:
43: public String getMeAsString(int i) {
44: return "" + (m_me + i);
45: }
46:
47: public static void main(String[] args) {
48: junit.textui.TestRunner.run(suite());
49: }
50:
51: public static junit.framework.Test suite() {
52: return new junit.framework.TestSuite(
53: CustomProceedChangeTargetTest.class);
54: }
55:
56: public static class Aspect {
57:
58: public static interface CustomJp extends JoinPoint {
59: int proceed(CustomProceedChangeTargetTest callee, int arg);
60: }
61:
62: @Around("execution(int test.CustomProceedChangeTargetTest.getMe(int)) && args(arg) && target(t)")
63: public Object around(CustomJp jp,
64: CustomProceedChangeTargetTest t, int arg)
65: throws Throwable {
66: int meOfOther = jp.proceed(
67: new CustomProceedChangeTargetTest(), arg);
68: return new Integer(meOfOther);
69: }
70:
71: public static interface CustomJp2 extends JoinPoint {
72: String proceed(CustomProceedChangeTargetTest callee, int arg);
73: }
74:
75: @Around("execution(String test.CustomProceedChangeTargetTest.getMeAsString(int)) && args(arg) && target(t)")
76: public Object around(CustomJp2 jp,
77: CustomProceedChangeTargetTest t, int arg)
78: throws Throwable {
79: String meOfOther = jp.proceed(
80: new CustomProceedChangeTargetTest(), arg);
81: return meOfOther;
82: }
83: }
84: }
|