/*
 * 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.MessageAck;
import org.activemq.message.Packet;

/**
 * To change the template for this generated type comment go to Window - Preferences - Java - Code Generation - Code
 * and Comments
 */
public class MessageAckReaderTest extends TestCase {
	private short id;
    private String consumerId;
    private String messageId;
    private Object transactedId;
    private boolean messageRead;
    private boolean persistent;
    private ActiveMQDestination destination;
    private boolean xaTransacted;
    private boolean expired;
    
 

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

	/*
	 * @see TestCase#setUp()
	 */
	protected void setUp() throws Exception {
		super.setUp();
        this.id = 3445;
		this.consumerId = "testid";
        this.messageId = "ID:ewrre-erer-err";
        this.transactedId = "45454";
        this.messageRead = true;
        this.persistent=true;
        this.xaTransacted=false;
        this.expired = true;
        this.destination = new ActiveMQTopic("FOO.BAR");
	}

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

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

	public void testGetPacketType() {
		MessageAckReader reader = new MessageAckReader();
		assertTrue(reader.getPacketType() == Packet.ACTIVEMQ_MSG_ACK);
	}

	public void testReadPacket() {
		MessageAck ack = new MessageAck();
		ack.setId(this.id);
        ack.setConsumerId(this.consumerId);
		ack.setTransactionId(this.transactedId);
        ack.setMessageID(messageId);
        ack.setMessageRead(this.messageRead);
        ack.setPersistent(persistent);
        ack.setDestination(destination);
        ack.setExpired(expired);

		MessageAckWriter writer = new MessageAckWriter();
		MessageAckReader reader = new MessageAckReader();
		try {
			byte[] data = writer.writePacketToByteArray(ack);
			MessageAck mack = (MessageAck) reader.readPacketFromByteArray(data);

			assertTrue(mack.getConsumerId().equals(this.consumerId));
            assertTrue(mack.getTransactionId().equals(this.transactedId));
            assertTrue(mack.isMessageRead()==this.messageRead);
            assertTrue(mack.isPersistent()==this.persistent);
            assertTrue(mack.isExpired()==this.expired);
            assertTrue(mack.isXaTransacted()==this.xaTransacted);
            assertTrue(mack.getDestination().equals(this.destination));
		}
		catch (Throwable e) {
			e.printStackTrace();
			fail(e.getMessage());
		}
	}

	public void testTime() {

		MessageAck ack = new MessageAck();
        ack.setId(this.id);
        ack.setConsumerId(this.consumerId);
        ack.setTransactionId(this.transactedId);
        ack.setMessageID(messageId);
        ack.setMessageRead(this.messageRead);
        ack.setPersistent(persistent);
        ack.setDestination(destination);

		MessageAckWriter writer = new MessageAckWriter();
		MessageAckReader reader = new MessageAckReader();
		try {
			int count = 100000;
			long startTime = System.currentTimeMillis();
			for (int i = 0; i < count; i++) {
				byte[] data = writer.writePacketToByteArray(ack);
				MessageAck mack = (MessageAck) 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);
		}
	}

}
