001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.common.proxy;
005:
006: import com.tc.common.proxy.subpkg.Factory;
007: import com.tc.common.proxy.subpkg.TestInterface;
008:
009: import java.io.IOException;
010: import java.lang.reflect.Proxy;
011:
012: import junit.framework.TestCase;
013:
014: /**
015: * @author andrew Unit test for {@link DelegatingInvocationHandlerTest}.
016: */
017: public class DelegatingInvocationHandlerTest extends TestCase {
018:
019: public static interface TheInterface {
020: String a(String arg);
021:
022: String b(String arg);
023:
024: String c(String arg);
025:
026: void d() throws IOException;
027:
028: void e() throws IOException;
029: }
030:
031: public static class Handler {
032: private int numACalls;
033: private String lastAArg;
034:
035: public Handler() {
036: reset();
037: }
038:
039: public String a(String arg) {
040: ++numACalls;
041: lastAArg = arg;
042: return "handler.a";
043: }
044:
045: public void reset() {
046: numACalls = 0;
047: lastAArg = null;
048: }
049:
050: public String getLastAArg() {
051: return this .lastAArg;
052: }
053:
054: public int getNumACalls() {
055: return this .numACalls;
056: }
057:
058: public void d() throws IOException {
059: throw new IOException("handler");
060: }
061: }
062:
063: public static class Delegate {
064: private int numACalls;
065: private String lastAArg;
066: private int numBCalls;
067: private String lastBArg;
068:
069: public Delegate() {
070: reset();
071: }
072:
073: public String a(String arg) {
074: ++numACalls;
075: lastAArg = arg;
076: return "delegate.a";
077: }
078:
079: public String b(String arg) {
080: ++numBCalls;
081: lastBArg = arg;
082: return "delegate.b";
083: }
084:
085: public void d() {
086: //;
087: }
088:
089: public void e() throws IOException {
090: throw new IOException("delegate");
091: }
092:
093: public void reset() {
094: numACalls = 0;
095: lastAArg = null;
096: }
097:
098: public String getLastAArg() {
099: return this .lastAArg;
100: }
101:
102: public String 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 {
121: this .handler = new Handler();
122: this .delegate = new Delegate();
123: this .invocationHandler = new DelegatingInvocationHandler(
124: delegate, handler);
125: theProxy = (TheInterface) Proxy.newProxyInstance(
126: DelegatingInvocationHandlerTest.class.getClassLoader(),
127: new Class[] { TheInterface.class }, invocationHandler);
128: }
129:
130: public void testDelegates() throws Exception {
131: String value = theProxy.a("x");
132: assertEquals("handler.a", value);
133: assertEquals(1, handler.getNumACalls());
134: assertEquals("x", handler.getLastAArg());
135: assertEquals(0, delegate.getNumACalls());
136: assertEquals(0, delegate.getNumBCalls());
137:
138: handler.reset();
139:
140: value = theProxy.b("y");
141: assertEquals("delegate.b", value);
142: assertEquals(0, handler.getNumACalls());
143: assertEquals(0, delegate.getNumACalls());
144: assertEquals(1, delegate.getNumBCalls());
145: assertEquals("y", delegate.getLastBArg());
146: }
147:
148: public void testException() throws Exception {
149: try {
150: theProxy.e();
151: fail("e() didn't throw exception");
152: } catch (IOException ioe) {
153: assertTrue(ioe.getMessage().indexOf("delegate") >= 0);
154: }
155:
156: try {
157: theProxy.d();
158: fail("d() didn't throw exception");
159: } catch (IOException ioe) {
160: assertTrue(ioe.getMessage().indexOf("handler") >= 0);
161: }
162: }
163:
164: public void testNonPublicDelegate() {
165: TestInterface proxy = (TestInterface) DelegateHelper
166: .createDelegate(TestInterface.class, Factory
167: .getInstance());
168: proxy.method();
169: }
170:
171: }
|