cvs commit: spice/components/netserve/src/test/org/jcomponent/netserve/connection/handlers ExceptingRequestHandler.java ExceptionOnCloseSocket.java ManagedRequestHandlerTestCase.java MockManagedRequestHandler.java MockRequestHandler.java RequestHandlerTestCase.java RequestManager.java ThreadPerRequestHandlerTestCase.java

Peter Donald <[email protected]>
Newsgroups gmane.comp.java.spice.cvs
Message-ID <[email protected]>
donaldp     03/10/28 19:00:19

  Added:       components/netserve/src/test/org/jcomponent/netserve/connection/handlers
                        ExceptingRequestHandler.java
                        ExceptionOnCloseSocket.java
                        ManagedRequestHandlerTestCase.java
                        MockManagedRequestHandler.java
                        MockRequestHandler.java RequestHandlerTestCase.java
                        RequestManager.java
                        ThreadPerRequestHandlerTestCase.java
  Log:
  Start the process of unit testing the handlers
  
  Revision  Changes    Path
  1.1                  spice/components/netserve/src/test/org/jcomponent/netserve/connection/handlers/ExceptingRequestHandler.java
  
  Index: ExceptingRequestHandler.java
  ===================================================================
  /*
   * Copyright (C) The Spice Group. All rights reserved.
   *
   * This software is published under the terms of the Spice
   * Software License version 1.1, a copy of which has been included
   * with this distribution in the LICENSE.txt file.
   */
  package org.jcomponent.netserve.connection.handlers;
  
  import java.net.Socket;
  
  /**
   *
   * @author <a href="mailto:peter at realityforge.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2003/10/29 03:00:14 $
   */
  class ExceptingRequestHandler
      extends MockRequestHandler
  {
      static final Exception EXCEPTION = new Exception();
  
      protected void doPerformRequest( Socket socket )
          throws Exception
      {
          super.doPerformRequest(socket );
          throw EXCEPTION;
      }
  }
  
  
  
  
  1.1                  spice/components/netserve/src/test/org/jcomponent/netserve/connection/handlers/ExceptionOnCloseSocket.java
  
  Index: ExceptionOnCloseSocket.java
  ===================================================================
  /*
   * Copyright (C) The Spice Group. All rights reserved.
   *
   * This software is published under the terms of the Spice
   * Software License version 1.1, a copy of which has been included
   * with this distribution in the LICENSE.txt file.
   */
  package org.jcomponent.netserve.connection.handlers;
  
  import java.net.Socket;
  import java.io.IOException;
  
  /**
   *
   * @author <a href="mailto:peter at realityforge.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2003/10/29 03:00:14 $
   */
  class ExceptionOnCloseSocket
      extends Socket
  {
      static final IOException IO_EXCEPTION = new IOException();
  
      public boolean isConnected()
      {
          return true;
      }
  
      public synchronized void close() throws IOException
      {
          throw IO_EXCEPTION;
      }
  }
  
  
  
  1.1                  spice/components/netserve/src/test/org/jcomponent/netserve/connection/handlers/ManagedRequestHandlerTestCase.java
  
  Index: ManagedRequestHandlerTestCase.java
  ===================================================================
  /*
   * Copyright (C) The Spice Group. All rights reserved.
   *
   * This software is published under the terms of the Spice
   * Software License version 1.1, a copy of which has been included
   * with this distribution in the LICENSE.txt file.
   */
  package org.jcomponent.netserve.connection.handlers;
  
  import com.mockobjects.dynamic.C;
  import com.mockobjects.dynamic.Mock;
  import java.net.Socket;
  import junit.framework.TestCase;
  import org.jcomponent.netserve.connection.RequestHandler;
  
  /**
   *
   * @author <a href="mailto:peter at realityforge.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2003/10/29 03:00:14 $
   */
  public class ManagedRequestHandlerTestCase
      extends TestCase
  {
      public void testManagedRequestHandler()
          throws Exception
      {
          final Mock mockHandler = new Mock( RequestHandler.class );
          final Socket socket = new Socket();
          mockHandler.expect( "handleConnection", C.args( C.eq( socket ) ) );
          final RequestHandler handler = (RequestHandler)mockHandler.proxy();
  
          final Mock mockManager = new Mock( RequestManager.class );
          mockManager.expectAndReturn( "aquireHandler",
                                       C.args( C.isA( Socket.class ) ),
                                       handler );
          mockManager.expect( "releaseHandler",
                              C.args( C.eq( handler ) ) );
  
          final RequestManager manager = (RequestManager)mockManager.proxy();
  
          final MockManagedRequestHandler managedHandler =
              new MockManagedRequestHandler( manager );
          managedHandler.handleConnection( socket );
  
          mockManager.verify();
          mockHandler.verify();
      }
  }
  
  
  
  1.1                  spice/components/netserve/src/test/org/jcomponent/netserve/connection/handlers/MockManagedRequestHandler.java
  
  Index: MockManagedRequestHandler.java
  ===================================================================
  /*
   * Copyright (C) The Spice Group. All rights reserved.
   *
   * This software is published under the terms of the Spice
   * Software License version 1.1, a copy of which has been included
   * with this distribution in the LICENSE.txt file.
   */
  package org.jcomponent.netserve.connection.handlers;
  
  import java.net.Socket;
  import org.jcomponent.netserve.connection.RequestHandler;
  
  /**
   *
   * @author <a href="mailto:peter at realityforge.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2003/10/29 03:00:14 $
   */
  class MockManagedRequestHandler
      extends ManagedRequestHandler
  {
      private final RequestManager m_requestManager;
  
      public MockManagedRequestHandler( RequestManager requestManager )
      {
          m_requestManager = requestManager;
      }
  
      protected RequestHandler aquireHandler( Socket socket )
      {
          return m_requestManager.aquireHandler( socket );
      }
  
      protected void releaseHandler( RequestHandler handler )
      {
          m_requestManager.releaseHandler( handler );
      }
  }
  
  
  
  
  1.1                  spice/components/netserve/src/test/org/jcomponent/netserve/connection/handlers/MockRequestHandler.java
  
  Index: MockRequestHandler.java
  ===================================================================
  /*
   * Copyright (C) The Spice Group. All rights reserved.
   *
   * This software is published under the terms of the Spice
   * Software License version 1.1, a copy of which has been included
   * with this distribution in the LICENSE.txt file.
   */
  package org.jcomponent.netserve.connection.handlers;
  
  import java.net.Socket;
  
  /**
   *
   * @author <a href="mailto:peter at realityforge.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2003/10/29 03:00:14 $
   */
  class MockRequestHandler
      extends AbstractRequestHandler
  {
      private Socket m_performRequestSocket;
  
      private Socket m_errorClosingConnectionSocket;
      private Throwable m_errorClosingConnectionThrowable;
  
      private Socket m_errorHandlingConnectionSocket;
      private Throwable m_errorHandlingConnectionThrowable;
  
      protected void doPerformRequest( Socket socket )
          throws Exception
      {
          m_performRequestSocket = socket;
      }
  
      protected void errorClosingConnection( Socket socket,
                                             Throwable t )
      {
          super.errorClosingConnection( socket, t );
          m_errorClosingConnectionSocket = socket;
          m_errorClosingConnectionThrowable = t;
      }
  
      protected void errorHandlingConnection( Socket socket,
                                              Throwable t )
      {
          super.errorHandlingConnection( socket, t );
          m_errorHandlingConnectionSocket = socket;
          m_errorHandlingConnectionThrowable = t;
      }
  
      Socket getPerformRequestSocket()
      {
          return m_performRequestSocket;
      }
  
      Socket getErrorClosingConnectionSocket()
      {
          return m_errorClosingConnectionSocket;
      }
  
      Throwable getErrorClosingConnectionThrowable()
      {
          return m_errorClosingConnectionThrowable;
      }
  
      Socket getErrorHandlingConnectionSocket()
      {
          return m_errorHandlingConnectionSocket;
      }
  
      Throwable getErrorHandlingConnectionThrowable()
      {
          return m_errorHandlingConnectionThrowable;
      }
  }
  
  
  
  
  1.1                  spice/components/netserve/src/test/org/jcomponent/netserve/connection/handlers/RequestHandlerTestCase.java
  
  Index: RequestHandlerTestCase.java
  ===================================================================
  /*
   * Copyright (C) The Spice Group. All rights reserved.
   *
   * This software is published under the terms of the Spice
   * Software License version 1.1, a copy of which has been included
   * with this distribution in the LICENSE.txt file.
   */
  package org.jcomponent.netserve.connection.handlers;
  
  import junit.framework.TestCase;
  import java.net.Socket;
  
  /**
   *
   * @author <a href="mailto:peter at realityforge.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2003/10/29 03:00:14 $
   */
  public class RequestHandlerTestCase
      extends TestCase
  {
      public void testEndConnectionWithError()
          throws Exception
      {
          final MockRequestHandler handler = new MockRequestHandler();
          final ExceptionOnCloseSocket socket = new ExceptionOnCloseSocket();
          handler.endConnection( socket );
          assertEquals( "getErrorClosingConnectionSocket",
                        socket,
                        handler.getErrorClosingConnectionSocket() );
          assertEquals( "getErrorClosingConnectionThrowable",
                        ExceptionOnCloseSocket.IO_EXCEPTION,
                        handler.getErrorClosingConnectionThrowable() );
      }
  
      public void testCreateRunnable()
          throws Exception
      {
          final MockRequestHandler handler = new MockRequestHandler();
          final Socket socket = new Socket();
          final Runnable runnable = handler.createRunnable( socket );
          runnable.run();
          assertEquals( "getPerformRequestSocket",
                        socket,
                        handler.getPerformRequestSocket() );
      }
  
      public void testPerformRequestWithError()
          throws Exception
      {
          final ExceptingRequestHandler handler = new ExceptingRequestHandler();
          final Socket socket = new Socket();
          handler.performRequest( socket );
          assertEquals( "getPerformRequestSocket",
                        socket,
                        handler.getPerformRequestSocket() );
          assertEquals( "getErrorHandlingConnectionSocket",
                        socket,
                        handler.getErrorHandlingConnectionSocket() );
          assertEquals( "getErrorClosingConnectionThrowable",
                        ExceptingRequestHandler.EXCEPTION,
                        handler.getErrorHandlingConnectionThrowable() );
      }
  }
  
  
  
  1.1                  spice/components/netserve/src/test/org/jcomponent/netserve/connection/handlers/RequestManager.java
  
  Index: RequestManager.java
  ===================================================================
  /*
   * Copyright (C) The Spice Group. All rights reserved.
   *
   * This software is published under the terms of the Spice
   * Software License version 1.1, a copy of which has been included
   * with this distribution in the LICENSE.txt file.
   */
  package org.jcomponent.netserve.connection.handlers;
  
  import org.jcomponent.netserve.connection.RequestHandler;
  import java.net.Socket;
  
  /**
   *
   * @author <a href="mailto:peter at realityforge.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2003/10/29 03:00:14 $
   */
  public interface RequestManager
  {
      RequestHandler aquireHandler( Socket socket );
  
      void releaseHandler( RequestHandler handler );
  }
  
  
  
  1.1                  spice/components/netserve/src/test/org/jcomponent/netserve/connection/handlers/ThreadPerRequestHandlerTestCase.java
  
  Index: ThreadPerRequestHandlerTestCase.java
  ===================================================================
  /*
   * Copyright (C) The Spice Group. All rights reserved.
   *
   * This software is published under the terms of the Spice
   * Software License version 1.1, a copy of which has been included
   * with this distribution in the LICENSE.txt file.
   */
  package org.jcomponent.netserve.connection.handlers;
  
  import junit.framework.TestCase;
  import com.mockobjects.dynamic.Mock;
  import com.mockobjects.dynamic.C;
  import org.jcomponent.netserve.connection.RequestHandler;
  import org.jcomponent.threadpool.ThreadPool;
  import java.net.Socket;
  
  /**
   *
   * @author <a href="mailto:peter at realityforge.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2003/10/29 03:00:14 $
   */
  public class ThreadPerRequestHandlerTestCase
      extends TestCase
  {
      public void testNullThreadPoolPassedToCtor()
          throws Exception
      {
          final Mock mockHandler = new Mock( RequestHandler.class );
          final RequestHandler handler = (RequestHandler)mockHandler.proxy();
          try
          {
              new ThreadPerRequestHandler( handler, null );
          }
          catch( final NullPointerException npe )
          {
              assertEquals( "npe.getMessage()", "threadPool", npe.getMessage() );
              return;
          }
          fail( "Expected to fail due to null ThreadPool passed into Ctor" );
      }
  
      public void testThreadPoolInvoked()
          throws Exception
      {
          final Mock mockHandler = new Mock( RequestHandler.class );
          final RequestHandler handler = (RequestHandler)mockHandler.proxy();
  
          final Mock mockPool = new Mock( ThreadPool.class );
          mockPool.expectAndReturn( "execute", C.args( C.isA( Runnable.class ) ), null );
          final ThreadPool threadPool = (ThreadPool)mockPool.proxy();
  
          final ThreadPerRequestHandler requestHandler =
              new ThreadPerRequestHandler( handler, threadPool );
          requestHandler.handleConnection( new Socket() );
  
          mockHandler.verify();
          mockPool.verify();
      }
  }
  
  
  


-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
Does SourceForge.net help you be more productive?  Does it
help you create better code?   SHARE THE LOVE, and help us help
YOU!  Click Here: http://sourceforge.net/donate/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.