1 package test.crispy; 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.impl.StaticLocalObjectProxy; 9 import net.sf.crispy.impl.StaticRmiProxy; 10 import net.sf.crispy.interceptor.StopWatchInterceptor; 11 import net.sf.crispy.proxy.DynamicProxyFactory; 12 import net.sf.crispy.proxy.InvocationHandler; 13 import net.sf.crispy.proxy.Proxy; 14 import net.sf.crispy.proxy.ProxyDecorator; 15 import net.sf.crispy.proxy.StaticProxyDecorator; 16 import net.sf.crispy.util.Invoker; 17 import test.crispy.example.interceptor.WaitInterceptor; 18 import test.crispy.example.service.Echo; 19 import test.crispy.example.service.EchoImpl; 20 21 public class ProxyDecoratorTest extends TestCase { 22 23 public void testNewInstance() throws Exception { 24 ProxyDecorator lvProxyDecorator = new ProxyDecorator(new StaticLocalObjectProxy(), new InterceptorHandler()); 25 Properties lvProperties = new Properties(); 26 lvProperties.put(Echo.class.getName(), EchoImpl.class.getName()); 27 lvProxyDecorator.setProperties(lvProperties); 28 Echo lvEcho = (Echo) lvProxyDecorator.newInstance(Echo.class); 29 assertNotNull(lvEcho); 30 String lvEchoStr = "echo"; 31 assertEquals(lvEcho.echo(lvEchoStr), lvEchoStr); 32 } 33 34 public void testInterceptorHandler() throws Exception { 35 InterceptorHandler lvHandler = new InterceptorHandler(); 36 lvHandler.addInterceptor(new WaitInterceptor()); 37 StopWatchInterceptor lvStopWatchInterceptor = new StopWatchInterceptor(); 38 lvHandler.addInterceptor(lvStopWatchInterceptor); 39 40 ProxyDecorator lvProxyDecorator = new ProxyDecorator(new StaticLocalObjectProxy(), lvHandler); 41 Properties lvProperties = new Properties(); 42 lvProperties.put(Echo.class.getName(), EchoImpl.class.getName()); 43 lvProxyDecorator.setProperties(lvProperties); 44 Object o = lvProxyDecorator.newInstance(Echo.class); 45 assertTrue(lvStopWatchInterceptor.getStopTimeNewInstance() > 0); 46 assertNotNull(o); 47 } 48 49 public void testInterceptorHandlerWithError() throws Exception { 50 InterceptorHandler lvHandler = new InterceptorHandler(); 51 lvHandler.addInterceptor(new WaitInterceptor(30)); 52 StopWatchInterceptor lvStopWatchInterceptor = new StopWatchInterceptor(); 53 lvHandler.addInterceptor(lvStopWatchInterceptor); 54 assertEquals(lvHandler.getInterceptorSize(), 2); 55 56 ProxyDecorator lvProxyDecorator = new ProxyDecorator(new StaticLocalObjectProxy(), lvHandler); 57 Properties lvProperties = new Properties(); 58 lvProperties.put(Echo.class.getName(), EchoImpl.class.getName()); 59 lvProxyDecorator.setProperties(lvProperties); 60 61 Echo echo = (Echo) lvProxyDecorator.newInstance(Echo.class); 62 63 assertTrue(lvStopWatchInterceptor.getStopTimeNewInstance() > 0); 64 assertNotNull(echo); 65 String lvEchoString = echo.echo("Hello Junit Echo"); 66 assertNotNull(lvEchoString); 67 68 assertNotNull(lvProxyDecorator.getProxyObject()); 69 assertNotNull(lvProxyDecorator.getProxy()); 70 assertNotNull(lvProxyDecorator.getProxyClass()); 71 assertNotNull(lvProxyDecorator.getProperties()); 72 } 73 74 75 public void testInvocationHandler() throws Exception { 76 InterceptorHandler lvHandler = new InterceptorHandler(); 77 lvHandler.addInterceptor(new WaitInterceptor(30)); 78 StopWatchInterceptor lvStopWatchInterceptor = new StopWatchInterceptor(); 79 lvHandler.addInterceptor(lvStopWatchInterceptor); 80 assertEquals(lvHandler.getInterceptorSize(), 2); 81 82 ProxyDecorator lvProxyDecorator = new ProxyDecorator(new StaticLocalObjectProxy(), lvHandler); 83 Properties lvProperties = new Properties(); 84 lvProperties.put(Echo.class.getName(), EchoImpl.class.getName()); 85 lvProxyDecorator.setProperties(lvProperties); 86 87 Object lvProxyObj = lvProxyDecorator.newInstance(Echo.class); 88 89 Method lvMethods[] = Invoker.findAllMethods(Echo.class, "renameKunde"); 90 try { 91 Object lvResult = InvocationHandler.doInvoke(DynamicProxyFactory.getDefaultDynamicProxy(), lvProxyObj, lvMethods[0], new Object [] {null, "Name"}, lvHandler); 92 fail("NullPointerException expected and not the result: " + lvResult); 93 } catch (Throwable e) { 94 assertTrue(true); 95 } 96 assertTrue(lvStopWatchInterceptor.getStopTimeInvokeMethod() > 0); 97 98 } 99 100 101 public void testStaticProxyDecorator() throws Exception { 102 StaticRmiProxy lvRmiProxy = new StaticRmiProxy(); 103 lvRmiProxy.setProxyClass(Echo.class); 104 EchoImpl lvEchoImpl = new EchoImpl(); 105 lvRmiProxy.setProxyObject(lvEchoImpl); 106 StaticProxyDecorator lvStaticProxyDecorator = new StaticProxyDecorator(lvRmiProxy); 107 108 assertEquals(lvStaticProxyDecorator.getProxyClass(), Echo.class); 109 assertEquals(lvStaticProxyDecorator.getProxyObject(), lvEchoImpl); 110 assertEquals(lvStaticProxyDecorator.getDefaultUrlAndPort(), StaticRmiProxy.DEFAULT_URL_AND_PORT); 111 } 112 113 public void testCreateInvocationStrategy () throws Exception { 114 Object lvInvocationStrategy = Proxy.createInvocationStrategy(null , 115 new Properties(), StaticRmiProxy.DEFAULT_URL_AND_PORT, 116 Echo.class, null); 117 assertNull(lvInvocationStrategy); 118 } 119 120 }