1 16 package net.sf.cglib.proxy; 17 18 import net.sf.cglib.CodeGenTestCase; 19 import java.lang.reflect.Method ; 20 import java.util.HashMap ; 21 import java.util.Map ; 22 23 import net.sf.cglib.proxysample.ProxySampleInterface_ReturnsBasic; 24 25 import junit.framework.*; 26 27 31 public class TestProxy extends CodeGenTestCase { 32 33 private class SimpleInvocationHandler implements InvocationHandler { 34 Object o = null; 35 public SimpleInvocationHandler(Object o) { 36 this.o = o; 37 } 38 public Object invoke(Object proxy, Method m, Object [] args) throws Throwable { 39 System.out.println("invoking " + m + " on " + o + " with " + args); 40 Object r = m.invoke(o, args); 41 System.out.println("done: " + m + " on " + o + " with " + args + ", result is " + r); 42 return r; 43 } 44 } 45 46 public void testGetProxyClassAndConstructor() throws Exception { 47 HashMap map = new HashMap (); 48 map.put("test", "test"); 49 InvocationHandler handler = new SimpleInvocationHandler(map); 50 Class proxyClass = Proxy.getProxyClass(TestProxy.class.getClassLoader(), new Class [] { Map .class }); 51 Map proxyMap = (Map ) proxyClass.getConstructor(new Class [] { InvocationHandler.class }). 52 newInstance(new Object [] { handler }); 53 assertEquals("proxy delegation not correct", 54 map.get("test"), proxyMap.get("test")); 55 } 56 57 public void testGetProxyInstance() throws Exception { 58 HashMap map = new HashMap (); 59 map.put("test", "test"); 60 InvocationHandler handler = new SimpleInvocationHandler(map); 61 Map proxyMap = (Map ) Proxy.newProxyInstance(TestProxy.class.getClassLoader(), new Class [] { Map .class }, handler); 62 assertEquals("proxy delegation not correct", map.get("test"), proxyMap.get("test")); 63 } 64 65 public void testIsProxyClass() throws Exception { 66 HashMap map = new HashMap (); 67 map.put("test", "test"); 68 InvocationHandler handler = new SimpleInvocationHandler(map); 69 Map proxyMap = (Map ) Proxy.newProxyInstance(TestProxy.class.getClassLoader(), new Class [] { Map .class }, handler); 70 assertTrue("real proxy not accepted", Proxy.isProxyClass(proxyMap.getClass())); 71 } 72 73 private class FakeProxy extends Proxy { 74 public FakeProxy(InvocationHandler ih) { 75 super(ih); 76 } 77 } 78 79 public void testIsNotProxyClass() throws Exception { 80 assertTrue("fake proxy accepted as real", 81 !Proxy.isProxyClass(FakeProxy.class)); 82 } 83 84 private static class ReturnNullHandler implements InvocationHandler { 85 public Object invoke(Object proxy, Method method, Object [] args) throws Throwable { 86 return null; 87 } 88 } 89 90 public void testReturnNull() throws Exception { 91 System.err.println("hello"); 92 ProxySampleInterface_ReturnsBasic rb = 93 (ProxySampleInterface_ReturnsBasic) 94 Proxy.newProxyInstance(null, 95 new Class []{ ProxySampleInterface_ReturnsBasic.class }, 96 new ReturnNullHandler()); 97 try { 98 int result = rb.getKala(11); 99 fail("must throw an exception, but returned " + result); 100 } catch (NullPointerException ignore) { } 101 } 102 103 public void testGetInvocationHandler() throws Exception { 104 HashMap map = new HashMap (); 105 map.put("test", "test"); 106 InvocationHandler handler = new InvocationHandler() { 107 public Object invoke(Object o, Method method, Object [] args) throws Exception { 108 throw new Exception ("test!"); 109 } 110 }; 111 Map proxyMap = (Map ) Proxy.newProxyInstance(TestProxy.class.getClassLoader(), new Class [] { Map .class }, handler); 112 assertSame("should be the same handler", handler, Proxy.getInvocationHandler(proxyMap)); 113 } 114 115 public void testException() throws Exception { 116 HashMap map = new HashMap (); 117 map.put("test", "test"); 118 InvocationHandler handler = new InvocationHandler() { 119 public Object invoke(Object o, Method method, Object [] args) throws Exception { 120 throw new Exception ("test!"); 121 } 122 }; 123 Map proxyMap = (Map ) Proxy.newProxyInstance(TestProxy.class.getClassLoader(), new Class [] { Map .class }, handler); 124 try { 125 proxyMap.get("test"); fail("proxy exception handling not correct, should throw exception"); 127 } catch (UndeclaredThrowableException e) { 128 System.out.println("exception: " + e); 129 } catch (Exception e) { 130 fail("proxy exception handling not correct, threw wrong exception: " + e); 131 } 132 } 133 134 public void testEquals() throws Exception { 135 final Object k1 = new Object (); 136 final Object k2 = new Object (); 137 InvocationHandler handler = new InvocationHandler() { 138 public Object invoke(Object o, Method method, Object [] args) throws Exception { 139 if (method.getName().equals("equals")) { 140 return (args[0] == k1) ? Boolean.TRUE : Boolean.FALSE; 141 } 142 return null; 143 } 144 }; 145 Object proxy = Proxy.newProxyInstance(TestProxy.class.getClassLoader(), new Class [] { Map .class }, handler); 146 assertTrue(proxy.equals(k1)); 147 assertTrue(!proxy.equals(k2)); 148 } 149 150 public TestProxy(String testName) { 151 super(testName); 152 } 153 154 public static void main(String [] args) { 155 junit.textui.TestRunner.run(suite()); 156 } 157 158 public static Test suite() { 159 return new TestSuite(TestProxy.class); 160 } 161 162 public void perform(ClassLoader loader) throws Throwable { 163 InvocationHandler handler = new InvocationHandler() { 164 public Object invoke(Object o, Method method, Object [] args) throws Exception { 165 throw new Exception ("test!"); 166 } 167 }; 168 Proxy.newProxyInstance(loader, new Class [] { Map .class }, handler); 169 170 } 171 172 public void testFailOnMemoryLeak() throws Throwable { 173 if(leaks()){ 174 fail("Memory Leak in Proxy"); 175 } 176 } 177 178 } 179 | Popular Tags |