KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > AfterReturningThrowingTest


1 /*******************************************************************************************
2  * Copyright (c) Jonas Bonér, Alexandre Vasseur. All rights reserved. *
3  * http://backport175.codehaus.org *
4  * --------------------------------------------------------------------------------------- *
5  * The software in this package is published under the terms of Apache License Version 2.0 *
6  * a copy of which has been included with this distribution in the license.txt file. *
7  *******************************************************************************************/

8 package test;
9
10 import org.codehaus.aspectwerkz.joinpoint.JoinPoint;
11 import org.codehaus.aspectwerkz.annotation.AfterReturning;
12 import org.codehaus.aspectwerkz.annotation.AfterThrowing;
13 import junit.framework.TestCase;
14
15 /**
16  * @author <a HREF="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
17  */

18 public class AfterReturningThrowingTest extends TestCase {
19
20     static StringBuffer JavaDoc s_log = new StringBuffer JavaDoc();
21     static void log(String JavaDoc s) {
22         s_log.append(s).append(" ");
23     }
24
25     public int credit() {
26         log("credit");
27         return 0;
28     }
29
30     public int debit(boolean fail) throws NoMoreCreditException {
31         log("debit");
32         if (fail) {
33             throw new NoMoreCreditException();
34         }
35         return 0;
36     }
37
38     public void greet() {
39        if (1==1) {
40            throw new RuntimeException JavaDoc("Just kidding!");
41        }
42     }
43
44     public void testGreet() {
45         s_log = new StringBuffer JavaDoc();
46         try {
47             greet();
48         } catch (Exception JavaDoc e) {
49             ;
50         } finally {
51             assertEquals("beforeGreeting afterGreetingException ", s_log.toString());
52         }
53     }
54
55     public void testAfterRet() {
56         s_log = new StringBuffer JavaDoc();
57         credit();
58         try {
59             debit(false);
60         } catch (NoMoreCreditException e) {
61             fail(e.toString());
62         }
63         assertEquals("credit AOP.credit debit AOP.debit ", s_log.toString());
64     }
65
66     public void testAfterThrow() {
67         s_log = new StringBuffer JavaDoc();
68         try {
69             debit(true);
70             fail("should throw");
71         } catch (NoMoreCreditException e) {
72             ;
73         }
74         assertEquals("debit AOP.debit ", s_log.toString());
75     }
76
77     private static class NoMoreCreditException extends Exception JavaDoc {}
78
79     public static class Aspect {
80
81         @AfterReturning("execution(* test.AfterReturningThrowingTest.credit()) || execution(* test.AfterReturningThrowingTest.debit(..))")
82         public void afterReturning(JoinPoint jp) {
83             log("AOP."+jp.getSignature().getName());
84         }
85
86         @AfterThrowing(pointcut = "execution(* test.AfterReturningThrowingTest.debit(..))",
87                        type = "test.AfterReturningThrowingTest$NoMoreCreditException")
88         public void afterThrowing(JoinPoint jp) {
89             log("AOP."+jp.getSignature().getName());
90         }
91     }
92
93     public static class XmlAspect {
94
95         public void beforeGreeting(JoinPoint joinPoint) {
96             log("beforeGreeting");
97         }
98
99         public void afterGreeting(JoinPoint joinPoint) {
100             log("afterGreeting");
101         }
102
103         public void afterGreeting(JoinPoint joinPoint, java.lang.Exception JavaDoc ex) {
104             log("afterGreetingException");
105         }
106     }
107
108
109
110     //--- JUnit
111

112     public static void main(String JavaDoc[] args) {
113         junit.textui.TestRunner.run(suite());
114     }
115
116     public static junit.framework.Test suite() {
117         return new junit.framework.TestSuite(AfterReturningThrowingTest.class);
118     }
119
120 }
121
Popular Tags