/*
 * 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.ConsumerInfo;
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 ConsumerInfoReaderTest extends TestCase
{
	private String consumerId;
	private ActiveMQDestination destination;
	private String clientId;
	private String subscriberName;
	private String selector;
	private long startTime;
	private boolean started;
	private int consumerNo;
    private int prefetchNumber;

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

	/*
	 * @see TestCase#setUp()
	 */
	protected void setUp() throws Exception
	{
		super.setUp();
		this.consumerId = "testConsumerId";
		this.clientId = "testclientId";
		this.subscriberName = "testsubscriberName";
		this.selector = "testselector";
		this.destination = new ActiveMQTopic("testtopic");
		this.startTime = System.currentTimeMillis();
		this.started = true;
		this.consumerNo = 3;
        this.prefetchNumber = 13;
	}

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

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

	public void testGetPacketType()
	{
		ConsumerInfoReader reader = new ConsumerInfoReader();
		assertTrue(reader.getPacketType()==Packet.CONSUMER_INFO);
	}

	public void testReadPacket()
	{
		ConsumerInfo info = new ConsumerInfo();
		info.setConsumerId(consumerId);
		info.setConsumerNo(this.consumerNo);
        info.setPrefetchNumber(this.prefetchNumber);
		info.setClientId(this.clientId);
		info.setConsumerName(this.subscriberName);
		info.setSelector(this.selector);
		info.setDestination(this.destination);
		info.setStartTime(this.startTime);
		info.setStarted(this.started);
		
		ConsumerInfoWriter writer = new ConsumerInfoWriter();
		ConsumerInfoReader reader = new ConsumerInfoReader();
		try {
		byte[] data = writer.writePacketToByteArray(info);
		ConsumerInfo testInfo = (ConsumerInfo) reader.readPacketFromByteArray(data);
		
			assertTrue(testInfo.getConsumerId().equals(this.consumerId));
			assertTrue(testInfo.getConsumerNo()==this.consumerNo);
            assertTrue(testInfo.getPrefetchNumber() == this.prefetchNumber);
			assertTrue(testInfo.getClientId().equals(this.clientId));
			assertTrue(testInfo.getConsumerName().equals(this.subscriberName));
			assertTrue(testInfo.getSelector().equals(this.selector));
			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(){

		ConsumerInfo info = new ConsumerInfo();
		info.setConsumerId(this.consumerId);
		info.setConsumerNo(this.consumerNo);
        info.setPrefetchNumber(this.prefetchNumber);
		info.setClientId(this.clientId);
		info.setConsumerName(this.subscriberName);
		info.setSelector(this.selector);
		info.setDestination(this.destination);
		info.setStartTime(this.startTime);
		info.setStarted(this.started);
		
		ConsumerInfoWriter writer = new ConsumerInfoWriter();
		ConsumerInfoReader reader = new ConsumerInfoReader();
		ConsumerInfo testInfo = null;
		try {
			int count = 100000;
			long startTime = System.currentTimeMillis();
			for (int i = 0; i < count; i++){
				byte[] data = writer.writePacketToByteArray(info);
				testInfo = (ConsumerInfo) 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);
		}
	}

}
