1 16 package net.sf.cglib.reflect; 17 18 import java.lang.reflect.Method ; 19 import junit.framework.*; 20 21 24 public class TestDelegates extends net.sf.cglib.CodeGenTestCase { 25 26 public interface StringMaker { 27 Object newInstance(char[] buf, int offset, int count); 28 } 29 30 public void testConstructor() throws Throwable { 31 StringMaker maker = (StringMaker)ConstructorDelegate.create(String .class, StringMaker.class); 32 assertTrue("nil".equals(maker.newInstance("vanilla".toCharArray(), 2, 3))); 33 } 34 35 public interface Substring { 36 String substring(int start, int end); 37 } 38 39 public interface Substring2 { 40 Object anyNameAllowed(int start, int end); 41 } 42 43 public interface IndexOf { 44 int indexOf(String str, int fromIndex); 45 } 46 47 public void testFancy() throws Throwable { 48 Substring delegate = (Substring)MethodDelegate.create("CGLIB", "substring", Substring.class); 49 assertTrue("LI".equals(delegate.substring(2, 4))); 50 } 51 52 public void testFancyNames() throws Throwable { 53 Substring2 delegate = (Substring2)MethodDelegate.create("CGLIB", "substring", Substring2.class); 54 assertTrue("LI".equals(delegate.anyNameAllowed(2, 4))); 55 } 56 57 public void testFancyTypes() throws Throwable { 58 String test = "abcabcabc"; 59 IndexOf delegate = (IndexOf)MethodDelegate.create(test, "indexOf", IndexOf.class); 60 assertTrue(delegate.indexOf("ab", 1) == test.indexOf("ab", 1)); 61 } 62 63 public void testEquals() throws Throwable { 64 String test = "abc"; 65 MethodDelegate mc1 = MethodDelegate.create(test, "indexOf", IndexOf.class); 66 MethodDelegate mc2 = MethodDelegate.create(test, "indexOf", IndexOf.class); 67 MethodDelegate mc3 = MethodDelegate.create("other", "indexOf", IndexOf.class); 68 MethodDelegate mc4 = MethodDelegate.create(test, "substring", Substring.class); 69 MethodDelegate mc5 = MethodDelegate.create(test, "substring", Substring2.class); 70 assertTrue(mc1.equals(mc2)); 71 assertTrue(!mc1.equals(mc3)); 72 assertTrue(!mc1.equals(mc4)); 73 assertTrue(mc4.equals(mc5)); 74 } 75 76 public static interface MainDelegate { 77 int main(String [] args); 78 } 79 80 public static class MainTest { 81 public static int alternateMain(String [] args) { 82 return 7; 83 } 84 } 85 86 public void testStaticDelegate() throws Throwable { 87 MainDelegate start = (MainDelegate)MethodDelegate.createStatic(MainTest.class, 88 "alternateMain", 89 MainDelegate.class); 90 assertTrue(start.main(null) == 7); 91 } 92 93 public static interface Listener { 94 public void onEvent(); 95 } 96 97 public static class Publisher { 98 public int test = 0; 99 private MulticastDelegate event = MulticastDelegate.create(Listener.class); 100 public void addListener(Listener listener) { 101 event = event.add(listener); 102 } 103 public void removeListener(Listener listener) { 104 event = event.remove(listener); 105 } 106 public void fireEvent() { 107 ((Listener)event).onEvent(); 108 } 109 } 110 111 public void testPublisher() throws Throwable { 112 final Publisher p = new Publisher(); 113 Listener l1 = new Listener() { 114 public void onEvent() { 115 p.test++; 116 } 117 }; 118 p.addListener(l1); 119 p.addListener(l1); 120 p.fireEvent(); 121 assertTrue(p.test == 2); 122 p.removeListener(l1); 123 p.fireEvent(); 124 assertTrue(p.test == 3); 125 } 126 127 public static interface SuperSimple { 128 public int execute(); 129 } 130 131 public void testMulticastReturnValue() throws Throwable { 132 SuperSimple ss1 = new SuperSimple() { 133 public int execute() { 134 return 1; 135 } 136 }; 137 SuperSimple ss2 = new SuperSimple() { 138 public int execute() { 139 return 2; 140 } 141 }; 142 MulticastDelegate multi = MulticastDelegate.create(SuperSimple.class); 143 multi = multi.add(ss1); 144 multi = multi.add(ss2); 145 assertTrue(((SuperSimple)multi).execute() == 2); 146 multi = multi.remove(ss1); 147 multi = multi.add(ss1); 148 assertTrue(((SuperSimple)multi).execute() == 1); 149 } 150 151 public TestDelegates(String testName) { 152 super(testName); 153 } 154 155 public static void main(String [] args) { 156 junit.textui.TestRunner.run(suite()); 157 } 158 159 public static Test suite() { 160 return new TestSuite(TestDelegates.class); 161 } 162 163 public void perform(ClassLoader loader) throws Throwable { 164 } 165 166 public void testFailOnMemoryLeak() throws Throwable { 167 } 168 169 } 170 | Popular Tags |