/*
 * Created on Mar 8, 2004
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package org.codehaus.activemq.message;

import junit.framework.TestCase;

import javax.jms.JMSException;
import org.codehaus.activemq.io.WireFormat;
import org.codehaus.activemq.io.impl.DefaultWireFormat;
import org.codehaus.activemq.io.impl.PacketReader;
import org.codehaus.activemq.io.impl.PacketWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

/**
 * @version $Revision: 1.2 $
 */
public abstract class PacketTestSupport extends TestCase {
    protected WireFormat wireFormat = new DefaultWireFormat();
    protected PacketWriter writer;
    protected PacketReader reader;
    protected int packetCount = 10;

    public void testWireFormat() throws IOException, JMSException {
        WireFormat wireFormat = new DefaultWireFormat();

        Packet packet = createPacket();
        System.out.println("Created packet: " + packet + " with type: " + packet.getPacketType());

        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        DataOutputStream dataOut = new DataOutputStream(buffer);
        wireFormat.writePacket(packet, dataOut);
        dataOut.close();
        byte[] bytes = buffer.toByteArray();

        DataInputStream dataIn = new DataInputStream(new ByteArrayInputStream(bytes));
        int type = dataIn.readByte();
        System.out.println("Read type: " + type);
        Packet otherPacket = wireFormat.readPacket(type, dataIn);

        System.out.println("Read packet: " + otherPacket);
        assertPacket(otherPacket, packet);
    }

    /*
    public void testReadPacket() throws IOException {
        Packet packet = createPacket();
        byte[] data = writer.writePacketToByteArray(packet);
        Packet testInfo = reader.readPacketFromByteArray(data);
        assertValidPacket(testInfo);
    }
    */

    public void testToFromBytes() throws JMSException, IOException {
        Packet packet = createPacket();
        byte[] data = wireFormat.toBytes(packet);

        System.out.println("Written byte array of size: " + data.length);

        Packet value = wireFormat.fromBytes(data);
        assertValidPacket(value);
    }

    public void testReadAndWriteDataInOut() throws JMSException, IOException {
        Packet packet = createPacket();
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        DataOutputStream out = new DataOutputStream(buffer);
        wireFormat.writePacket(packet, out);
        out.close();

        byte[] data = buffer.toByteArray();
        System.out.println("Written byte array of size: " + data.length);

        DataInputStream in = new DataInputStream(new ByteArrayInputStream(data));
        Packet value = wireFormat.readPacket(in);
        assertValidPacket(value);
    }


    public void testReadAndWriteDataInOutLoop() throws JMSException, IOException {
        Packet packet = createPacket();
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        DataOutputStream out = new DataOutputStream(buffer);

        for (int i = 0; i < packetCount; i++) {
            wireFormat.writePacket(packet, out);
        }

        DataInputStream in = new DataInputStream(new ByteArrayInputStream(buffer.toByteArray()));
        for (int i = 0; i < packetCount; i++) {
            Packet value = wireFormat.readPacket(in);
            assertValidPacket(value);
        }
    }

    protected void assertPacket(Packet packet, Packet expected) {
        assertValidPacket(packet);
    }

    protected abstract Packet createPacket();


    protected abstract void assertValidPacket(Packet packet);

}
