/*
 * 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.activemq.message;

import junit.framework.TestCase;
import junit.textui.TestRunner;

import javax.jms.JMSException;
import org.activemq.io.WireFormat;
import org.activemq.io.impl.DefaultWireFormat;
import java.io.IOException;

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

    private WireFormat wireFormat = new DefaultWireFormat();

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

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

    public void testSetText() {
        ActiveMQTextMessage msg = new ActiveMQTextMessage();
        String str = "testText";
        try {
            msg.setText(str);
            assertTrue(msg.getText() == str);
        }
        catch (JMSException e) {
            e.printStackTrace();
        }
        boolean readOnlyTest = false;
        msg.setReadOnly(true);
        try {
            msg.setText(str);
        }
        catch (JMSException e) {
            readOnlyTest = true;
        }
        assertTrue(readOnlyTest);
    }


    public void testReadAndWriteMessage() throws JMSException, IOException {
        ActiveMQTextMessage message = new ActiveMQTextMessage();
        message.setJMSMessageID("abc:123");
        message.setExternalMessageId(true);
        message.setText("Testing 1, 2, 3");

        byte[] data = wireFormat.toBytes(message);
        Packet packet = wireFormat.fromBytes(data);

        assertTrue(packet instanceof ActiveMQTextMessage);
        ActiveMQTextMessage message2 = (ActiveMQTextMessage) packet;
        assertEquals("Message IDs", message.getJMSMessageID(), message2.getJMSMessageID());
        assertEquals("Message Text", message.getText(), message2.getText());
    }
}
