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 java.lang.reflect.Proxy;
007:
008: import junit.framework.TestCase;
009:
010: /**
011: * @author andrew Unit test for {@link GenericInvocationHandler}.
012: */
013: public class GenericInvocationHandlerTest extends TestCase {
014:
015: private static interface Foo {
016: public String a(String x);
017:
018: public int b(int b);
019:
020: public String b(String b);
021:
022: public String c(String c);
023: }
024:
025: private static class OurHandler {
026: private int aCallCount;
027: private String lastACallArg;
028: private int b1CallCount;
029: private int lastB1CallArg;
030: private int b2CallCount;
031: private String lastB2CallArg;
032:
033: public String a(String x) {
034: ++aCallCount;
035: lastACallArg = x;
036: return "a!";
037: }
038:
039: public int b(int b) {
040: ++b1CallCount;
041: lastB1CallArg = b;
042: return 42;
043: }
044:
045: public String b(String b) {
046: ++b2CallCount;
047: lastB2CallArg = b;
048: return "b2!";
049: }
050:
051: public void reset() {
052: aCallCount = 0;
053: lastACallArg = null;
054: b1CallCount = 0;
055: lastB1CallArg = 0;
056: b2CallCount = 0;
057: lastB2CallArg = null;
058: }
059:
060: public int getACallCount() {
061: return this .aCallCount;
062: }
063:
064: public int getB1CallCount() {
065: return this .b1CallCount;
066: }
067:
068: public int getB2CallCount() {
069: return this .b2CallCount;
070: }
071:
072: public String getLastACallArg() {
073: return this .lastACallArg;
074: }
075:
076: public int getLastB1CallArg() {
077: return this .lastB1CallArg;
078: }
079:
080: public String getLastB2CallArg() {
081: return this .lastB2CallArg;
082: }
083: }
084:
085: private GenericInvocationHandler invocationHandler;
086: private OurHandler handlerObject;
087: private Foo theProxy;
088:
089: protected void setUp() throws Exception {
090: handlerObject = new OurHandler();
091: invocationHandler = new GenericInvocationHandler(handlerObject);
092: theProxy = (Foo) Proxy.newProxyInstance(
093: GenericInvocationHandlerTest.class.getClassLoader(),
094: new Class[] { Foo.class }, invocationHandler);
095: }
096:
097: public void testDelegatesMethodsToHandler() throws Exception {
098: checkCallCounts(0, 0, 0);
099:
100: assertEquals("a!", theProxy.a("g"));
101: checkCallCounts(1, 0, 0);
102: assertEquals("g", handlerObject.getLastACallArg());
103: handlerObject.reset();
104:
105: assertEquals(42, theProxy.b(16));
106: checkCallCounts(0, 1, 0);
107: assertEquals(16, handlerObject.getLastB1CallArg());
108: handlerObject.reset();
109:
110: assertEquals("b2!", theProxy.b("h"));
111: checkCallCounts(0, 0, 1);
112: assertEquals("h", handlerObject.getLastB2CallArg());
113: handlerObject.reset();
114: }
115:
116: public void testThrowsExceptionOnMissingMethod() throws Exception {
117: try {
118: theProxy.c("a");
119: fail("Should've gotten an exception");
120: } catch (NoSuchMethodError err) {
121: // ok
122: }
123: }
124:
125: private void checkCallCounts(int aCallCount, int bCallCount,
126: int cCallCount) {
127: assertEquals(aCallCount, handlerObject.getACallCount());
128: assertEquals(bCallCount, handlerObject.getB1CallCount());
129: assertEquals(cCallCount, handlerObject.getB2CallCount());
130: }
131:
132: }
|