001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.util;
006:
007: import junit.framework.TestCase;
008:
009: public class ReflectiveProxyTest extends TestCase {
010:
011: interface Test1Interface {
012: void foo();
013:
014: void foo(int i);
015:
016: void foo(Object o);
017: }
018:
019: private static class Test1Class {
020:
021: int foo0 = 0;
022: int foo1 = 0;
023: Object foo2 = null;
024:
025: public void foo() {
026: foo0++;
027: }
028:
029: public void foo(int i) {
030: foo1 = i;
031: }
032:
033: public void foo(Object o) {
034: foo2 = o;
035: }
036: }
037:
038: public void test1() throws Exception {
039: Test1Class o = new Test1Class();
040: Test1Interface proxy = (Test1Interface) ReflectiveProxy
041: .createProxyIfPossible(Test1Interface.class, o);
042: assertNotNull(proxy);
043:
044: assertEquals(0, o.foo0);
045: proxy.foo();
046: assertEquals(1, o.foo0);
047:
048: assertEquals(0, o.foo1);
049: proxy.foo(42);
050: assertEquals(42, o.foo1);
051:
052: assertEquals(null, o.foo2);
053: proxy.foo(this );
054: assertEquals(this , o.foo2);
055: }
056:
057: /*
058: * a differing return type disallows proxy
059: */
060: interface Test2Interface {
061: void foo();
062: }
063:
064: static class Test2Class {
065: public Test2Class foo() {
066: return this ;
067: }
068: }
069:
070: public void test2() throws Exception {
071: Object o = new Test2Class();
072: Test2Interface proxy = (Test2Interface) ReflectiveProxy
073: .createProxyIfPossible(Test2Interface.class, o);
074: assertNull(proxy);
075: }
076:
077: /*
078: * a differing arg list disallows proxy
079: */
080: interface Test3Interface {
081: void foo(int o);
082: }
083:
084: static class Test3Class {
085: public void foo() {
086: //
087: }
088: }
089:
090: public void test3() throws Exception {
091: Object o = new Test3Class();
092: Test3Interface proxy = (Test3Interface) ReflectiveProxy
093: .createProxyIfPossible(Test3Interface.class, o);
094: assertNull(proxy);
095: }
096:
097: /*
098: * non-public (but matching method) disallows proxy
099: */
100: interface Test4Interface {
101: void foo();
102: }
103:
104: static class Test4Class {
105: void foo() {
106: //
107: }
108: }
109:
110: public void test4() throws Exception {
111: Object o = new Test4Class();
112: Test4Interface proxy = (Test4Interface) ReflectiveProxy
113: .createProxyIfPossible(Test4Interface.class, o);
114: assertNull(proxy);
115: }
116:
117: }
|