1 package test.crispy; 2 3 import java.lang.reflect.Method; 4 5 import junit.framework.TestCase; 6 import net.sf.crispy.InterceptorContext; 7 import net.sf.crispy.InterceptorHandler; 8 import net.sf.crispy.InvocationException; 9 import net.sf.crispy.filter.SimpleNameInterceptorFilter; 10 import net.sf.crispy.interceptor.StopWatchInterceptor; 11 import net.sf.crispy.proxy.DynamicProxyFactory; 12 import test.crispy.example.service.Calculator; 13 import test.crispy.example.service.CalculatorImpl; 14 15 public class InterceptorHandlerTest extends TestCase { 16 17 public void testAddInterceptor() throws Exception { 18 InterceptorHandler lvHandler = new InterceptorHandler(); 19 lvHandler.addInterceptor(new StopWatchInterceptor()); 20 assertTrue(lvHandler.getInterceptorSize() == 1); 21 assertNotNull(lvHandler.getInterceptorByPos(0)); 22 assertNotNull(lvHandler.getInterceptorIterator().next()); 23 } 24 25 public void testAddInterceptorPosition() throws Exception { 26 InterceptorHandler lvHandler = new InterceptorHandler(); 27 int lvPos = lvHandler.addInterceptor(new StopWatchInterceptor()); 28 assertTrue(lvPos == 0); 29 lvPos = lvHandler.addInterceptor(new StopWatchInterceptor()); 30 assertTrue(lvPos == 1); 31 assertTrue(lvHandler.getInterceptorSize() == 2); 32 } 33 34 public void testDelInterceptor() throws Exception { 35 InterceptorHandler lvHandler = new InterceptorHandler(); 36 lvHandler.addInterceptor(new StopWatchInterceptor()); 37 assertTrue(lvHandler.getInterceptorSize() == 1); 38 lvHandler.removeInterceptorByPos(0); 39 assertTrue(lvHandler.getInterceptorSize() == 0); 40 } 41 42 public void testFireEvent() throws Exception { 43 InterceptorHandler lvHandler = new InterceptorHandler(); 44 lvHandler.addInterceptor(new StopWatchInterceptor()); 45 46 lvHandler.fireBeforeNewInstance(Calculator.class); 47 lvHandler.fireAfterNewInstance(Calculator.class, new CalculatorImpl()); 48 49 Method lvMethod = Calculator.class.getMethod("add", new Class[]{int.class, int.class}); 50 Object lvArgs[] = new Object[] {new Integer(3), new Integer(5)}; 51 InterceptorContext lvContext = new InterceptorContext(DynamicProxyFactory.getDefaultDynamicProxy(), lvMethod, lvArgs); 52 lvHandler.fireBeforeMethodInvocation(lvContext); 53 lvHandler.fireAfterMethodInvocation(lvContext); 54 } 55 56 public void testFilter() throws Exception { 57 InterceptorHandler lvHandler = new InterceptorHandler(); 58 SimpleNameInterceptorFilter lvFilter = new SimpleNameInterceptorFilter(Calculator.class); 59 60 lvHandler.addInterceptorFilter(lvFilter); 61 assertEquals(lvHandler.getInterceptorFilterSize(), 1); 62 assertTrue(lvHandler.getInterceptorFilterByPos(0) instanceof SimpleNameInterceptorFilter); 63 64 lvHandler.removeInterceptorFilter(0); 65 assertEquals(lvHandler.getInterceptorSize(), 0); 66 } 67 68 public void testThrowableFilter() throws Exception { 69 InterceptorHandler lvHandler = new InterceptorHandler(); 70 SimpleNameInterceptorFilter lvFilter = new SimpleNameInterceptorFilter(new InvocationException("JUnit-Test-Exception")); 71 72 lvHandler.addInterceptorFilter(lvFilter); 73 74 lvHandler.fireOnError(new NullPointerException("JUnit-NullPointer-Exception")); 75 } 76 77 78 }