01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.common.proxy;
05:
06: import com.tc.util.Util;
07:
08: import java.lang.reflect.Method;
09: 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",
21: new Class[] { Integer.TYPE });
22: i1_m2 = i1.class.getDeclaredMethod("m2", new Class[] {});
23: } catch (Exception e) {
24: throw new RuntimeException(e);
25: }
26: }
27:
28: public void testMethods() {
29: c1 obj = new c1();
30: Object proxy = MethodMonitorProxy
31: .createProxy(
32: obj,
33: new MethodInvocationEventListener[] { new MethodInvocationEventListener() {
34: public void methodInvoked(
35: MethodInvocationEvent event) {
36: System.out.println("method invoked: "
37: + event.getMethod());
38: System.out.println("args: "
39: + Util.enumerateArray(event
40: .getArguments()));
41: System.out
42: .println("start time: "
43: + event
44: .getExecutionStartTime());
45: System.out.println("end time: "
46: + event.getExecutionEndTime());
47: System.out
48: .println("elapsed time: "
49: + (event
50: .getExecutionEndTime() - event
51: .getExecutionStartTime()));
52: }
53: } });
54:
55: assertTrue(proxy instanceof i1);
56: i1 iface = (i1) proxy;
57:
58: iface.m1(0);
59:
60: try {
61: iface.m2();
62: fail("no exception received in client");
63: } catch (Exception e) {
64: assertTrue(e.getMessage().equals("yippy"));
65: assertTrue(e.getCause().getMessage().equals("skippy"));
66: }
67: }
68:
69: interface i1 {
70: public int m1(int a1);
71:
72: public void m2() throws Exception;
73: }
74:
75: static class c1 implements i1 {
76: public int m1(int a1) {
77: if (a1 < 0) {
78: throw new RuntimeException();
79: } else {
80: return a1;
81: }
82: }
83:
84: public void m2() throws Exception {
85: Thread.sleep(new Random().nextInt(500));
86: throw new Exception("yippy", new RuntimeException("skippy"));
87: }
88: }
89:
90: }
|