001    package com.mockrunner.mock.jms;
002    
003    import java.io.Serializable;
004    import java.io.UnsupportedEncodingException;
005    import java.util.Enumeration;
006    import java.util.HashMap;
007    import java.util.Map;
008    import java.util.Vector;
009    
010    import javax.jms.DeliveryMode;
011    import javax.jms.Destination;
012    import javax.jms.JMSException;
013    import javax.jms.Message;
014    import javax.jms.MessageFormatException;
015    import javax.jms.MessageNotWriteableException;
016    
017    import com.mockrunner.base.NestedApplicationException;
018    
019    /**
020     * Mock implementation of JMS <code>Message</code>.
021     */
022    public class MockMessage implements Message, Cloneable, Serializable
023    {
024        private String messageId;
025        private long timestamp;
026        private String correlationId;
027        private Destination replyTo;
028        private Destination destination;
029        private int deliveryMode;
030        private boolean redelivered;
031        private String type;
032        private long expiration;
033        private int priority;
034        private boolean acknowledged;
035        private Map properties;
036        private boolean isInWriteMode;
037        private boolean isInWriteModeProperties;
038        
039        public MockMessage()
040        {
041            messageId = null;
042            timestamp = System.currentTimeMillis();
043            deliveryMode = DeliveryMode.PERSISTENT;
044            redelivered = false;
045            expiration = 0;
046            priority = 4;
047            acknowledged = false;
048            properties = new HashMap();
049            isInWriteMode = true;
050            isInWriteModeProperties = true;
051        }
052        
053        public boolean isAcknowledged()
054        {
055            return acknowledged;
056        }
057        
058        public String getJMSMessageID() throws JMSException
059        {
060            return messageId;
061        }
062    
063        public void setJMSMessageID(String messageId) throws JMSException
064        {
065            this.messageId = messageId;
066        }
067    
068        public long getJMSTimestamp() throws JMSException
069        {
070            return timestamp;
071        }
072    
073        public void setJMSTimestamp(long timestamp) throws JMSException
074        {
075            this.timestamp = timestamp;
076        }
077    
078        public byte[] getJMSCorrelationIDAsBytes() throws JMSException
079        {
080            if(null == correlationId) return null;
081            try
082            {
083                return correlationId.getBytes("ISO-8859-1");
084            }
085            catch(UnsupportedEncodingException exc)
086            {
087                throw new JMSException(exc.getMessage());
088            }
089        }
090    
091        public void setJMSCorrelationIDAsBytes(byte[] correlationId) throws JMSException
092        {
093            try
094            {
095                if(null == correlationId)
096                {
097                    this.correlationId = null;
098                }
099                else
100                {
101                    this.correlationId = new String(correlationId, "ISO-8859-1");
102                }
103            }
104            catch(UnsupportedEncodingException exc)
105            {
106                throw new JMSException(exc.getMessage());
107            }
108        }
109    
110        public void setJMSCorrelationID(String correlationId) throws JMSException
111        {
112            this.correlationId = correlationId;
113        }
114    
115        public String getJMSCorrelationID() throws JMSException
116        {
117            return correlationId;
118        }
119    
120        public Destination getJMSReplyTo() throws JMSException
121        {
122            return replyTo;
123        }
124    
125        public void setJMSReplyTo(Destination replyTo) throws JMSException
126        {
127            this.replyTo = replyTo;
128        }
129    
130        public Destination getJMSDestination() throws JMSException
131        {
132            return destination;
133        }
134    
135        public void setJMSDestination(Destination destination) throws JMSException
136        {
137            this.destination = destination;
138        }
139    
140        public int getJMSDeliveryMode() throws JMSException
141        {
142            return deliveryMode;
143        }
144    
145        public void setJMSDeliveryMode(int deliveryMode) throws JMSException
146        {
147            this.deliveryMode = deliveryMode;
148        }
149    
150        public boolean getJMSRedelivered() throws JMSException
151        {
152            return redelivered;
153        }
154    
155        public void setJMSRedelivered(boolean redelivered) throws JMSException
156        {
157            this.redelivered = redelivered;
158        }
159    
160        public String getJMSType() throws JMSException
161        {
162            return type;
163        }
164    
165        public void setJMSType(String type) throws JMSException
166        {
167            this.type = type;
168        }
169    
170        public long getJMSExpiration() throws JMSException
171        {
172            return expiration;
173        }
174    
175        public void setJMSExpiration(long expiration) throws JMSException
176        {
177            this.expiration = expiration;
178        }
179    
180        public int getJMSPriority() throws JMSException
181        {
182            return priority;
183        }
184    
185        public void setJMSPriority(int priority) throws JMSException
186        {
187            this.priority = priority;
188        }
189    
190        public void clearProperties() throws JMSException
191        {
192            isInWriteModeProperties = true;
193            properties.clear();
194        }
195    
196        public boolean propertyExists(String name) throws JMSException
197        {
198            return properties.containsKey(name);
199        }
200    
201        public boolean getBooleanProperty(String name) throws JMSException
202        {
203            Object value = getObjectProperty(name);
204            if(value == null)
205            {
206                return Boolean.valueOf(null).booleanValue();
207            }
208            if(value instanceof String)
209            {
210                return Boolean.valueOf((String)value).booleanValue();
211            }
212            if(value instanceof Boolean)
213            {
214                return ((Boolean)value).booleanValue();
215            }
216            throw new MessageFormatException("Cannot convert property " + name + " of type " + value.getClass().getName() + " to boolean");
217        }
218    
219        public byte getByteProperty(String name) throws JMSException
220        {
221            Object value = getObjectProperty(name);
222            if(value == null)
223            {
224                return Byte.valueOf(null).byteValue();
225            }
226            if(value instanceof String)
227            {
228                return Byte.valueOf((String)value).byteValue();
229            }
230            if(value instanceof Byte)
231            {
232                return ((Number)value).byteValue();
233            }
234            throw new MessageFormatException("Cannot convert property " + name + " of type " + value.getClass().getName() + " to byte");
235        }
236    
237        public short getShortProperty(String name) throws JMSException
238        {
239            Object value = getObjectProperty(name);
240            if(value == null)
241            {
242                return Short.valueOf(null).shortValue();
243            }
244            if(value instanceof String)
245            {
246                return Short.valueOf((String)value).shortValue();
247            }
248            if((value instanceof Short) || (value instanceof Byte))
249            {
250                return ((Number)value).shortValue();
251            }
252            throw new MessageFormatException("Cannot convert property " + name + " of type " + value.getClass().getName() + " to short");
253        }
254    
255        public int getIntProperty(String name) throws JMSException
256        {
257            Object value = getObjectProperty(name);
258            if(value == null)
259            {
260                return Integer.valueOf(null).intValue();
261            }
262            if(value instanceof String)
263            {
264                return Integer.valueOf((String)value).intValue();
265            }
266            if((value instanceof Integer) || (value instanceof Short) || (value instanceof Byte))
267            {
268                return ((Number)value).intValue();
269            }
270            throw new MessageFormatException("Cannot convert property " + name + " of type " + value.getClass().getName() + " to int");
271        }
272    
273        public long getLongProperty(String name) throws JMSException
274        {
275            Object value = getObjectProperty(name);
276            if(value == null)
277            {
278                return Long.valueOf(null).longValue();
279            }
280            if(value instanceof String)
281            {
282                return Long.valueOf((String)value).longValue();
283            }
284            if((value instanceof Long) || (value instanceof Integer) || (value instanceof Short) || (value instanceof Byte))
285            {
286                return ((Number)value).longValue();
287            }
288            throw new MessageFormatException("Cannot convert property " + name + " of type " + value.getClass().getName() + " to long");
289        }
290    
291        public float getFloatProperty(String name) throws JMSException
292        {
293            Object value = getObjectProperty(name);
294            if(value == null)
295            {
296                return Float.valueOf(null).floatValue();
297            }
298            if(value instanceof String)
299            {
300                return Float.valueOf((String)value).floatValue();
301            }
302            if(value instanceof Float)
303            {
304                return ((Number)value).floatValue();
305            }
306            throw new MessageFormatException("Cannot convert property " + name + " of type " + value.getClass().getName() + " to float");
307        }
308    
309        public double getDoubleProperty(String name) throws JMSException
310        {
311            Object value = getObjectProperty(name);
312            if(value == null)
313            {
314                return Double.valueOf(null).doubleValue();
315            }
316            if(value instanceof String)
317            {
318               return Double.valueOf((String)value).doubleValue();
319            }
320            if((value instanceof Double) || (value instanceof Float))
321            {
322                return ((Number)value).doubleValue();
323            }
324            throw new MessageFormatException("Cannot convert property " + name + " of type " + value.getClass().getName() + " to double");
325        }
326    
327        public String getStringProperty(String name) throws JMSException
328        {
329            Object value = getObjectProperty(name);
330            if(null == value) return null;
331            return value.toString();
332        }
333    
334        public Object getObjectProperty(String name) throws JMSException
335        {
336            return properties.get(name);
337        }
338    
339        public Enumeration getPropertyNames() throws JMSException
340        {
341            return new Vector(properties.keySet()).elements();
342        }
343    
344        public void setBooleanProperty(String name, boolean value) throws JMSException
345        {
346            setObjectProperty(name, new Boolean(value));
347        }
348    
349        public void setByteProperty(String name, byte value) throws JMSException
350        {
351            setObjectProperty(name, new Byte(value));
352        }
353    
354        public void setShortProperty(String name, short value) throws JMSException
355        {
356            setObjectProperty(name, new Short(value));
357        }
358    
359        public void setIntProperty(String name, int value) throws JMSException
360        {
361            setObjectProperty(name, new Integer(value));
362        }
363    
364        public void setLongProperty(String name, long value) throws JMSException
365        {
366            setObjectProperty(name, new Long(value));
367        }
368    
369        public void setFloatProperty(String name, float value) throws JMSException
370        {
371            setObjectProperty(name, new Float(value));
372        }
373    
374        public void setDoubleProperty(String name, double value) throws JMSException
375        {
376            setObjectProperty(name, new Double(value));
377        }
378    
379        public void setStringProperty(String name, String value) throws JMSException
380        {
381            setObjectProperty(name, value);
382        }
383    
384        public void setObjectProperty(String name, Object object) throws JMSException
385        {
386            if(!isInWriteModeProperties)
387            {
388                throw new MessageNotWriteableException("Message is in read mode");
389            }
390            if(null == name || name.length() <= 0)
391            {
392                throw new IllegalArgumentException("Property names must not be null or empty strings");
393            }
394            if(null == object) return;
395            if((object instanceof String) || (object instanceof Number) || (object instanceof Boolean))
396            {
397                properties.put(name, object);
398                return;
399            }
400            throw new MessageFormatException(object.getClass().getName() + " not a valid type");
401        }
402    
403        public void acknowledge() throws JMSException
404        {
405            acknowledged = true;
406        }
407    
408        public void clearBody() throws JMSException
409        {
410            isInWriteMode = true;
411        }
412        
413        public void setReadOnly(boolean isReadOnly)
414        {
415            isInWriteMode = !isReadOnly;
416        }
417        
418        public void setReadOnlyProperties(boolean isReadOnly)
419        {
420            isInWriteModeProperties = !isReadOnly;
421        }
422        
423        public Object clone()
424        {
425            try
426            {
427                MockMessage clone = (MockMessage)super.clone();
428                clone.properties = new HashMap(properties);
429                return clone;
430            }
431            catch(CloneNotSupportedException exc)
432            {
433                throw new NestedApplicationException(exc);
434            }
435        }
436        
437        protected boolean isInWriteMode()
438        {
439            return isInWriteMode;
440        }
441    }