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