KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Enumeration JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javax.jms.JMSException JavaDoc;
25 import javax.jms.Message JavaDoc;
26 import javax.jms.MessageFormatException JavaDoc;
27 import javax.jms.MessageNotWriteableException JavaDoc;
28
29 import junit.framework.TestCase;
30
31 import org.apache.activemq.openwire.OpenWireFormat;
32 import org.apache.activemq.state.CommandVisitor;
33 import org.apache.activemq.util.ByteSequence;
34 import org.apache.activemq.wireformat.WireFormat;
35
36 /**
37  * @version $Revision$
38  */

39 public class ActiveMQMessageTest extends TestCase {
40     
41     private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
42             .getLog(ActiveMQMessageTest.class);
43     
44     private String JavaDoc jmsMessageID;
45     private String JavaDoc jmsCorrelationID;
46     private ActiveMQDestination jmsDestination;
47     private ActiveMQDestination jmsReplyTo;
48     private int jmsDeliveryMode;
49     private boolean jmsRedelivered;
50     private String JavaDoc jmsType;
51     private long jmsExpiration;
52     private int jmsPriority;
53     private long jmsTimestamp;
54     protected boolean readOnlyMessage;
55     private long[] consumerIDs;
56
57
58     public static void main(String JavaDoc[] args) {
59     }
60
61     /*
62      * @see TestCase#setUp()
63      */

64     protected void setUp() throws Exception JavaDoc {
65         super.setUp();
66         this.jmsMessageID = "testid";
67         this.jmsCorrelationID = "testcorrelationid";
68         this.jmsDestination = new ActiveMQTopic("test.topic");
69         this.jmsReplyTo = new ActiveMQTempTopic("test.replyto.topic:001");
70         this.jmsDeliveryMode = Message.DEFAULT_DELIVERY_MODE;
71         this.jmsRedelivered = true;
72         this.jmsType = "test type";
73         this.jmsExpiration = 100000;
74         this.jmsPriority = 5;
75         this.jmsTimestamp = System.currentTimeMillis();
76         this.readOnlyMessage = false;
77         this.consumerIDs = new long[3];
78         for (int i = 0; i < this.consumerIDs.length; i++) {
79             this.consumerIDs[i] = i;
80         }
81
82     }
83
84     /*
85      * @see TestCase#tearDown()
86      */

87     protected void tearDown() throws Exception JavaDoc {
88         super.tearDown();
89     }
90
91     /**
92      * Constructor for ActiveMQMessageTest.
93      *
94      * @param arg0
95      */

96     public ActiveMQMessageTest(String JavaDoc arg0) {
97         super(arg0);
98     }
99
100     public void testGetDataStructureType() {
101         ActiveMQMessage msg = new ActiveMQMessage();
102         assertEquals(msg.getDataStructureType(), CommandTypes.ACTIVEMQ_MESSAGE);
103     }
104
105     public void testHashCode() throws Exception JavaDoc {
106         ActiveMQMessage msg = new ActiveMQMessage();
107         msg.setJMSMessageID(this.jmsMessageID);
108         assertTrue(msg.hashCode() == jmsMessageID.hashCode());
109     }
110
111     public void testSetReadOnly() {
112         ActiveMQMessage msg = new ActiveMQMessage();
113         msg.setReadOnlyProperties(true);
114         boolean test = false;
115         try {
116             msg.setIntProperty("test", 1);
117         } catch (MessageNotWriteableException JavaDoc me) {
118             test = true;
119         } catch (JMSException JavaDoc e) {
120             e.printStackTrace(System.err);
121             test = false;
122         }
123         assertTrue(test);
124     }
125
126     public void testSetToForeignJMSID() throws Exception JavaDoc {
127         ActiveMQMessage msg = new ActiveMQMessage();
128         msg.setJMSMessageID("ID:EMS-SERVER.8B443C380083:429");
129         
130     }
131     /*
132      * Class to test for boolean equals(Object)
133      */

134     public void testEqualsObject() throws Exception JavaDoc {
135         ActiveMQMessage msg1 = new ActiveMQMessage();
136         ActiveMQMessage msg2 = new ActiveMQMessage();
137         msg1.setJMSMessageID(this.jmsMessageID);
138         assertTrue(!msg1.equals(msg2));
139         msg2.setJMSMessageID(this.jmsMessageID);
140         assertTrue(msg1.equals(msg2));
141     }
142
143     public void testShallowCopy() throws Exception JavaDoc {
144         ActiveMQMessage msg1 = new ActiveMQMessage();
145         msg1.setJMSMessageID(jmsMessageID);
146         ActiveMQMessage msg2 = (ActiveMQMessage) msg1.copy();
147         assertTrue(msg1 != msg2 && msg1.equals(msg2));
148     }
149
150     public void testCopy() throws Exception JavaDoc {
151         this.jmsMessageID = "testid";
152         this.jmsCorrelationID = "testcorrelationid";
153         this.jmsDestination = new ActiveMQTopic("test.topic");
154         this.jmsReplyTo = new ActiveMQTempTopic("test.replyto.topic:001");
155         this.jmsDeliveryMode = Message.DEFAULT_DELIVERY_MODE;
156         this.jmsRedelivered = true;
157         this.jmsType = "test type";
158         this.jmsExpiration = 100000;
159         this.jmsPriority = 5;
160         this.jmsTimestamp = System.currentTimeMillis();
161         this.readOnlyMessage = false;
162
163         ActiveMQMessage msg1 = new ActiveMQMessage();
164         msg1.setJMSMessageID(this.jmsMessageID);
165         msg1.setJMSCorrelationID(this.jmsCorrelationID);
166         msg1.setJMSDestination(this.jmsDestination);
167         msg1.setJMSReplyTo(this.jmsReplyTo);
168         msg1.setJMSDeliveryMode(this.jmsDeliveryMode);
169         msg1.setJMSRedelivered(this.jmsRedelivered);
170         msg1.setJMSType(this.jmsType);
171         msg1.setJMSExpiration(this.jmsExpiration);
172         msg1.setJMSPriority(this.jmsPriority);
173         msg1.setJMSTimestamp(this.jmsTimestamp);
174         msg1.setReadOnlyProperties(true);
175         ActiveMQMessage msg2 = new ActiveMQMessage();
176         msg1.copy(msg2);
177         assertTrue(msg1.getJMSMessageID().equals(msg2.getJMSMessageID()));
178         assertTrue(msg1.getJMSCorrelationID().equals(msg2.getJMSCorrelationID()));
179         assertTrue(msg1.getJMSDestination().equals(msg2.getJMSDestination()));
180         assertTrue(msg1.getJMSReplyTo().equals(msg2.getJMSReplyTo()));
181         assertTrue(msg1.getJMSDeliveryMode() == msg2.getJMSDeliveryMode());
182         assertTrue(msg1.getJMSRedelivered() == msg2.getJMSRedelivered());
183         assertTrue(msg1.getJMSType().equals(msg2.getJMSType()));
184         assertTrue(msg1.getJMSExpiration() == msg2.getJMSExpiration());
185         assertTrue(msg1.getJMSPriority() == msg2.getJMSPriority());
186         assertTrue(msg1.getJMSTimestamp() == msg2.getJMSTimestamp());
187         
188         log.info("Message is: " + msg1);
189     }
190
191     public void testGetAndSetJMSMessageID() throws Exception JavaDoc {
192         ActiveMQMessage msg = new ActiveMQMessage();
193         msg.setJMSMessageID(this.jmsMessageID);
194         assertEquals(msg.getJMSMessageID(), this.jmsMessageID);
195     }
196
197     public void testGetAndSetJMSTimestamp() {
198         ActiveMQMessage msg = new ActiveMQMessage();
199         msg.setJMSTimestamp(this.jmsTimestamp);
200         assertTrue(msg.getJMSTimestamp() == this.jmsTimestamp);
201     }
202
203     public void testGetJMSCorrelationIDAsBytes() throws Exception JavaDoc {
204         ActiveMQMessage msg = new ActiveMQMessage();
205         msg.setJMSCorrelationID(this.jmsCorrelationID);
206         byte[] testbytes = msg.getJMSCorrelationIDAsBytes();
207         String JavaDoc str2 = new String JavaDoc(testbytes);
208         assertTrue(this.jmsCorrelationID.equals(str2));
209     }
210
211     public void testSetJMSCorrelationIDAsBytes() throws Exception JavaDoc {
212         ActiveMQMessage msg = new ActiveMQMessage();
213         byte[] testbytes = this.jmsCorrelationID.getBytes();
214         msg.setJMSCorrelationIDAsBytes(testbytes);
215         testbytes = msg.getJMSCorrelationIDAsBytes();
216         String JavaDoc str2 = new String JavaDoc(testbytes);
217         assertTrue(this.jmsCorrelationID.equals(str2));
218     }
219
220     public void testGetAndSetJMSCorrelationID() {
221         ActiveMQMessage msg = new ActiveMQMessage();
222         msg.setJMSCorrelationID(this.jmsCorrelationID);
223         assertTrue(msg.getJMSCorrelationID().equals(this.jmsCorrelationID));
224     }
225
226     public void testGetAndSetJMSReplyTo() throws JMSException JavaDoc {
227         ActiveMQMessage msg = new ActiveMQMessage();
228         msg.setJMSReplyTo(this.jmsReplyTo);
229         assertTrue(msg.getJMSReplyTo().equals(this.jmsReplyTo));
230     }
231
232     public void testGetAndSetJMSDestination() throws Exception JavaDoc {
233         ActiveMQMessage msg = new ActiveMQMessage();
234         msg.setJMSDestination(this.jmsDestination);
235         assertTrue(msg.getJMSDestination().equals(this.jmsDestination));
236     }
237
238     public void testGetAndSetJMSDeliveryMode() {
239         ActiveMQMessage msg = new ActiveMQMessage();
240         msg.setJMSDeliveryMode(this.jmsDeliveryMode);
241         assertTrue(msg.getJMSDeliveryMode() == this.jmsDeliveryMode);
242     }
243
244     public void testGetAndSetMSRedelivered() {
245         ActiveMQMessage msg = new ActiveMQMessage();
246         msg.setJMSRedelivered(this.jmsRedelivered);
247         assertTrue(msg.getJMSRedelivered() == this.jmsRedelivered);
248     }
249
250     public void testGetAndSetJMSType() {
251         ActiveMQMessage msg = new ActiveMQMessage();
252         msg.setJMSType(this.jmsType);
253         assertTrue(msg.getJMSType().equals(this.jmsType));
254     }
255
256     public void testGetAndSetJMSExpiration() {
257         ActiveMQMessage msg = new ActiveMQMessage();
258         msg.setJMSExpiration(this.jmsExpiration);
259         assertTrue(msg.getJMSExpiration() == this.jmsExpiration);
260     }
261
262     public void testGetAndSetJMSPriority() {
263         ActiveMQMessage msg = new ActiveMQMessage();
264         msg.setJMSPriority(this.jmsPriority);
265         assertTrue(msg.getJMSPriority() == this.jmsPriority);
266     }
267
268     public void testClearProperties() throws JMSException JavaDoc {
269         ActiveMQMessage msg = new ActiveMQMessage();
270         msg.setStringProperty("test", "test");
271         msg.setContent(new ByteSequence(new byte[1],0,0));
272         msg.setJMSMessageID(this.jmsMessageID);
273         msg.clearProperties();
274         assertNull(msg.getStringProperty("test"));
275         assertNotNull(msg.getJMSMessageID());
276         assertNotNull(msg.getContent());
277     }
278
279     public void testPropertyExists() throws JMSException JavaDoc {
280         ActiveMQMessage msg = new ActiveMQMessage();
281         msg.setStringProperty("test", "test");
282         assertTrue(msg.propertyExists("test"));
283     }
284
285     public void testGetBooleanProperty() throws JMSException JavaDoc {
286         ActiveMQMessage msg = new ActiveMQMessage();
287         String JavaDoc name = "booleanProperty";
288         msg.setBooleanProperty(name, true);
289         assertTrue(msg.getBooleanProperty(name));
290     }
291
292     public void testGetByteProperty() throws JMSException JavaDoc {
293         ActiveMQMessage msg = new ActiveMQMessage();
294         String JavaDoc name = "byteProperty";
295         msg.setByteProperty(name, (byte) 1);
296         assertTrue(msg.getByteProperty(name) == 1);
297     }
298
299     public void testGetShortProperty() throws JMSException JavaDoc {
300         ActiveMQMessage msg = new ActiveMQMessage();
301         String JavaDoc name = "shortProperty";
302         msg.setShortProperty(name, (short) 1);
303         assertTrue(msg.getShortProperty(name) == 1);
304     }
305
306     public void testGetIntProperty() throws JMSException JavaDoc {
307         ActiveMQMessage msg = new ActiveMQMessage();
308         String JavaDoc name = "intProperty";
309         msg.setIntProperty(name, 1);
310         assertTrue(msg.getIntProperty(name) == 1);
311     }
312
313     public void testGetLongProperty() throws JMSException JavaDoc {
314         ActiveMQMessage msg = new ActiveMQMessage();
315         String JavaDoc name = "longProperty";
316         msg.setLongProperty(name, 1);
317         assertTrue(msg.getLongProperty(name) == 1);
318     }
319
320     public void testGetFloatProperty() throws JMSException JavaDoc {
321         ActiveMQMessage msg = new ActiveMQMessage();
322         String JavaDoc name = "floatProperty";
323         msg.setFloatProperty(name, 1.3f);
324         assertTrue(msg.getFloatProperty(name) == 1.3f);
325     }
326
327     public void testGetDoubleProperty() throws JMSException JavaDoc {
328         ActiveMQMessage msg = new ActiveMQMessage();
329         String JavaDoc name = "doubleProperty";
330         msg.setDoubleProperty(name, 1.3d);
331         assertTrue(msg.getDoubleProperty(name) == 1.3);
332     }
333
334     public void testGetStringProperty() throws JMSException JavaDoc {
335         ActiveMQMessage msg = new ActiveMQMessage();
336         String JavaDoc name = "stringProperty";
337         msg.setStringProperty(name, name);
338         assertTrue(msg.getStringProperty(name).equals(name));
339     }
340
341     public void testGetObjectProperty() throws JMSException JavaDoc {
342         ActiveMQMessage msg = new ActiveMQMessage();
343         String JavaDoc name = "floatProperty";
344         msg.setFloatProperty(name, 1.3f);
345         assertTrue(msg.getObjectProperty(name) instanceof Float JavaDoc);
346         assertTrue(((Float JavaDoc) msg.getObjectProperty(name)).floatValue() == 1.3f);
347     }
348
349     public void testGetPropertyNames() throws JMSException JavaDoc {
350         ActiveMQMessage msg = new ActiveMQMessage();
351         String JavaDoc name = "floatProperty";
352         msg.setFloatProperty(name, 1.3f);
353         for (Enumeration JavaDoc iter = msg.getPropertyNames(); iter.hasMoreElements();) {
354             assertTrue(iter.nextElement().equals(name));
355         }
356     }
357
358     public void testSetObjectProperty() throws JMSException JavaDoc {
359         ActiveMQMessage msg = new ActiveMQMessage();
360         String JavaDoc name = "property";
361
362         try {
363             msg.setObjectProperty(name, "string");
364             msg.setObjectProperty(name, Byte.valueOf("1"));
365             msg.setObjectProperty(name, Short.valueOf("1"));
366             msg.setObjectProperty(name, Integer.valueOf("1"));
367             msg.setObjectProperty(name, Long.valueOf("1"));
368             msg.setObjectProperty(name, Float.valueOf("1.1f"));
369             msg.setObjectProperty(name, Double.valueOf("1.1"));
370             msg.setObjectProperty(name, Boolean.TRUE);
371             msg.setObjectProperty(name, null);
372         } catch (MessageFormatException JavaDoc e) {
373             fail("should accept object primitives and String");
374         }
375         try {
376             msg.setObjectProperty(name, new byte[5]);
377             fail("should accept only object primitives and String");
378         } catch (MessageFormatException JavaDoc e) {
379         }
380         try {
381             msg.setObjectProperty(name, new Object JavaDoc());
382             fail("should accept only object primitives and String");
383         } catch (MessageFormatException JavaDoc e) {
384         }
385     }
386
387     public void testConvertProperties() throws Exception JavaDoc {
388         org.apache.activemq.command.Message msg = new org.apache.activemq.command.Message() {
389             public org.apache.activemq.command.Message copy() {
390                 return null;
391             }
392             public void beforeMarshall(WireFormat wireFormat) throws IOException JavaDoc {
393                 super.beforeMarshall(wireFormat);
394             }
395             public byte getDataStructureType() {
396                 return 0;
397             }
398             public Response visit(CommandVisitor visitor) throws Exception JavaDoc {
399                 return null;
400             }
401         };
402
403         msg.setProperty("stringProperty", "string");
404         msg.setProperty("byteProperty", Byte.valueOf("1"));
405         msg.setProperty("shortProperty", Short.valueOf("1"));
406         msg.setProperty("intProperty", Integer.valueOf("1"));
407         msg.setProperty("longProperty", Long.valueOf("1"));
408         msg.setProperty("floatProperty", Float.valueOf("1.1f"));
409         msg.setProperty("doubleProperty", Double.valueOf("1.1"));
410         msg.setProperty("booleanProperty", Boolean.TRUE);
411         msg.setProperty("nullProperty", null);
412
413         msg.beforeMarshall(new OpenWireFormat());
414
415         Map JavaDoc properties = msg.getProperties();
416         assertEquals(properties.get("stringProperty"),"string");
417         assertEquals(((Byte JavaDoc)properties.get("byteProperty")).byteValue(),1);
418         assertEquals(((Short JavaDoc)properties.get("shortProperty")).shortValue(),1);
419         assertEquals(((Integer JavaDoc)properties.get("intProperty")).intValue(),1);
420         assertEquals(((Long JavaDoc)properties.get("longProperty")).longValue(),1);
421         assertEquals(((Float JavaDoc)properties.get("floatProperty")).floatValue(),1.1f,0);
422         assertEquals(((Double JavaDoc)properties.get("doubleProperty")).doubleValue(),1.1,0);
423         assertEquals(((Boolean JavaDoc)properties.get("booleanProperty")).booleanValue(),true);
424         assertNull(properties.get("nullProperty"));
425
426     }
427
428     public void testSetNullProperty() throws JMSException JavaDoc {
429         Message msg = new ActiveMQMessage();
430         String JavaDoc name = "cheese";
431         msg.setStringProperty(name, "Cheddar");
432         assertEquals("Cheddar", msg.getStringProperty(name));
433
434         msg.setStringProperty(name, null);
435         assertEquals(null, msg.getStringProperty(name));
436     }
437
438     public void testSetNullPropertyName() throws JMSException JavaDoc {
439         Message msg = new ActiveMQMessage();
440
441         try {
442             msg.setStringProperty(null, "Cheese");
443             fail("Should have thrown exception");
444         } catch (IllegalArgumentException JavaDoc e) {
445             log.info("Worked, caught: " + e);
446         }
447     }
448
449     public void testSetEmptyPropertyName() throws JMSException JavaDoc {
450         Message msg = new ActiveMQMessage();
451
452         try {
453             msg.setStringProperty("", "Cheese");
454             fail("Should have thrown exception");
455         } catch (IllegalArgumentException JavaDoc e) {
456             log.info("Worked, caught: " + e);
457         }
458     }
459
460     public void testGetAndSetJMSXDeliveryCount() throws JMSException JavaDoc {
461         Message msg = new ActiveMQMessage();
462         msg.setIntProperty("JMSXDeliveryCount", 1);
463         int count = msg.getIntProperty("JMSXDeliveryCount");
464         assertTrue("expected delivery count = 1 - got: " + count, count == 1);
465     }
466
467     public void testClearBody() throws JMSException JavaDoc {
468         ActiveMQBytesMessage message = new ActiveMQBytesMessage();
469         message.clearBody();
470         assertFalse(message.isReadOnlyBody());
471         assertNull(message.getContent());
472     }
473
474     public void testBooleanPropertyConversion() throws JMSException JavaDoc {
475         ActiveMQMessage msg = new ActiveMQMessage();
476         String JavaDoc propertyName = "property";
477         msg.setBooleanProperty(propertyName, true);
478
479         assertEquals(((Boolean JavaDoc) msg.getObjectProperty(propertyName)).booleanValue(), true);
480         assertTrue(msg.getBooleanProperty(propertyName));
481         assertEquals(msg.getStringProperty(propertyName), "true");
482         try {
483             msg.getByteProperty(propertyName);
484             fail("Should have thrown exception");
485         } catch (MessageFormatException JavaDoc e) {
486         }
487         try {
488             msg.getShortProperty(propertyName);
489             fail("Should have thrown exception");
490         } catch (MessageFormatException JavaDoc e) {
491         }
492         try {
493             msg.getIntProperty(propertyName);
494             fail("Should have thrown exception");
495         } catch (MessageFormatException JavaDoc e) {
496         }
497         try {
498             msg.getLongProperty(propertyName);
499             fail("Should have thrown exception");
500         } catch (MessageFormatException JavaDoc e) {
501         }
502         try {
503             msg.getFloatProperty(propertyName);
504             fail("Should have thrown exception");
505         } catch (MessageFormatException JavaDoc e) {
506         }
507         try {
508             msg.getDoubleProperty(propertyName);
509             fail("Should have thrown exception");
510         } catch (MessageFormatException JavaDoc e) {
511         }
512     }
513
514     public void testBytePropertyConversion() throws JMSException JavaDoc {
515         ActiveMQMessage msg = new ActiveMQMessage();
516         String JavaDoc propertyName = "property";
517         msg.setByteProperty(propertyName, (byte) 1);
518
519         assertEquals(((Byte JavaDoc) msg.getObjectProperty(propertyName)).byteValue(), 1);
520         assertEquals(msg.getByteProperty(propertyName), 1);
521         assertEquals(msg.getShortProperty(propertyName), 1);
522         assertEquals(msg.getIntProperty(propertyName), 1);
523         assertEquals(msg.getLongProperty(propertyName), 1);
524         assertEquals(msg.getStringProperty(propertyName), "1");
525         try {
526             msg.getBooleanProperty(propertyName);
527             fail("Should have thrown exception");
528         } catch (MessageFormatException JavaDoc e) {
529         }
530         try {
531             msg.getFloatProperty(propertyName);
532             fail("Should have thrown exception");
533         } catch (MessageFormatException JavaDoc e) {
534         }
535         try {
536             msg.getDoubleProperty(propertyName);
537             fail("Should have thrown exception");
538         } catch (MessageFormatException JavaDoc e) {
539         }
540     }
541
542     public void testShortPropertyConversion() throws JMSException JavaDoc {
543         ActiveMQMessage msg = new ActiveMQMessage();
544         String JavaDoc propertyName = "property";
545         msg.setShortProperty(propertyName, (short) 1);
546
547         assertEquals(((Short JavaDoc) msg.getObjectProperty(propertyName)).shortValue(), 1);
548         assertEquals(msg.getShortProperty(propertyName), 1);
549         assertEquals(msg.getIntProperty(propertyName), 1);
550         assertEquals(msg.getLongProperty(propertyName), 1);
551         assertEquals(msg.getStringProperty(propertyName), "1");
552         try {
553             msg.getBooleanProperty(propertyName);
554             fail("Should have thrown exception");
555         } catch (MessageFormatException JavaDoc e) {
556         }
557         try {
558             msg.getByteProperty(propertyName);
559             fail("Should have thrown exception");
560         } catch (MessageFormatException JavaDoc e) {
561         }
562         try {
563             msg.getFloatProperty(propertyName);
564             fail("Should have thrown exception");
565         } catch (MessageFormatException JavaDoc e) {
566         }
567         try {
568             msg.getDoubleProperty(propertyName);
569             fail("Should have thrown exception");
570         } catch (MessageFormatException JavaDoc e) {
571         }
572     }
573
574     public void testIntPropertyConversion() throws JMSException JavaDoc {
575         ActiveMQMessage msg = new ActiveMQMessage();
576         String JavaDoc propertyName = "property";
577         msg.setIntProperty(propertyName, (int) 1);
578
579         assertEquals(((Integer JavaDoc) msg.getObjectProperty(propertyName)).intValue(), 1);
580         assertEquals(msg.getIntProperty(propertyName), 1);
581         assertEquals(msg.getLongProperty(propertyName), 1);
582         assertEquals(msg.getStringProperty(propertyName), "1");
583         try {
584             msg.getBooleanProperty(propertyName);
585             fail("Should have thrown exception");
586         } catch (MessageFormatException JavaDoc e) {
587         }
588         try {
589             msg.getByteProperty(propertyName);
590             fail("Should have thrown exception");
591         } catch (MessageFormatException JavaDoc e) {
592         }
593         try {
594             msg.getShortProperty(propertyName);
595             fail("Should have thrown exception");
596         } catch (MessageFormatException JavaDoc e) {
597         }
598         try {
599             msg.getFloatProperty(propertyName);
600             fail("Should have thrown exception");
601         } catch (MessageFormatException JavaDoc e) {
602         }
603         try {
604             msg.getDoubleProperty(propertyName);
605             fail("Should have thrown exception");
606         } catch (MessageFormatException JavaDoc e) {
607         }
608     }
609
610     public void testLongPropertyConversion() throws JMSException JavaDoc {
611         ActiveMQMessage msg = new ActiveMQMessage();
612         String JavaDoc propertyName = "property";
613         msg.setLongProperty(propertyName, 1);
614
615         assertEquals(((Long JavaDoc) msg.getObjectProperty(propertyName)).longValue(), 1);
616         assertEquals(msg.getLongProperty(propertyName), 1);
617         assertEquals(msg.getStringProperty(propertyName), "1");
618         try {
619             msg.getBooleanProperty(propertyName);
620             fail("Should have thrown exception");
621         } catch (MessageFormatException JavaDoc e) {
622         }
623         try {
624             msg.getByteProperty(propertyName);
625             fail("Should have thrown exception");
626         } catch (MessageFormatException JavaDoc e) {
627         }
628         try {
629             msg.getShortProperty(propertyName);
630             fail("Should have thrown exception");
631         } catch (MessageFormatException JavaDoc e) {
632         }
633         try {
634             msg.getIntProperty(propertyName);
635             fail("Should have thrown exception");
636         } catch (MessageFormatException JavaDoc e) {
637         }
638         try {
639             msg.getFloatProperty(propertyName);
640             fail("Should have thrown exception");
641         } catch (MessageFormatException JavaDoc e) {
642         }
643         try {
644             msg.getDoubleProperty(propertyName);
645             fail("Should have thrown exception");
646         } catch (MessageFormatException JavaDoc e) {
647         }
648     }
649
650     public void testFloatPropertyConversion() throws JMSException JavaDoc {
651         ActiveMQMessage msg = new ActiveMQMessage();
652         String JavaDoc propertyName = "property";
653         msg.setFloatProperty(propertyName, (float) 1.5);
654         assertEquals(((Float JavaDoc) msg.getObjectProperty(propertyName)).floatValue(), 1.5, 0);
655         assertEquals(msg.getFloatProperty(propertyName), 1.5, 0);
656         assertEquals(msg.getDoubleProperty(propertyName), 1.5, 0);
657         assertEquals(msg.getStringProperty(propertyName), "1.5");
658         try {
659             msg.getBooleanProperty(propertyName);
660             fail("Should have thrown exception");
661         } catch (MessageFormatException JavaDoc e) {
662         }
663         try {
664             msg.getByteProperty(propertyName);
665             fail("Should have thrown exception");
666         } catch (MessageFormatException JavaDoc e) {
667         }
668         try {
669             msg.getShortProperty(propertyName);
670             fail("Should have thrown exception");
671         } catch (MessageFormatException JavaDoc e) {
672         }
673         try {
674             msg.getIntProperty(propertyName);
675             fail("Should have thrown exception");
676         } catch (MessageFormatException JavaDoc e) {
677         }
678         try {
679             msg.getLongProperty(propertyName);
680             fail("Should have thrown exception");
681         } catch (MessageFormatException JavaDoc e) {
682         }
683     }
684
685     public void testDoublePropertyConversion() throws JMSException JavaDoc {
686         ActiveMQMessage msg = new ActiveMQMessage();
687         String JavaDoc propertyName = "property";
688         msg.setDoubleProperty(propertyName, 1.5);
689         assertEquals(((Double JavaDoc) msg.getObjectProperty(propertyName)).doubleValue(), 1.5, 0);
690         assertEquals(msg.getDoubleProperty(propertyName), 1.5, 0);
691         assertEquals(msg.getStringProperty(propertyName), "1.5");
692         try {
693             msg.getBooleanProperty(propertyName);
694             fail("Should have thrown exception");
695         } catch (MessageFormatException JavaDoc e) {
696         }
697         try {
698             msg.getByteProperty(propertyName);
699             fail("Should have thrown exception");
700         } catch (MessageFormatException JavaDoc e) {
701         }
702         try {
703             msg.getShortProperty(propertyName);
704             fail("Should have thrown exception");
705         } catch (MessageFormatException JavaDoc e) {
706         }
707         try {
708             msg.getIntProperty(propertyName);
709             fail("Should have thrown exception");
710         } catch (MessageFormatException JavaDoc e) {
711         }
712         try {
713             msg.getLongProperty(propertyName);
714             fail("Should have thrown exception");
715         } catch (MessageFormatException JavaDoc e) {
716         }
717         try {
718             msg.getFloatProperty(propertyName);
719             fail("Should have thrown exception");
720         } catch (MessageFormatException JavaDoc e) {
721         }
722     }
723
724     public void testStringPropertyConversion() throws JMSException JavaDoc {
725         ActiveMQMessage msg = new ActiveMQMessage();
726         String JavaDoc propertyName = "property";
727         String JavaDoc stringValue = "true";
728         msg.setStringProperty(propertyName, stringValue);
729         assertEquals(msg.getStringProperty(propertyName), stringValue);
730         assertEquals((String JavaDoc) msg.getObjectProperty(propertyName), stringValue);
731         assertEquals(msg.getBooleanProperty(propertyName), true);
732
733         stringValue = "1";
734         msg.setStringProperty(propertyName, stringValue);
735         assertEquals(msg.getByteProperty(propertyName), 1);
736         assertEquals(msg.getShortProperty(propertyName), 1);
737         assertEquals(msg.getIntProperty(propertyName), 1);
738         assertEquals(msg.getLongProperty(propertyName), 1);
739
740         stringValue = "1.5";
741         msg.setStringProperty(propertyName, stringValue);
742         assertEquals(msg.getFloatProperty(propertyName), 1.5, 0);
743         assertEquals(msg.getDoubleProperty(propertyName), 1.5, 0);
744
745         stringValue = "bad";
746         msg.setStringProperty(propertyName, stringValue);
747         try {
748             msg.getByteProperty(propertyName);
749             fail("Should have thrown exception");
750         } catch (NumberFormatException JavaDoc e) {
751         }
752         try {
753             msg.getShortProperty(propertyName);
754             fail("Should have thrown exception");
755         } catch (NumberFormatException JavaDoc e) {
756         }
757         try {
758             msg.getIntProperty(propertyName);
759             fail("Should have thrown exception");
760         } catch (NumberFormatException JavaDoc e) {
761         }
762         try {
763             msg.getLongProperty(propertyName);
764             fail("Should have thrown exception");
765         } catch (NumberFormatException JavaDoc e) {
766         }
767         try {
768             msg.getFloatProperty(propertyName);
769             fail("Should have thrown exception");
770         } catch (NumberFormatException JavaDoc e) {
771         }
772         try {
773             msg.getDoubleProperty(propertyName);
774             fail("Should have thrown exception");
775         } catch (NumberFormatException JavaDoc e) {
776         }
777         assertFalse(msg.getBooleanProperty(propertyName));
778     }
779
780     public void testObjectPropertyConversion() throws JMSException JavaDoc {
781         ActiveMQMessage msg = new ActiveMQMessage();
782         String JavaDoc propertyName = "property";
783         Object JavaDoc obj = new Object JavaDoc();
784         try {
785             ((org.apache.activemq.command.Message)msg).setProperty(propertyName, obj); //bypass object check
786
} catch (IOException JavaDoc e) {
787         }
788         try {
789             msg.getStringProperty(propertyName);
790             fail("Should have thrown exception");
791         } catch (MessageFormatException JavaDoc e) {
792         }
793         try {
794             msg.getBooleanProperty(propertyName);
795             fail("Should have thrown exception");
796         } catch (MessageFormatException JavaDoc e) {
797         }
798         try {
799             msg.getByteProperty(propertyName);
800             fail("Should have thrown exception");
801         } catch (MessageFormatException JavaDoc e) {
802         }
803         try {
804             msg.getShortProperty(propertyName);
805             fail("Should have thrown exception");
806         } catch (MessageFormatException JavaDoc e) {
807         }
808         try {
809             msg.getIntProperty(propertyName);
810             fail("Should have thrown exception");
811         } catch (MessageFormatException JavaDoc e) {
812         }
813         try {
814             msg.getLongProperty(propertyName);
815             fail("Should have thrown exception");
816         } catch (MessageFormatException JavaDoc e) {
817         }
818         try {
819             msg.getFloatProperty(propertyName);
820             fail("Should have thrown exception");
821         } catch (MessageFormatException JavaDoc e) {
822         }
823         try {
824             msg.getDoubleProperty(propertyName);
825             fail("Should have thrown exception");
826         } catch (MessageFormatException JavaDoc e) {
827         }
828
829     }
830
831     public void testReadOnlyProperties() throws JMSException JavaDoc {
832         ActiveMQMessage msg = new ActiveMQMessage();
833         String JavaDoc propertyName = "property";
834         msg.setReadOnlyProperties(true);
835
836         try {
837             msg.setObjectProperty(propertyName, new Object JavaDoc());
838             fail("Should have thrown exception");
839         } catch (MessageNotWriteableException JavaDoc e) {
840         }
841         try {
842             msg.setStringProperty(propertyName, "test");
843             fail("Should have thrown exception");
844         } catch (MessageNotWriteableException JavaDoc e) {
845         }
846         try {
847             msg.setBooleanProperty(propertyName, true);
848             fail("Should have thrown exception");
849         } catch (MessageNotWriteableException JavaDoc e) {
850         }
851         try {
852             msg.setByteProperty(propertyName, (byte) 1);
853             fail("Should have thrown exception");
854         } catch (MessageNotWriteableException JavaDoc e) {
855         }
856         try {
857             msg.setShortProperty(propertyName, (short) 1);
858             fail("Should have thrown exception");
859         } catch (MessageNotWriteableException JavaDoc e) {
860         }
861         try {
862             msg.setIntProperty(propertyName, 1);
863             fail("Should have thrown exception");
864         } catch (MessageNotWriteableException JavaDoc e) {
865         }
866         try {
867             msg.setLongProperty(propertyName, 1);
868             fail("Should have thrown exception");
869         } catch (MessageNotWriteableException JavaDoc e) {
870         }
871         try {
872             msg.setFloatProperty(propertyName, (float) 1.5);
873             fail("Should have thrown exception");
874         } catch (MessageNotWriteableException JavaDoc e) {
875         }
876         try {
877             msg.setDoubleProperty(propertyName, 1.5);
878             fail("Should have thrown exception");
879         } catch (MessageNotWriteableException JavaDoc e) {
880         }
881     }
882
883     public void testIsExpired() {
884         ActiveMQMessage msg = new ActiveMQMessage();
885         msg.setJMSExpiration(System.currentTimeMillis() - 1);
886         assertTrue(msg.isExpired());
887         msg.setJMSExpiration(System.currentTimeMillis() + 10000);
888         assertFalse(msg.isExpired());
889     }
890
891 }
892
Popular Tags