directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r371272 - in /directory/sandbox/trustin/mina-spi/examples/src/test/java/org/apache/mina/examples/echoserver: AbstractTest.java AcceptorTest.java ConnectorTest.java
Date Sun, 22 Jan 2006 11:57:57 GMT
Author: trustin
Date: Sun Jan 22 03:57:50 2006
New Revision: 371272

URL: http://svn.apache.org/viewcvs?rev=371272&view=rev
Log:
Fixed failing tests

Modified:
    directory/sandbox/trustin/mina-spi/examples/src/test/java/org/apache/mina/examples/echoserver/AbstractTest.java
    directory/sandbox/trustin/mina-spi/examples/src/test/java/org/apache/mina/examples/echoserver/AcceptorTest.java
    directory/sandbox/trustin/mina-spi/examples/src/test/java/org/apache/mina/examples/echoserver/ConnectorTest.java

Modified: directory/sandbox/trustin/mina-spi/examples/src/test/java/org/apache/mina/examples/echoserver/AbstractTest.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trustin/mina-spi/examples/src/test/java/org/apache/mina/examples/echoserver/AbstractTest.java?rev=371272&r1=371271&r2=371272&view=diff
==============================================================================
--- directory/sandbox/trustin/mina-spi/examples/src/test/java/org/apache/mina/examples/echoserver/AbstractTest.java
(original)
+++ directory/sandbox/trustin/mina-spi/examples/src/test/java/org/apache/mina/examples/echoserver/AbstractTest.java
Sun Jan 22 03:57:50 2006
@@ -16,7 +16,7 @@
  *   limitations under the License.
  *
  */
-package org.apache.IoService.examples.echoserver;
+package org.apache.mina.examples.echoserver;
 
 import java.io.IOException;
 
@@ -25,7 +25,7 @@
 import org.apache.mina.common.ByteBuffer;
 import org.apache.mina.common.IoService;
 import org.apache.mina.common.IoSession;
-import org.apache.mina.filter.LoggingFilter;
+import org.apache.mina.examples.echoserver.ssl.BogusSSLContextFactory;
 import org.apache.mina.filter.SSLFilter;
 import org.apache.mina.util.SessionLog;
 
