directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r157501 - in directory/network/mina/trunk/src/test/org/apache/mina/examples/echoserver: AbstractTest.java AcceptorTest.java ConnectorTest.java Test.java
Date Tue, 15 Mar 2005 03:34:19 GMT
Author: trustin
Date: Mon Mar 14 19:34:17 2005
New Revision: 157501

URL: http://svn.apache.org/viewcvs?view=rev&rev=157501
Log:
* Splitted echoserver Test into AbstractTest and AcceptorTest.
* Added a ConnectorTest that tests IoConnectors.

Added:
    directory/network/mina/trunk/src/test/org/apache/mina/examples/echoserver/AbstractTest.java
  (with props)
    directory/network/mina/trunk/src/test/org/apache/mina/examples/echoserver/AcceptorTest.java
      - copied, changed from r157368, directory/network/mina/trunk/src/test/org/apache/mina/examples/echoserver/Test.java
    directory/network/mina/trunk/src/test/org/apache/mina/examples/echoserver/ConnectorTest.java
  (with props)
Removed:
    directory/network/mina/trunk/src/test/org/apache/mina/examples/echoserver/Test.java

Added: directory/network/mina/trunk/src/test/org/apache/mina/examples/echoserver/AbstractTest.java
URL: http://svn.apache.org/viewcvs/directory/network/mina/trunk/src/test/org/apache/mina/examples/echoserver/AbstractTest.java?view=auto&rev=157501
==============================================================================
--- directory/network/mina/trunk/src/test/org/apache/mina/examples/echoserver/AbstractTest.java
(added)
+++ directory/network/mina/trunk/src/test/org/apache/mina/examples/echoserver/AbstractTest.java
Mon Mar 14 19:34:17 2005
@@ -0,0 +1,128 @@
+/*
+ * @(#) $Id$
+ */
+package org.apache.mina.examples.echoserver;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+
+import junit.framework.TestCase;
+
+import org.apache.mina.common.ByteBuffer;
+import org.apache.mina.io.IoAcceptor;
+import org.apache.mina.io.datagram.DatagramAcceptor;
+import org.apache.mina.io.filter.IoThreadPoolFilter;
+import org.apache.mina.io.socket.SocketAcceptor;
+
+/**
+ * Tests echo server example.
+ * 
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$
+ */
+public class AbstractTest extends TestCase
+{
+    protected int port;
+
+    protected IoAcceptor acceptor;
+
+    protected IoAcceptor datagramAcceptor;
+
+    protected IoThreadPoolFilter threadPoolFilter;
+
+    protected static void assertEquals( byte[] expected, byte[] actual )
+    {
+        assertEquals( toString( expected ), toString( actual ) );
+    }
+
+    protected static void assertEquals( ByteBuffer expected, ByteBuffer actual )
+    {
+        assertEquals( toString( expected ), toString( actual ) );
+    }
+
+    protected static String toString( byte[] buf )
+    {
+        StringBuffer str = new StringBuffer( buf.length * 4 );
+        for( int i = 0; i < buf.length; i ++ )
+        {
+            str.append( buf[ i ] );
+            str.append( ' ' );
+        }
+        return str.toString();
+    }
+    
+    protected static String toString( ByteBuffer buf )
+    {
+        return buf.getHexDump();
+    }
+
+    protected void setUp() throws Exception
+    {
+        acceptor = new SocketAcceptor();
+        datagramAcceptor = new DatagramAcceptor();
+
+        // Find an availble test port and bind to it.
+        boolean socketBound = false;
+        boolean datagramBound = false;
+
+        // Let's start from port #1 to detect possible resource leak
+        // because test will fail in port 1-1023 if user run this test
+        // as a normal user.
+        for( port = 1; port <= 65535; port ++ )
+        {
+            socketBound = false;
+            datagramBound = false;
+            try
+            {
+                acceptor.bind( new InetSocketAddress( port ),
+                        new EchoProtocolHandler() );
+                socketBound = true;
+
+                datagramAcceptor.bind( new InetSocketAddress( port ),
+                        new EchoProtocolHandler() );
+                datagramBound = true;
+
+                break;
+            }
+            catch( IOException e )
+            {
+            }
+            finally
+            {
+                if( !socketBound || !datagramBound )
+                {
+                    if( socketBound )
+                    {
+                        acceptor.unbind( new InetSocketAddress( port ) );
+                    }
+                    if( datagramBound )
+                    {
+                        datagramAcceptor
+                                .unbind( new InetSocketAddress( port ) );
+                    }
+                }
+            }
+        }
+
+        // If there is no port available, test fails.
+        if( !socketBound || !datagramBound )
+        {
+            throw new IOException( "Cannot bind any test port." );
+        }
+
+        System.out.println( "Using port " + port + " for testing." );
+
+        threadPoolFilter = new IoThreadPoolFilter();
+        threadPoolFilter.start();
+
+        acceptor.addFilter( Integer.MAX_VALUE, threadPoolFilter );
+        datagramAcceptor.addFilter( Integer.MAX_VALUE, threadPoolFilter );
+    }
+
+    protected void tearDown() throws Exception
+    {
+        acceptor.unbind( new InetSocketAddress( port ) );
+        datagramAcceptor.unbind( new InetSocketAddress( port ) );
+        threadPoolFilter.stop();
+    }
+}

