KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > CustomProceedChangeTargetTest


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.annotation.Around;
12 import org.codehaus.aspectwerkz.joinpoint.JoinPoint;
13
14 /**
15  * @author <a HREF="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
16  */

17 public class CustomProceedChangeTargetTest extends TestCase {
18
19     static int s_instance = 0;
20     int m_me;
21
22     public CustomProceedChangeTargetTest() {
23         m_me = ++s_instance;
24     }
25
26     public void testPassOtherTarget() {
27         s_instance = 0;
28         CustomProceedChangeTargetTest one = new CustomProceedChangeTargetTest();//1
29

30         // as an around
31
int meOfOne = one.getMe(1);//advised, new instance[2] + 1 -> 3
32
assertFalse(meOfOne==one.m_me);
33         assertTrue(meOfOne==3);
34
35         String JavaDoc meOfOneAsString = one.getMeAsString(1);//advised, new instance[3] + 1 -> 4
36
assertFalse(meOfOneAsString.equals(""+(one.m_me+1)));
37         assertTrue("4".equals(meOfOneAsString));
38     }
39
40     public int getMe(int i) {
41         return m_me + i;
42     }
43
44     public String JavaDoc getMeAsString(int i) {
45         return "" + (m_me + i);
46     }
47
48     public static void main(String JavaDoc[] args) {
49         junit.textui.TestRunner.run(suite());
50     }
51
52     public static junit.framework.Test suite() {
53         return new junit.framework.TestSuite(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 JavaDoc around(CustomJp jp, CustomProceedChangeTargetTest t, int arg) throws Throwable JavaDoc {
64             int meOfOther = jp.proceed(new CustomProceedChangeTargetTest(), arg);
65             return new Integer JavaDoc(meOfOther);
66         }
67
68         public static interface CustomJp2 extends JoinPoint {
69             String JavaDoc proceed(CustomProceedChangeTargetTest callee, int arg);
70         }
71
72         @Around("execution(String test.CustomProceedChangeTargetTest.getMeAsString(int)) && args(arg) && target(t)")
73         public Object JavaDoc around(CustomJp2 jp, CustomProceedChangeTargetTest t, int arg) throws Throwable JavaDoc {
74             String JavaDoc meOfOther = jp.proceed(new CustomProceedChangeTargetTest(), arg);
75             return meOfOther;
76         }
77     }
78 }
79
Popular Tags