@@ -37,6 +37,7 @@
  */
 public abstract class AbstractTest extends TestCase
 {
+    protected boolean useSSL;
     protected int port;
 
     protected AbstractTest()
@@ -71,6 +72,11 @@
 
     protected void setUp() throws Exception
     {
+        // Disable SSL by default
+        useSSL = false;
+        final SSLFilter sslFilter =
+            new SSLFilter( BogusSSLContextFactory.getInstance( true ) );
+
         // Find an availble test port and bind to it.
         boolean socketBound = false;
         boolean datagramBound = false;
@@ -89,6 +95,14 @@
             {
                 IoService.bind( "nio:socket:*:" + port, new EchoProtocolHandler()
                 {
+                    public void sessionCreated( IoSession session )
+                    {
+                        if( useSSL )
+                        {
+                            session.getFilterChain().addFirst( "SSL", sslFilter );
+                        }
+                    }
+
                     // This is for TLS reentrance test
                     public void messageReceived( IoSession session, Object message ) throws
Exception
                     {
@@ -98,10 +112,10 @@
                         }
                         
                         ByteBuffer buf = ( ByteBuffer ) message;
-                        if( buf.remaining() == 1 && buf.get() == ( byte ) '.' )
+                        if( session.getFilterChain().contains( "SSL" ) && buf.remaining()
== 1 && buf.get() == ( byte ) '.' )
                         {
                             SessionLog.info( session, "TLS Reentrance" );
-                            ( ( SSLFilter ) acceptor.getFilterChain().get( "SSL" ) ).startSSL(
session );
+                            ( ( SSLFilter ) session.getFilterChain().get( "SSL" ) ).startSSL(
session );
 
                             // Send a response
                             buf = ByteBuffer.allocate( 1 );
@@ -142,16 +156,15 @@
             throw new IOException( "Cannot bind any test port." );
         }
 
-        registry.getAcceptor( TransportType.SOCKET ).getFilterChain().addLast( "logger",
new LoggingFilter() );
-        registry.getAcceptor( TransportType.DATAGRAM ).getFilterChain().addLast( "logger",
new LoggingFilter() );
-
         System.out.println( "Using port " + port + " for testing." );
     }
 
     protected void tearDown() throws Exception
     {
-        registry.unbindAll();
-        registry.getAcceptor( TransportType.SOCKET ).getFilterChain().remove( "logger" );
-        registry.getAcceptor( TransportType.DATAGRAM ).getFilterChain().remove( "logger"
);
+        if( port != 0 )
+        {
+            IoService.unbind( "nio:socket:*:" + port );
+            IoService.unbind( "nio:datagram:*:" + port );
+        }
     }
 }

Modified: directory/sandbox/trustin/mina-spi/examples/src/test/java/org/apache/mina/examples/echoserver/AcceptorTest.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trustin/mina-spi/examples/src/test/java/org/apache/mina/examples/echoserver/AcceptorTest.java?rev=371272&r1=371271&r2=371272&view=diff
==============================================================================
--- directory/sandbox/trustin/mina-spi/examples/src/test/java/org/apache/mina/examples/echoserver/AcceptorTest.java
(original)
+++ directory/sandbox/trustin/mina-spi/examples/src/test/java/org/apache/mina/examples/echoserver/AcceptorTest.java
Sun Jan 22 03:57:50 2006
@@ -30,12 +30,8 @@
 
 import org.apache.commons.net.EchoTCPClient;
 import org.apache.commons.net.EchoUDPClient;
-import org.apache.mina.common.IoAcceptor;
-import org.apache.mina.common.TransportType;
-import org.apache.mina.examples.echoserver.ssl.BogusSSLContextFactory;
 import org.apache.mina.examples.echoserver.ssl.SSLServerSocketFactory;
 import org.apache.mina.examples.echoserver.ssl.SSLSocketFactory;
-import org.apache.mina.filter.SSLFilter;
 
 /**
  * Tests echo server example.
@@ -58,10 +54,7 @@
     public void testTCPWithSSL() throws Exception
     {
         // Add an SSL filter
-        SSLFilter sslFilter =
-            new SSLFilter( BogusSSLContextFactory.getInstance( true ) );
-        IoAcceptor acceptor = registry.getAcceptor( TransportType.SOCKET );
-        acceptor.getFilterChain().addLast( "SSL", sslFilter );
+        useSSL = true;
         
         // Create a commons-net socket factory
         SSLSocketFactory.setSslEnabled(true);

Modified: directory/sandbox/trustin/mina-spi/examples/src/test/java/org/apache/mina/examples/echoserver/ConnectorTest.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trustin/mina-spi/examples/src/test/java/org/apache/mina/examples/echoserver/ConnectorTest.java?rev=371272&r1=371271&r2=371272&view=diff
==============================================================================
--- directory/sandbox/trustin/mina-spi/examples/src/test/java/org/apache/mina/examples/echoserver/ConnectorTest.java
(original)
+++ directory/sandbox/trustin/mina-spi/examples/src/test/java/org/apache/mina/examples/echoserver/ConnectorTest.java
Sun Jan 22 03:57:50 2006
@@ -19,17 +19,16 @@
 package org.apache.mina.examples.echoserver;
 
 import java.net.BindException;
-import java.net.InetSocketAddress;
 
 import junit.framework.Assert;
 
 import org.apache.mina.common.ByteBuffer;
 import org.apache.mina.common.ConnectFuture;
 import org.apache.mina.common.IoHandlerAdapter;
+import org.apache.mina.common.IoService;
 import org.apache.mina.common.IoSession;
 import org.apache.mina.common.WriteFuture;
 import org.apache.mina.examples.echoserver.ssl.BogusSSLContextFactory;
-import org.apache.mina.filter.LoggingFilter;
 import org.apache.mina.filter.SSLFilter;
 import org.apache.mina.util.AvailablePortFinder;
 import org.apache.mina.util.SessionLog;
@@ -45,61 +44,55 @@
     private static final int TIMEOUT = 10000; // 10 seconds
     private final int COUNT = 10;
     private final int DATA_SIZE = 16;
+    private SSLFilter connectorSSLFilter;
 
     public ConnectorTest()
     {
     }
+    
+    protected void setUp() throws Exception
+    {
+        super.setUp();
+
+        connectorSSLFilter =
+            new SSLFilter( BogusSSLContextFactory.getInstance( false ) );
+        connectorSSLFilter.setUseClientMode( true ); // set client mode
+    }
 
     public void testTCP() throws Exception
     {
-        IoConnector connector = new SocketConnector();
-        testConnector( connector );
+        testConnector( "socket" );
     }
     
     public void testTCPWithSSL() throws Exception
     {
-        // Add an SSL filter to acceptor
-        SSLFilter acceptorSSLFilter =
-            new SSLFilter( BogusSSLContextFactory.getInstance( true ) );
-        IoAcceptor acceptor = registry.getAcceptor( TransportType.SOCKET );
-        acceptor.getFilterChain().addLast( "SSL", acceptorSSLFilter );
-        
-        // Create a connector
-        IoConnector connector = new SocketConnector();
-        
-        // Add an SSL filter to connector
-        SSLFilter connectorSSLFilter =
-            new SSLFilter( BogusSSLContextFactory.getInstance( false ) );
-        connectorSSLFilter.setUseClientMode( true ); // set client mode
-        connector.getFilterChain().addLast( "SSL", connectorSSLFilter );
-
-        testConnector( connector );
+        useSSL = true;
+        testConnector( "socket" );
     }
     
     public void testUDP() throws Exception
     {
-        IoConnector connector = new DatagramConnector();
-        testConnector( connector );
+        testConnector( "datagram" );
     }
     
-    private void testConnector( IoConnector connector ) throws Exception
+    private void testConnector( String transportType ) throws Exception
     {
         System.out.println("* Without localAddress");
-        testConnector( connector, false );
+        testConnector( transportType, false );
             
         System.out.println("* With localAddress");
-        testConnector( connector, true );
+        testConnector( transportType, true );
     }
     
-    private void testConnector( IoConnector connector, boolean useLocalAddress ) throws Exception
+    private void testConnector( String transportType, boolean useLocalAddress ) throws Exception
     {
         EchoConnectorHandler handler = new EchoConnectorHandler();
         
         IoSession session = null;
         if( !useLocalAddress )
         {
-            ConnectFuture future = connector.connect(
-                    new InetSocketAddress( "localhost", port ),
+            ConnectFuture future = IoService.connect(
+                    "nio:" + transportType + ":localhost:" + port,
                     handler );
             future.join();
             session = future.getSession();
@@ -107,14 +100,16 @@
         else
         {
             int clientPort = port;
-            for( int i = 0; i < 65536; i ++ )
+            for( int i = 1; i < 65536; i ++ )
             {
+                System.out.println( i );
+                
                 clientPort = AvailablePortFinder.getNextAvailable( clientPort + 1 );
                 try
                 {
-                    ConnectFuture future = connector.connect(
-                            new InetSocketAddress( "localhost", port ),
-                            new InetSocketAddress( clientPort ),
+                    ConnectFuture future = IoService.connect(
+                            "nio:" + transportType + ":localhost:" + port,
+                            "nio:" + transportType + ":*:" + clientPort,
                             handler );
                     future.join();
                     session = future.getSession();
@@ -122,6 +117,7 @@
                 }
                 catch( BindException e )
                 {
+                    e.printStackTrace();
                     // Try again until we succeed to bind.
                 }
             }
@@ -136,11 +132,9 @@
         testConnector0( session );
         
         // Send closeNotify to test TLS closure if it is TLS connection.
-        SSLFilter sslf = ( SSLFilter ) connector.getFilterChain().get( "SSL" );
-        if( sslf != null )
+        if( useSSL )
         {
-            connector.getFilterChain().addFirst( "log", new LoggingFilter() );
-            sslf.stopSSL( session ).join();
+            connectorSSLFilter.stopSSL( session ).join();
             
             System.out.println( "-------------------------------------------------------------------------------"
);
             // Test again after we finished TLS session.
@@ -164,9 +158,8 @@
             Assert.assertEquals( ( byte ) '.', handler.readBuf.get() );
             
             // Now start TLS connection
-            Assert.assertTrue( sslf.startSSL( session ) );
+            Assert.assertTrue( connectorSSLFilter.startSSL( session ) );
             testConnector0( session );
-            connector.getFilterChain().remove( "log" );
         }
         
         session.close().join();
@@ -187,7 +180,7 @@
             
             writeFuture = session.write( buf );
             
-            if( session.getTransportType().isConnectionless() ) 
+            if( session.getProvider().isConnectionless() ) 
             {
                 // This will align message arrival order in connectionless transport types
                 waitForResponse( handler, ( i + 1 ) * DATA_SIZE );
@@ -242,7 +235,7 @@
         junit.textui.TestRunner.run( ConnectorTest.class );
     }
     
-    private static class EchoConnectorHandler extends IoHandlerAdapter
+    private class EchoConnectorHandler extends IoHandlerAdapter
     {
         private ByteBuffer readBuf = ByteBuffer.allocate( 1024 );
         
@@ -251,6 +244,14 @@
             readBuf.setAutoExpand( true );
         }
 
+        public void sessionCreated( IoSession session )
+        {
+            if( useSSL )
+            {
+                session.getFilterChain().addFirst( "SSL", connectorSSLFilter );
+            }
+        }
+        
         public void messageReceived( IoSession session, Object message )
         {
             readBuf.put( ( ByteBuffer ) message );
@@ -263,6 +264,7 @@
         public void exceptionCaught( IoSession session, Throwable cause )
         {
             cause.printStackTrace();
+            session.close();
         }
     }
 }



Mime
View raw message