/*
 * Created on Mar 5, 2004
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package org.activemq.message;

import java.io.IOException;

import javax.jms.JMSException;

import org.activemq.message.ActiveMQStreamMessage;
import org.activemq.message.Packet;

import junit.framework.TestCase;

/**
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class ActiveMQStreamMessageTest extends TestCase
{

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

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

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

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

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

	public void testShallowCopy()
	{
		ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
		byte[] data = new byte[50];
		
		try {
            msg.setBodyAsBytes(data,0,data.length);
            ActiveMQStreamMessage msg2 = (ActiveMQStreamMessage)msg.shallowCopy();
			assertTrue(msg2.getBodyAsBytes() != null && msg2.getBodyAsBytes()==msg.getBodyAsBytes());
		}catch(Exception ioe){
			ioe.printStackTrace();
			assertTrue(false);
		}
	}

	public void testDeepCopy()
	{
		ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
		byte[] data = new byte[50];
		msg.setBodyAsBytes(data,0,data.length);
		
		try {
            ActiveMQStreamMessage msg2 = (ActiveMQStreamMessage)msg.deepCopy();
			assertTrue(msg2.getBodyAsBytes() != null && 
					msg2.getBodyAsBytes()!=msg.getBodyAsBytes() &&
					msg2.getBodyAsBytes().getLength()==data.length);
		}catch(Exception ioe){
			ioe.printStackTrace();
			assertTrue(false);
		}
	}
	
	public void testSetData()
	{

		ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
		byte[] data = new byte[50];
		msg.setBodyAsBytes(data,0,data.length);
		try {
			assertTrue(data==msg.getBodyAsBytes().getBuf());
		}catch(IOException ioe){
			ioe.printStackTrace();
			assertTrue(false);
		}
	}
	
	
	public void testReadBoolean()
	{
		ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
		try {
			msg.writeBoolean(true);
			msg.reset();
			assertTrue(msg.readBoolean());
			msg.reset();
			assertTrue(msg.readString().equals("true"));
		}catch(JMSException jmsEx){
			jmsEx.printStackTrace();
			assertTrue(false);
		}
	}

	public void testreadByte()
	{
		ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
		try {
			byte test = (byte)4;
			msg.writeByte(test);
			msg.reset();
			assertTrue(msg.readByte()==test);
			msg.reset();
			assertTrue(msg.readShort()==test);
			msg.reset();
			assertTrue(msg.readInt()==test);
			msg.reset();
			assertTrue(msg.readLong()==test);
			msg.reset();
			assertTrue(msg.readString().equals(new Byte(test).toString()));
		}catch(JMSException jmsEx){
			jmsEx.printStackTrace();
			assertTrue(false);
		}
	}

	public void testReadShort()
	{
		ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
		try {
			short test = (short)4;
			msg.writeShort(test);
			msg.reset();
			assertTrue(msg.readShort()==test);
			msg.reset();
			assertTrue(msg.readInt()==test);
			msg.reset();
			assertTrue(msg.readLong()==test);
			msg.reset();
			assertTrue(msg.readString().equals(new Short(test).toString()));
		}catch(JMSException jmsEx){
			jmsEx.printStackTrace();
			assertTrue(false);
		}
	}

	public void testReadChar()
	{
		ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
		try {
			char test = 'z';
			msg.writeChar(test);
			msg.reset();
			assertTrue(msg.readChar()==test);
			msg.reset();
			assertTrue(msg.readString().equals(new Character(test).toString()));
		}catch(JMSException jmsEx){
			jmsEx.printStackTrace();
			assertTrue(false);
		}
	}

	public void testReadInt()
	{
		ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
		try {
			int test = 4;
			msg.writeInt(test);
			msg.reset();
			assertTrue(msg.readInt()==test);
			msg.reset();
			assertTrue(msg.readLong()==test);
			msg.reset();
			assertTrue(msg.readString().equals(new Integer(test).toString()));
		}catch(JMSException jmsEx){
			jmsEx.printStackTrace();
			assertTrue(false);
		}
	}

	public void testReadLong()
	{
		ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
		try {
			long test = 4l;
			msg.writeLong(test);
			msg.reset();
			assertTrue(msg.readLong()==test);
			msg.reset();
			assertTrue(msg.readString().equals(new Long(test).toString()));
		}catch(JMSException jmsEx){
			jmsEx.printStackTrace();
			assertTrue(false);
		}
	}

	public void testReadFloat()
	{
		ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
		try {
			float test = 4.4f;
			msg.writeFloat(test);
			msg.reset();
			assertTrue(msg.readFloat()==test);
			msg.reset();
			assertTrue(msg.readDouble()==test);
			msg.reset();
			assertTrue(msg.readString().equals(new Float(test).toString()));
		}catch(JMSException jmsEx){
			jmsEx.printStackTrace();
			assertTrue(false);
		}
	}

	public void testReadDouble()
	{
		ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
		try {
			double test = 4.4d;
			msg.writeDouble(test);
			msg.reset();
			assertTrue(msg.readDouble()==test);
			msg.reset();
			assertTrue(msg.readString().equals(new Double(test).toString()));
		}catch(JMSException jmsEx){
			jmsEx.printStackTrace();
			assertTrue(false);
		}
	}

	public void testReadString()
	{
		ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
		try {
			byte testByte = (byte)2;
			msg.writeString(new Byte(testByte).toString());
			msg.reset();
			assertTrue(msg.readByte()==testByte);
			msg.clearBody();
			short testShort = 3;
			msg.writeString(new Short(testShort).toString());
			msg.reset();
			assertTrue(msg.readShort()==testShort);
			msg.clearBody();
			int testInt = 4;
			msg.writeString(new Integer(testInt).toString());
			msg.reset();
			assertTrue(msg.readInt()==testInt);
			msg.clearBody();
			long testLong = 6l;
			msg.writeString(new Long(testLong).toString());
			msg.reset();
			assertTrue(msg.readLong()==testLong);
			msg.clearBody();
			float testFloat = 6.6f;
			msg.writeString(new Float(testFloat).toString());
			msg.reset();
			assertTrue(msg.readFloat()==testFloat);
			msg.clearBody();
			double testDouble = 7.7d;
			msg.writeString(new Double(testDouble).toString());
			msg.reset();
			assertTrue(msg.readDouble()==testDouble);
			msg.clearBody();
			msg.writeString("true");
			msg.reset();
			assertTrue(msg.readBoolean());
			
			
		}catch(JMSException jmsEx){
			jmsEx.printStackTrace();
			assertTrue(false);
		}
	}

	public void testReadBytes()
	{
		ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
		try {
			byte[] test = new byte[50];
			for(int i =0; i< test.length;i++){
				test[i]=(byte)i;
			}
			msg.writeBytes(test);
			msg.reset();
			byte[] valid = new byte[test.length];
			msg.readBytes(valid);
			for(int i = 0; i < valid.length;i++){
				assertTrue(valid[i]==test[i]);
			}
		}catch(JMSException jmsEx){
			jmsEx.printStackTrace();
			assertTrue(false);
		}
	}

	public void testReadObject()
	{
		ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
		try {
			byte testByte = (byte)2;
			msg.writeByte(testByte);
			msg.reset();
			assertTrue(((Byte)msg.readObject()).byteValue()==testByte);
			msg.clearBody();
			
			short testShort = 3;
			msg.writeShort(testShort);
			msg.reset();
			assertTrue(((Short)msg.readObject()).shortValue()==testShort);
			msg.clearBody();
			
			int testInt = 4;
			msg.writeInt(testInt);
			msg.reset();
			assertTrue(((Integer)msg.readObject()).intValue()==testInt);
			msg.clearBody();
			
			long testLong = 6l;
			msg.writeLong(testLong);
			msg.reset();
			assertTrue(((Long)msg.readObject()).longValue()==testLong);
			msg.clearBody();
			
			float testFloat = 6.6f;
			msg.writeFloat(testFloat);
			msg.reset();
			assertTrue(((Float)msg.readObject()).floatValue()==testFloat);
			msg.clearBody();
			
			double testDouble = 7.7d;
			msg.writeDouble(testDouble);
			msg.reset();
			assertTrue(((Double)msg.readObject()).doubleValue()==testDouble);
			msg.clearBody();
			
			char testChar = 'z';
			msg.writeChar(testChar);
			msg.reset();
			assertTrue(((Character)msg.readObject()).charValue()==testChar);
			msg.clearBody();
			
			byte[] data = new byte[50];
			for(int i = 0; i < data.length;i++){
				data[i] = (byte)i;
			}
			msg.writeBytes(data);
			msg.reset();
			byte[] valid = (byte[])msg.readObject();
			assertTrue(valid.length==data.length);
			for(int i =0; i < valid.length;i++){
				assertTrue(valid[i]==data[i]);
			}
			msg.clearBody();
			msg.writeBoolean(true);
			msg.reset();
			assertTrue(((Boolean)msg.readObject()).booleanValue());
			
			
		}catch(JMSException jmsEx){
			jmsEx.printStackTrace();
			assertTrue(false);
		}
	}

	
}