Propchange: directory/network/mina/trunk/src/test/org/apache/mina/examples/echoserver/AbstractTest.java
------------------------------------------------------------------------------
    svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision

Copied: directory/network/mina/trunk/src/test/org/apache/mina/examples/echoserver/AcceptorTest.java
(from r157368, directory/network/mina/trunk/src/test/org/apache/mina/examples/echoserver/Test.java)
URL: http://svn.apache.org/viewcvs/directory/network/mina/trunk/src/test/org/apache/mina/examples/echoserver/AcceptorTest.java?view=diff&rev=157501&p1=directory/network/mina/trunk/src/test/org/apache/mina/examples/echoserver/Test.java&r1=157368&p2=directory/network/mina/trunk/src/test/org/apache/mina/examples/echoserver/AcceptorTest.java&r2=157501
==============================================================================
--- directory/network/mina/trunk/src/test/org/apache/mina/examples/echoserver/Test.java (original)
+++ directory/network/mina/trunk/src/test/org/apache/mina/examples/echoserver/AcceptorTest.java
Mon Mar 14 19:34:17 2005
@@ -5,7 +5,6 @@
 
 import java.io.IOException;
 import java.net.InetAddress;
-import java.net.InetSocketAddress;
 import java.net.ServerSocket;
 import java.net.Socket;
 import java.net.SocketTimeoutException;
@@ -14,18 +13,12 @@
 import javax.net.ServerSocketFactory;
 import javax.net.SocketFactory;
 
-import junit.framework.TestCase;
-
 import org.apache.commons.net.EchoTCPClient;
 import org.apache.commons.net.EchoUDPClient;
 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.io.IoAcceptor;
-import org.apache.mina.io.datagram.DatagramAcceptor;
-import org.apache.mina.io.filter.IoThreadPoolFilter;
 import org.apache.mina.io.filter.SSLFilter;
-import org.apache.mina.io.socket.SocketAcceptor;
 
 /**
  * Tests echo server example.
@@ -33,102 +26,8 @@
  * @author Trustin Lee (trustin@apache.org)
  * @version $Rev$, $Date$
  */
