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

import java.util.HashMap;
import javax.jms.DeliveryMode;
import junit.framework.TestCase;
import org.activemq.message.ActiveMQDestination;
import org.activemq.message.ActiveMQMessage;
import org.activemq.message.ActiveMQTemporaryTopic;
import org.activemq.message.ActiveMQTopic;
import org.activemq.message.Packet;

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

	private String jmsMessageID;
	private String jmsClientID;
	private String jmsCorrelationID;
	private ActiveMQDestination jmsDestination;
	private ActiveMQDestination jmsReplyTo;
	private int jmsDeliveryMode;
	private boolean jmsRedelivered;
	private String jmsType;
	private long jmsExpiration;
	private int jmsPriority;
	private long jmsTimestamp;
	private HashMap properties;
	private String entryBrokerName;
	private String entryClusterName;
	private int[] consumerIDs;
	private String transactionId;

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

	/*
	 * @see TestCase#setUp()
	 */
	protected void setUp() throws Exception {
		super.setUp();
		this.jmsMessageID = "testid";
		this.jmsClientID = "testclientid";
		this.jmsCorrelationID = "testcorrelationid";
		this.jmsDestination = new ActiveMQTopic("test.topic");
		this.jmsReplyTo = new ActiveMQTemporaryTopic("test.replyto.topic");
		this.jmsDeliveryMode = DeliveryMode.NON_PERSISTENT;
		this.jmsRedelivered = true;
		this.jmsType = "test type";
		this.jmsExpiration = 100000;
		this.jmsPriority = 5;
		this.jmsTimestamp = System.currentTimeMillis();
		this.entryBrokerName = "test broker";
		this.entryClusterName = "test cluster";
		this.consumerIDs = new int[3];
		this.transactionId = "testtransactonId";
		for (int i = 0; i < this.consumerIDs.length; i++) {
			this.consumerIDs[i] = i;
		}
		this.properties= new HashMap();
		this.properties.put("doubletype",new Double(4.6d));
		this.properties.put("floatType",new Float(3.3f));
		this.properties.put("inttype",new Integer(2));
		this.properties.put("longtype",new Long(45l));
	}

	protected void initializeMessage(ActiveMQMessage msg) {
		msg.setJMSMessageID(this.jmsMessageID);
		msg.setJMSClientID(this.jmsClientID);
		msg.setJMSCorrelationID(this.jmsCorrelationID);
		msg.setJMSDestination(this.jmsDestination);
		msg.setJMSReplyTo(this.jmsReplyTo);
		msg.setJMSDeliveryMode(this.jmsDeliveryMode);
		msg.setJMSRedelivered(this.jmsRedelivered);
		msg.setJMSType(this.jmsType);
		msg.setJMSExpiration(this.jmsExpiration);
		msg.setJMSPriority(this.jmsPriority);
		msg.setJMSTimestamp(this.jmsTimestamp);
		msg.setProperties(this.properties);
		msg.setReadOnly(true);
		msg.setEntryBrokerName(this.entryBrokerName);
		msg.setEntryClusterName(this.entryClusterName);
		msg.setConsumerNos(this.consumerIDs);
		msg.setTransactionId(this.transactionId);
        msg.setExternalMessageId(true);
	}

	protected void testEquals(ActiveMQMessage msg1, ActiveMQMessage msg2) {
		assertTrue(msg1.getJMSMessageID().equals(msg2.getJMSMessageID()));
		assertTrue(msg1.getJMSClientID().equals(msg2.getJMSClientID()));
		assertTrue(msg1.getJMSCorrelationID().equals(msg2.getJMSCorrelationID()));
		assertTrue(msg1.getJMSDestination().equals(msg2.getJMSDestination()));
		assertTrue(msg1.getJMSReplyTo().equals(msg2.getJMSReplyTo()));
		assertTrue(msg1.getJMSDeliveryMode() == msg2.getJMSDeliveryMode());
		assertTrue(msg1.getJMSRedelivered() == msg2.getJMSRedelivered());
		assertTrue(msg1.getJMSType().equals(msg2.getJMSType()));
		assertTrue(msg1.getJMSExpiration() == msg2.getJMSExpiration());
		assertTrue(msg1.getJMSPriority() == msg2.getJMSPriority());
		assertTrue(msg1.getJMSTimestamp() == msg2.getJMSTimestamp());
		assertTrue(msg1.getEntryBrokerName().equals(msg2.getEntryBrokerName()));
		assertTrue(msg1.getEntryClusterName().equals(msg2.getEntryClusterName()));
		assertTrue(msg1.getTransactionId().equals(msg2.getTransactionId()));
		
		int[] cid1 = msg1.getConsumerNos();
		int[] cid2 = msg2.getConsumerNos();
		assertTrue(cid1.length==cid2.length);
		for(int i =0; i < cid1.length;i++){
			assertTrue(cid1[i]==cid2[i]);
		}
		assertTrue(msg1.getProperties().equals(msg2.getProperties()));
	}

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

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

	public void testGetPacketType() {
        DefaultWireFormat wf = new DefaultWireFormat();
		ActiveMQMessageReader reader = new ActiveMQMessageReader(wf);
		assertTrue(reader.getPacketType() == Packet.ACTIVEMQ_MESSAGE);
	}

	public void testReadPacket() throws Exception{
        DefaultWireFormat wf = new DefaultWireFormat();
		ActiveMQMessageReader reader = new ActiveMQMessageReader(wf);
		ActiveMQMessageWriter writer = new ActiveMQMessageWriter(wf);
		ActiveMQMessage msg1 = new ActiveMQMessage();
		initializeMessage(msg1);
		
		byte[] data = writer.writePacketToByteArray(msg1);
		ActiveMQMessage msg2 = (ActiveMQMessage) reader.readPacketFromByteArray(data);
		testEquals(msg1, msg2);
         ActiveMQMessage msg3 = msg2.shallowCopy();
         testEquals(msg1,msg3);
         data = writer.writePacketToByteArray(msg3);
         ActiveMQMessage msg4 = (ActiveMQMessage) reader.readPacketFromByteArray(data);
         testEquals(msg1,msg4);
		

	}

}
