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