-public class Test extends TestCase
+public class AcceptorTest extends AbstractTest
 {
-    private int port;
-
-    protected IoAcceptor acceptor;
-
-    private IoAcceptor datagramAcceptor;
-
-    private IoThreadPoolFilter threadPoolFilter;
-
-    public static void assertEquals( byte[] expected, byte[] actual )
-    {
-        assertEquals( toString( expected ), toString( actual ) );
-    }
-
-    private static String toString( byte[] buf )
-    {
-        StringBuffer str = new StringBuffer( buf.length * 4 );
-        for( int i = 0; i < buf.length; i ++ )
-        {
-            str.append( buf[ i ] );
-            str.append( ' ' );
-        }
-        return str.toString();
-    }
-
-    protected void setUp() throws Exception
-    {
-        acceptor = new SocketAcceptor();
-        datagramAcceptor = new DatagramAcceptor();
-
-        // Find an availble test port and bind to it.
-        boolean socketBound = false;
-        boolean datagramBound = false;
-
-        // Let's start from port #1 to detect possible resource leak
-        // because test will fail in port 1-1023 if user run this test
-        // as a normal user.
-        for( port = 1; port <= 65535; port ++ )
-        {
-            socketBound = false;
-            datagramBound = false;
-            try
-            {
-                acceptor.bind( new InetSocketAddress( port ),
-                        new EchoProtocolHandler() );
-                socketBound = true;
-
-                datagramAcceptor.bind( new InetSocketAddress( port ),
-                        new EchoProtocolHandler() );
-                datagramBound = true;
-
-                break;
-            }
-            catch( IOException e )
-            {
-            }
-            finally
-            {
-                if( !socketBound || !datagramBound )
-                {
-                    if( socketBound )
-                    {
-                        acceptor.unbind( new InetSocketAddress( port ) );
-                    }
-                    if( datagramBound )
-                    {
-                        datagramAcceptor
-                                .unbind( new InetSocketAddress( port ) );
-                    }
-                }
-            }
-        }
-
-        // If there is no port available, test fails.
-        if( !socketBound || !datagramBound )
-        {
-            throw new IOException( "Cannot bind any test port." );
-        }
-
-        System.out.println( "Using port " + port + " for testing." );
-
-        threadPoolFilter = new IoThreadPoolFilter();
-        threadPoolFilter.start();
-
-        acceptor.addFilter( Integer.MAX_VALUE, threadPoolFilter );
-        datagramAcceptor.addFilter( Integer.MAX_VALUE, threadPoolFilter );
-    }
-
-    protected void tearDown() throws Exception
-    {
-        acceptor.unbind( new InetSocketAddress( port ) );
-        datagramAcceptor.unbind( new InetSocketAddress( port ) );
-        threadPoolFilter.stop();
-    }
-
     public void testTCP() throws Exception
     {
         EchoTCPClient client = new EchoTCPClient();
@@ -285,6 +184,6 @@
 
     public static void main( String[] args )
     {
-        junit.textui.TestRunner.run( Test.class );
+        junit.textui.TestRunner.run( AcceptorTest.class );
     }
 }

Added: directory/network/mina/trunk/src/test/org/apache/mina/examples/echoserver/ConnectorTest.java
URL: http://svn.apache.org/viewcvs/directory/network/mina/trunk/src/test/org/apache/mina/examples/echoserver/ConnectorTest.java?view=auto&rev=157501
==============================================================================
--- directory/network/mina/trunk/src/test/org/apache/mina/examples/echoserver/ConnectorTest.java
(added)
+++ directory/network/mina/trunk/src/test/org/apache/mina/examples/echoserver/ConnectorTest.java
Mon Mar 14 19:34:17 2005
@@ -0,0 +1,156 @@
+/*
+ * @(#) $Id$
+ */
+package org.apache.mina.examples.echoserver;
+
+import java.net.InetSocketAddress;
+
+import junit.framework.Assert;
+
+import org.apache.mina.common.ByteBuffer;
+import org.apache.mina.examples.echoserver.ssl.BogusSSLContextFactory;
+import org.apache.mina.io.IoConnector;
+import org.apache.mina.io.IoHandlerAdapter;
+import org.apache.mina.io.IoSession;
+import org.apache.mina.io.datagram.DatagramConnector;
+import org.apache.mina.io.filter.SSLFilter;
+import org.apache.mina.io.socket.SocketConnector;
+
+/**
+ * Tests echo server example.
+ * 
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$
+ */
+public class ConnectorTest extends AbstractTest
+{
+    public void testTCP() throws Exception
+    {
+        IoConnector connector = new SocketConnector();
+        connector.addFilter( Integer.MAX_VALUE, super.threadPoolFilter );
+        testTCP0( connector );
+    }
+
+    /**
+     * Client-side SSL doesn't work for now.
+     */
+    public void _testTCPWithSSL() throws Exception
+    {
+        // Add an SSL filter to acceptor
+        SSLFilter acceptorSSLFilter = new SSLFilter( BogusSSLContextFactory.getInstance(
true ) );
+        acceptor.addFilter( Integer.MAX_VALUE - 1, acceptorSSLFilter );
+
+        // Create a connector
+        IoConnector connector = new SocketConnector();
+        connector.addFilter( Integer.MAX_VALUE, super.threadPoolFilter );
+        
+        // Add an SSL filter to connector
+        SSLFilter connectorSSLFilter = new SSLFilter( BogusSSLContextFactory.getInstance(
false ) );
+        connectorSSLFilter.setDebug( SSLFilter.Debug.ON );
+        connector.addFilter( Integer.MAX_VALUE - 1, connectorSSLFilter );
+
+        testTCP0( connector );
+    }
+    
+    private void testTCP0( IoConnector connector ) throws Exception
+    {
+        EchoConnectorHandler handler = new EchoConnectorHandler();
+        ByteBuffer readBuf = handler.readBuf;
+        IoSession session = connector.connect( new InetSocketAddress( port ),
+                                               handler );
+        
+        for( int i = 0; i < 10; i ++ )
+        {
+            ByteBuffer buf = ByteBuffer.allocate( 16 );
+            buf.limit( 16 );
+            fillWriteBuffer( buf, i );
+            buf.flip();
+
+            Object marker;
+            if( ( i & 1 ) == 0 )
+            {
+                marker = new Integer( i );
+            }
+            else
+            {
+                marker = null;
+            }
+
+            session.write( buf, marker );
+
+            // This will align message arrival order in UDP
+            for( int j = 0; j < 30; j ++ )
+            {
+                if( readBuf.position() == ( i + 1 ) * 16 )
+                {
+                    break;
+                }
+                Thread.sleep( 10 );
+            }
+        }
+        
+        Thread.sleep( 300 );
+        session.close();
+        
+        Assert.assertEquals( 160, readBuf.position() );
+        readBuf.flip();
+        
+        ByteBuffer expectedBuf = ByteBuffer.allocate( 160 );
+        for( int i = 0; i < 10; i ++ ) {
+            expectedBuf.limit( ( i + 1 ) * 16 );
+            fillWriteBuffer( expectedBuf, i );
+        }
+        expectedBuf.position( 0 );
+        assertEquals(expectedBuf, readBuf);
+    }
+
+    public void testUDP() throws Exception
+    {
+        IoConnector connector = new DatagramConnector();
+        connector.addFilter( Integer.MAX_VALUE, super.threadPoolFilter );
+        testTCP0( connector );
+    }
+    
+    private void fillWriteBuffer( ByteBuffer writeBuf, int i )
+    {
+        while( writeBuf.remaining() > 0 )
+        {
+            writeBuf.put( ( byte ) ( i ++ ) );
+        }
+    }
+
+    public static void main( String[] args )
+    {
+        junit.textui.TestRunner.run( ConnectorTest.class );
+    }
+    
+    private static class EchoConnectorHandler extends IoHandlerAdapter
+    {
+        private ByteBuffer readBuf = ByteBuffer.allocate( 1024 );
+        private int counter = 0;
+
+        public void dataRead( IoSession session, ByteBuffer buf )
+        {
+            readBuf.put( buf );
+        }
+        
+        public void dataWritten( IoSession session, Object marker )
+        {
+            if( ( counter & 1 ) == 0 )
+            {
+                Assert.assertEquals( new Integer( counter ), marker );
+            }
+            else
+            {
+                Assert.assertNull( marker );
+            }
+            
+            counter ++;
+        }
+
+        public void exceptionCaught( IoSession session, Throwable cause )
+        {
+            cause.printStackTrace();
+        }
+    }
+}

Propchange: directory/network/mina/trunk/src/test/org/apache/mina/examples/echoserver/ConnectorTest.java
------------------------------------------------------------------------------
    svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision



Mime
View raw message