/*
 * Created on Mar 3, 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.util.Enumeration;
import java.util.HashMap;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageNotWriteableException;

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 ActiveMQMessageTest 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 boolean readOnlyProperties;
    protected boolean readOnlyMessage;
    private String entryBrokerName;
    private String entryClusterName;
    private int[] consumerIDs;


    public static void main(String[] args) {
    }

    /*
     * @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 = Message.DEFAULT_DELIVERY_MODE;
        this.jmsRedelivered = true;
        this.jmsType = "test type";
        this.jmsExpiration = 100000;
        this.jmsPriority = 5;
        this.jmsTimestamp = System.currentTimeMillis();
        this.properties = new HashMap();
        this.readOnlyProperties = false;
        this.readOnlyMessage = false;
        this.entryBrokerName = "test broker";
        this.entryClusterName = "test cluster";
        this.consumerIDs = new int[3];
        for (int i = 0; i < this.consumerIDs.length; i++) {
            this.consumerIDs[i] = i;
        }

    }

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

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

    public void testHashCode() {
        ActiveMQMessage msg = new ActiveMQMessage();
        assertTrue(msg.hashCode() > 0);
        msg.setJMSMessageID(this.jmsMessageID);
        assertTrue(msg.hashCode() == jmsMessageID.hashCode());
    }

    public void testGetPacketType() {
        ActiveMQMessage msg = new ActiveMQMessage();
        assertTrue(msg.getPacketType() == Packet.ACTIVEMQ_MESSAGE);
    }

    public void testSetReadOnly() {
        ActiveMQMessage msg = new ActiveMQMessage();
        msg.setReadOnly(true);
        boolean test = false;
        try {
            msg.setIntProperty("test", 1);
        }
        catch (MessageNotWriteableException me) {
            test = true;
        }
        catch (JMSException e) {
            e.printStackTrace(System.err);
            test = false;
        }
        assertTrue(test);
    }

    /*
     * Class to test for boolean equals(Object)
     */
    public void testEqualsObject() {
        ActiveMQMessage msg1 = new ActiveMQMessage();
        ActiveMQMessage msg2 = new ActiveMQMessage();
        msg1.setJMSMessageID(this.jmsMessageID);
        assertTrue(!msg1.equals(msg2));
        msg2.setJMSMessageID(this.jmsMessageID);
        assertTrue(msg1.equals(msg2));
    }

    public void testShallowCopy() throws Exception {
        ActiveMQMessage msg1 = new ActiveMQMessage();
        msg1.setJMSMessageID(jmsMessageID);
        ActiveMQMessage msg2 = msg1.shallowCopy();
        assertTrue(msg1 != msg2 && msg1.equals(msg2));
    }

    public void testInitializeOther() {
        this.jmsMessageID = "testid";
        this.jmsClientID = "testclientid";
        this.jmsCorrelationID = "testcorrelationid";
        this.jmsDestination = new ActiveMQTopic("test.topic");
        this.jmsReplyTo = new ActiveMQTemporaryTopic("test.replyto.topic");
        this.jmsDeliveryMode = Message.DEFAULT_DELIVERY_MODE;
        this.jmsRedelivered = true;
        this.jmsType = "test type";
        this.jmsExpiration = 100000;
        this.jmsPriority = 5;
        this.jmsTimestamp = System.currentTimeMillis();
        this.properties = new HashMap();
        this.readOnlyProperties = false;
        this.readOnlyMessage = false;
        this.entryBrokerName = "test broker";
        this.entryClusterName = "test cluster";
        this.consumerIDs = new int[3];

        ActiveMQMessage msg1 = new ActiveMQMessage();
        msg1.setJMSMessageID(this.jmsMessageID);
        msg1.setJMSClientID(this.jmsClientID);
        msg1.setJMSCorrelationID(this.jmsCorrelationID);
        msg1.setJMSDestination(this.jmsDestination);
        msg1.setJMSReplyTo(this.jmsReplyTo);
        msg1.setJMSDeliveryMode(this.jmsDeliveryMode);
        msg1.setJMSRedelivered(this.jmsRedelivered);
        msg1.setJMSType(this.jmsType);
        msg1.setJMSExpiration(this.jmsExpiration);
        msg1.setJMSPriority(this.jmsPriority);
        msg1.setJMSTimestamp(this.jmsTimestamp);
        msg1.setProperties(this.properties);
        msg1.setReadOnly(true);
        msg1.setEntryBrokerName(this.entryBrokerName);
        msg1.setEntryClusterName(this.entryClusterName);
        msg1.setConsumerNos(this.consumerIDs);
        ActiveMQMessage msg2 = new ActiveMQMessage();
        msg1.initializeOther(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.getProperties().equals(msg2.getProperties()));
        assertTrue(msg1.getEntryBrokerName().equals(msg2.getEntryBrokerName()));
        assertTrue(msg1.getEntryClusterName().equals(msg2.getEntryClusterName()));
        assertTrue(msg1.getConsumerNos().equals(msg2.getConsumerNos()));

    }

    public void testGetJMSMessageID() {
        ActiveMQMessage msg = new ActiveMQMessage();
        msg.setJMSMessageID(this.jmsMessageID);
        assertTrue(msg.getJMSMessageID() == this.jmsMessageID);
    }

    public void testSetJMSMessageID() {

        ActiveMQMessage msg = new ActiveMQMessage();
        msg.setJMSMessageID(this.jmsMessageID);
        assertTrue(msg.getJMSMessageID() == this.jmsMessageID);
    }

    public void testGetJMSTimestamp() {
        ActiveMQMessage msg = new ActiveMQMessage();
        msg.setJMSTimestamp(this.jmsTimestamp);
        assertTrue(msg.getJMSTimestamp() == this.jmsTimestamp);
    }

    public void testSetJMSTimestamp() {
        ActiveMQMessage msg = new ActiveMQMessage();
        msg.setJMSTimestamp(this.jmsTimestamp);
        assertTrue(msg.getJMSTimestamp() == this.jmsTimestamp);
    }

    public void testGetJMSCorrelationIDAsBytes() {
        ActiveMQMessage msg = new ActiveMQMessage();
        msg.setJMSCorrelationID(this.jmsCorrelationID);
        byte[] testbytes = msg.getJMSCorrelationIDAsBytes();
        String str2 = new String(testbytes);
        assertTrue(this.jmsCorrelationID.equals(str2));
    }

    public void testSetJMSCorrelationIDAsBytes() {
        ActiveMQMessage msg = new ActiveMQMessage();
        byte[] testbytes = this.jmsCorrelationID.getBytes();
        msg.setJMSCorrelationIDAsBytes(testbytes);
        testbytes = msg.getJMSCorrelationIDAsBytes();
        String str2 = new String(testbytes);
        assertTrue(this.jmsCorrelationID.equals(str2));
    }

    public void testSetJMSCorrelationID() {
        ActiveMQMessage msg = new ActiveMQMessage();
        msg.setJMSCorrelationID(this.jmsCorrelationID);
        assertTrue(msg.getJMSCorrelationID().equals(this.jmsCorrelationID));
    }

    public void testGetJMSCorrelationID() {
        ActiveMQMessage msg = new ActiveMQMessage();
        msg.setJMSCorrelationID(this.jmsCorrelationID);
        assertTrue(msg.getJMSCorrelationID().equals(this.jmsCorrelationID));
    }

    public void testGetJMSReplyTo() {
        ActiveMQMessage msg = new ActiveMQMessage();
        msg.setJMSReplyTo(this.jmsReplyTo);
        assertTrue(msg.getJMSReplyTo().equals(this.jmsReplyTo));
    }

    public void testSetJMSReplyTo() {
        ActiveMQMessage msg = new ActiveMQMessage();
        msg.setJMSReplyTo(this.jmsReplyTo);
        assertTrue(msg.getJMSReplyTo().equals(this.jmsReplyTo));
    }

    public void testGetJMSDestination() {
        ActiveMQMessage msg = new ActiveMQMessage();
        msg.setJMSDestination(this.jmsDestination);
        assertTrue(msg.getJMSDestination().equals(this.jmsDestination));
    }

    public void testSetJMSDestination() {
        ActiveMQMessage msg = new ActiveMQMessage();
        msg.setJMSDestination(this.jmsDestination);
        assertTrue(msg.getJMSDestination().equals(this.jmsDestination));
    }

    public void testGetJMSDeliveryMode() {
        ActiveMQMessage msg = new ActiveMQMessage();
        msg.setJMSDeliveryMode(this.jmsDeliveryMode);
        assertTrue(msg.getJMSDeliveryMode() == this.jmsDeliveryMode);
    }

    public void testSetJMSDeliveryMode() {
        ActiveMQMessage msg = new ActiveMQMessage();
        msg.setJMSDeliveryMode(this.jmsDeliveryMode);
        assertTrue(msg.getJMSDeliveryMode() == this.jmsDeliveryMode);
    }

    public void testGetJMSRedelivered() {
        ActiveMQMessage msg = new ActiveMQMessage();
        msg.setJMSRedelivered(this.jmsRedelivered);
        assertTrue(msg.getJMSRedelivered() == this.jmsRedelivered);
    }

    public void testSetJMSRedelivered() {
        ActiveMQMessage msg = new ActiveMQMessage();
        msg.setJMSRedelivered(this.jmsRedelivered);
        assertTrue(msg.getJMSRedelivered() == this.jmsRedelivered);
    }

    public void testGetJMSType() {
        ActiveMQMessage msg = new ActiveMQMessage();
        msg.setJMSType(this.jmsType);
        assertTrue(msg.getJMSType().equals(this.jmsType));
    }

    public void testSetJMSType() {
        ActiveMQMessage msg = new ActiveMQMessage();
        msg.setJMSType(this.jmsType);
        assertTrue(msg.getJMSType().equals(this.jmsType));
    }

    public void testGetJMSExpiration() {
        ActiveMQMessage msg = new ActiveMQMessage();
        msg.setJMSExpiration(this.jmsExpiration);
        assertTrue(msg.getJMSExpiration() == this.jmsExpiration);
    }

    public void testSetJMSExpiration() {
        ActiveMQMessage msg = new ActiveMQMessage();
        msg.setJMSExpiration(this.jmsExpiration);
        assertTrue(msg.getJMSExpiration() == this.jmsExpiration);
    }

    public void testGetJMSPriority() {
        ActiveMQMessage msg = new ActiveMQMessage();
        msg.setJMSPriority(this.jmsPriority);
        assertTrue(msg.getJMSPriority() == this.jmsPriority);
    }

    public void testSetJMSPriority() {
        ActiveMQMessage msg = new ActiveMQMessage();
        msg.setJMSPriority(this.jmsPriority);
        assertTrue(msg.getJMSPriority() == this.jmsPriority);
    }

    public void testClearProperties() {
        HashMap props = new HashMap();
        props.put("test", new Integer(1));
        ActiveMQMessage msg = new ActiveMQMessage();
        msg.setProperties(props);
        msg.clearProperties();
        assertTrue(props.size() == 0);
    }

    public void testPropertyExists() {
        HashMap props = new HashMap();
        props.put("test", new Integer(1));
        ActiveMQMessage msg = new ActiveMQMessage();
        msg.setProperties(props);
        msg.clearProperties();
        assertTrue(props.size() == 0);
    }

    public void testGetBooleanProperty() throws JMSException {
        ActiveMQMessage msg = new ActiveMQMessage();
        String name = "booleanProperty";
        msg.setBooleanProperty(name, true);
        assertTrue(msg.getBooleanProperty(name));
    }

    public void testGetByteProperty() throws JMSException {
        ActiveMQMessage msg = new ActiveMQMessage();
        String name = "byteProperty";
        msg.setByteProperty(name, (byte) 1);
        assertTrue(msg.getByteProperty(name) == 1);
    }

    public void testGetShortProperty() throws JMSException {
        ActiveMQMessage msg = new ActiveMQMessage();
        String name = "shortProperty";
        msg.setShortProperty(name, (short) 1);
        assertTrue(msg.getShortProperty(name) == 1);
    }

    public void testGetIntProperty() throws JMSException {
        ActiveMQMessage msg = new ActiveMQMessage();
        String name = "intProperty";
        msg.setIntProperty(name, 1);
        assertTrue(msg.getIntProperty(name) == 1);
    }

    public void testGetLongProperty() throws JMSException {
        ActiveMQMessage msg = new ActiveMQMessage();
        String name = "longProperty";
        msg.setLongProperty(name, 1);
        assertTrue(msg.getLongProperty(name) == 1);
    }

    public void testGetFloatProperty() throws JMSException {
        ActiveMQMessage msg = new ActiveMQMessage();
        String name = "floatProperty";
        msg.setFloatProperty(name, 1.3f);
        assertTrue(msg.getFloatProperty(name) == 1.3f);
    }

    public void testGetDoubleProperty() throws JMSException {
        ActiveMQMessage msg = new ActiveMQMessage();
        String name = "doubleProperty";
        msg.setDoubleProperty(name, 1.3d);
        assertTrue(msg.getDoubleProperty(name) == 1.3);
    }

    public void testGetStringProperty() throws JMSException {
        ActiveMQMessage msg = new ActiveMQMessage();
        String name = "stringProperty";
        msg.setStringProperty(name, name);
        assertTrue(msg.getStringProperty(name).equals(name));
    }

    public void testGetObjectProperty() throws JMSException {
        ActiveMQMessage msg = new ActiveMQMessage();
        String name = "floatProperty";
        msg.setFloatProperty(name, 1.3f);
        assertTrue(msg.getObjectProperty(name) instanceof Float);
        assertTrue(((Float) msg.getObjectProperty(name)).floatValue() == 1.3f);
    }

    public void testGetPropertyNames() throws JMSException {
        ActiveMQMessage msg = new ActiveMQMessage();
        String name = "floatProperty";
        msg.setFloatProperty(name, 1.3f);
        for (Enumeration iter = msg.getPropertyNames(); iter.hasMoreElements();) {
            assertTrue(iter.nextElement().equals(name));
        }
    }

    public void testGetProperties() throws JMSException {
        ActiveMQMessage msg = new ActiveMQMessage();
        String name = "floatProperty";
        msg.setFloatProperty(name, 1.3f);
        HashMap props = msg.getProperties();
        assertTrue(props.get(name) instanceof Float);
        assertTrue(((Float) props.get(name)).floatValue() == 1.3f);
    }


    public void testSetObjectProperty() throws JMSException {
        ActiveMQMessage msg = new ActiveMQMessage();
        String name = "floatProperty";
        msg.setFloatProperty(name, 1.3f);
        HashMap props = msg.getProperties();
        assertTrue(props.get(name) instanceof Float);
        assertTrue(((Float) props.get(name)).floatValue() == 1.3f);
    }


    public void testSetNullProperty() throws JMSException {
        Message msg = new ActiveMQMessage();
        String name = "cheese";
        msg.setStringProperty(name, "Cheddar");
        assertEquals("Cheddar", msg.getStringProperty(name));

        msg.setStringProperty(name, null);
        assertEquals(null, msg.getStringProperty(name));
    }

    public void testSetNullPropertyName() throws JMSException {
        Message msg = new ActiveMQMessage();

        try {
            msg.setStringProperty(null, "Cheese");
            fail("Should have thrown exception");
        }
        catch (IllegalArgumentException e) {
            System.out.println("Worked, caught: " + e);
        }
    }

    public void testSetEmptyPropertyName() throws JMSException {
        Message msg = new ActiveMQMessage();

        try {
            msg.setStringProperty("", "Cheese");
            fail("Should have thrown exception");
        }
        catch (IllegalArgumentException e) {
            System.out.println("Worked, caught: " + e);
        }
    }
    
    public void testGetJMSXDeliveryCount() throws JMSException{
        Message msg = new ActiveMQMessage();
        int count = msg.getIntProperty("JMSXDeliveryCount");
        assertTrue("expected delivery count = 1 - got: " + count, count ==1);
    }

    public void testAcknowledge() {
        //TODO Implement acknowledge().
    }

    public void testClearBody() {
        //TODO Implement clearBody().
    }


    public void testGetEntryBrokerName() {
        ActiveMQMessage msg = new ActiveMQMessage();
        msg.setEntryBrokerName(this.entryBrokerName);
        assertTrue(msg.getEntryBrokerName() == this.entryBrokerName);
    }


    public void testGetEntryClusterName() {
        ActiveMQMessage msg = new ActiveMQMessage();
        msg.setEntryClusterName(this.entryClusterName);
        assertTrue(msg.getEntryClusterName() == this.entryClusterName);
    }


    public void testGetConsumerIDs() {
        ActiveMQMessage msg = new ActiveMQMessage();
        msg.setConsumerNos(this.consumerIDs);
        assertTrue(msg.getConsumerNos() == this.consumerIDs);
    }

}
