1 7 package org.jboss.test.remoting.transport.local; 8 9 import java.util.ArrayList ; 10 import java.util.Iterator ; 11 import java.util.List ; 12 import javax.management.MBeanServer ; 13 import org.jboss.remoting.Client; 14 import org.jboss.remoting.InvocationRequest; 15 import org.jboss.remoting.InvokerLocator; 16 import org.jboss.remoting.ServerInvocationHandler; 17 import org.jboss.remoting.ServerInvoker; 18 import org.jboss.remoting.callback.Callback; 19 import org.jboss.remoting.callback.InvokerCallbackHandler; 20 import org.jboss.remoting.transport.Connector; 21 22 import junit.framework.TestCase; 23 24 30 public class LocalInvocationTestCase extends TestCase 31 { 32 private static final String RESPONSE_VALUE = "This is the return to SampleInvocationHandler invocation"; 33 34 public LocalInvocationTestCase(String name) 35 { 36 super(name); 37 } 38 39 public static void setupConfiguration(InvokerLocator locator, ServerInvocationHandler invocationHandler) throws Exception 40 { 41 Connector connector = new Connector(); 42 connector.setInvokerLocator(locator.getLocatorURI()); 43 connector.start(); 44 connector.addInvocationHandler("mock", invocationHandler); 45 } 46 47 public void testInvocation() throws Throwable 48 { 49 InvokerLocator locator = new InvokerLocator("socket://localhost:6789"); 50 SampleInvocationHandler invocationHandler = new SampleInvocationHandler(); 51 52 LocalInvocationTestCase.setupConfiguration(locator, invocationHandler); 54 55 Client remotingClient = new Client(locator, null); 57 Object response = remotingClient.invoke("Do something", null); 58 59 System.out.println("Invocation response: " + response); 60 assertEquals(response, RESPONSE_VALUE); 61 } 62 63 public static class SampleInvocationHandler implements ServerInvocationHandler 64 { 65 private List listeners = new ArrayList (); 66 67 72 public void setMBeanServer(MBeanServer server) 73 { 74 } 76 77 82 public void setInvoker(ServerInvoker invoker) 83 { 84 } 86 87 94 public Object invoke(InvocationRequest invocation) throws Throwable 95 { 96 98 Callback callbackInvocationRequest = new Callback("This is the payload of callback invocation"); 101 Iterator itr = listeners.iterator(); 102 while(itr.hasNext()) 103 { 104 InvokerCallbackHandler callbackHandler = (InvokerCallbackHandler) itr.next(); 105 callbackHandler.handleCallback(callbackInvocationRequest); 106 } 107 108 return RESPONSE_VALUE; 109 110 } 111 112 118 public void addListener(InvokerCallbackHandler callbackHandler) 119 { 120 listeners.add(callbackHandler); 121 } 122 123 129 public void removeListener(InvokerCallbackHandler callbackHandler) 130 { 131 listeners.remove(callbackHandler); 132 } 133 } 134 } | Popular Tags |