/**
 *
 * 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.geronimo.network.protocol;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import junit.framework.TestCase;


/**
 * @version $Revision: 1.1 $ $Date: 2004/03/18 04:05:51 $
 */
public class PacketStreamTest extends TestCase {

    EchoUpProtocol eup = new EchoUpProtocol();
    boolean failed;

    public void testStream() throws Exception {
        new Thread(new WriterThread((short) 1024), "Test Writer").start();

        PacketInputStream in = new PacketInputStream(eup);
        ObjectInputStream objIn = new ObjectInputStream(in);
        String msg = (String) objIn.readObject();

        assertEquals(msg, "Hello World!");
        assertFalse("Writer thread failed", failed);
    }

    public void testStreamManyPackets() throws Exception {
        new Thread(new WriterThread((short) 2), "Test Writer").start();

        PacketInputStream in = new PacketInputStream(eup);
        ObjectInputStream objIn = new ObjectInputStream(in);
        String msg = (String) objIn.readObject();

        assertEquals(msg, "Hello World!");
        assertFalse("Writer thread failed", failed);
    }

    class WriterThread implements Runnable {

        short packetSize;

        WriterThread(short packetSize) {
            this.packetSize = packetSize;
        }

        public void run() {
            try {
                PacketOutputStream out = new PacketOutputStream(eup, packetSize);
                ObjectOutputStream objOut = new ObjectOutputStream(out);
                objOut.writeObject(new String("Hello World!"));
                objOut.flush();
            } catch (IOException e) {
                failed = true;
            }
        }
    }


    public void setUp() throws Exception {
        eup = new EchoUpProtocol();
        failed = false;
    }

}

@@ -764,16 +764,63 @@
 tream;%0A%0A
+import EDU.oswego.cs.dl.util.concurrent.Latch;%0A
 import j
@@ -875,17 +875,17 @@
 sion: 1.
-1
+2
  $ $Date
@@ -898,19 +898,19 @@
 /03/
-18 04:05:51
+20 23:03:06
  $%0A 
@@ -984,39 +984,38 @@
 ocol eup
- = new EchoUpProtocol()
+;%0A    Latch startLatch
 ;%0A    bo
@@ -1264,32 +1264,63 @@
 InputStream(in);
+%0A%0A        startLatch.release();
 %0A        String 
@@ -1709,16 +1709,47 @@
 eam(in);
+%0A%0A        startLatch.release();
 %0A       
@@ -2108,16 +2108,55 @@
   try %7B%0A
+                startLatch.acquire();%0A%0A
         
@@ -2467,16 +2467,94 @@
 = true;%0A
+            %7D catch (InterruptedException e) %7B%0A                failed = true;%0A
         
@@ -2576,17 +2576,16 @@
 %0A    %7D%0A%0A
-%0A
     publ
@@ -2651,24 +2651,58 @@
 Protocol();%0A
+        startLatch = new Latch();%0A
         fail

Make sure the stack is properly constructed before sending info.