1 16 package net.sf.cglib.proxy; 17 18 import net.sf.cglib.CodeGenTestCase; 19 import java.beans.*; 20 import java.lang.reflect.Method ; 21 import java.util.*; 22 import junit.framework.*; 23 24 28 public class TestProxyRefDispatcher extends CodeGenTestCase { 29 interface Foo { 30 String foo(); 31 } 32 33 interface Bar { 34 String bar(); 35 } 36 37 public void testSimple() throws Exception { 38 final Object [] impls = new Object []{ 39 new Foo() { 40 public String foo() { 41 return "foo1"; 42 } 43 }, 44 new Bar() { 45 public String bar() { 46 return "bar1"; 47 } 48 } 49 }; 50 51 final Object [] proxyReference = new Object [1]; 52 Callback[] callbacks = new Callback[]{ 53 new ProxyRefDispatcher() { 54 public Object loadObject(Object proxy) { 55 proxyReference[0] = proxy; 56 return impls[0]; 57 } 58 }, 59 new ProxyRefDispatcher() { 60 public Object loadObject(Object proxy) { 61 proxyReference[0] = proxy; 62 return impls[1]; 63 } 64 } 65 }; 66 67 Enhancer e = new Enhancer(); 68 e.setInterfaces(new Class []{ Foo.class, Bar.class }); 69 e.setCallbacks(callbacks); 70 e.setCallbackFilter(new CallbackFilter() { 71 public int accept(Method method) { 72 return (method.getDeclaringClass().equals(Foo.class)) ? 0 : 1; 73 } 74 }); 75 Object obj = e.create(); 76 77 assertNull(proxyReference[0]); 78 assertTrue(((Foo)obj).foo().equals("foo1")); 79 assertSame(obj, proxyReference[0]); 80 proxyReference[0] = null; 81 assertTrue(((Bar)obj).bar().equals("bar1")); 82 assertSame(obj, proxyReference[0]); 83 proxyReference[0] = null; 84 85 impls[0] = new Foo() { 86 public String foo() { 87 return "foo2"; 88 } 89 }; 90 assertTrue(((Foo)obj).foo().equals("foo2")); 91 assertSame(obj, proxyReference[0]); 92 } 93 94 public TestProxyRefDispatcher(String testName) { 95 super(testName); 96 } 97 98 public static void main(String [] args) { 99 junit.textui.TestRunner.run(suite()); 100 } 101 102 public static Test suite() { 103 return new TestSuite(TestProxyRefDispatcher.class); 104 } 105 106 public void perform(ClassLoader loader) throws Throwable { 107 } 108 109 public void testFailOnMemoryLeak() throws Throwable { 110 } 111 112 } 113 | Popular Tags |