001: package com.mockrunner.test.jms;
002:
003: import java.util.ArrayList;
004: import java.util.Arrays;
005: import java.util.Enumeration;
006:
007: import javax.jms.DeliveryMode;
008: import javax.jms.MessageFormatException;
009: import javax.jms.MessageNotWriteableException;
010:
011: import junit.framework.TestCase;
012:
013: import com.mockrunner.mock.jms.MockMessage;
014: import com.mockrunner.mock.jms.MockQueue;
015: import com.mockrunner.mock.jms.MockTopic;
016:
017: public class MockMessageTest extends TestCase {
018: public void testPropertyTypes() throws Exception {
019: MockMessage message = new MockMessage();
020: message.setStringProperty("string1", "123.4");
021: assertEquals("123.4", message.getObjectProperty("string1"));
022: assertEquals("123.4", message.getStringProperty("string1"));
023: assertEquals(123.4, message.getDoubleProperty("string1"), 0.01);
024: assertEquals(123.4, message.getFloatProperty("string1"), 0.01);
025: try {
026: message.getLongProperty("string1");
027: fail();
028: } catch (NumberFormatException exc) {
029: //should throw Exception
030: }
031: message.setDoubleProperty("double1", 123.4);
032: assertEquals(new Double(123.4), message
033: .getObjectProperty("double1"));
034: assertEquals("123.4", message.getStringProperty("double1"));
035: assertEquals(123.4, message.getDoubleProperty("double1"), 0.01);
036: message.setFloatProperty("float1", 123.4f);
037: assertEquals(new Float(123.4), message
038: .getObjectProperty("float1"));
039: assertEquals("123.4", message.getStringProperty("float1"));
040: assertEquals(123.4, message.getFloatProperty("float1"), 0.01);
041: try {
042: message.getLongProperty("float1");
043: fail();
044: } catch (MessageFormatException exc) {
045: //should throw Exception
046: }
047: try {
048: message.getFloatProperty("double1");
049: fail();
050: } catch (MessageFormatException exc) {
051: //should throw Exception
052: }
053: message.setByteProperty("byte1", (byte) 123);
054: assertEquals(new Byte((byte) 123), message
055: .getObjectProperty("byte1"));
056: assertEquals("123", message.getStringProperty("byte1"));
057: assertEquals((byte) 123, message.getByteProperty("byte1"));
058: assertEquals(123, message.getLongProperty("byte1"));
059: assertEquals(123, message.getIntProperty("byte1"));
060: assertEquals(123, message.getShortProperty("byte1"));
061: assertEquals(123, message.getLongProperty("byte1"));
062: try {
063: message.getBooleanProperty("byte1");
064: fail();
065: } catch (MessageFormatException exc) {
066: //should throw Exception
067: }
068: message.setIntProperty("int1", 12345);
069: assertEquals(new Integer(12345), message
070: .getObjectProperty("int1"));
071: assertEquals("12345", message.getStringProperty("int1"));
072: assertEquals(12345, message.getLongProperty("int1"));
073: assertEquals(12345, message.getIntProperty("int1"));
074: try {
075: message.getShortProperty("int1");
076: fail();
077: } catch (MessageFormatException exc) {
078: //should throw Exception
079: }
080: message.setBooleanProperty("boolean1", true);
081: assertEquals(Boolean.TRUE, message
082: .getObjectProperty("boolean1"));
083: assertEquals("true", message.getStringProperty("boolean1"));
084: assertEquals(true, message.getBooleanProperty("boolean1"));
085: try {
086: message.getDoubleProperty("boolean1");
087: fail();
088: } catch (MessageFormatException exc) {
089: //should throw Exception
090: }
091: Enumeration names = message.getPropertyNames();
092: ArrayList nameList = new ArrayList();
093: while (names.hasMoreElements()) {
094: nameList.add(names.nextElement());
095: }
096: assertTrue(nameList.size() == 6);
097: assertTrue(nameList.contains("string1"));
098: assertTrue(nameList.contains("double1"));
099: assertTrue(nameList.contains("float1"));
100: assertTrue(nameList.contains("int1"));
101: assertTrue(nameList.contains("byte1"));
102: assertTrue(nameList.contains("boolean1"));
103: }
104:
105: public void testNullPropertyName() throws Exception {
106: MockMessage message = new MockMessage();
107: try {
108: message.setDoubleProperty(null, 123.4);
109: fail();
110: } catch (IllegalArgumentException exc) {
111: //should throw exception
112: }
113: try {
114: message.setObjectProperty("", "test");
115: fail();
116: } catch (IllegalArgumentException exc) {
117: //should throw exception
118: }
119: try {
120: message.setByteProperty(null, (byte) 1);
121: fail();
122: } catch (IllegalArgumentException exc) {
123: //should throw exception
124: }
125: }
126:
127: public void testNullProperties() throws Exception {
128: MockMessage message = new MockMessage();
129: message.setObjectProperty("null", null);
130: assertFalse(message.propertyExists("null"));
131: assertNull(message.getObjectProperty("null"));
132: assertNull(message.getStringProperty("test"));
133: try {
134: message.getDoubleProperty("null");
135: fail();
136: } catch (NullPointerException exc) {
137: //should throw exception
138: }
139: try {
140: message.getFloatProperty("null");
141: fail();
142: } catch (NullPointerException exc) {
143: //should throw exception
144: }
145: try {
146: message.getByteProperty("null");
147: fail();
148: } catch (NumberFormatException exc) {
149: //should throw exception
150: }
151: try {
152: message.getIntProperty("test");
153: fail();
154: } catch (NumberFormatException exc) {
155: //should throw exception
156: }
157: try {
158: message.getShortProperty("null");
159: fail();
160: } catch (NumberFormatException exc) {
161: //should throw exception
162: }
163: assertFalse(message.getBooleanProperty("null"));
164: assertFalse(message.getBooleanProperty("test"));
165: }
166:
167: public void testReadOnlyProperties() throws Exception {
168: MockMessage message = new MockMessage();
169: message.setStringProperty("string", "test");
170: message.setReadOnly(true);
171: message.setDoubleProperty("double", 123);
172: message.setReadOnlyProperties(true);
173: try {
174: message.setStringProperty("string", "anothertest");
175: fail();
176: } catch (MessageNotWriteableException exc) {
177: //should throw exception
178: }
179: try {
180: message.setDoubleProperty("double", 456);
181: fail();
182: } catch (MessageNotWriteableException exc) {
183: //should throw exception
184: }
185: message.clearBody();
186: try {
187: message.setBooleanProperty("boolean", true);
188: fail();
189: } catch (MessageNotWriteableException exc) {
190: //should throw exception
191: }
192: assertEquals("test", message.getStringProperty("string"));
193: assertEquals(123, message.getDoubleProperty("double"), 0);
194: assertFalse(message.getBooleanProperty("boolean"));
195: assertFalse(message.propertyExists("boolean"));
196: message.clearProperties();
197: message.setBooleanProperty("boolean", true);
198: assertTrue(message.getBooleanProperty("boolean"));
199: assertTrue(message.propertyExists("boolean"));
200: }
201:
202: public void testSetAndGetCorrelationID() throws Exception {
203: MockMessage message = new MockMessage();
204: assertNull(message.getJMSCorrelationID());
205: assertNull(message.getJMSCorrelationIDAsBytes());
206: message
207: .setJMSCorrelationIDAsBytes("ABC"
208: .getBytes("ISO-8859-1"));
209: assertEquals("ABC", message.getJMSCorrelationID());
210: assertTrue(Arrays.equals("ABC".getBytes("ISO-8859-1"), message
211: .getJMSCorrelationIDAsBytes()));
212: message.setJMSCorrelationIDAsBytes(null);
213: assertNull(message.getJMSCorrelationID());
214: assertNull(message.getJMSCorrelationIDAsBytes());
215: message.setJMSCorrelationID("test");
216: assertEquals("test", message.getJMSCorrelationID());
217: assertTrue(Arrays.equals("test".getBytes("ISO-8859-1"), message
218: .getJMSCorrelationIDAsBytes()));
219: }
220:
221: public void testClone() throws Exception {
222: MockQueue queue = new MockQueue("MyQueue");
223: MockTopic topic = new MockTopic("MyTopic");
224: MockMessage message = new MockMessage();
225: message.setStringProperty("string", "test");
226: message.setIntProperty("int", 12345);
227: message.setBooleanProperty("boolean", true);
228: message.setJMSCorrelationID("testID");
229: message.setJMSPriority(3);
230: message.setJMSDestination(queue);
231: message.setJMSReplyTo(topic);
232: message.setJMSDeliveryMode(DeliveryMode.NON_PERSISTENT);
233: MockMessage newMessage = (MockMessage) message.clone();
234: assertNotSame(message, newMessage);
235: assertEquals("test", newMessage.getStringProperty("string"));
236: assertEquals(12345, newMessage.getIntProperty("int"));
237: assertEquals(true, newMessage.getBooleanProperty("boolean"));
238: assertEquals("testID", newMessage.getJMSCorrelationID());
239: assertEquals(3, newMessage.getJMSPriority());
240: assertEquals(DeliveryMode.NON_PERSISTENT, newMessage
241: .getJMSDeliveryMode());
242: assertSame(queue, newMessage.getJMSDestination());
243: assertSame(topic, newMessage.getJMSReplyTo());
244: }
245: }
|