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

import javax.jms.JMSException;
import javax.jms.MessageFormatException;

import org.codehaus.activemq.message.ActiveMQBytesMessage;
import org.codehaus.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 ActiveMQBytesMessageTest extends TestCase {
	public static void main(String[] args) {
		junit.textui.TestRunner.run(ActiveMQBytesMessageTest.class);
	}
	/*
	 * @see TestCase#setUp()
	 */
	protected void setUp() throws Exception {
		super.setUp();
	}
	/*
	 * @see TestCase#tearDown()
	 */
	protected void tearDown() throws Exception {
		super.tearDown();
	}
	/**
	 * Constructor for ActiveMQBytesMessageTest.
	 * 
	 * @param arg0
	 */
	public ActiveMQBytesMessageTest(String arg0) {
		super(arg0);
	}
	public void testGetPacketType() {
		ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
		assertTrue(msg.getPacketType() == Packet.ACTIVEMQ_BYTES_MESSAGE);
	}
	public void testShallowCopy() throws Exception {
		ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
		byte[] data = new byte[50];
		msg.setBodyAsBytes(data);
		ActiveMQBytesMessage msg2 = (ActiveMQBytesMessage) msg.shallowCopy();
		try {
			assertTrue(msg2.getBodyAsBytes() != null
					&& msg2.getBodyAsBytes() == msg.getBodyAsBytes());
		} catch (IOException ioe) {
			ioe.printStackTrace();
			assertTrue(false);
		}
	}
	public void testDeepCopy() throws Exception {
		ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
		byte[] data = new byte[50];
		msg.setBodyAsBytes(data);
		ActiveMQBytesMessage msg2 = (ActiveMQBytesMessage) msg.deepCopy();
		try {
			assertTrue(msg2.getBodyAsBytes() != null
					&& msg2.getBodyAsBytes() != msg.getBodyAsBytes()
					&& msg2.getBodyAsBytes().length == data.length);
		} catch (IOException ioe) {
			ioe.printStackTrace();
			assertTrue(false);
		}
	}
	public void testSetData() {
		ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
		byte[] data = new byte[50];
		msg.setBodyAsBytes(data);
		try {
			assertTrue(data == msg.getBodyAsBytes());
		} catch (IOException ioe) {
			ioe.printStackTrace();
			assertTrue(false);
		}
	}
	public void testGetBodyLength() {
		ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
		int len = 10;
		try {
			for (int i = 0; i < len; i++) {
				msg.writeLong(5l);
			}
		} catch (JMSException ex) {
			ex.printStackTrace();
		}
		try {
			msg.reset();
			assertTrue(msg.getBodyLength() == (len * 8));
		} catch (Throwable e) {
			e.printStackTrace();
			assertTrue(false);
		}
	}
	public void testReadBoolean() {
		ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
		try {
			msg.writeBoolean(true);
			msg.reset();
			assertTrue(msg.readBoolean());
		} catch (JMSException jmsEx) {
			jmsEx.printStackTrace();
			assertTrue(false);
		}
	}
	public void testReadByte() {
		ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
		try {
			msg.writeByte((byte) 2);
			msg.reset();
			assertTrue(msg.readByte() == 2);
		} catch (JMSException jmsEx) {
			jmsEx.printStackTrace();
			assertTrue(false);
		}
	}
	public void testReadUnsignedByte() {
		ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
		try {
			msg.writeByte((byte) 2);
			msg.reset();
			assertTrue(msg.readUnsignedByte() == 2);
		} catch (JMSException jmsEx) {
			jmsEx.printStackTrace();
			assertTrue(false);
		}
	}
	public void testReadShort() {
		ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
		try {
			msg.writeShort((short) 3000);
			msg.reset();
			assertTrue(msg.readShort() == 3000);
		} catch (JMSException jmsEx) {
			jmsEx.printStackTrace();
			assertTrue(false);
		}
	}
	public void testReadUnsignedShort() {
		ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
		try {
			msg.writeShort((short) 3000);
			msg.reset();
			assertTrue(msg.readUnsignedShort() == 3000);
		} catch (JMSException jmsEx) {
			jmsEx.printStackTrace();
			assertTrue(false);
		}
	}
	public void testReadChar() {
		ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
		try {
			msg.writeChar('a');
			msg.reset();
			assertTrue(msg.readChar() == 'a');
		} catch (JMSException jmsEx) {
			jmsEx.printStackTrace();
			assertTrue(false);
		}
	}
	public void testReadInt() {
		ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
		try {
			msg.writeInt(3000);
			msg.reset();
			assertTrue(msg.readInt() == 3000);
		} catch (JMSException jmsEx) {
			jmsEx.printStackTrace();
			assertTrue(false);
		}
	}
	public void testReadLong() {
		ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
		try {
			msg.writeLong(3000);
			msg.reset();
			assertTrue(msg.readLong() == 3000);
		} catch (JMSException jmsEx) {
			jmsEx.printStackTrace();
			assertTrue(false);
		}
	}
	public void testReadFloat() {
		ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
		try {
			msg.writeFloat(3.3f);
			msg.reset();
			assertTrue(msg.readFloat() == 3.3f);
		} catch (JMSException jmsEx) {
			jmsEx.printStackTrace();
			assertTrue(false);
		}
	}
	public void testReadDouble() {
		ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
		try {
			msg.writeDouble(3.3d);
			msg.reset();
			assertTrue(msg.readDouble() == 3.3d);
		} catch (JMSException jmsEx) {
			jmsEx.printStackTrace();
			assertTrue(false);
		}
	}
	public void testReadUTF() {
		ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
		try {
			String str = "this is a test";
			msg.writeUTF(str);
			msg.reset();
			assertTrue(msg.readUTF().equals(str));
		} catch (JMSException jmsEx) {
			jmsEx.printStackTrace();
			assertTrue(false);
		}
	}
	/*
	 * Class to test for int readBytes(byte[])
	 */
	public void testReadBytesbyteArray() {
		ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
		try {
			byte[] data = new byte[50];
			for (int i = 0; i < data.length; i++) {
				data[i] = (byte) i;
			}
			msg.writeBytes(data);
			msg.reset();
			byte[] test = new byte[data.length];
			msg.readBytes(test);
			for (int i = 0; i < test.length; i++) {
				assertTrue(test[i] == i);
			}
		} catch (JMSException jmsEx) {
			jmsEx.printStackTrace();
			assertTrue(false);
		}
	}
	public void testWriteObject() {
		ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
		try {
			msg.writeObject(new Boolean(true));
			msg.writeObject(new Long(300l));
			msg.writeObject(new Float(3.3f));
			msg.writeObject(new Character('q'));
			msg.writeObject(new Byte((byte) 1));
			msg.writeObject(new Short((short) 3));
			msg.writeObject(new String("fred"));
			try {
				msg.writeObject(new Object());
				assertTrue(false);
			} catch (MessageFormatException mfe) {
				assertTrue(true);
			}
		} catch (JMSException jmsEx) {
			jmsEx.printStackTrace();
			assertTrue(false);
		}
	}
}
