KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > common > proxy > MethodMonitorProxyTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.common.proxy;
5
6 import com.tc.util.Util;
7
8 import java.lang.reflect.Method JavaDoc;
9 import java.util.Random JavaDoc;
10
11 import junit.framework.TestCase;
12
13 public class MethodMonitorProxyTest extends TestCase {
14
15   final static Method JavaDoc i1_m1;
16   final static Method JavaDoc i1_m2;
17
18   static {
19     try {
20       i1_m1 = i1.class.getDeclaredMethod("m1", new Class JavaDoc[] { Integer.TYPE });
21       i1_m2 = i1.class.getDeclaredMethod("m2", new Class JavaDoc[] {});
22     } catch (Exception JavaDoc e) {
23       throw new RuntimeException JavaDoc(e);
24     }
25   }
26
27   public void testMethods() {
28     c1 obj = new c1();
29     Object JavaDoc 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 JavaDoc 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 JavaDoc;
58   }
59
60   static class c1 implements i1 {
61     public int m1(int a1) {
62       if (a1 < 0) {
63         throw new RuntimeException JavaDoc();
64       } else {
65         return a1;
66       }
67     }
68
69     public void m2() throws Exception JavaDoc {
70       Thread.sleep(new Random JavaDoc().nextInt(500));
71       throw new Exception JavaDoc("yippy", new RuntimeException JavaDoc("skippy"));
72     }
73   }
74
75 }
76
Popular Tags