1 4 package com.tc.common.proxy; 5 6 import com.tc.util.Util; 7 8 import java.lang.reflect.Method ; 9 import java.util.Random ; 10 11 import junit.framework.TestCase; 12 13 public class MethodMonitorProxyTest extends TestCase { 14 15 final static Method i1_m1; 16 final static Method i1_m2; 17 18 static { 19 try { 20 i1_m1 = i1.class.getDeclaredMethod("m1", new Class [] { Integer.TYPE }); 21 i1_m2 = i1.class.getDeclaredMethod("m2", new Class [] {}); 22 } catch (Exception e) { 23 throw new RuntimeException (e); 24 } 25 } 26 27 public void testMethods() { 28 c1 obj = new c1(); 29 Object proxy = MethodMonitorProxy 30 .createProxy(obj, new MethodInvocationEventListener[] { new MethodInvocationEventListener() { 31 public void methodInvoked(MethodInvocationEvent event) { 32 System.out.println("method invoked: " + event.getMethod()); 33 System.out.println("args: " + Util.enumerateArray(event.getArguments())); 34 System.out.println("start time: " + event.getExecutionStartTime()); 35 System.out.println("end time: " + event.getExecutionEndTime()); 36 System.out.println("elapsed time: " + (event.getExecutionEndTime() - event.getExecutionStartTime())); 37 } 38 } }); 39 40 assertTrue(proxy instanceof i1); 41 i1 iface = (i1) proxy; 42 43 iface.m1(0); 44 45 try { 46 iface.m2(); 47 fail("no exception received in client"); 48 } catch (Exception e) { 49 assertTrue(e.getMessage().equals("yippy")); 50 assertTrue(e.getCause().getMessage().equals("skippy")); 51 } 52 } 53 54 interface i1 { 55 public int m1(int a1); 56 57 public void m2() throws Exception ; 58 } 59 60 static class c1 implements i1 { 61 public int m1(int a1) { 62 if (a1 < 0) { 63 throw new RuntimeException (); 64 } else { 65 return a1; 66 } 67 } 68 69 public void m2() throws Exception { 70 Thread.sleep(new Random ().nextInt(500)); 71 throw new Exception ("yippy", new RuntimeException ("skippy")); 72 } 73 } 74 75 } 76 | Popular Tags |