Source code: org/activemq/message/ActiveMQMessageTest.java
1 /*
2 * Created on Mar 3, 2004
3 *
4 * To change the template for this generated file go to
5 * Window - Preferences - Java - Code Generation - Code and Comments
6 */
7 package org.activemq.message;
8
9 import java.util.Enumeration;
10 import java.util.HashMap;
11 import java.util.Map;
12
13 import javax.jms.JMSException;
14 import javax.jms.Message;
15 import javax.jms.MessageNotWriteableException;
16
17 import junit.framework.TestCase;
18
19 /**
20 * To change the template for this generated type comment go to
21 * Window - Preferences - Java - Code Generation - Code and Comments
22 */
23 public class ActiveMQMessageTest extends TestCase {
24 private String jmsMessageID;
25 private String jmsClientID;
26 private String jmsCorrelationID;
27 private ActiveMQDestination jmsDestination;
28 private ActiveMQDestination jmsReplyTo;
29 private int jmsDeliveryMode;
30 private boolean jmsRedelivered;
31 private String jmsType;
32 private long jmsExpiration;
33 private int jmsPriority;
34 private long jmsTimestamp;
35 private Map properties;
36 protected boolean readOnlyMessage;
37 private String entryBrokerName;
38 private String entryClusterName;
39 private int[] consumerIDs;
40
41
42 public static void main(String[] args) {
43 }
44
45 /*
46 * @see TestCase#setUp()
47 */
48 protected void setUp() throws Exception {
49 super.setUp();
50 this.jmsMessageID = "testid";
51 this.jmsClientID = "testclientid";
52 this.jmsCorrelationID = "testcorrelationid";
53 this.jmsDestination = new ActiveMQTopic("test.topic");
54 this.jmsReplyTo = new ActiveMQTemporaryTopic("test.replyto.topic");
55 this.jmsDeliveryMode = Message.DEFAULT_DELIVERY_MODE;
56 this.jmsRedelivered = true;
57 this.jmsType = "test type";
58 this.jmsExpiration = 100000;
59 this.jmsPriority = 5;
60 this.jmsTimestamp = System.currentTimeMillis();
61 this.properties = new HashMap();
62 this.readOnlyMessage = false;
63 this.entryBrokerName = "test broker";
64 this.entryClusterName = "test cluster";
65 this.consumerIDs = new int[3];
66 for (int i = 0; i < this.consumerIDs.length; i++) {
67 this.consumerIDs[i] = i;
68 }
69
70 }
71
72 /*
73 * @see TestCase#tearDown()
74 */
75 protected void tearDown() throws Exception {
76 super.tearDown();
77 }
78
79 /**
80 * Constructor for ActiveMQMessageTest.
81 *
82 * @param arg0
83 */
84 public ActiveMQMessageTest(String arg0) {
85 super(arg0);
86 }
87
88 public void testHashCode() throws Exception {
89 ActiveMQMessage msg = new ActiveMQMessage();
90 msg.setJMSMessageID(this.jmsMessageID);
91 assertTrue(msg.hashCode() == jmsMessageID.hashCode());
92 }
93
94 public void testGetPacketType() {
95 ActiveMQMessage msg = new ActiveMQMessage();
96 assertTrue(msg.getPacketType() == Packet.ACTIVEMQ_MESSAGE);
97 }
98
99 public void testSetReadOnly() {
100 ActiveMQMessage msg = new ActiveMQMessage();
101 msg.setReadOnly(true);
102 boolean test = false;
103 try {
104 msg.setIntProperty("test", 1);
105 }
106 catch (MessageNotWriteableException me) {
107 test = true;
108 }
109 catch (JMSException e) {
110 e.printStackTrace(System.err);
111 test = false;
112 }
113 assertTrue(test);
114 }
115
116 /*
117 * Class to test for boolean equals(Object)
118 */
119 public void testEqualsObject() {
120 ActiveMQMessage msg1 = new ActiveMQMessage();
121 ActiveMQMessage msg2 = new ActiveMQMessage();
122 msg1.setJMSMessageID(this.jmsMessageID);
123 assertTrue(!msg1.equals(msg2));
124 msg2.setJMSMessageID(this.jmsMessageID);
125 assertTrue(msg1.equals(msg2));
126 }
127
128 public void testShallowCopy() throws Exception {
129 ActiveMQMessage msg1 = new ActiveMQMessage();
130 msg1.setJMSMessageID(jmsMessageID);
131 ActiveMQMessage msg2 = msg1.shallowCopy();
132 assertTrue(msg1 != msg2 && msg1.equals(msg2));
133 }
134
135 public void testInitializeOther() {
136 this.jmsMessageID = "testid";
137 this.jmsClientID = "testclientid";
138 this.jmsCorrelationID = "testcorrelationid";
139 this.jmsDestination = new ActiveMQTopic("test.topic");
140 this.jmsReplyTo = new ActiveMQTemporaryTopic("test.replyto.topic");
141 this.jmsDeliveryMode = Message.DEFAULT_DELIVERY_MODE;
142 this.jmsRedelivered = true;
143 this.jmsType = "test type";
144 this.jmsExpiration = 100000;
145 this.jmsPriority = 5;
146 this.jmsTimestamp = System.currentTimeMillis();
147 this.properties = new HashMap();
148 this.readOnlyMessage = false;
149 this.entryBrokerName = "test broker";
150 this.entryClusterName = "test cluster";
151 this.consumerIDs = new int[3];
152
153 ActiveMQMessage msg1 = new ActiveMQMessage();
154 msg1.setJMSMessageID(this.jmsMessageID);
155 msg1.setJMSClientID(this.jmsClientID);
156 msg1.setJMSCorrelationID(this.jmsCorrelationID);
157 msg1.setJMSDestination(this.jmsDestination);
158 msg1.setJMSReplyTo(this.jmsReplyTo);
159 msg1.setJMSDeliveryMode(this.jmsDeliveryMode);
160 msg1.setJMSRedelivered(this.jmsRedelivered);
161 msg1.setJMSType(this.jmsType);
162 msg1.setJMSExpiration(this.jmsExpiration);
163 msg1.setJMSPriority(this.jmsPriority);
164 msg1.setJMSTimestamp(this.jmsTimestamp);
165 msg1.setProperties(this.properties);
166 msg1.setReadOnly(true);
167 msg1.setEntryBrokerName(this.entryBrokerName);
168 msg1.setEntryClusterName(this.entryClusterName);
169 msg1.setConsumerNos(this.consumerIDs);
170 ActiveMQMessage msg2 = new ActiveMQMessage();
171 msg1.initializeOther(msg2);
172 assertTrue(msg1.getJMSMessageID().equals(msg2.getJMSMessageID()));
173 assertTrue(msg1.getJMSClientID().equals(msg2.getJMSClientID()));
174 assertTrue(msg1.getJMSCorrelationID().equals(msg2.getJMSCorrelationID()));
175 assertTrue(msg1.getJMSDestination().equals(msg2.getJMSDestination()));
176 assertTrue(msg1.getJMSReplyTo().equals(msg2.getJMSReplyTo()));
177 assertTrue(msg1.getJMSDeliveryMode() == msg2.getJMSDeliveryMode());
178 assertTrue(msg1.getJMSRedelivered() == msg2.getJMSRedelivered());
179 assertTrue(msg1.getJMSType().equals(msg2.getJMSType()));
180 assertTrue(msg1.getJMSExpiration() == msg2.getJMSExpiration());
181 assertTrue(msg1.getJMSPriority() == msg2.getJMSPriority());
182 assertTrue(msg1.getJMSTimestamp() == msg2.getJMSTimestamp());
183 assertTrue(msg1.getProperties().equals(msg2.getProperties()));
184 assertTrue(msg1.getEntryBrokerName().equals(msg2.getEntryBrokerName()));
185 assertTrue(msg1.getEntryClusterName().equals(msg2.getEntryClusterName()));
186 assertTrue(msg1.getConsumerNos().equals(msg2.getConsumerNos()));
187
188 }
189
190 public void testGetJMSMessageID() {
191 ActiveMQMessage msg = new ActiveMQMessage();
192 msg.setJMSMessageID(this.jmsMessageID);
193 assertTrue(msg.getJMSMessageID() == this.jmsMessageID);
194 }
195
196 public void testSetJMSMessageID() {
197
198 ActiveMQMessage msg = new ActiveMQMessage();
199 msg.setJMSMessageID(this.jmsMessageID);
200 assertTrue(msg.getJMSMessageID() == this.jmsMessageID);
201 }
202
203 public void testGetJMSTimestamp() {
204 ActiveMQMessage msg = new ActiveMQMessage();
205 msg.setJMSTimestamp(this.jmsTimestamp);
206 assertTrue(msg.getJMSTimestamp() == this.jmsTimestamp);
207 }
208
209 public void testSetJMSTimestamp() {
210 ActiveMQMessage msg = new ActiveMQMessage();
211 msg.setJMSTimestamp(this.jmsTimestamp);
212 assertTrue(msg.getJMSTimestamp() == this.jmsTimestamp);
213 }
214
215 public void testGetJMSCorrelationIDAsBytes() {
216 ActiveMQMessage msg = new ActiveMQMessage();
217 msg.setJMSCorrelationID(this.jmsCorrelationID);
218 byte[] testbytes = msg.getJMSCorrelationIDAsBytes();
219 String str2 = new String(testbytes);
220 assertTrue(this.jmsCorrelationID.equals(str2));
221 }
222
223 public void testSetJMSCorrelationIDAsBytes() {
224 ActiveMQMessage msg = new ActiveMQMessage();
225 byte[] testbytes = this.jmsCorrelationID.getBytes();
226 msg.setJMSCorrelationIDAsBytes(testbytes);
227 testbytes = msg.getJMSCorrelationIDAsBytes();
228 String str2 = new String(testbytes);
229 assertTrue(this.jmsCorrelationID.equals(str2));
230 }
231
232 public void testSetJMSCorrelationID() {
233 ActiveMQMessage msg = new ActiveMQMessage();
234 msg.setJMSCorrelationID(this.jmsCorrelationID);
235 assertTrue(msg.getJMSCorrelationID().equals(this.jmsCorrelationID));
236 }
237
238 public void testGetJMSCorrelationID() {
239 ActiveMQMessage msg = new ActiveMQMessage();
240 msg.setJMSCorrelationID(this.jmsCorrelationID);
241 assertTrue(msg.getJMSCorrelationID().equals(this.jmsCorrelationID));
242 }
243
244 public void testGetJMSReplyTo() {
245 ActiveMQMessage msg = new ActiveMQMessage();
246 msg.setJMSReplyTo(this.jmsReplyTo);
247 assertTrue(msg.getJMSReplyTo().equals(this.jmsReplyTo));
248 }
249
250 public void testSetJMSReplyTo() {
251 ActiveMQMessage msg = new ActiveMQMessage();
252 msg.setJMSReplyTo(this.jmsReplyTo);
253 assertTrue(msg.getJMSReplyTo().equals(this.jmsReplyTo));
254 }
255
256 public void testGetJMSDestination() {
257 ActiveMQMessage msg = new ActiveMQMessage();
258 msg.setJMSDestination(this.jmsDestination);
259 assertTrue(msg.getJMSDestination().equals(this.jmsDestination));
260 }
261
262 public void testSetJMSDestination() {
263 ActiveMQMessage msg = new ActiveMQMessage();
264 msg.setJMSDestination(this.jmsDestination);
265 assertTrue(msg.getJMSDestination().equals(this.jmsDestination));
266 }
267
268 public void testGetJMSDeliveryMode() {
269 ActiveMQMessage msg = new ActiveMQMessage();
270 msg.setJMSDeliveryMode(this.jmsDeliveryMode);
271 assertTrue(msg.getJMSDeliveryMode() == this.jmsDeliveryMode);
272 }
273
274 public void testSetJMSDeliveryMode() {
275 ActiveMQMessage msg = new ActiveMQMessage();
276 msg.setJMSDeliveryMode(this.jmsDeliveryMode);
277 assertTrue(msg.getJMSDeliveryMode() == this.jmsDeliveryMode);
278 }
279
280 public void testGetJMSRedelivered() {
281 ActiveMQMessage msg = new ActiveMQMessage();
282 msg.setJMSRedelivered(this.jmsRedelivered);
283 assertTrue(msg.getJMSRedelivered() == this.jmsRedelivered);
284 }
285
286 public void testSetJMSRedelivered() {
287 ActiveMQMessage msg = new ActiveMQMessage();
288 msg.setJMSRedelivered(this.jmsRedelivered);
289 assertTrue(msg.getJMSRedelivered() == this.jmsRedelivered);
290 }
291
292 public void testGetJMSType() {
293 ActiveMQMessage msg = new ActiveMQMessage();
294 msg.setJMSType(this.jmsType);
295 assertTrue(msg.getJMSType().equals(this.jmsType));
296 }
297
298 public void testSetJMSType() {
299 ActiveMQMessage msg = new ActiveMQMessage();
300 msg.setJMSType(this.jmsType);
301 assertTrue(msg.getJMSType().equals(this.jmsType));
302 }
303
304 public void testGetJMSExpiration() {
305 ActiveMQMessage msg = new ActiveMQMessage();
306 msg.setJMSExpiration(this.jmsExpiration);
307 assertTrue(msg.getJMSExpiration() == this.jmsExpiration);
308 }
309
310 public void testSetJMSExpiration() {
311 ActiveMQMessage msg = new ActiveMQMessage();
312 msg.setJMSExpiration(this.jmsExpiration);
313 assertTrue(msg.getJMSExpiration() == this.jmsExpiration);
314 }
315
316 public void testGetJMSPriority() {
317 ActiveMQMessage msg = new ActiveMQMessage();
318 msg.setJMSPriority(this.jmsPriority);
319 assertTrue(msg.getJMSPriority() == this.jmsPriority);
320 }
321
322 public void testSetJMSPriority() {
323 ActiveMQMessage msg = new ActiveMQMessage();
324 msg.setJMSPriority(this.jmsPriority);
325 assertTrue(msg.getJMSPriority() == this.jmsPriority);
326 }
327
328 public void testClearProperties() {
329 HashMap props = new HashMap();
330 props.put("test", new Integer(1));
331 ActiveMQMessage msg = new ActiveMQMessage();
332 msg.setProperties(props);
333 msg.clearProperties();
334 assertTrue(props.size() == 0);
335 }
336
337 public void testPropertyExists() {
338 HashMap props = new HashMap();
339 props.put("test", new Integer(1));
340 ActiveMQMessage msg = new ActiveMQMessage();
341 msg.setProperties(props);
342 msg.clearProperties();
343 assertTrue(props.size() == 0);
344 }
345
346 public void testGetBooleanProperty() throws JMSException {
347 ActiveMQMessage msg = new ActiveMQMessage();
348 String name = "booleanProperty";
349 msg.setBooleanProperty(name, true);
350 assertTrue(msg.getBooleanProperty(name));
351 }
352
353 public void testGetByteProperty() throws JMSException {
354 ActiveMQMessage msg = new ActiveMQMessage();
355 String name = "byteProperty";
356 msg.setByteProperty(name, (byte) 1);
357 assertTrue(msg.getByteProperty(name) == 1);
358 }
359
360 public void testGetShortProperty() throws JMSException {
361 ActiveMQMessage msg = new ActiveMQMessage();
362 String name = "shortProperty";
363 msg.setShortProperty(name, (short) 1);
364 assertTrue(msg.getShortProperty(name) == 1);
365 }
366
367 public void testGetIntProperty() throws JMSException {
368 ActiveMQMessage msg = new ActiveMQMessage();
369 String name = "intProperty";
370 msg.setIntProperty(name, 1);
371 assertTrue(msg.getIntProperty(name) == 1);
372 }
373
374 public void testGetLongProperty() throws JMSException {
375 ActiveMQMessage msg = new ActiveMQMessage();
376 String name = "longProperty";
377 msg.setLongProperty(name, 1);
378 assertTrue(msg.getLongProperty(name) == 1);
379 }
380
381 public void testGetFloatProperty() throws JMSException {
382 ActiveMQMessage msg = new ActiveMQMessage();
383 String name = "floatProperty";
384 msg.setFloatProperty(name, 1.3f);
385 assertTrue(msg.getFloatProperty(name) == 1.3f);
386 }
387
388 public void testGetDoubleProperty() throws JMSException {
389 ActiveMQMessage msg = new ActiveMQMessage();
390 String name = "doubleProperty";
391 msg.setDoubleProperty(name, 1.3d);
392 assertTrue(msg.getDoubleProperty(name) == 1.3);
393 }
394
395 public void testGetStringProperty() throws JMSException {
396 ActiveMQMessage msg = new ActiveMQMessage();
397 String name = "stringProperty";
398 msg.setStringProperty(name, name);
399 assertTrue(msg.getStringProperty(name).equals(name));
400 }
401
402 public void testGetObjectProperty() throws JMSException {
403 ActiveMQMessage msg = new ActiveMQMessage();
404 String name = "floatProperty";
405 msg.setFloatProperty(name, 1.3f);
406 assertTrue(msg.getObjectProperty(name) instanceof Float);
407 assertTrue(((Float) msg.getObjectProperty(name)).floatValue() == 1.3f);
408 }
409
410 public void testGetPropertyNames() throws JMSException {
411 ActiveMQMessage msg = new ActiveMQMessage();
412 String name = "floatProperty";
413 msg.setFloatProperty(name, 1.3f);
414 for (Enumeration iter = msg.getPropertyNames(); iter.hasMoreElements();) {
415 assertTrue(iter.nextElement().equals(name));
416 }
417 }
418
419 public void testGetProperties() throws JMSException {
420 ActiveMQMessage msg = new ActiveMQMessage();
421 String name = "floatProperty";
422 msg.setFloatProperty(name, 1.3f);
423 Map props = msg.getProperties();
424 assertTrue(props.get(name) instanceof Float);
425 assertTrue(((Float) props.get(name)).floatValue() == 1.3f);
426 }
427
428
429 public void testSetObjectProperty() throws JMSException {
430 ActiveMQMessage msg = new ActiveMQMessage();
431 String name = "floatProperty";
432 msg.setFloatProperty(name, 1.3f);
433 Map props = msg.getProperties();
434 assertTrue(props.get(name) instanceof Float);
435 assertTrue(((Float) props.get(name)).floatValue() == 1.3f);
436 }
437
438
439 public void testSetNullProperty() throws JMSException {
440 Message msg = new ActiveMQMessage();
441 String name = "cheese";
442 msg.setStringProperty(name, "Cheddar");
443 assertEquals("Cheddar", msg.getStringProperty(name));
444
445 msg.setStringProperty(name, null);
446 assertEquals(null, msg.getStringProperty(name));
447 }
448
449 public void testSetNullPropertyName() throws JMSException {
450 Message msg = new ActiveMQMessage();
451
452 try {
453 msg.setStringProperty(null, "Cheese");
454 fail("Should have thrown exception");
455 }
456 catch (IllegalArgumentException e) {
457 System.out.println("Worked, caught: " + e);
458 }
459 }
460
461 public void testSetEmptyPropertyName() throws JMSException {
462 Message msg = new ActiveMQMessage();
463
464 try {
465 msg.setStringProperty("", "Cheese");
466 fail("Should have thrown exception");
467 }
468 catch (IllegalArgumentException e) {
469 System.out.println("Worked, caught: " + e);
470 }
471 }
472
473 public void testGetJMSXDeliveryCount() throws JMSException{
474 Message msg = new ActiveMQMessage();
475 int count = msg.getIntProperty("JMSXDeliveryCount");
476 assertTrue("expected delivery count = 1 - got: " + count, count ==1);
477 }
478
479 public void testAcknowledge() {
480 //TODO Implement acknowledge().
481 }
482
483 public void testClearBody() {
484 //TODO Implement clearBody().
485 }
486
487
488 public void testGetEntryBrokerName() {
489 ActiveMQMessage msg = new ActiveMQMessage();
490 msg.setEntryBrokerName(this.entryBrokerName);
491 assertTrue(msg.getEntryBrokerName() == this.entryBrokerName);
492 }
493
494
495 public void testGetEntryClusterName() {
496 ActiveMQMessage msg = new ActiveMQMessage();
497 msg.setEntryClusterName(this.entryClusterName);
498 assertTrue(msg.getEntryClusterName() == this.entryClusterName);
499 }
500
501
502 public void testGetConsumerIDs() {
503 ActiveMQMessage msg = new ActiveMQMessage();
504 msg.setConsumerNos(this.consumerIDs);
505 assertTrue(msg.getConsumerNos() == this.consumerIDs);
506 }
507
508 }