/*
 * 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 javax.jms.JMSException;
import org.codehaus.activemq.message.ActiveMQTextMessage;
import org.codehaus.activemq.message.Packet;

/**
 */
public class ActiveMQTextMessageReaderTest extends ActiveMQMessageReaderTest {

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

	/*
	 * @see TestCase#setUp()
	 */
	protected void setUp() throws Exception {
		super.setUp();
		StringBuffer buf = new StringBuffer();
		char start = 'a';
		int total = 1024 * 100;
		int count = 0;
		while (count < total) {
			char c = start;
			for (int i = 0; i < 26; i++) {
				buf.append(c);
				c += 1;
				count++;
			}
		}
		testStr = buf.toString();

	}

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

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

	public void testGetPacketType() {
		ActiveMQTextMessage msg = new ActiveMQTextMessage();
		assertTrue(msg.getPacketType() == Packet.ACTIVEMQ_TEXT_MESSAGE);
	}

	public void testReadPacket() {
		ActiveMQTextMessageReader reader = new ActiveMQTextMessageReader();
		ActiveMQTextMessageWriter writer = new ActiveMQTextMessageWriter();
		ActiveMQTextMessage msg1 = new ActiveMQTextMessage();
		try {
			msg1.setText(this.testStr);
			super.initializeMessage(msg1);
			byte[] data = writer.writePacketToByteArray(msg1);
			ActiveMQTextMessage msg2 = (ActiveMQTextMessage) reader.readPacketFromByteArray(data);
			super.testEquals(msg1, msg2);
			assertTrue(msg1.getText().equals(msg2.getText()));
		}
		catch (IOException e) {
			e.printStackTrace();
			assertTrue(false);
		}
		catch (JMSException ex) {
			ex.printStackTrace();
			assertTrue(false);
		}
	}
	
	public void testTime(){

		ActiveMQTextMessageReader reader = new ActiveMQTextMessageReader();
		ActiveMQTextMessageWriter writer = new ActiveMQTextMessageWriter();
		ActiveMQTextMessage msg1 = new ActiveMQTextMessage();
		
		ActiveMQTextMessage msg2 = null;
		try {
			int count = 10000;
			long startTime = System.currentTimeMillis();
			//msg1.setText(this.testStr);
			msg1.setText("this is a test str");
			super.initializeMessage(msg1);
			for (int i = 0; i < count; i++){
				byte[] data = writer.writePacketToByteArray(msg1);
				msg2 = (ActiveMQTextMessage) 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);
		}
	}

}
