Author: ron.sigal(a)jboss.com
Date: 2010-07-31 22:32:35 -0400 (Sat, 31 Jul 2010)
New Revision: 5947
Added:
remoting2/branches/2.x/src/tests/org/jboss/test/remoting/transport/bisocket/configuration/BisocketCallbackSocketConfigurationTestCase.java
Log:
JBREM-1226: New unit test.
Added:
remoting2/branches/2.x/src/tests/org/jboss/test/remoting/transport/bisocket/configuration/BisocketCallbackSocketConfigurationTestCase.java
===================================================================
---
remoting2/branches/2.x/src/tests/org/jboss/test/remoting/transport/bisocket/configuration/BisocketCallbackSocketConfigurationTestCase.java
(rev 0)
+++
remoting2/branches/2.x/src/tests/org/jboss/test/remoting/transport/bisocket/configuration/BisocketCallbackSocketConfigurationTestCase.java 2010-08-01
02:32:35 UTC (rev 5947)
@@ -0,0 +1,291 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.test.remoting.transport.bisocket.configuration;
+
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.net.SocketException;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+
+import javax.management.MBeanServer;
+import javax.net.ServerSocketFactory;
+
+import junit.framework.TestCase;
+
+import org.apache.log4j.ConsoleAppender;
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import org.apache.log4j.PatternLayout;
+import org.jboss.remoting.Client;
+import org.jboss.remoting.InvocationRequest;
+import org.jboss.remoting.InvokerLocator;
+import org.jboss.remoting.Remoting;
+import org.jboss.remoting.ServerInvocationHandler;
+import org.jboss.remoting.ServerInvoker;
+import org.jboss.remoting.callback.Callback;
+import org.jboss.remoting.callback.HandleCallbackException;
+import org.jboss.remoting.callback.InvokerCallbackHandler;
+import org.jboss.remoting.transport.Connector;
+import org.jboss.remoting.transport.PortUtil;
+
+
+/**
+ * Unit test for JBREM-1226.
+ *
+ * @author <a href="mailto:ron.sigal@jboss.com">Ron Sigal</a>
+ * @version $Rev$
+ * <p>
+ * Copyright Jul 31, 2010
+ * </p>
+ */
+public class BisocketCallbackSocketConfigurationTestCase extends TestCase
+{
+ private static Logger log =
Logger.getLogger(BisocketCallbackSocketConfigurationTestCase.class);
+
+ protected static final boolean REUSE_ADDRESS = false;
+ protected static final boolean KEEP_ALIVE = true;
+ protected static final boolean OOBINLINE = true;
+ protected static final int RECEIVE_BUFFER_SIZE = 10222;
+ protected static final int SEND_BUFFER_SIZE = 10333;
+ protected static final boolean SO_LINGER = true;
+ protected static final int SO_LINGER_DURATION = 444;
+ protected static final int TRAFFIC_CLASS = 5;
+
+ private static boolean firstTime = true;
+
+ protected String host;
+ protected int port;
+ protected String locatorURI;
+ protected InvokerLocator serverLocator;
+ protected Connector connector;
+ protected TestInvocationHandler invocationHandler;
+
+
+ public void setUp() throws Exception
+ {
+ if (firstTime)
+ {
+ firstTime = false;
+ Logger.getLogger("org.jboss.remoting").setLevel(Level.TRACE);
+ Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
+ String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
+ PatternLayout layout = new PatternLayout(pattern);
+ ConsoleAppender consoleAppender = new ConsoleAppender(layout);
+ Logger.getRootLogger().addAppender(consoleAppender);
+ }
+ }
+
+
+ public void tearDown()
+ {
+ }
+
+
+ public void testCalllbackSocketConfiguration() throws Throwable
+ {
+ log.info("entering " + getName());
+
+ // Start server.
+ setupServer();
+
+ // Create client.
+ InvokerLocator clientLocator = new InvokerLocator(locatorURI);
+ HashMap clientConfig = new HashMap();
+ clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
+ addExtraClientConfig(clientConfig);
+ Client client = new Client(clientLocator, clientConfig);
+ client.connect();
+ log.info("client is connected");
+
+ // Test connection.
+ assertEquals("abc", client.invoke("abc"));
+ log.info("connection is good");
+
+ // Set up callbacks.
+ TestCallbackHandler callbackHandler = new TestCallbackHandler();
+ client.addListener(callbackHandler, new HashMap());
+ client.invoke("callback");
+ assertEquals(1, callbackHandler.counter);
+
+ // Verify socket parameters are set properly.
+ log.info("number of sockets: " + TestServerSocket.sockets.size());
+ for (Iterator it = TestServerSocket.sockets.iterator(); it.hasNext(); )
+ {
+ Socket s = (Socket) it.next();
+ assertEquals(REUSE_ADDRESS, s.getReuseAddress());
+ assertEquals(KEEP_ALIVE, s.getKeepAlive());
+ assertEquals(OOBINLINE, s.getOOBInline());
+ assertEquals(RECEIVE_BUFFER_SIZE, s.getReceiveBufferSize());
+ assertEquals(SEND_BUFFER_SIZE, s.getSendBufferSize());
+ assertEquals(SO_LINGER_DURATION, s.getSoLinger());
+ assertEquals(TRAFFIC_CLASS, s.getTrafficClass());
+ }
+
+ client.removeListener(callbackHandler);
+ client.disconnect();
+ shutdownServer();
+ log.info(getName() + " PASSES");
+ }
+
+
+ protected String getTransport()
+ {
+ return "bisocket";
+ }
+
+
+ protected void addExtraClientConfig(Map config) {}
+ protected void addExtraServerConfig(Map config) {}
+
+
+ protected void setupServer() throws Exception
+ {
+ host = InetAddress.getLocalHost().getHostAddress();
+ port = PortUtil.findFreePort(host);
+ locatorURI = getTransport() + "://" + host + ":" + port;
+ String metadata = System.getProperty("remoting.metadata");
+ if (metadata != null)
+ {
+ locatorURI += "/?" + metadata;
+ }
+ serverLocator = new InvokerLocator(locatorURI);
+ log.info("Starting remoting server with locator uri of: " + locatorURI);
+ HashMap config = new HashMap();
+ config.put(InvokerLocator.FORCE_REMOTE, "true");
+ config.put(Remoting.CUSTOM_SERVER_SOCKET_FACTORY, new TestServerSocketFactory());
+ config.put("reuseAddress", new Boolean(REUSE_ADDRESS).toString());
+ config.put("keepAlive", new Boolean(KEEP_ALIVE).toString());
+ config.put("oOBInline", new Boolean(OOBINLINE).toString());
+ config.put("receiveBufferSize", new
Integer(RECEIVE_BUFFER_SIZE).toString());
+ config.put("sendBufferSize", new Integer(SEND_BUFFER_SIZE).toString());
+ config.put("soLinger", new Boolean(SO_LINGER).toString());
+ config.put("soLingerDuration", new
Integer(SO_LINGER_DURATION).toString());
+ config.put("trafficClass", new Integer(TRAFFIC_CLASS).toString());
+ addExtraServerConfig(config);
+ connector = new Connector(serverLocator, config);
+ connector.create();
+ invocationHandler = new TestInvocationHandler();
+ connector.addInvocationHandler("test", invocationHandler);
+ connector.start();
+ }
+
+
+ protected void shutdownServer() throws Exception
+ {
+ if (connector != null)
+ connector.stop();
+ }
+
+
+ static class TestInvocationHandler implements ServerInvocationHandler
+ {
+ InvokerCallbackHandler callbackHandler;
+
+ public void addListener(InvokerCallbackHandler callbackHandler)
+ {
+ this.callbackHandler = callbackHandler;
+ }
+ public Object invoke(final InvocationRequest invocation) throws Throwable
+ {
+ Object parameter = invocation.getParameter();
+ log.info(this + " received parameter: " + parameter);
+ if ("callback".equals(parameter))
+ {
+ callbackHandler.handleCallback(new Callback("callback"));
+ log.info(this + " sent callback");
+ }
+ return parameter;
+ }
+ public void removeListener(InvokerCallbackHandler callbackHandler) {}
+ public void setMBeanServer(MBeanServer server) {}
+ public void setInvoker(ServerInvoker invoker) {}
+ }
+
+
+ static class TestCallbackHandler implements InvokerCallbackHandler
+ {
+ public int counter;
+
+ public void handleCallback(Callback callback) throws HandleCallbackException
+ {
+ counter++;
+ log.info("received callback");
+ }
+ }
+
+
+ static class TestServerSocketFactory extends ServerSocketFactory
+ {
+ public ServerSocket createServerSocket() throws IOException
+ {
+ return new TestServerSocket();
+ }
+ public ServerSocket createServerSocket(int arg0) throws IOException
+ {
+ return new TestServerSocket(arg0);
+ }
+ public ServerSocket createServerSocket(int arg0, int arg1) throws IOException
+ {
+ return new TestServerSocket(arg0, arg1);
+ }
+ public ServerSocket createServerSocket(int arg0, int arg1, InetAddress arg2) throws
IOException
+ {
+ return new TestServerSocket(arg0, arg1, arg2);
+ }
+ }
+
+
+ static class TestServerSocket extends ServerSocket
+ {
+ static public Set sockets = new HashSet();
+
+ public TestServerSocket() throws IOException
+ {
+ super();
+ }
+ public TestServerSocket(int port) throws IOException
+ {
+ super(port);
+ }
+ public TestServerSocket(int port, int backlog) throws IOException
+ {
+ super(port, backlog);
+ }
+ public TestServerSocket(int port, int backlog, InetAddress bindAddr) throws
IOException
+ {
+ super(port, backlog, bindAddr);
+ }
+ public Socket accept() throws IOException
+ {
+ Socket s = super.accept();
+ sockets.add(s);
+ log.info(this + " added " + s + " to sockets set");
+ return s;
+ }
+ }
+}
\ No newline at end of file