/*
 * 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 java.io.IOException;
import java.util.HashMap;
import javax.jms.JMSException;
import org.codehaus.activemq.message.ActiveMQMapMessage;
import org.codehaus.activemq.message.Packet;

/**
 * @author Rob To change the template for this generated type comment go to Window - Preferences - Java - Code
 *         Generation - Code and Comments
 */
public class ActiveMQMapMessageReaderTest extends ActiveMQMessageReaderTest {

	private HashMap table;
	public static void main(String[] args) {
		junit.textui.TestRunner.run(ActiveMQMapMessageReaderTest.class);
	}

	/*
	 * @see TestCase#setUp()
	 */
	protected void setUp() throws Exception {
		super.setUp();
		this.table = new HashMap();
		for (int i = 0; i < 10; i++) {
			String str = "test" + i;
			this.table.put(str, str);
		}
	}

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

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

	public void testGetPacketType() {
		ActiveMQMapMessage msg = new ActiveMQMapMessage();
		assertTrue(msg.getPacketType() == Packet.ACTIVEMQ_MAP_MESSAGE);
	}

	public void testReadPacket() {
		ActiveMQMapMessageReader reader = new ActiveMQMapMessageReader();
		ActiveMQMapMessageWriter writer = new ActiveMQMapMessageWriter();
		ActiveMQMapMessage msg1 = new ActiveMQMapMessage();
		try {
			msg1.setTable(this.table);
			super.initializeMessage(msg1);
			byte[] data = writer.writePacketToByteArray(msg1);
			ActiveMQMapMessage msg2 = (ActiveMQMapMessage) reader.readPacketFromByteArray(data);
			super.testEquals(msg1, msg2);
			assertTrue(msg1.getTable().equals(msg2.getTable()));
		}
        catch(JMSException jmsEx){
            jmsEx.printStackTrace();
            assertTrue(false);
        }
		catch (IOException e) {
			e.printStackTrace();
			assertTrue(false);
		}
	}

	public void testTime() {

		ActiveMQMapMessageReader reader = new ActiveMQMapMessageReader();
		ActiveMQMapMessageWriter writer = new ActiveMQMapMessageWriter();
		ActiveMQMapMessage msg1 = new ActiveMQMapMessage();

		ActiveMQMapMessage msg2 = null;
		try {
			int count = 10000;
			long startTime = System.currentTimeMillis();

			msg1.setTable(this.table);
			super.initializeMessage(msg1);
			for (int i = 0; i < count; i++) {
				byte[] data = writer.writePacketToByteArray(msg1);
				msg2 = (ActiveMQMapMessage) 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);
		}
	}

}
