1 18 package org.apache.activemq.command; 19 20 import java.io.IOException ; 21 22 import javax.jms.JMSException ; 23 import javax.jms.MessageNotReadableException ; 24 import javax.jms.MessageNotWriteableException ; 25 26 import junit.framework.TestCase; 27 import junit.textui.TestRunner; 28 29 import org.apache.activemq.util.ByteSequence; 30 31 34 public class ActiveMQTextMessageTest extends TestCase { 35 36 public static void main(String [] args) { 37 TestRunner.run(ActiveMQTextMessageTest.class); 38 } 39 40 public void testGetDataStructureType() { 41 ActiveMQTextMessage msg = new ActiveMQTextMessage(); 42 assertEquals(msg.getDataStructureType(), CommandTypes.ACTIVEMQ_TEXT_MESSAGE); 43 } 44 45 public void testShallowCopy() throws JMSException { 46 ActiveMQTextMessage msg = new ActiveMQTextMessage(); 47 String string = "str"; 48 msg.setText(string); 49 Message copy = msg.copy(); 50 assertTrue(msg.getText() == ((ActiveMQTextMessage) copy).getText()); 51 } 52 53 public void testSetText() { 54 ActiveMQTextMessage msg = new ActiveMQTextMessage(); 55 String str = "testText"; 56 try { 57 msg.setText(str); 58 assertEquals(msg.getText(), str); 59 } catch (JMSException e) { 60 e.printStackTrace(); 61 } 62 } 63 64 public void testGetBytes() throws JMSException , IOException { 65 ActiveMQTextMessage msg = new ActiveMQTextMessage(); 66 String str = "testText"; 67 msg.setText(str); 68 msg.beforeMarshall(null); 69 70 ByteSequence bytes = msg.getContent(); 71 msg = new ActiveMQTextMessage(); 72 msg.setContent(bytes); 73 74 assertEquals(msg.getText(), str); 75 } 76 77 public void testClearBody() throws JMSException , IOException { 78 ActiveMQTextMessage textMessage = new ActiveMQTextMessage(); 79 textMessage.setText("string"); 80 textMessage.clearBody(); 81 assertFalse(textMessage.isReadOnlyBody()); 82 assertNull(textMessage.getText()); 83 try { 84 textMessage.setText("String"); 85 textMessage.getText(); 86 } catch (MessageNotWriteableException mnwe) { 87 fail("should be writeable"); 88 } catch (MessageNotReadableException mnre) { 89 fail("should be readable"); 90 } 91 } 92 93 public void testReadOnlyBody() throws JMSException { 94 ActiveMQTextMessage textMessage = new ActiveMQTextMessage(); 95 textMessage.setText("test"); 96 textMessage.setReadOnlyBody(true); 97 try { 98 textMessage.getText(); 99 } catch (MessageNotReadableException e) { 100 fail("should be readable"); 101 } 102 try { 103 textMessage.setText("test"); 104 fail("should throw exception"); 105 } catch (MessageNotWriteableException mnwe) { 106 } 107 } 108 109 public void testWriteOnlyBody() throws JMSException { ActiveMQTextMessage textMessage = new ActiveMQTextMessage(); 111 textMessage.setReadOnlyBody(false); 112 try { 113 textMessage.setText("test"); 114 textMessage.getText(); 115 } catch (MessageNotReadableException e) { 116 fail("should be readable"); 117 } 118 textMessage.setReadOnlyBody(true); 119 try { 120 textMessage.getText(); 121 textMessage.setText("test"); 122 fail("should throw exception"); 123 } catch (MessageNotReadableException e) { 124 fail("should be readable"); 125 } catch (MessageNotWriteableException mnwe) { 126 } 127 } 128 } 129 | Popular Tags |