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            try
041            {
042                return getText();
043            }
044            catch(JMSException exc)
045            {
046                return exc.getMessage();
047            }
048        }
049    
050        public void clearBody() throws JMSException
051        {
052            super.clearBody();
053            text = null;
054        }
055        
056        /**
057         * Compares the underlying String. If the Strings of 
058         * both messages are <code>null</code>, this
059         * method returns <code>true</code>.
060         */
061        public boolean equals(Object otherObject)
062        {
063            if(null == otherObject) return false;
064            if(!(otherObject instanceof MockTextMessage)) return false;
065            MockTextMessage otherMessage = (MockTextMessage)otherObject;
066            if(null == text && null == otherMessage.text) return true;
067            return text.equals(otherMessage.text);
068        }
069    
070        public int hashCode()
071        {
072            if(null == text) return 0;
073            return text.hashCode();
074        }
075        
076        public Object clone()
077        {
078            return super.clone();
079        }
080    }