KickJava   Java API By Example, From Geeks To Geeks.

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


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.common.proxy.subpkg.Factory;
7 import com.tc.common.proxy.subpkg.TestInterface;
8
9 import java.io.IOException JavaDoc;
10 import java.lang.reflect.Proxy JavaDoc;
11
12 import junit.framework.TestCase;
13
14 /**
15  * @author andrew Unit test for {@link DelegatingInvocationHandlerTest}.
16  */

17 public class DelegatingInvocationHandlerTest extends TestCase {
18
19   public static interface TheInterface {
20     String JavaDoc a(String JavaDoc arg);
21
22     String JavaDoc b(String JavaDoc arg);
23
24     String JavaDoc c(String JavaDoc arg);
25
26     void d() throws IOException JavaDoc;
27
28     void e() throws IOException JavaDoc;
29   }
30
31   public static class Handler {
32     private int numACalls;
33     private String JavaDoc lastAArg;
34
35     public Handler() {
36       reset();
37     }
38
39     public String JavaDoc a(String JavaDoc arg) {
40       ++numACalls;
41       lastAArg = arg;
42       return "handler.a";
43     }
44
45     public void reset() {
46       numACalls = 0;
47       lastAArg = null;
48     }
49
50     public String JavaDoc getLastAArg() {
51       return this.lastAArg;
52     }
53
54     public int getNumACalls() {
55       return this.numACalls;
56     }
57
58     public void d() throws IOException JavaDoc {
59       throw new IOException JavaDoc("handler");
60     }
61   }
62
63   public static class Delegate {
64     private int numACalls;
65     private String JavaDoc lastAArg;
66     private int numBCalls;
67     private String JavaDoc lastBArg;
68
69     public Delegate() {
70       reset();
71     }
72
73     public String JavaDoc a(String JavaDoc arg) {
74       ++numACalls;
75       lastAArg = arg;
76       return "delegate.a";
77     }
78
79     public String JavaDoc b(String JavaDoc arg) {
80       ++numBCalls;
81       lastBArg = arg;
82       return "delegate.b";
83     }
84
85     public void d() {
86       //;
87
}
88
89     public void e() throws IOException JavaDoc {
90       throw new IOException JavaDoc("delegate");
91     }
92
93     public void reset() {
94       numACalls = 0;
95       lastAArg = null;
96     }
97
98     public String JavaDoc getLastAArg() {
99       return this.lastAArg;
100     }
101
102     public String JavaDoc getLastBArg() {
103       return this.lastBArg;
104     }
105
106     public int getNumACalls() {
107       return this.numACalls;
108     }
109
110     public int getNumBCalls() {
111       return this.numBCalls;
112     }
113   }
114
115   private Handler handler;
116   private Delegate delegate;
117   private DelegatingInvocationHandler invocationHandler;
118   private TheInterface theProxy;
119
120   protected void setUp() throws Exception JavaDoc {
121     this.handler = new Handler();
122     this.delegate = new Delegate();
123     this.invocationHandler = new DelegatingInvocationHandler(delegate, handler);
124     theProxy = (TheInterface) Proxy.newProxyInstance(DelegatingInvocationHandlerTest.class.getClassLoader(),
125                                                      new Class JavaDoc[] { TheInterface.class }, invocationHandler);
126   }
127
128   public void testDelegates() throws Exception JavaDoc {
129     String JavaDoc value = theProxy.a("x");
130     assertEquals("handler.a", value);
131     assertEquals(1, handler.getNumACalls());
132     assertEquals("x", handler.getLastAArg());
133     assertEquals(0, delegate.getNumACalls());
134     assertEquals(0, delegate.getNumBCalls());
135
136     handler.reset();
137
138     value = theProxy.b("y");
139     assertEquals("delegate.b", value);
140     assertEquals(0, handler.getNumACalls());
141     assertEquals(0, delegate.getNumACalls());
142     assertEquals(1, delegate.getNumBCalls());
143     assertEquals("y", delegate.getLastBArg());
144   }
145
146   public void testException() throws Exception JavaDoc {
147     try {
148       theProxy.e();
149       fail("e() didn't throw exception");
150     } catch (IOException JavaDoc ioe) {
151       assertTrue(ioe.getMessage().indexOf("delegate") >= 0);
152     }
153
154     try {
155       theProxy.d();
156       fail("d() didn't throw exception");
157     } catch (IOException JavaDoc ioe) {
158       assertTrue(ioe.getMessage().indexOf("handler") >= 0);
159     }
160   }
161
162   public void testNonPublicDelegate() {
163     TestInterface proxy = (TestInterface) DelegateHelper.createDelegate(TestInterface.class, Factory.getInstance());
164     proxy.method();
165   }
166
167 }
168
Popular Tags