/*
 * 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.Hashtable;
import javax.jms.JMSException;
import org.codehaus.activemq.message.ActiveMQObjectMessage;
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 ActiveMQObjectMessageReaderTest extends ActiveMQMessageReaderTest {

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

	/*
	 * @see TestCase#setUp()
	 */
	protected void setUp() throws Exception {
		super.setUp();
		this.table = new Hashtable();
		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 ActiveMQObjectMessageReaderTest(String arg0) {
		super(arg0);
	}

	public void testGetPacketType() {
		ActiveMQObjectMessage msg = new ActiveMQObjectMessage();
		assertTrue(msg.getPacketType() == Packet.ACTIVEMQ_OBJECT_MESSAGE);
	}

	public void testReadPacket() {
		ActiveMQObjectMessageReader reader = new ActiveMQObjectMessageReader();
		ActiveMQObjectMessageWriter writer = new ActiveMQObjectMessageWriter();
		ActiveMQObjectMessage msg1 = new ActiveMQObjectMessage();
		try {
			msg1.setObject(this.table);
			super.initializeMessage(msg1);
			byte[] data = writer.writePacketToByteArray(msg1);
			ActiveMQObjectMessage msg2 = (ActiveMQObjectMessage) reader.readPacketFromByteArray(data);
			super.testEquals(msg1, msg2);
			assertTrue(msg1.getObject().equals(msg2.getObject()));
		}
		catch (IOException e) {
			e.printStackTrace();
			assertTrue(false);
		}
		catch (JMSException ex) {
			ex.printStackTrace();
			assertTrue(false);
		}
	}
	
	public void testTime(){

		ActiveMQObjectMessageReader reader = new ActiveMQObjectMessageReader();
		ActiveMQObjectMessageWriter writer = new ActiveMQObjectMessageWriter();
		ActiveMQObjectMessage msg1 = new ActiveMQObjectMessage();
		
		ActiveMQObjectMessage msg2 = null;
		try {
			int count = 10000;
			long startTime = System.currentTimeMillis();
		
			msg1.setObject(this.table);
			super.initializeMessage(msg1);
			for (int i = 0; i < count; i++){
				byte[] data = writer.writePacketToByteArray(msg1);
				msg2 = (ActiveMQObjectMessage) 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);
		}
	}

}
