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 TestDispatcher 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 Callback[] callbacks = new Callback[]{ 52 new Dispatcher() { 53 public Object loadObject() { 54 return impls[0]; 55 } 56 }, 57 new Dispatcher() { 58 public Object loadObject() { 59 return impls[1]; 60 } 61 } 62 }; 63 64 Enhancer e = new Enhancer(); 65 e.setInterfaces(new Class []{ Foo.class, Bar.class }); 66 e.setCallbacks(callbacks); 67 e.setCallbackFilter(new CallbackFilter() { 68 public int accept(Method method) { 69 return (method.getDeclaringClass().equals(Foo.class)) ? 0 : 1; 70 } 71 }); 72 Object obj = e.create(); 73 74 assertTrue(((Foo)obj).foo().equals("foo1")); 75 assertTrue(((Bar)obj).bar().equals("bar1")); 76 77 impls[0] = new Foo() { 78 public String foo() { 79 return "foo2"; 80 } 81 }; 82 assertTrue(((Foo)obj).foo().equals("foo2")); 83 } 84 85 public TestDispatcher(String testName) { 86 super(testName); 87 } 88 89 public static void main(String [] args) { 90 junit.textui.TestRunner.run(suite()); 91 } 92 93 public static Test suite() { 94 return new TestSuite(TestDispatcher.class); 95 } 96 97 public void perform(ClassLoader loader) throws Throwable { 98 } 99 100 public void testFailOnMemoryLeak() throws Throwable { 101 } 102 103 } 104 | Popular Tags |