/*
 * 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 junit.framework.TestCase;
import org.activemq.message.ActiveMQDestination;
import org.activemq.message.ActiveMQTopic;
import org.activemq.message.Packet;
import org.activemq.message.ProducerInfo;

/**
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class ProducerInfoReaderTest extends TestCase {
    private short id;
    private String producerId;
    private ActiveMQDestination destination;
    private String clientId;
    private long startTime;
    private boolean started;

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

    /*
     * @see TestCase#setUp()
     */
    protected void setUp() throws Exception {
        super.setUp();
        this.id = 3445;
        this.producerId = "testProducerId";
        this.clientId = "testclientId";
        this.destination = new ActiveMQTopic("testtopic");
        this.startTime = System.currentTimeMillis();
        this.started = true;
    }

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

    /**
     * Constructor for ProducerInfoReaderTest.
     *
     * @param arg0
     */
    public ProducerInfoReaderTest(String arg0) {
        super(arg0);
    }

    public void testGetPacketType() {
        ProducerInfoReader reader = new ProducerInfoReader();
        assertTrue(reader.getPacketType() == Packet.PRODUCER_INFO);
    }

    public void testReadPacket() {
        ProducerInfo info = new ProducerInfo();
        info.setId(this.id);
        info.setClientId(this.clientId);
        info.setDestination(this.destination);
        info.setStartTime(this.startTime);
        info.setStarted(this.started);

        ProducerInfoWriter writer = new ProducerInfoWriter();
        ProducerInfoReader reader = new ProducerInfoReader();
        try {
            byte[] data = writer.writePacketToByteArray(info);
            ProducerInfo testInfo = (ProducerInfo) reader.readPacketFromByteArray(data);

            assertTrue(testInfo.getId() == this.id);
            assertTrue(testInfo.getClientId().equals(this.clientId));
            assertTrue(testInfo.getDestination().equals(this.destination));
            assertTrue(testInfo.getStartTime() == this.startTime);
            assertTrue(testInfo.isStarted() == this.started);
        }
        catch (Throwable e) {
            e.printStackTrace();
            assertTrue(false);
        }
    }

    public void testTime() {

        ProducerInfo info = new ProducerInfo();
        info.setId(this.id);
        info.setClientId(this.clientId);
        info.setDestination(this.destination);
        info.setStartTime(this.startTime);
        info.setStarted(this.started);

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

}
