001: /*******************************************************************************************
002: * Copyright (c) Jonas Bonér, Alexandre Vasseur. All rights reserved. *
003: * http://backport175.codehaus.org *
004: * --------------------------------------------------------------------------------------- *
005: * The software in this package is published under the terms of Apache License Version 2.0 *
006: * a copy of which has been included with this distribution in the license.txt file. *
007: *******************************************************************************************/package test;
008:
009: import org.codehaus.aspectwerkz.joinpoint.JoinPoint;
010: import org.codehaus.aspectwerkz.annotation.AfterReturning;
011: import org.codehaus.aspectwerkz.annotation.AfterThrowing;
012: import junit.framework.TestCase;
013:
014: /**
015: * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
016: */
017: public class AfterReturningThrowingTest extends TestCase {
018:
019: static StringBuffer s_log = new StringBuffer();
020:
021: static void log(String s) {
022: s_log.append(s).append(" ");
023: }
024:
025: public int credit() {
026: log("credit");
027: return 0;
028: }
029:
030: public int debit(boolean fail) throws NoMoreCreditException {
031: log("debit");
032: if (fail) {
033: throw new NoMoreCreditException();
034: }
035: return 0;
036: }
037:
038: public void greet() {
039: if (1 == 1) {
040: throw new RuntimeException("Just kidding!");
041: }
042: }
043:
044: public void testGreet() {
045: s_log = new StringBuffer();
046: try {
047: greet();
048: } catch (Exception e) {
049: ;
050: } finally {
051: assertEquals("beforeGreeting afterGreetingException ",
052: s_log.toString());
053: }
054: }
055:
056: public void testAfterRet() {
057: s_log = new StringBuffer();
058: credit();
059: try {
060: debit(false);
061: } catch (NoMoreCreditException e) {
062: fail(e.toString());
063: }
064: assertEquals("credit AOP.credit debit AOP.debit ", s_log
065: .toString());
066: }
067:
068: public void testAfterThrow() {
069: s_log = new StringBuffer();
070: try {
071: debit(true);
072: fail("should throw");
073: } catch (NoMoreCreditException e) {
074: ;
075: }
076: assertEquals("debit AOP.debit ", s_log.toString());
077: }
078:
079: private static class NoMoreCreditException extends Exception {
080: }
081:
082: public static class Aspect {
083:
084: @AfterReturning("execution(* test.AfterReturningThrowingTest.credit()) || execution(* test.AfterReturningThrowingTest.debit(..))")
085: public void afterReturning(JoinPoint jp) {
086: log("AOP." + jp.getSignature().getName());
087: }
088:
089: @AfterThrowing(pointcut="execution(* test.AfterReturningThrowingTest.debit(..))",type="test.AfterReturningThrowingTest$NoMoreCreditException")
090: public void afterThrowing(JoinPoint jp) {
091: log("AOP." + jp.getSignature().getName());
092: }
093: }
094:
095: public static class XmlAspect {
096:
097: public void beforeGreeting(JoinPoint joinPoint) {
098: log("beforeGreeting");
099: }
100:
101: public void afterGreeting(JoinPoint joinPoint) {
102: log("afterGreeting");
103: }
104:
105: public void afterGreeting(JoinPoint joinPoint,
106: java.lang.Exception ex) {
107: log("afterGreetingException");
108: }
109: }
110:
111: //--- JUnit
112:
113: public static void main(String[] args) {
114: junit.textui.TestRunner.run(suite());
115: }
116:
117: public static junit.framework.Test suite() {
118: return new junit.framework.TestSuite(
119: AfterReturningThrowingTest.class);
120: }
121:
122: }
|