Author: trustin
Date: Tue Nov  2 06:34:23 2004
New Revision: 56376

Modified:
   
incubator/directory/seda/trunk/src/test/org/apache/seda/examples/DiscardProtocolProviderTest.java
   
incubator/directory/seda/trunk/src/test/org/apache/seda/examples/EchoProtocolProviderTest.java
Log:
Added UDP test cases for Echo and Discard.

UDP tests don't work for now because UDP I/O events doesn't generate Connect 
and Disconnect events which make encoder manager and decoder manager ready for 
the client key.

Modified: 
incubator/directory/seda/trunk/src/test/org/apache/seda/examples/DiscardProtocolProviderTest.java
==============================================================================
--- 
incubator/directory/seda/trunk/src/test/org/apache/seda/examples/DiscardProtocolProviderTest.java
   (original)
+++ 
incubator/directory/seda/trunk/src/test/org/apache/seda/examples/DiscardProtocolProviderTest.java
   Tue Nov  2 06:34:23 2004
@@ -19,8 +19,10 @@
 
 import java.io.InputStream;
 import java.io.OutputStream;
-
+import java.net.DatagramPacket;
+import java.net.DatagramSocket;
 import java.net.InetAddress;
+import java.net.InetSocketAddress;
 import java.net.Socket;
 import java.net.SocketTimeoutException;
 
@@ -41,7 +43,7 @@
         super(new DiscardProtocolProvider());
     }
 
-    public void testDiscardProtocol() throws Exception
+    public void testTCP() throws Exception
     {
         Socket socket = new Socket(InetAddress.getLocalHost(), port);
         socket.setSoTimeout(100);
@@ -71,6 +73,34 @@
 
         in.close();
         out.close();
+        socket.close();
+    }
+
+    public void testUDP() throws Exception
+    {
+       DatagramSocket socket = new DatagramSocket();
+
+        byte[] data = new byte[512];
+        byte value = (byte) 0;
+
+        for (int i = data.length - 1; i >= 0; i--)
+        {
+            data[i] = (byte) i;
+        }
+
+       socket.send(new DatagramPacket(data, 0, data.length, new 
InetSocketAddress("localhost", port)));
+       
+
+       socket.setSoTimeout(100);
+        try
+        {
+               socket.receive(new DatagramPacket(data, 0, data.length));
+            fail("Discard protocol provider returns data.");
+        }
+        catch (SocketTimeoutException e)
+        {
+            // it is ok because there should be no data returned
+        }
         socket.close();
     }
 }

Modified: 
incubator/directory/seda/trunk/src/test/org/apache/seda/examples/EchoProtocolProviderTest.java
==============================================================================
--- 
incubator/directory/seda/trunk/src/test/org/apache/seda/examples/EchoProtocolProviderTest.java
      (original)
+++ 
incubator/directory/seda/trunk/src/test/org/apache/seda/examples/EchoProtocolProviderTest.java
      Tue Nov  2 06:34:23 2004
@@ -21,6 +21,7 @@
 import java.net.SocketTimeoutException;
 
 import org.apache.commons.net.EchoTCPClient;
+import org.apache.commons.net.EchoUDPClient;
 import org.apache.seda.ProtocolTestCase;
 
 
@@ -38,7 +39,7 @@
         super(new EchoProtocolProvider());
     }
 
-    public void testEchoProtocol() throws Exception
+    public void testTCP() throws Exception
     {
         EchoTCPClient client = new EchoTCPClient();
         client.connect(InetAddress.getLocalHost(), port);
@@ -88,10 +89,44 @@
         client.disconnect();
     }
 
-       /**
-        * @param writeBuf
-        * @param i
-        */
+    public void testUDP() throws Exception
+    {
+       EchoUDPClient client = new EchoUDPClient();
+        client.open();
+        client.setSoTimeout(3000);
+
+        byte[] writeBuf = new byte[16];
+
+        for (int i = 0; i < 10; i++)
+        {
+            fillWriteBuffer(writeBuf, i);
+            client.send(writeBuf, writeBuf.length, InetAddress.getLocalHost(), 
port);
+        }
+
+        byte[] readBuf = new byte[writeBuf.length];
+
+        for (int i = 0; i < 10; i++)
+        {
+            fillWriteBuffer(writeBuf, i);
+
+            assertEquals(readBuf.length, client.receive(readBuf, 
readBuf.length));
+            assertEquals(writeBuf, readBuf);
+        }
+
+        client.setSoTimeout(100);
+
+        try
+        {
+            client.receive(readBuf);
+            fail("Unexpected incoming data.");
+        }
+        catch (SocketTimeoutException e)
+        {
+        }
+
+        client.close();
+    }
+
        private void fillWriteBuffer(byte[] writeBuf, int i) {
                for (int j = writeBuf.length - 1; j >= 0; j--)
                {

Reply via email to