From: Tom E. <tom...@jb...> - 2005-06-16 21:13:26
|
User: telrod Date: 05/06/16 17:12:49 Added: src/tests/org/jboss/test/remoting/transport/web ComplexObject.java WebInvocationHandler.java WebInvokerTestClient.java Log: JBREM-134 - updated to handle the adding and removing of multiple callback servers via locator urls. Also added code so will throw exception if callback listener removed and is not registered. Revision Changes Path 1.1 date: 2005/06/16 21:12:49; author: telrod; state: Exp;JBossRemoting/src/tests/org/jboss/test/remoting/transport/web/ComplexObject.java Index: ComplexObject.java =================================================================== /* * JBoss, the OpenSource J2EE webOS * * Distributable under LGPL license. * See terms of license at gnu.org. */ package org.jboss.test.remoting.transport.web; import java.io.Serializable; /** * @author <a href="mailto:tom...@jb...">Tom Elrod</a> */ public class ComplexObject implements Serializable { public int i = 42; public String s = "test"; public boolean b = true; public ComplexObject() { } public ComplexObject(int i, String s, boolean b) { this.i = i; this.s = s; this.b = b; } public boolean equals(Object o) { if(o instanceof ComplexObject) { ComplexObject co = (ComplexObject) o; if(co.i == i && co.s.equals(s) && co.b == b) { return true; } else { return false; } } else { return false; } } } 1.1 date: 2005/06/16 21:12:49; author: telrod; state: Exp;JBossRemoting/src/tests/org/jboss/test/remoting/transport/web/WebInvocationHandler.java Index: WebInvocationHandler.java =================================================================== /* * JBoss, the OpenSource J2EE webOS * * Distributable under LGPL license. * See terms of license at gnu.org. */ package org.jboss.test.remoting.transport.web; import javax.management.MBeanServer; import org.jboss.remoting.InvocationRequest; import org.jboss.remoting.InvokerCallbackHandler; import org.jboss.remoting.ServerInvocationHandler; import org.jboss.remoting.ServerInvoker; /** * @author <a href="mailto:tom...@jb...">Tom Elrod</a> */ public class WebInvocationHandler implements ServerInvocationHandler { // String to be returned from invocation handler upon client invocation calls. public static final String RESPONSE_VALUE = "This is the return to SampleInvocationHandler invocation"; public static final ComplexObject OBJECT_RESPONSE_VALUE = new ComplexObject(5, "dub", false); public static final String NULL_RETURN_PARAM = "return_null"; public static final String OBJECT_RETURN_PARAM = "return_object"; public static final String THROW_EXCEPTION_PARAM = "throw_exception"; public static final String STRING_RETURN_PARAM = "return_string"; public static final String HTML_PAGE_RESPONSE = "<html><head><title>Test HTML page</title></head><body>" + "<h1>HTTP/Servlet Test HTML page</h1><p>This is a simple page served for test." + "<p>Should show up in browser or via invoker client</body></html>"; /** * called to handle a specific invocation * * @param invocation * @return * @throws Throwable */ public Object invoke(InvocationRequest invocation) throws Throwable { // Print out the invocation request System.out.println("Invocation request is: " + invocation.getParameter()); if(NULL_RETURN_PARAM.equals(invocation.getParameter())) { return null; } else if(THROW_EXCEPTION_PARAM.equals(invocation.getParameter())) { throw new Exception("This is an exception being thrown as part of test case. It is intentional."); } else if(invocation.getParameter() instanceof ComplexObject) { return OBJECT_RESPONSE_VALUE; } else if(STRING_RETURN_PARAM.equals(invocation.getParameter())) { // Just going to return static string as this is just simple example code. return RESPONSE_VALUE; } else { return HTML_PAGE_RESPONSE; } } /** * Adds a callback handler that will listen for callbacks from * the server invoker handler. * * @param callbackHandler */ public void addListener(InvokerCallbackHandler callbackHandler) { // NO OP as do not handling callback listeners in this example } /** * Removes the callback handler that was listening for callbacks * from the server invoker handler. * * @param callbackHandler */ public void removeListener(InvokerCallbackHandler callbackHandler) { // NO OP as do not handling callback listeners in this example } /** * set the mbean server that the handler can reference * * @param server */ public void setMBeanServer(MBeanServer server) { // NO OP as do not need reference to MBeanServer for this handler } /** * set the invoker that owns this handler * * @param invoker */ public void setInvoker(ServerInvoker invoker) { // NO OP as do not need reference back to the server invoker } } 1.1 date: 2005/06/16 21:12:49; author: telrod; state: Exp;JBossRemoting/src/tests/org/jboss/test/remoting/transport/web/WebInvokerTestClient.java Index: WebInvokerTestClient.java =================================================================== /* * JBoss, the OpenSource J2EE webOS * * Distributable under LGPL license. * See terms of license at gnu.org. */ package org.jboss.test.remoting.transport.web; import java.util.HashMap; import java.util.Map; import java.util.Properties; import org.jboss.remoting.Client; import org.jboss.remoting.InvokerLocator; import org.jboss.remoting.transport.http.HTTPServerInvoker; import junit.framework.TestCase; /** * @author <a href="mailto:tom...@jb...">Tom Elrod</a> */ public abstract class WebInvokerTestClient extends TestCase { public abstract String getLocatorURI(); public void testInvocation() throws Exception { Client remotingClient = null; try { InvokerLocator locator = new InvokerLocator(getLocatorURI()); System.out.println("Calling remoting server with locator uri of: " + getLocatorURI()); remotingClient = new Client(locator); Map metadata = new HashMap(); metadata.put(Client.RAW, Boolean.TRUE); metadata.put("TYPE", "POST"); Properties headerProps = new Properties(); headerProps.put("Content-type", "application/soap+xml"); metadata.put("HEADER", headerProps); Object response = null; // test with null return expected response = remotingClient.invoke(WebInvocationHandler.NULL_RETURN_PARAM, metadata); System.out.println("First response should be null and was: " + response); assertNull(response); response = remotingClient.invoke("Do something", metadata); System.out.println("Second response should be " + WebInvocationHandler.HTML_PAGE_RESPONSE + " and was: " + response); assertEquals(WebInvocationHandler.HTML_PAGE_RESPONSE, response); headerProps.put("Content-type", HTTPServerInvoker.BINARY); response = remotingClient.invoke(new ComplexObject(2, "foo", true), metadata); System.out.println("Third response should be " + WebInvocationHandler.OBJECT_RESPONSE_VALUE + " and was: " + response); assertEquals(WebInvocationHandler.OBJECT_RESPONSE_VALUE, response); response = remotingClient.invoke(WebInvocationHandler.STRING_RETURN_PARAM, metadata); System.out.println("Fourth response should be " + WebInvocationHandler.RESPONSE_VALUE + " and was: " + response); assertEquals(WebInvocationHandler.RESPONSE_VALUE, response); try { response = remotingClient.invoke(WebInvocationHandler.THROW_EXCEPTION_PARAM, metadata); assertTrue("Should have received an exception.", false); } catch(Throwable throwable) { assertTrue("Received exception as expected", true); //throwable.printStackTrace(); } remotingClient.invokeOneway("Do something", metadata, true); remotingClient.invokeOneway("Do something", metadata, false); } catch(Throwable throwable) { throw new Exception(throwable); } finally { if(remotingClient != null) { remotingClient.disconnect(); } } } } |