1 package test.crispy; 2 3 import java.lang.reflect.Method; 4 import java.util.HashMap; 5 import java.util.Map; 6 import java.util.Properties; 7 8 import junit.framework.TestCase; 9 import net.sf.crispy.ExecutorDecorator; 10 import net.sf.crispy.InvocationStrategy; 11 import net.sf.crispy.Property; 12 import net.sf.crispy.impl.JaxRpcExecutor; 13 import net.sf.crispy.impl.RestExecutor; 14 import net.sf.crispy.impl.RmiExecutor; 15 import net.sf.crispy.impl.XmlRpcExecutor; 16 import net.sf.crispy.strategy.NameSpacePlusMethodInvocationStrategy; 17 import net.sf.crispy.strategy.UrlPlusClassNameInvocationStrategy; 18 import test.crispy.example.service.Calculator; 19 import test.crispy.example.service.CalculatorImpl; 20 import test.crispy.example.service.EchoImpl; 21 22 public class ExecutorDecoratorTest extends TestCase { 23 24 public void testGetDefaultInvocationStrategy() throws Exception { 25 ExecutorDecorator lvExecutorDecorator = new ExecutorDecorator(new XmlRpcExecutor()); 26 InvocationStrategy lvDefaultInvocationStrategy = lvExecutorDecorator.getDefaultInvocationStrategy(); 27 assertTrue(lvDefaultInvocationStrategy instanceof NameSpacePlusMethodInvocationStrategy); 28 } 29 /** Test DefaultInvocationStrategy by XmlRpcExecutor. */ 30 public void testInvocationStrategy() throws Exception { 31 ExecutorDecorator lvExecutorDecorator = new ExecutorDecorator(new XmlRpcExecutor()); 32 Method lvMethodAdd = Calculator.class.getMethod("add", new Class[] {int.class, int.class}); 33 String lvInvocationStrategyString = (String) lvExecutorDecorator.createInvocationStrategy(Calculator.class, lvMethodAdd); 34 35 Map lvMap = new HashMap(); 36 lvMap.put(InvocationStrategy.CLASS_NAME, Calculator.class.getName()); 37 lvMap.put(InvocationStrategy.METHOD_NAME, lvMethodAdd.getName()); 38 NameSpacePlusMethodInvocationStrategy lvNameSpacePlusMethodInvocationStrategy = new NameSpacePlusMethodInvocationStrategy(); 39 String lvInvocationStrategyString_2 = (String) lvNameSpacePlusMethodInvocationStrategy.convert(lvMap); 40 41 assertEquals(lvInvocationStrategyString, lvInvocationStrategyString_2); 42 } 43 44 /** Test default invocation string by XmlRpcExecutor: class + '.' + method_name */ 45 public void testInvocationStrategyString() throws Exception { 46 ExecutorDecorator lvExecutorDecorator = new ExecutorDecorator(new XmlRpcExecutor()); 47 Method lvMethod = Calculator.class.getMethod("add", new Class[]{int.class, int.class}); 48 String lvInvocationStrategyStr = lvExecutorDecorator.createInvocationStrategy(Calculator.class, lvMethod).toString(); 49 assertEquals(lvInvocationStrategyStr, Calculator.class.getName() + ".add"); 50 } 51 52 /** Test invocation string by XmlRpcExecutor with namespace: namespace + '.' + method_name */ 53 public void testInvocationStrategyNameSpaceString() throws Exception { 54 ExecutorDecorator lvExecutorDecorator = new ExecutorDecorator(new XmlRpcExecutor()); 55 Properties lvProperties = new Properties(); 56 lvProperties.put(Property.INVOCATION_STRATEGY_NAMESPACE, "mynamespace"); 57 lvExecutorDecorator.setProperties(lvProperties); 58 Method lvMethod = Calculator.class.getMethod("add", new Class[]{int.class, int.class}); 59 String lvInvocationStrategyStr = lvExecutorDecorator.createInvocationStrategy(Calculator.class, lvMethod).toString(); 60 assertEquals(lvInvocationStrategyStr, "mynamespace.add"); 61 } 62 63 /** Test default InvocationStrategy overwrite with UrlPlusClassNameInvocationStrategy. */ 64 public void testSetInvocationStrategyUrlPlusClassNameWithProperties() throws Exception { 65 ExecutorDecorator lvExecutorDecorator = new ExecutorDecorator(new XmlRpcExecutor()); 66 Properties lvProperties = new Properties(); 67 String lvUrl = "http://localhost:8080/axis/services"; 68 lvProperties.put(Property.REMOTE_URL_AND_PORT, lvUrl); 69 lvProperties.put(Property.INVOCATION_STRATEGY, UrlPlusClassNameInvocationStrategy.class.getName()); 70 lvExecutorDecorator.setProperties(lvProperties); 71 Method lvMethod = Calculator.class.getMethod("add", new Class[]{int.class, int.class}); 72 String lvInvocationStrategyStr = lvExecutorDecorator.createInvocationStrategy(Calculator.class, lvMethod).toString(); 73 assertEquals(lvInvocationStrategyStr, lvUrl + "/Calculator"); 74 } 75 76 /** Test invocation string by JaxRpcExecutor with url: url + '/' + class_name */ 77 public void testInvocationStrategyUrlPlusClassName() throws Exception { 78 ExecutorDecorator lvExecutorDecorator = new ExecutorDecorator(new JaxRpcExecutor()); 79 Properties lvProperties = new Properties(); 80 String lvUrl = "http://localhost:8080/axis/services"; 81 lvProperties.put(Property.REMOTE_URL_AND_PORT, lvUrl); 82 lvExecutorDecorator.setProperties(lvProperties); 83 Method lvMethod = Calculator.class.getMethod("add", new Class[]{int.class, int.class}); 84 String lvInvocationStrategyStr = lvExecutorDecorator.createInvocationStrategy(Calculator.class, lvMethod).toString(); 85 assertEquals(lvInvocationStrategyStr, lvUrl + "/Calculator"); 86 } 87 88 /** Test invocation string by RestExecutor with url: url + '/' + class_name */ 89 public void testInvocationStrategyRest() throws Exception { 90 ExecutorDecorator lvExecutorDecorator = new ExecutorDecorator(new RestExecutor()); 91 Properties lvProperties = new Properties(); 92 String lvUrl = "http://localhost:8092/crispy/rest"; 93 lvProperties.put(Property.REMOTE_URL_AND_PORT, lvUrl); 94 lvExecutorDecorator.setProperties(lvProperties); 95 Method lvMethod = Calculator.class.getMethod("add", new Class[]{int.class, int.class}); 96 String lvInvocationStrategyStr = lvExecutorDecorator.createInvocationStrategy(Calculator.class, lvMethod).toString(); 97 assertEquals(lvInvocationStrategyStr, lvUrl + "?class=test.crispy.example.service.Calculator&method=add"); 98 99 lvProperties.put(Property.INVOCATION_STRATEGY_NAMESPACE, "service"); 100 lvExecutorDecorator.setProperties(lvProperties); 101 lvMethod = Calculator.class.getMethod("add", new Class[]{int.class, int.class}); 102 lvInvocationStrategyStr = lvExecutorDecorator.createInvocationStrategy(Calculator.class, lvMethod).toString(); 103 assertEquals(lvInvocationStrategyStr, lvUrl + "?method=service.calculator.add"); 104 } 105 106 /** RmiExecutor has no InvocationStrategy, return value is null. */ 107 public void testWithoutDefaultStrategy() throws Exception { 108 ExecutorDecorator lvExecutorDecorator = new ExecutorDecorator(new RmiExecutor()); 109 Method lvMethod = Calculator.class.getMethod("add", new Class[]{int.class, int.class}); 110 Object lvInvocationStrategy = lvExecutorDecorator.createInvocationStrategy(Calculator.class, lvMethod); 111 assertNull(lvInvocationStrategy); 112 } 113 114 /** Invoke a not valide remote method, how toString or getClass (java.lang.Object-method)*/ 115 public void testExecuteNotRemoteMethod() throws Exception { 116 ExecutorDecorator lvExecutorDecorator = new ExecutorDecorator(new XmlRpcExecutor()); 117 Method lvMethod = CalculatorImpl.class.getMethod("toString", null); 118 Object lvResult = lvExecutorDecorator.execute(Calculator.class, new CalculatorImpl(), lvMethod, null); 119 assertEquals(lvResult.toString(), "toString: test.crispy.example.service.Calculator"); 120 121 lvMethod = CalculatorImpl.class.getMethod("getClass", null); 122 lvResult = lvExecutorDecorator.execute(Calculator.class, new CalculatorImpl(), lvMethod, null); 123 assertEquals(lvResult.toString(), Calculator.class.toString()); 124 } 125 126 public void testExecuteNotRemoteMethod2() throws Exception { 127 Object lvResult = ExecutorDecorator.notRemoteMethod(EchoImpl.class.getMethod("wait", new Class[] { long.class }), EchoImpl.class); 128 assertNull(lvResult); 129 130 lvResult = ExecutorDecorator.notRemoteMethod(EchoImpl.class.getMethod("notify", null), EchoImpl.class); 131 assertNull(lvResult); 132 } 133 }