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

import org.activemq.message.ActiveMQXid;
import org.activemq.message.Packet;
import org.activemq.message.PacketTestSupport;
import org.activemq.message.TransactionType;
import org.activemq.message.XATransactionInfo;


/**
 * @version $Revision: 1.1.1.1 $
 */
public class XATransactionInfoReaderTest extends PacketTestSupport {
    protected static final int TYPE = TransactionType.START;
    protected static final ActiveMQXid XID = new ActiveMQXid(123, new byte[]{1, 2, 3, 4}, new byte[]{5, 6, 7, 8, 9, 10});

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

    /*
     * @see TestCase#setUp()
     */
    protected void setUp() throws Exception {
        super.setUp();
    }


    public void testGetPacketType() {
        XATransactionInfoReader reader = new XATransactionInfoReader();
        assertTrue(reader.getPacketType() == Packet.XA_TRANSACTION_INFO);
    }

    public void testReadXidPacket() throws Exception {
        XATransactionInfo info = (XATransactionInfo) createXidPacket();

        XATransactionInfoWriter writer = new XATransactionInfoWriter();
        XATransactionInfoReader reader = new XATransactionInfoReader();
        byte[] data = writer.writePacketToByteArray(info);
        XATransactionInfo testInfo = (XATransactionInfo) reader.readPacketFromByteArray(data);

        assertPacket(testInfo, info);
    }

    public void testReadNoXidPacket() throws Exception {
        XATransactionInfo info = (XATransactionInfo) createNoXidPacket();

        XATransactionInfoWriter writer = new XATransactionInfoWriter();
        XATransactionInfoReader reader = new XATransactionInfoReader();
        byte[] data = writer.writePacketToByteArray(info);
        XATransactionInfo testInfo = (XATransactionInfo) reader.readPacketFromByteArray(data);

        assertPacket(testInfo, info);
    }

    public void testTime() {
        XATransactionInfo info = (XATransactionInfo) createXidPacket();

        XATransactionInfoWriter writer = new XATransactionInfoWriter();
        XATransactionInfoReader reader = new XATransactionInfoReader();
        XATransactionInfo testInfo = null;
        try {
            int count = 100000;
            long startTime = System.currentTimeMillis();
            for (int i = 0; i < count; i++) {
                byte[] data = writer.writePacketToByteArray(info);
                testInfo = (XATransactionInfo) 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();
            fail();
        }
    }

    protected void assertPacket(Packet packet, Packet expected) {
        XATransactionInfo testInfo = (XATransactionInfo) packet;
        XATransactionInfo xaExpected = (XATransactionInfo) expected;
        assertEquals(xaExpected.getType(), testInfo.getType());
        assertEquals(xaExpected.getXid(), testInfo.getXid());
    }

    protected void assertValidPacket(Packet packet) {
        XATransactionInfo testInfo = (XATransactionInfo) packet;
        assertEquals(testInfo.getType(), TYPE);
        assertEquals(testInfo.getXid(), XID);
    }

    protected Packet createPacket() {
        return createXidPacket();
    }

    protected Packet createXidPacket() {
        XATransactionInfo info = new XATransactionInfo();
        info.setType(TYPE);
        info.setXid(XID);
        return info;
    }

    protected Packet createNoXidPacket() {
        XATransactionInfo info = new XATransactionInfo();
        info.setType(TransactionType.GET_RM_ID);
        return info;
    }

}
