1 package test.crispy.impl.proxy; 2 3 import java.lang.reflect.Method; 4 import java.util.Properties; 5 6 import junit.framework.TestCase; 7 import net.sf.crispy.InterceptorHandler; 8 import net.sf.crispy.InvocationStrategy; 9 import net.sf.crispy.Property; 10 import net.sf.crispy.concurrent.AsynchronousCallback; 11 import net.sf.crispy.proxy.CommonsProxyFactory; 12 import net.sf.crispy.proxy.DynamicJdkProxy; 13 import net.sf.crispy.proxy.DynamicProxy; 14 import net.sf.crispy.proxy.DynamicProxyFactory; 15 import net.sf.crispy.proxy.Proxy; 16 import net.sf.crispy.strategy.NameSpacePlusMethodInvocationStrategy; 17 import net.sf.crispy.util.Invoker; 18 19 import org.apache.commons.proxy.factory.cglib.CglibProxyFactory; 20 import org.apache.commons.proxy.factory.javassist.JavassistProxyFactory; 21 22 import test.crispy.concurrent.SystemOutAsynchronousCallback; 23 import test.crispy.example.service.Echo; 24 import test.crispy.example.service.EchoImpl; 25 26 public class DynamicProxyTest extends TestCase { 27 28 public void testCglibProxy() throws Exception { 29 DynamicProxy lvDynamicProxy = DynamicProxyFactory.createDynamicProxy(Property.VALUE_FOR_CGLIB_DYNAMIC_PROXY); 30 assertTrue(lvDynamicProxy instanceof CommonsProxyFactory); 31 assertEquals(((CommonsProxyFactory) lvDynamicProxy).getProxyFactory().getClass(), CglibProxyFactory.class); 32 33 lvDynamicProxy = DynamicProxyFactory.createDynamicProxy("net.sf.crispy.impl.DynamicCglibProxy"); 34 assertEquals(((CommonsProxyFactory) lvDynamicProxy).getProxyFactory().getClass(), CglibProxyFactory.class); 35 } 36 37 public void testJdkProxy() throws Exception { 38 DynamicProxy lvDynamicProxy = DynamicProxyFactory.createDynamicProxy(Property.VALUE_FOR_JDK_DYNAMIC_PROXY); 39 assertEquals(lvDynamicProxy.getClass(), DynamicJdkProxy.class); 40 41 lvDynamicProxy = DynamicProxyFactory.createDynamicProxy("net.sf.crispy.impl.DynamicJdkProxy"); 42 assertEquals(lvDynamicProxy.getClass(), DynamicJdkProxy.class); 43 44 lvDynamicProxy = DynamicProxyFactory.createDynamicProxy(null); 45 assertEquals(lvDynamicProxy.getClass(), DynamicJdkProxy.class); 46 47 lvDynamicProxy = DynamicProxyFactory.getDefaultDynamicProxy(); 48 assertEquals(lvDynamicProxy.getClass(), DynamicJdkProxy.class); 49 } 50 51 public void testJavassistProxy() throws Exception { 52 DynamicProxy lvDynamicProxy = DynamicProxyFactory.createDynamicProxy(Property.VALUE_FOR_JAVASSIST_DYNAMIC_PROXY); 53 assertEquals(((CommonsProxyFactory) lvDynamicProxy).getProxyFactory().getClass(), JavassistProxyFactory.class); 54 55 lvDynamicProxy = DynamicProxyFactory.createDynamicProxy(JavassistProxyFactory.class.getName()); 56 assertEquals(((CommonsProxyFactory) lvDynamicProxy).getProxyFactory().getClass(), JavassistProxyFactory.class); 57 } 58 59 public void testNewInstance() throws Exception { 60 DynamicProxy lvDynamicProxy = DynamicProxyFactory.createDynamicProxy(Property.VALUE_FOR_JDK_DYNAMIC_PROXY); 61 Echo lvEcho = (Echo) lvDynamicProxy.newInstance(Echo.class); 62 assertNotNull(lvEcho); 63 assertEquals("toString: " + Echo.class.getName(), lvEcho.toString()); 64 } 65 66 public void testProperties() throws Exception { 67 DynamicProxy lvDynamicProxy = DynamicProxyFactory.createDynamicProxy(Property.VALUE_FOR_JDK_DYNAMIC_PROXY); 68 assertNull(lvDynamicProxy.getUser()); 69 assertNull(lvDynamicProxy.getPasswd()); 70 71 lvDynamicProxy.getProperties().put(Property.SECURITY_USER, "user"); 72 lvDynamicProxy.getProperties().put(Property.SECURITY_PASSWD, "passwd"); 73 assertEquals(lvDynamicProxy.getUser(), "user"); 74 assertEquals(lvDynamicProxy.getPasswd(), "passwd"); 75 76 } 77 78 public void testCommonsProxyFactoryCreateDynamicProxy() throws Exception { 79 DynamicProxy lvDynamicProxy = new CommonsProxyFactory().createDynamicProxy(null); 80 assertTrue(lvDynamicProxy instanceof CommonsProxyFactory); 81 CommonsProxyFactory lvCommonsProxyFactory = (CommonsProxyFactory) lvDynamicProxy; 82 lvCommonsProxyFactory.setProxyClass(Echo.class); 83 lvCommonsProxyFactory.setInterceptorHandler(new InterceptorHandler ()); 84 Method lvMethods[] = Invoker.findAllMethods(EchoImpl.class, "toString"); 85 try { 86 Object lvResult = lvCommonsProxyFactory.invoke(null, lvMethods[0], new Object[0]); 87 assertEquals("toString: test.crispy.example.service.Echo", lvResult); 88 } catch (Throwable e) { 89 e.printStackTrace(); 90 } 91 } 92 93 public void testCreateAsynchronousCallback() throws Exception { 94 Properties lvProperties = null; 95 AsynchronousCallback lvAsynchronousCallback = DynamicProxyFactory.createAsynchronousCallback(lvProperties); 96 assertNull(lvAsynchronousCallback); 97 98 lvProperties = new Properties(); 99 lvAsynchronousCallback = DynamicProxyFactory.createAsynchronousCallback(lvProperties); 100 assertNull(lvAsynchronousCallback); 101 102 lvProperties.put(Property.ASYNCHRONOUS_CALLBACK_CLASS, SystemOutAsynchronousCallback.class.getName()); 103 lvAsynchronousCallback = DynamicProxyFactory.createAsynchronousCallback(lvProperties); 104 assertNotNull(lvAsynchronousCallback); 105 assertTrue(lvAsynchronousCallback instanceof SystemOutAsynchronousCallback); 106 } 107 108 public void testCreateAsynchronousCallbackWithNotValidClassString() throws Exception { 109 String lvNotValidClassString = "NotValidAsynchronousCallbackString"; 110 Properties lvProperties = new Properties(); 111 lvProperties.put(Property.ASYNCHRONOUS_CALLBACK_CLASS, lvNotValidClassString); 112 try { 113 DynamicProxyFactory.createAsynchronousCallback(lvProperties); 114 fail(lvNotValidClassString + " is not a valid AsynchronousCallback-Class"); 115 } catch (Exception e) { 116 assertTrue(true); 117 } 118 } 119 120 public void testCreateAsynchronousCallbackWithNotValidAsynchronousCallbackString() throws Exception { 121 String lvNotValidAsynchronousCallbackString = EchoImpl.class.getName(); 122 Properties lvProperties = new Properties(); 123 lvProperties.put(Property.ASYNCHRONOUS_CALLBACK_CLASS, lvNotValidAsynchronousCallbackString); 124 try { 125 DynamicProxyFactory.createAsynchronousCallback(lvProperties); 126 fail(lvNotValidAsynchronousCallbackString + " is not a valid AsynchronousCallback-Class"); 127 } catch (Exception e) { 128 assertTrue(true); 129 } 130 } 131 132 public void testMaxSizeOfThreads() throws Exception { 133 Properties lvProperties = null; 134 int lvMaxSizeOfThreads = DynamicProxyFactory.getMaxSizeOfThreads(lvProperties); 135 assertEquals(0, lvMaxSizeOfThreads); 136 137 lvProperties = new Properties(); 138 lvMaxSizeOfThreads = DynamicProxyFactory.getMaxSizeOfThreads(lvProperties); 139 assertEquals(0, lvMaxSizeOfThreads); 140 141 lvProperties.put(Property.ASYNCHRONOUS_MAX_SIZE_OF_THREADS, "3"); 142 lvMaxSizeOfThreads = DynamicProxyFactory.getMaxSizeOfThreads(lvProperties); 143 assertEquals(3, lvMaxSizeOfThreads); 144 145 try { 146 lvProperties.put(Property.ASYNCHRONOUS_MAX_SIZE_OF_THREADS, "abc"); 147 DynamicProxyFactory.getMaxSizeOfThreads(lvProperties); 148 fail("The ASYNCHRONOUS_MAX_SIZE_OF_THREADS value must be a digit"); 149 } catch (Exception e) { 150 assertTrue(true); 151 } 152 } 153 154 public void testMaxThreads() throws Exception { 155 DynamicProxy lvDynamicProxy = DynamicProxyFactory.getDefaultDynamicProxy(); 156 assertEquals(0, lvDynamicProxy.getMaxThreads()); 157 lvDynamicProxy.setAsynchronousCallback(null, null, 3); 158 assertEquals(3, lvDynamicProxy.getMaxThreads()); 159 } 160 161 public void testIsMethodInFilter() throws Exception { 162 DynamicProxy lvDynamicProxy = DynamicProxyFactory.getDefaultDynamicProxy(); 163 boolean lvIsMethodInFilter = lvDynamicProxy.isMethodInFilter(null); 164 assertFalse(lvIsMethodInFilter); 165 166 lvDynamicProxy.setAsynchronousCallback(null, new String [] {"echo", "ping" }, 0); 167 lvIsMethodInFilter = lvDynamicProxy.isMethodInFilter("echo"); 168 assertTrue(lvIsMethodInFilter); 169 170 lvIsMethodInFilter = lvDynamicProxy.isMethodInFilter("echoFalse"); 171 assertFalse(lvIsMethodInFilter); 172 } 173 174 public void testProxyCreateInvocationStrategy () throws Exception { 175 InvocationStrategy lvInvocationStrategy = new NameSpacePlusMethodInvocationStrategy(); 176 Properties lvProperties = new Properties(); 177 lvProperties.put(Property.REMOTE_URL_AND_PORT, "http://localhost:1234"); 178 Object o = Proxy.createInvocationStrategy(lvInvocationStrategy, lvProperties, null, Echo.class, Echo.class.getMethod("ping", new Class[0])); 179 assertNotNull(o); 180 assertEquals("test.crispy.example.service.Echo.ping", o); 181 } 182 183 public void testProxyCreateInvocationStrategyWithException() throws Exception { 184 Properties lvProperties = new Properties(); 185 lvProperties.put(Property.REMOTE_URL_AND_PORT, "http://localhost:1234"); 186 lvProperties.put(Property.INVOCATION_STRATEGY, "java.util.Vector"); 187 try { 188 Proxy.createInvocationStrategy(null, lvProperties, null, Echo.class, Echo.class.getMethod("ping", new Class[0])); 189 fail("Must thrown Exception, java.util.Vector is not a InvocationStrategy!"); 190 } catch (Exception e) { 191 assertTrue(true); 192 } 193 } 194 }