001    package com.mockrunner.mock.jms;
002    
003    import javax.jms.JMSException;
004    import javax.jms.MessageNotWriteableException;
005    import javax.jms.TextMessage;
006    
007    /**
008     * Mock implementation of JMS <code>TextMessage</code>.
009     */
010    public class MockTextMessage extends MockMessage implements TextMessage
011    {
012        private String text;
013        
014        public MockTextMessage()
015        {
016            this(null);
017        }
018    
019        public MockTextMessage(String text)
020        {
021            this.text = text;
022        }
023    
024        public void setText(String text) throws JMSException
025        {
026            if(!isInWriteMode())
027            {
028                throw new MessageNotWriteableException("Message is in read mode");
029            }
030            this.text = text;
031        }
032    
033        public String getText() throws JMSException
034        {
035            return text;
036        }
037        
038        public String toString()
039        {
040            return this.getClass().getName() + ": " + text; 
041        }
042    
043        public void clearBody() throws JMSException
044        {
045            super.clearBody();
046            text = null;
047        }
048        
049        /**
050         * Compares the underlying String. If the Strings of 
051         * both messages are <code>null</code>, this
052         * method returns <code>true</code>.
053         */
054        public boolean equals(Object otherObject)
055        {
056            if(null == otherObject) return false;
057            if(!(otherObject instanceof MockTextMessage)) return false;
058            MockTextMessage otherMessage = (MockTextMessage)otherObject;
059            if(null == text && null == otherMessage.text) return true;
060            return text.equals(otherMessage.text);
061        }
062    
063        public int hashCode()
064        {
065            if(null == text) return 0;
066            return text.hashCode();
067        }
068    }