1 8 package test.intercept.get; 9 10 import junit.framework.TestCase; 11 import org.codehaus.aspectwerkz.joinpoint.JoinPoint; 12 import org.codehaus.aspectwerkz.intercept.BeforeAdvice; 13 import org.codehaus.aspectwerkz.intercept.Advisable; 14 import org.codehaus.aspectwerkz.intercept.AroundAdvice; 15 import org.codehaus.aspectwerkz.intercept.AfterAdvice; 16 17 20 public class InterceptTest extends TestCase { 21 private static String LOG = ""; 22 23 public static void log(String msg) { 24 LOG += msg; 25 } 26 27 public void testIsAdvisable() { 28 assertTrue(this instanceof Advisable); 29 } 30 31 public void testAddAround() { 32 LOG = ""; 33 int tmp1 = adviseWithAround; 34 assertEquals("", LOG); 35 36 ((Advisable) this).aw_addAdvice( 37 "get(* test.intercept.get.InterceptTest.adviseWithAround)", 38 new AroundAdvice() { 39 public Object invoke(JoinPoint jp) throws Throwable { 40 InterceptTest.log("around1_pre "); 41 Object result = jp.proceed(); 42 InterceptTest.log("around1_post "); 43 return result; 44 } 45 } 46 ); 47 48 LOG = ""; 49 int tmp2 = adviseWithAround; 50 assertEquals("around1_pre around1_post ", LOG); 51 } 52 53 54 public void testAddAndRemoveAround() { 55 LOG = ""; 56 String tmp1 = adviseWithAround2; 57 assertEquals("", LOG); 58 59 ((Advisable) this).aw_addAdvice( 60 "get(* test.intercept.get.InterceptTest.adviseWithAround2)", 61 new AroundAdvice() { 62 public Object invoke(JoinPoint jp) throws Throwable { 63 InterceptTest.log("around1_pre "); 64 Object result = jp.proceed(); 65 InterceptTest.log("around1_post "); 66 return result; 67 } 68 } 69 ); 70 71 LOG = ""; 72 String tmp2 = adviseWithAround2; 73 assertEquals("around1_pre around1_post ", LOG); 74 75 ((Advisable) this).aw_removeAdvice("get(* test.intercept.get.InterceptTest.adviseWithAround2)", AroundAdvice.class); 76 77 LOG = ""; 78 String tmp3 = adviseWithAround2; 79 assertEquals("", LOG); 80 } 81 82 public void testAddAroundStack() { 83 LOG = ""; 84 int tmp1 = adviseWithAroundStack; 85 assertEquals("", LOG); 86 87 ((Advisable) this).aw_addAdvice( 88 "get(* test.intercept.get.InterceptTest.adviseWithAroundStack)", 89 new AroundAdvice() { 90 public Object invoke(JoinPoint jp) throws Throwable { 91 InterceptTest.log("around2_pre "); 92 Object result = jp.proceed(); 93 InterceptTest.log("around2_post "); 94 return result; 95 } 96 } 97 ); 98 99 LOG = ""; 100 int tmp2 = adviseWithAroundStack; 101 assertEquals("around2_pre around2_post ", LOG); 102 103 ((Advisable) this).aw_addAdvice( 104 "get(* test.intercept.get.InterceptTest.adviseWithAroundStack)", 105 new AroundAdvice() { 106 public Object invoke(JoinPoint jp) throws Throwable { 107 InterceptTest.log("around3_pre "); 108 Object result = jp.proceed(); 109 InterceptTest.log("around3_post "); 110 return result; 111 } 112 } 113 ); 114 115 LOG = ""; 116 int tmp3 = adviseWithAroundStack; 117 assertEquals("around2_pre around3_pre around3_post around2_post ", LOG); 118 } 119 120 public void testAddBefore() { 121 LOG = ""; 122 Object tmp1 = adviseWithBefore; 123 assertEquals("", LOG); 124 125 ((Advisable) this).aw_addAdvice( 126 "get(* test.intercept.get.InterceptTest.adviseWithBefore)", 127 new BeforeAdvice() { 128 public void invoke(JoinPoint jp) throws Throwable { 129 InterceptTest.log("before "); 130 } 131 } 132 ); 133 134 LOG = ""; 135 Object tmp2 = adviseWithBefore; 136 assertEquals("before ", LOG); 137 } 138 139 public void testAddAfter() { 140 LOG = ""; 141 boolean tmp1 = adviseWithAfter; 142 assertEquals("", LOG); 143 144 ((Advisable) this).aw_addAdvice( 145 "get(* test.intercept.get.InterceptTest.adviseWithAfter)", 146 new AfterAdvice() { 147 public void invoke(JoinPoint jp) throws Throwable { 148 InterceptTest.log("afterFinally "); 149 } 150 } 151 ); 152 153 LOG = ""; 154 boolean tmp2 = adviseWithAfter; 155 assertEquals("afterFinally ", LOG); 156 } 157 158 public static void main(String [] args) { 159 junit.textui.TestRunner.run(suite()); 160 } 161 162 public static junit.framework.Test suite() { 163 return new junit.framework.TestSuite(InterceptTest.class); 164 } 165 166 int adviseWithAround = -1; 167 168 String adviseWithAround2 = "lala"; 169 170 int adviseWithAroundStack = 135; 171 172 Object adviseWithBefore = new Boolean (true); 173 174 boolean adviseWithAfter = false; 175 } 176 | Popular Tags |