[jboss-cvs] JBossRemoting/src/tests/org/jboss/test/remoting/socketfactory ...

Ron Sigal ron_sigal at yahoo.com
Tue Jan 16 01:25:52 EST 2007


  User: rsigal  
  Date: 07/01/16 01:25:52

  Added:       src/tests/org/jboss/test/remoting/socketfactory   Tag:
                        remoting_2_x SSLCreationListenerTestRoot.java
                        CreationListenerTestRoot.java
  Log:
  JBREM-298: Added unit tests for socket creation listeners.
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.1.2.1   +60 -0     JBossRemoting/src/tests/org/jboss/test/remoting/socketfactory/Attic/SSLCreationListenerTestRoot.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: SSLCreationListenerTestRoot.java
  ===================================================================
  RCS file: SSLCreationListenerTestRoot.java
  diff -N SSLCreationListenerTestRoot.java
  --- /dev/null	1 Jan 1970 00:00:00 -0000
  +++ SSLCreationListenerTestRoot.java	16 Jan 2007 06:25:52 -0000	1.1.2.1
  @@ -0,0 +1,60 @@
  +/*
  +* JBoss, Home of Professional Open Source
  +* Copyright 2005, 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.socketfactory;
  +
  +import javax.net.ServerSocketFactory;
  +import javax.net.SocketFactory;
  +import javax.net.ssl.SSLServerSocketFactory;
  +import javax.net.ssl.SSLSocketFactory;
  +
  +/** 
  + * @author <a href="ron.sigal at jboss.com">Ron Sigal</a>
  + * @version $Revision: 1.1.2.1 $
  + * <p>
  + * Copyright Jan 12, 2007
  + * </p>
  + */
  +public abstract class SSLCreationListenerTestRoot extends CreationListenerTestRoot
  +{  
  +   public void setUp() throws Exception
  +   {
  +      super.setUp();
  +      String trustStoreFilePath = this.getClass().getResource("../.truststore").getFile();
  +      System.setProperty("javax.net.ssl.trustStore", trustStoreFilePath);
  +      System.setProperty("javax.net.ssl.trustStorePassword", "unit-tests-client");
  +      String keyStoreFilePath = this.getClass().getResource("../.keystore").getFile();
  +      System.setProperty("javax.net.ssl.keyStore", keyStoreFilePath);
  +      System.setProperty("javax.net.ssl.keyStorePassword", "unit-tests-server");
  +      System.setProperty("org.jboss.security.ignoreHttpsHost", "true");
  +   }
  +   
  +   protected SocketFactory getSocketFactory()
  +   {
  +      return SSLSocketFactory.getDefault();
  +   }
  +   
  +   
  +   protected ServerSocketFactory getServerSocketFactory()
  +   {
  +      return SSLServerSocketFactory.getDefault();
  +   }
  +}
  
  
  
  1.1.2.1   +447 -0    JBossRemoting/src/tests/org/jboss/test/remoting/socketfactory/Attic/CreationListenerTestRoot.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: CreationListenerTestRoot.java
  ===================================================================
  RCS file: CreationListenerTestRoot.java
  diff -N CreationListenerTestRoot.java
  --- /dev/null	1 Jan 1970 00:00:00 -0000
  +++ CreationListenerTestRoot.java	16 Jan 2007 06:25:52 -0000	1.1.2.1
  @@ -0,0 +1,447 @@
  +/*
  +* JBoss, Home of Professional Open Source
  +* Copyright 2005, 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.socketfactory;
  +
  +import java.io.IOException;
  +import java.io.InputStream;
  +import java.io.OutputStream;
  +import java.net.InetAddress;
  +import java.net.InetSocketAddress;
  +import java.net.ServerSocket;
  +import java.net.Socket;
  +import java.net.SocketAddress;
  +import java.net.SocketException;
  +import java.net.UnknownHostException;
  +import java.nio.channels.ServerSocketChannel;
  +import java.nio.channels.SocketChannel;
  +import java.util.HashMap;
  +
  +import javax.management.MBeanServer;
  +import javax.net.ServerSocketFactory;
  +import javax.net.SocketFactory;
  +import javax.net.ssl.SSLSocketFactory;
  +
  +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.InvokerCallbackHandler;
  +import org.jboss.remoting.socketfactory.CreationListenerServerSocketFactory;
  +import org.jboss.remoting.socketfactory.CreationListenerSocketFactory;
  +import org.jboss.remoting.socketfactory.SocketCreationListener;
  +import org.jboss.remoting.transport.Connector;
  +import org.jboss.remoting.transport.PortUtil;
  +
  +/** 
  + * @author <a href="ron.sigal at jboss.com">Ron Sigal</a>
  + * @version $Revision: 1.1.2.1 $
  + * <p>
  + * Copyright Jan 10, 2007
  + * </p>
  + */
  +public abstract class CreationListenerTestRoot extends TestCase
  +{
  +   protected static Logger log = Logger.getLogger(CreationListenerTestRoot.class);
  +   protected static boolean firstTime = true;
  +   
  +   
  +   public void setUp() throws Exception
  +   {
  +      if (firstTime)
  +      {
  +         firstTime = false;
  +         Logger.getLogger("org.jboss.remoting").setLevel(Level.DEBUG);
  +         Logger.getLogger("org.jboss.test.remoting").setLevel(Level.DEBUG);
  +         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 testSimpleClientSocketCases() throws Exception
  +   {
  +      log.info("entering " + getName());
  +      final InetAddress address = InetAddress.getLocalHost();
  +      final String host = address.getHostAddress();
  +      final int port = PortUtil.findFreePort(host);
  +      
  +      class T extends Thread
  +      {
  +         boolean running;
  +         boolean failed = false;
  +         void shutdown() {running = false;}
  +         boolean failed() {return failed;}
  +         public void run()
  +         {
  +            running = true;
  +            try
  +            {
  +               ServerSocket ss = new ServerSocket(port, 100, address);
  +               while (running)
  +               {
  +                  ss.accept();
  +               }
  +               ss.close();
  +            }
  +            catch (IOException e)
  +            {
  +               failed = true;
  +            }
  +         }
  +      };
  +      T t = new T();
  +      t.start();
  +
  +      TestListener listener = new TestListener();
  +      SocketFactory sf = getSocketFactory();
  +      SocketFactory clsf = new CreationListenerSocketFactory(sf, listener);
  +      
  +      assertFalse(listener.visited());
  +      clsf.createSocket();
  +      assertTrue(listener.visited());
  +      
  +      listener.reset();
  +      assertFalse(listener.visited());
  +      clsf.createSocket(host, port);
  +      assertTrue(listener.visited());
  +      
  +      listener.reset();
  +      assertFalse(listener.visited());
  +      clsf.createSocket(host, port, address, PortUtil.findFreePort(host));
  +      assertTrue(listener.visited());
  +      
  +      listener.reset();
  +      assertFalse(listener.visited());
  +      clsf.createSocket(address, port);
  +      assertTrue(listener.visited());
  +      
  +      listener.reset();
  +      assertFalse(listener.visited());
  +      clsf.createSocket(address, port, address, PortUtil.findFreePort(host));
  +      assertTrue(listener.visited());
  +      
  +      
  +      assertTrue(!t.failed());
  +      t.shutdown();
  +      log.info(getName() + " PASSES");
  +   }
  +   
  +   
  +   public void testSimpleServerSocketCases() throws Exception
  +   {
  +      log.info("entering " + getName());
  +      final InetAddress address = InetAddress.getLocalHost();
  +      final String host = address.getHostAddress();
  +      
  +      class T extends Thread
  +      {
  +         private ServerSocket ss;
  +         private boolean failed = false;
  +         boolean failed() {return failed;}
  +         T(ServerSocket ss) {this.ss = ss;}
  +         public void run()
  +         {
  +            try
  +            {
  +               ss.accept();
  +               ss.close();
  +            }
  +            catch (IOException e)
  +            {
  +               log.error(e);
  +               failed = true;
  +            }
  +         }
  +      }
  +      
  +      TestListener listener = new TestListener();
  +      ServerSocketFactory ssf = getServerSocketFactory();
  +      ServerSocketFactory clssf = new CreationListenerServerSocketFactory(ssf, listener);
  +      
  +      ServerSocket ss = clssf.createServerSocket();
  +      int freePort = PortUtil.findFreePort(host);
  +      ss.bind(new InetSocketAddress(host, freePort));
  +      T t = new T(ss);
  +      t.start();
  +      assertFalse(listener.visited());
  +      new Socket(host, freePort);
  +      Thread.sleep(500);
  +      assertTrue(listener.visited());
  +      assertTrue(!t.failed());
  +
  +      freePort = PortUtil.findFreePort(host);
  +      ss = clssf.createServerSocket(freePort);
  +      t = new T(ss);
  +      t.start();
  +      listener.reset();
  +      assertFalse(listener.visited());
  +      new Socket(host, freePort);
  +      Thread.sleep(500);
  +      assertTrue(listener.visited());
  +      assertTrue(!t.failed());
  +      
  +      freePort = PortUtil.findFreePort(host);
  +      ss = clssf.createServerSocket(freePort, 100);
  +      t = new T(ss);
  +      t.start();
  +      listener.reset();
  +      assertFalse(listener.visited());
  +      new Socket(host, freePort);
  +      Thread.sleep(500);
  +      assertTrue(listener.visited());
  +      assertTrue(!t.failed());
  +      
  +      freePort = PortUtil.findFreePort(host);
  +      ss = clssf.createServerSocket(freePort, 100, InetAddress.getLocalHost());
  +      t = new T(ss);
  +      t.start();
  +      listener.reset();
  +      assertFalse(listener.visited());
  +      new Socket(host, freePort);
  +      Thread.sleep(500);
  +      assertTrue(listener.visited());
  +      assertTrue(!t.failed());
  +      log.info(getName() + " PASSES");
  +   }
  +   
  +   
  +   public void testClientSideListener() throws Throwable
  +   {
  +      log.info("entering " + getName());
  +      String host = InetAddress.getLocalHost().getHostAddress();
  +      int port = PortUtil.findFreePort(host);
  +      String locatorURI = getTransport() + "://" + host + ":" + port;
  +      InvokerLocator locator = new InvokerLocator(locatorURI);
  +      Connector connector = new Connector(locator);
  +      connector.create();
  +      connector.addInvocationHandler("test", new TestHandler());
  +      connector.start();
  +      
  +      HashMap config = new HashMap();
  +      config.put(InvokerLocator.FORCE_REMOTE, "true");
  +      TestListener listener = new TestListener();
  +      config.put(Remoting.SOCKET_CREATION_LISTENER, listener);
  +      Client client = new Client(locator, config);
  +      assertFalse(listener.visited());
  +      client.connect();
  +      Integer i = (Integer) client.invoke(new Integer(17));
  +      assertEquals(18, i.intValue());
  +      Thread.sleep(500);
  +      assertTrue(listener.visited());
  +      
  +      client.disconnect();
  +      connector.stop();
  +      log.info(getName() + " PASSES");
  +   }
  +
  +   
  +   public void testServerSideListener() throws Throwable
  +   {
  +      log.info("entering " + getName());
  +      String host = InetAddress.getLocalHost().getHostAddress();
  +      int port = PortUtil.findFreePort(host);
  +      String locatorURI = getTransport() + "://" + host + ":" + port;
  +      InvokerLocator locator = new InvokerLocator(locatorURI);
  +      HashMap serverConfig = new HashMap();
  +      ServerSocketFactory ssf = getServerSocketFactory();
  +      serverConfig.put(Remoting.CUSTOM_SERVER_SOCKET_FACTORY, ssf);
  +      TestListener listener = new TestListener();
  +      serverConfig.put(Remoting.SOCKET_CREATION_LISTENER, listener);
  +      Connector connector = new Connector(locator, serverConfig);
  +      connector.create();
  +      connector.addInvocationHandler("test", new TestHandler());
  +      connector.start();
  +      
  +      HashMap clientConfig = new HashMap();
  +      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
  +      Client client = new Client(locator, clientConfig);
  +      client.connect();
  +      Integer i = (Integer) client.invoke(new Integer(29));
  +      assertEquals(30, i.intValue());
  +      Thread.sleep(500);
  +      assertTrue(listener.visited());
  +      
  +      client.disconnect();
  +      connector.stop();
  +      log.info(getName() + " PASSES");
  +   }
  +   
  +   
  +   public void testExceptionFromClientListener() throws Throwable
  +   {
  +      log.info("entering " + getName());
  +      String host = InetAddress.getLocalHost().getHostAddress();
  +      int port = PortUtil.findFreePort(host);
  +      String locatorURI = getTransport() + "://" + host + ":" + port;
  +      InvokerLocator locator = new InvokerLocator(locatorURI);
  +      Connector connector = new Connector(locator);
  +      connector.create();
  +      connector.addInvocationHandler("test", new TestHandler());
  +      connector.start();
  +      
  +      HashMap config = new HashMap();
  +      config.put(InvokerLocator.FORCE_REMOTE, "true");
  +      TestListener listener = new FailingTestListener();
  +      config.put(Remoting.SOCKET_CREATION_LISTENER, listener);
  +      Client client = new Client(locator, config);
  +      
  +      try
  +      {
  +         
  +         client.connect();
  +         client.invoke(new Integer(17));
  +         fail("didn't get expected exception");
  +      }
  +      catch (Exception e)
  +      {
  +         log.info("CAUGHT EXPECTED EXCEPTION");
  +      }
  +      
  +      client.disconnect();
  +      connector.stop();
  +      log.info(getName() + " PASSES");
  +   }
  +   
  +   
  +   public void testExceptionFromServerListener() throws Throwable
  +   {
  +      log.info("entering " + getName());
  +      String host = InetAddress.getLocalHost().getHostAddress();
  +      int port = PortUtil.findFreePort(host);
  +      String locatorURI = getTransport() + "://" + host + ":" + port;
  +      InvokerLocator locator = new InvokerLocator(locatorURI);
  +      HashMap serverConfig = new HashMap();
  +      ServerSocketFactory ssf = getServerSocketFactory();
  +      serverConfig.put(Remoting.CUSTOM_SERVER_SOCKET_FACTORY, ssf);
  +      TestListener listener = new FailingTestListener();
  +      serverConfig.put(Remoting.SOCKET_CREATION_LISTENER, listener);
  +      Connector connector = new Connector(locator, serverConfig);
  +      connector.create();
  +      connector.addInvocationHandler("test", new TestHandler());
  +      connector.start();
  +      
  +      HashMap clientConfig = new HashMap();
  +      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
  +      clientConfig.put(ServerInvoker.TIMEOUT, "1000");
  +      final Client client = new Client(locator, clientConfig);
  +      
  +      class TestThread extends Thread
  +      {
  +         public boolean failed = false;
  +         
  +         public void run()
  +         {
  +            try
  +            {
  +               client.connect();
  +               client.invoke(new Integer(29));
  +               failed = true;
  +               client.disconnect();
  +               fail("invoke() should have timed out");
  +            }
  +            catch (Throwable t)
  +            {
  +               log.info("CAUGHT EXPECTED EXCEPTION");
  +            }
  +         }
  +      };
  +
  +      TestThread t = new TestThread();
  +      t.start();
  +      Thread.sleep(2000);
  +      assertFalse(t.failed);
  +      connector.stop();
  +      log.info(getName() + " PASSES");
  +   }
  +   
  +   
  +   protected abstract String getTransport();
  +   
  +   
  +   protected SocketFactory getSocketFactory()
  +   {
  +      return SocketFactory.getDefault();
  +   }
  +   
  +   
  +   protected ServerSocketFactory getServerSocketFactory()
  +   {
  +      return ServerSocketFactory.getDefault();
  +   }
  +   
  +   
  +   public class TestListener implements SocketCreationListener
  +   {
  +      private boolean visited;
  +      
  +      public void socketCreated(Socket socket) throws IOException
  +      {
  +         visited = true;
  +      }
  +      
  +      public boolean visited()
  +      {
  +         return visited;
  +      }
  +      
  +      public void reset()
  +      {
  +         visited = false;
  +      }
  +   }
  +   
  +   
  +   public class FailingTestListener extends TestListener
  +   {
  +      public void socketCreated(Socket socket) throws IOException
  +      {
  +         throw new IOException();
  +      }
  +   }
  +   
  +   
  +   public class TestHandler implements ServerInvocationHandler
  +   {
  +
  +      public void setMBeanServer(MBeanServer server) {}
  +      public void setInvoker(ServerInvoker invoker) {}
  +
  +      public Object invoke(InvocationRequest invocation) throws Throwable
  +      {
  +         Integer i = (Integer) invocation.getParameter();
  +         return new Integer(i.intValue() + 1);
  +      }
  +
  +      public void addListener(InvokerCallbackHandler callbackHandler) {}
  +      public void removeListener(InvokerCallbackHandler callbackHandler) {}
  +   }
  +}
  \ No newline at end of file
  
  
  



More information about the jboss-cvs-commits mailing list