KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.lang.reflect.Proxy JavaDoc;
7
8 import junit.framework.TestCase;
9
10 /**
11  * @author andrew Unit test for {@link GenericInvocationHandler}.
12  */

13 public class GenericInvocationHandlerTest extends TestCase {
14
15   private static interface Foo {
16     public String JavaDoc a(String JavaDoc x);
17
18     public int b(int b);
19
20     public String JavaDoc b(String JavaDoc b);
21
22     public String JavaDoc c(String JavaDoc c);
23   }
24
25   private static class OurHandler {
26     private int aCallCount;
27     private String JavaDoc lastACallArg;
28     private int b1CallCount;
29     private int lastB1CallArg;
30     private int b2CallCount;
31     private String JavaDoc lastB2CallArg;
32
33     public String JavaDoc a(String JavaDoc x) {
34       ++aCallCount;
35       lastACallArg = x;
36       return "a!";
37     }
38
39     public int b(int b) {
40       ++b1CallCount;
41       lastB1CallArg = b;
42       return 42;
43     }
44
45     public String JavaDoc b(String JavaDoc b) {
46       ++b2CallCount;
47       lastB2CallArg = b;
48       return "b2!";
49     }
50
51     public void reset() {
52       aCallCount = 0;
53       lastACallArg = null;
54       b1CallCount = 0;
55       lastB1CallArg = 0;
56       b2CallCount = 0;
57       lastB2CallArg = null;
58     }
59
60     public int getACallCount() {
61       return this.aCallCount;
62     }
63
64     public int getB1CallCount() {
65       return this.b1CallCount;
66     }
67
68     public int getB2CallCount() {
69       return this.b2CallCount;
70     }
71
72     public String JavaDoc getLastACallArg() {
73       return this.lastACallArg;
74     }
75
76     public int getLastB1CallArg() {
77       return this.lastB1CallArg;
78     }
79
80     public String JavaDoc getLastB2CallArg() {
81       return this.lastB2CallArg;
82     }
83   }
84
85   private GenericInvocationHandler invocationHandler;
86   private OurHandler handlerObject;
87   private Foo theProxy;
88
89   protected void setUp() throws Exception JavaDoc {
90     handlerObject = new OurHandler();
91     invocationHandler = new GenericInvocationHandler(handlerObject);
92     theProxy = (Foo) Proxy.newProxyInstance(GenericInvocationHandlerTest.class.getClassLoader(),
93                                             new Class JavaDoc[] { Foo.class }, invocationHandler);
94   }
95
96   public void testDelegatesMethodsToHandler() throws Exception JavaDoc {
97     checkCallCounts(0, 0, 0);
98
99     assertEquals("a!", theProxy.a("g"));
100     checkCallCounts(1, 0, 0);
101     assertEquals("g", handlerObject.getLastACallArg());
102     handlerObject.reset();
103
104     assertEquals(42, theProxy.b(16));
105     checkCallCounts(0, 1, 0);
106     assertEquals(16, handlerObject.getLastB1CallArg());
107     handlerObject.reset();
108
109     assertEquals("b2!", theProxy.b("h"));
110     checkCallCounts(0, 0, 1);
111     assertEquals("h", handlerObject.getLastB2CallArg());
112     handlerObject.reset();
113   }
114
115   public void testThrowsExceptionOnMissingMethod() throws Exception JavaDoc {
116     try {
117       theProxy.c("a");
118       fail("Should've gotten an exception");
119     } catch (NoSuchMethodError JavaDoc err) {
120       // ok
121
}
122   }
123
124   private void checkCallCounts(int aCallCount, int bCallCount, int cCallCount) {
125     assertEquals(aCallCount, handlerObject.getACallCount());
126     assertEquals(bCallCount, handlerObject.getB1CallCount());
127     assertEquals(cCallCount, handlerObject.getB2CallCount());
128   }
129
130 }
131
Popular Tags