1 22 23 package org.jboss.test.javassist.test; 24 25 26 import java.util.Set ; 27 import java.util.HashSet ; 28 import java.util.AbstractCollection ; 29 30 import javassist.util.proxy.ProxyFactory; 31 import junit.framework.TestCase; 32 33 import org.jboss.logging.Logger; 34 import org.jboss.test.javassist.test.support.ThingMethodHandler; 35 import org.jboss.test.javassist.test.support.AThing; 36 import org.jboss.test.javassist.test.support.IThing; 37 import org.jboss.test.javassist.test.support.AbstractThing; 38 import org.jboss.test.javassist.test.support.MyCollectionHandler; 39 40 46 public class DynProxyTestCase 47 extends TestCase 48 { 49 private static final Logger log = Logger.getLogger(DynProxyTestCase.class); 50 51 public DynProxyTestCase(String name) 52 { 53 super(name); 54 } 55 56 60 public void testInterfaceProxy() throws Exception 61 { 62 log.info("+++ testInterfaceProxy"); 63 ProxyFactory factory = new ProxyFactory(); 64 AThing athing = new AThing(); 65 ThingMethodHandler handler = new ThingMethodHandler(athing); 66 factory.setHandler(handler); 67 Class [] ifaces = {IThing.class}; 68 factory.setInterfaces(ifaces); 69 Class [] sig = {}; 70 Object [] args = {}; 71 IThing proxy = (IThing) factory.create(sig, args); 72 proxy.method1(); 73 assertEquals("method1Count", 1, athing.getMethod1Count()); 74 proxy.method2("testInterfaceProxy"); 75 assertEquals("method2Count", 1, athing.getMethod2Count()); 76 proxy.method3(proxy); 77 assertEquals("method3Count", 1, athing.getMethod3Count()); 78 assertEquals("method2Count", 2, athing.getMethod2Count()); 79 } 80 81 85 public void testAbstractProxy() throws Exception 86 { 87 log.info("+++ testAbstractProxy"); 88 ProxyFactory factory = new ProxyFactory(); 89 AThing athing = new AThing(); 90 ThingMethodHandler handler = new ThingMethodHandler(athing); 91 factory.setHandler(handler); 92 factory.setSuperclass(AbstractThing.class); 93 Class [] sig = {}; 94 Object [] args = {}; 95 AbstractThing proxy = (AbstractThing) factory.create(sig, args); 96 proxy.method1(); 97 assertEquals("method1Count", 1, athing.getMethod1Count()); 98 proxy.method2("testInterfaceProxy"); 99 assertEquals("method2Count", 1, athing.getMethod2Count()); 100 proxy.method3(athing); 101 assertEquals("method3Count", 1, athing.getMethod3Count()); 102 assertEquals("method2Count", 2, athing.getMethod2Count()); 103 } 104 105 111 public void testAbstractJDKClassProxy() throws Exception 112 { 113 log.info("+++ testAbstractJDKClassProxy"); 114 ProxyFactory factory = new ProxyFactory(); 115 HashSet aset = new HashSet (); 116 MyCollectionHandler handler = new MyCollectionHandler(aset); 117 factory.setHandler(handler); 118 factory.setSuperclass(java.util.AbstractCollection .class); 119 Class [] sig = {}; 120 Object [] args = {}; 121 AbstractCollection proxy = (AbstractCollection ) factory.create(sig, args); 122 proxy.add("Add"); 123 assertEquals("size", 1, aset.size()); 124 proxy.remove("Add"); 125 assertEquals("size", 0, aset.size()); 126 assertEquals("isEmpty", true, proxy.isEmpty()); 127 } 128 } 129 | Popular Tags |