1 16 17 package org.springframework.aop.framework.adapter; 18 19 import java.lang.reflect.Method ; 20 import java.rmi.RemoteException ; 21 22 import javax.servlet.ServletException ; 23 import javax.transaction.TransactionRolledbackException ; 24 25 import junit.framework.TestCase; 26 import org.aopalliance.intercept.MethodInvocation; 27 import org.easymock.MockControl; 28 29 import org.springframework.aop.ThrowsAdvice; 30 import org.springframework.aop.framework.MethodCounter; 31 32 35 public class ThrowsAdviceInterceptorTests extends TestCase { 36 37 public void testNoHandlerMethods() { 38 Object o = new Object (); 39 try { 40 new ThrowsAdviceInterceptor(o); 41 fail("Should require one handler method at least"); 42 } 43 catch (IllegalArgumentException ex) { 44 } 46 } 47 48 public void testNotInvoked() throws Throwable { 49 MyThrowsHandler th = new MyThrowsHandler(); 50 ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th); 51 Object ret = new Object (); 52 MockControl mc = MockControl.createControl(MethodInvocation.class); 53 MethodInvocation mi = (MethodInvocation) mc.getMock(); 54 mi.proceed(); 55 mc.setReturnValue(ret, 1); 56 mc.replay(); 57 assertEquals(ret, ti.invoke(mi)); 58 assertEquals(0, th.getCalls()); 59 mc.verify(); 60 } 61 62 public void testNoHandlerMethodForThrowable() throws Throwable { 63 MyThrowsHandler th = new MyThrowsHandler(); 64 ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th); 65 assertEquals(2, ti.getHandlerMethodCount()); 66 Exception ex = new Exception (); 67 MockControl mc = MockControl.createControl(MethodInvocation.class); 68 MethodInvocation mi = (MethodInvocation) mc.getMock(); 69 mi.proceed(); 70 mc.setThrowable(ex); 71 mc.replay(); 72 try { 73 ti.invoke(mi); 74 fail(); 75 } 76 catch (Exception caught) { 77 assertEquals(ex, caught); 78 } 79 assertEquals(0, th.getCalls()); 80 mc.verify(); 81 } 82 83 public void testCorrectHandlerUsed() throws Throwable { 84 MyThrowsHandler th = new MyThrowsHandler(); 85 ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th); 86 ServletException ex = new ServletException (); 87 MockControl mc = MockControl.createControl(MethodInvocation.class); 88 MethodInvocation mi = (MethodInvocation) mc.getMock(); 89 mi.getMethod(); 90 mc.setReturnValue(Object .class.getMethod("hashCode", (Class []) null), 1); 91 mi.getArguments(); 92 mc.setReturnValue(null); 93 mi.getThis(); 94 mc.setReturnValue(new Object ()); 95 mi.proceed(); 96 mc.setThrowable(ex); 97 mc.replay(); 98 try { 99 ti.invoke(mi); 100 fail(); 101 } 102 catch (Exception caught) { 103 assertEquals(ex, caught); 104 } 105 assertEquals(1, th.getCalls()); 106 assertEquals(1, th.getCalls("servletException")); 107 mc.verify(); 108 } 109 110 public void testCorrectHandlerUsedForSubclass() throws Throwable { 111 MyThrowsHandler th = new MyThrowsHandler(); 112 ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th); 113 TransactionRolledbackException ex = new TransactionRolledbackException (); 115 MockControl mc = MockControl.createControl(MethodInvocation.class); 116 MethodInvocation mi = (MethodInvocation) mc.getMock(); 117 mi.proceed(); 118 mc.setThrowable(ex); 119 mc.replay(); 120 try { 121 ti.invoke(mi); 122 fail(); 123 } 124 catch (Exception caught) { 125 assertEquals(ex, caught); 126 } 127 assertEquals(1, th.getCalls()); 128 assertEquals(1, th.getCalls("remoteException")); 129 mc.verify(); 130 } 131 132 public void testHandlerMethodThrowsException() throws Throwable { 133 final Throwable t = new Throwable (); 134 MyThrowsHandler th = new MyThrowsHandler() { 135 public void afterThrowing(RemoteException ex) throws Throwable { 136 super.afterThrowing(ex); 137 throw t; 138 } 139 }; 140 ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th); 141 TransactionRolledbackException ex = new TransactionRolledbackException (); 143 MockControl mc = MockControl.createControl(MethodInvocation.class); 144 MethodInvocation mi = (MethodInvocation) mc.getMock(); 145 mi.proceed(); 146 mc.setThrowable(ex); 147 mc.replay(); 148 try { 149 ti.invoke(mi); 150 fail(); 151 } 152 catch (Throwable caught) { 153 assertEquals(t, caught); 154 } 155 assertEquals(1, th.getCalls()); 156 assertEquals(1, th.getCalls("remoteException")); 157 mc.verify(); 158 } 159 160 public static class MyThrowsHandler extends MethodCounter implements ThrowsAdvice { 161 public void afterThrowing(Method m, Object [] args, Object target, ServletException ex) { 163 count("servletException"); 164 } 165 public void afterThrowing(RemoteException ex) throws Throwable { 166 count("remoteException"); 167 } 168 169 170 public void afterThrowing(Method m, Exception ex) throws Throwable { 171 throw new UnsupportedOperationException ("Shouldn't be called"); 172 } 173 } 174 175 public interface IEcho { 176 int echoException(int i, Throwable t) throws Throwable ; 177 int getA(); 178 void setA(int a); 179 } 180 181 public static class Echo implements IEcho { 182 private int a; 183 184 public int echoException(int i, Throwable t) throws Throwable { 185 if (t != null) 186 throw t; 187 return i; 188 } 189 public void setA(int a) { 190 this.a = a; 191 } 192 public int getA() { 193 return a; 194 } 195 } 196 197 } 198 | Popular Tags |