Author: trustin
Date: Sat Oct 30 20:57:24 2004
New Revision: 56116

Added:
   
incubator/directory/seda/trunk/src/test/org/apache/seda/examples/EchoProtocolProviderTest.java
   (contents, props changed)
Modified:
   
incubator/directory/seda/trunk/src/test/org/apache/seda/examples/DiscardProtocolProviderTest.java
Log:
* DiscardProtocolProviderTest now fails if there are any incoming data
* Added: EchoProtocolProviderTest

Note: These test cases fail for now.


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
   Sat Oct 30 20:57:24 2004
@@ -17,9 +17,13 @@
 
 package org.apache.seda.examples;
 
+import java.io.InputStream;
+import java.io.OutputStream;
+
 import java.net.InetAddress;
+import java.net.Socket;
+import java.net.SocketTimeoutException;
 
-import org.apache.commons.net.DiscardTCPClient;
 import org.apache.seda.ProtocolTestCase;
 
 
@@ -39,19 +43,34 @@
 
     public void testDiscardProtocol() throws Exception
     {
-        DiscardTCPClient client = new DiscardTCPClient();
-        client.connect(InetAddress.getLocalHost(), port);
-        
+        Socket socket = new Socket(InetAddress.getLocalHost(), port);
+        socket.setSoTimeout(100);
+
+        InputStream in = socket.getInputStream();
+        OutputStream out = socket.getOutputStream();
+
         byte[] data = new byte[512];
-        byte value= (byte) 0;
-        for (int i = 0; i < 10; i++) {
-               for (int j = data.length-1; j >= 0; j--) {
-                       data[j] = (byte) j;
-               }
-               
-               client.getOutputStream().write(data);
+        byte value = (byte) 0;
+
+        for (int i = data.length - 1; i >= 0; i--)
+        {
+            data[i] = (byte) i;
         }
-        
-        client.disconnect();
+
+        out.write(data);
+
+        try
+        {
+            if (in.read() != 0)
+                fail("Discard protocol provider returns data.");
+        }
+        catch (SocketTimeoutException e)
+        {
+            // it is ok because there should be no data returned
+        }
+
+        in.close();
+        out.close();
+        socket.close();
     }
 }

Added: 
incubator/directory/seda/trunk/src/test/org/apache/seda/examples/EchoProtocolProviderTest.java
==============================================================================
--- (empty file)
+++ 
incubator/directory/seda/trunk/src/test/org/apache/seda/examples/EchoProtocolProviderTest.java
      Sat Oct 30 20:57:24 2004
@@ -0,0 +1,70 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+
+package org.apache.seda.examples;
+
+import java.net.InetAddress;
+import java.util.Arrays;
+
+import org.apache.commons.net.EchoTCPClient;
+import org.apache.seda.ProtocolTestCase;
+
+
+/**
+ * Document me.
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]"> Apache Directory
+ *         Project</a>
+ * @version $Rev$
+ */
+public class EchoProtocolProviderTest extends ProtocolTestCase
+{
+    public EchoProtocolProviderTest()
+    {
+        super(new EchoProtocolProvider());
+    }
+
+    public void testEchoProtocol() throws Exception
+    {
+        EchoTCPClient client = new EchoTCPClient();
+        client.connect(InetAddress.getLocalHost(), port);
+        client.setSoTimeout(3000);
+        
+        byte[] writeBuf = new byte[512];
+        byte[] readBuf = new byte[writeBuf.length];
+        byte value= (byte) 0;
+        for (int i = 0; i < 10; i++) {
+               for (int j = writeBuf.length-1; j >= 0; j--) {
+                       writeBuf[j] = (byte) j;
+               }
+               
+               client.getOutputStream().write(writeBuf);
+               
+               int readBytes = 0;
+               while (readBytes < readBuf.length) {
+                       int nBytes = client.getInputStream().read(readBuf, 
readBytes, readBuf.length - readBytes);
+                       if (nBytes < 0)
+                               fail("Unexpected disconnection.");
+                       readBytes += nBytes;
+               }
+               
+               assertTrue(Arrays.equals(writeBuf, readBuf));
+        }
+        
+        client.disconnect();
+    }
+}

Reply via email to