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


/**
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class ReceiptReaderTest extends PacketTestSupport {
    private String id;
    private String correlationId;

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

    /*
     * @see TestCase#setUp()
     */
    protected void setUp() throws Exception {
        super.setUp();
        this.id = "testid";
        this.correlationId = "testCorrelationId";
    }

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

    public void testGetPacketType() {
        ReceiptReader reader = new ReceiptReader();
        assertTrue(reader.getPacketType() == Packet.RECEIPT_INFO);
    }

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

        ReceiptWriter writer = new ReceiptWriter();
        ReceiptReader reader = new ReceiptReader();
        try {
            byte[] data = writer.writePacketToByteArray(info);
            Receipt receipt = (Receipt) reader.readPacketFromByteArray(data);

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

    public void testTime() {

        Receipt info = (Receipt) createPacket();

        ReceiptWriter writer = new ReceiptWriter();
        ReceiptReader reader = new ReceiptReader();

        try {
            int count = 100000;
            long startTime = System.currentTimeMillis();
            for (int i = 0; i < count; i++) {
                byte[] data = writer.writePacketToByteArray(info);
                Receipt test = (Receipt) 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) {
        Receipt receipt = (Receipt) packet;
        assertTrue(receipt.getId().equals(this.id));
        assertTrue(receipt.getCorrelationId().equals(this.correlationId));
    }

    protected Packet createPacket() {
        Receipt info = new Receipt();
        info.setId(this.id);
        info.setCorrelationId(this.correlationId);
        return info;
    }

}
