/*
 * 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.Enumeration;
import javax.jms.JMSException;
import org.codehaus.activemq.message.ActiveMQStreamMessage;
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 ActiveMQStreamMessageReaderTest extends ActiveMQMessageReaderTest {

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

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

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

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

	public void testGetPacketType() {
		ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
		assertTrue(msg.getPacketType() == Packet.ACTIVEMQ_STREAM_MESSAGE);
	}

	public void testReadPacket() {
		ActiveMQStreamMessageReader reader = new ActiveMQStreamMessageReader();
		ActiveMQStreamMessageWriter writer = new ActiveMQStreamMessageWriter();
		ActiveMQStreamMessage msg1 = new ActiveMQStreamMessage();
		try {
			msg1.setBooleanProperty("booleanprop",true);
			msg1.setLongProperty("longproperty",67l);
			msg1.setFloatProperty("floatproperty",4.6f);
			super.initializeMessage(msg1);
			byte[] data = writer.writePacketToByteArray(msg1);
			ActiveMQStreamMessage msg2 = (ActiveMQStreamMessage) reader.readPacketFromByteArray(data);
			super.testEquals(msg1, msg2);
			for (Enumeration e = msg1.getPropertyNames();e.hasMoreElements();){
				String name = e.nextElement().toString();
				assertTrue(msg1.getObjectProperty(name).equals(msg2.getObjectProperty(name)));
			}
		}
		catch (IOException e) {
			e.printStackTrace();
			assertTrue(false);
		}
		catch (JMSException ex) {
			ex.printStackTrace();
			assertTrue(false);
		}
	}
	
	public void testTime(){

		ActiveMQStreamMessageReader reader = new ActiveMQStreamMessageReader();
		ActiveMQStreamMessageWriter writer = new ActiveMQStreamMessageWriter();
		ActiveMQStreamMessage msg1 = new ActiveMQStreamMessage();
		
		ActiveMQStreamMessage msg2 = null;
		try {
			int count = 10000;
			long startTime = System.currentTimeMillis();
		
			msg1.setBooleanProperty("booleanprop",true);
			msg1.setLongProperty("longproperty",67l);
			msg1.setFloatProperty("floatproperty",4.6f);
			super.initializeMessage(msg1);
			for (int i = 0; i < count; i++){
				byte[] data = writer.writePacketToByteArray(msg1);
				msg2 = (ActiveMQStreamMessage) 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);
		}
	}

}
