KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > command > ActiveMQTextMessageTest


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.command;
19
20 import java.io.IOException JavaDoc;
21
22 import javax.jms.JMSException JavaDoc;
23 import javax.jms.MessageNotReadableException JavaDoc;
24 import javax.jms.MessageNotWriteableException JavaDoc;
25
26 import junit.framework.TestCase;
27 import junit.textui.TestRunner;
28
29 import org.apache.activemq.util.ByteSequence;
30
31 /**
32  * @version $Revision$
33  */

34 public class ActiveMQTextMessageTest extends TestCase {
35
36     public static void main(String JavaDoc[] 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 JavaDoc {
46         ActiveMQTextMessage msg = new ActiveMQTextMessage();
47         String JavaDoc 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 JavaDoc str = "testText";
56         try {
57             msg.setText(str);
58             assertEquals(msg.getText(), str);
59         } catch (JMSException JavaDoc e) {
60             e.printStackTrace();
61         }
62     }
63
64     public void testGetBytes() throws JMSException JavaDoc, IOException JavaDoc {
65         ActiveMQTextMessage msg = new ActiveMQTextMessage();
66         String JavaDoc 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 JavaDoc, IOException JavaDoc {
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 JavaDoc mnwe) {
87             fail("should be writeable");
88         } catch (MessageNotReadableException JavaDoc mnre) {
89             fail("should be readable");
90         }
91     }
92
93     public void testReadOnlyBody() throws JMSException JavaDoc {
94         ActiveMQTextMessage textMessage = new ActiveMQTextMessage();
95         textMessage.setText("test");
96         textMessage.setReadOnlyBody(true);
97         try {
98             textMessage.getText();
99         } catch (MessageNotReadableException JavaDoc e) {
100             fail("should be readable");
101         }
102         try {
103             textMessage.setText("test");
104             fail("should throw exception");
105         } catch (MessageNotWriteableException JavaDoc mnwe) {
106         }
107     }
108
109     public void testWriteOnlyBody() throws JMSException JavaDoc { // should always be readable
110
ActiveMQTextMessage textMessage = new ActiveMQTextMessage();
111         textMessage.setReadOnlyBody(false);
112         try {
113             textMessage.setText("test");
114             textMessage.getText();
115         } catch (MessageNotReadableException JavaDoc 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 JavaDoc e) {
124             fail("should be readable");
125         } catch (MessageNotWriteableException JavaDoc mnwe) {
126         }
127     }
128 }
129
Popular Tags