/*
 * 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.io.impl;

import org.codehaus.activemq.message.Packet;
import org.codehaus.activemq.message.PacketTestSupport;
import org.codehaus.activemq.message.TransactionInfo;


/**
 * @version $Revision: 1.1 $
 */
public class TransactionInfoReaderTest extends PacketTestSupport {
    private String TransactionId;
    private int type;

    public static void main(String[] args) {
        junit.textui.TestRunner.run(TransactionInfoReaderTest.class);
    }

    /*
     * @see TestCase#setUp()
     */
    protected void setUp() throws Exception {
        super.setUp();
        this.TransactionId = "testTransactionId";
        this.type = 113;
    }


    public void testGetPacketType() {
        TransactionInfoReader reader = new TransactionInfoReader();
        assertTrue(reader.getPacketType() == Packet.TRANSACTION_INFO);
    }

    public void testReadPacket() {
        TransactionInfo info = (TransactionInfo) createPacket();

        TransactionInfoWriter writer = new TransactionInfoWriter();
        TransactionInfoReader reader = new TransactionInfoReader();
        try {
            byte[] data = writer.writePacketToByteArray(info);
            TransactionInfo testInfo = (TransactionInfo) reader.readPacketFromByteArray(data);

            assertPacket(testInfo, info);
        }
        catch (Throwable e) {
            e.printStackTrace();
            assertTrue(false);
        }
    }

    public void testTime() {
        TransactionInfo info = (TransactionInfo) createPacket();

        TransactionInfoWriter writer = new TransactionInfoWriter();
        TransactionInfoReader reader = new TransactionInfoReader();
        TransactionInfo testInfo = null;
        try {
            int count = 100000;
            long startTime = System.currentTimeMillis();
            for (int i = 0; i < count; i++) {
                byte[] data = writer.writePacketToByteArray(info);
                testInfo = (TransactionInfo) reader.readPacketFromByteArray(data);
            }
            long finishTime = System.currentTimeMillis();
            long totalTime = finishTime - startTime;
            long ps = (count * 1000) / totalTime;
            System.out.println("Time taken :" + totalTime + " for " + count + "iterations, = " + ps + " per sec.");

        }
        catch (Throwable e) {
            e.printStackTrace();
            assertTrue(false);
        }
    }

    protected void assertValidPacket(Packet packet) {
        TransactionInfo testInfo = (TransactionInfo) packet;
        assertTrue(testInfo.getId().equals(this.TransactionId));
        assertTrue(testInfo.getType() == this.type);
    }

    protected Packet createPacket() {
        TransactionInfo info = new TransactionInfo();
        info.setId(this.TransactionId);
        info.setType(this.type);
        return info;
    }

}
