1 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 18 public class AfterReturningThrowingTest extends TestCase { 19 20 static StringBuffer s_log = new StringBuffer (); 21 static void log(String 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 ("Just kidding!"); 41 } 42 } 43 44 public void testGreet() { 45 s_log = new StringBuffer (); 46 try { 47 greet(); 48 } catch (Exception e) { 49 ; 50 } finally { 51 assertEquals("beforeGreeting afterGreetingException ", s_log.toString()); 52 } 53 } 54 55 public void testAfterRet() { 56 s_log = new StringBuffer (); 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 (); 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 {} 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 ex) { 104 log("afterGreetingException"); 105 } 106 } 107 108 109 110 112 public static void main(String [] 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 |