/*
 * 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 org.codehaus.activemq.message.ActiveMQBytesMessage;
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 ActiveMQBytesMessageReaderTest extends ActiveMQMessageReaderTest {

	private byte[] testdata;
	public static void main(String[] args) {
		junit.textui.TestRunner.run(ActiveMQBytesMessageReaderTest.class);
	}

	/*
	 * @see TestCase#setUp()
	 */
	protected void setUp() throws Exception {
		super.setUp();
		testdata = new byte[1024 *2];
		for (int i = 0; i < testdata.length;i++){
			testdata[i] = (byte)i;
		}

	}

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

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

	public void testGetPacketType() {
		ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
		assertTrue(msg.getPacketType() == Packet.ACTIVEMQ_BYTES_MESSAGE);
	}

	public void testReadPacket() {
		ActiveMQBytesMessageReader reader = new ActiveMQBytesMessageReader();
		ActiveMQBytesMessageWriter writer = new ActiveMQBytesMessageWriter();
		ActiveMQBytesMessage msg1 = new ActiveMQBytesMessage();
		try {
			msg1.setBodyAsBytes(testdata);
			super.initializeMessage(msg1);
			byte[] data = writer.writePacketToByteArray(msg1);
			ActiveMQBytesMessage msg2 = (ActiveMQBytesMessage) reader.readPacketFromByteArray(data);
			super.testEquals(msg1, msg2);
			byte[] d2 = msg2.getBodyAsBytes();
			assertTrue(d2.length==testdata.length);
			for(int i = 0; i < testdata.length; i++)
			assertTrue(d2[i]==testdata[i]);
		}
		catch (IOException e) {
			e.printStackTrace();
			assertTrue(false);
		}
		
	}
	
	public void testTime(){

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

}
