View Javadoc

1   /**
2    * 
3    */
4   package com.github.smokestack.jms;
5   
6   import java.util.Collections;
7   import java.util.Enumeration;
8   import java.util.HashMap;
9   import java.util.Map;
10  
11  import javax.jms.DeliveryMode;
12  import javax.jms.Destination;
13  import javax.jms.JMSException;
14  import javax.jms.Message;
15  import javax.jms.Queue;
16  import javax.jms.Topic;
17  
18  import com.github.smokestack.exception.NotYetImplementedException;
19  
20  import org.apache.commons.lang.builder.ReflectionToStringBuilder;
21  import org.apache.commons.lang.builder.ToStringStyle;
22  import org.hamcrest.collection.IsIn;
23  import org.hamcrest.core.AnyOf;
24  import org.hamcrest.core.Is;
25  import org.hamcrest.core.IsInstanceOf;
26  
27  import static org.hamcrest.MatcherAssert.assertThat;
28  
29  /**
30   * @author gliptak
31   *
32   */
33  public class MockMessage implements Message {
34  
35  	public static final String JMSCorrelationID = "JMSCorrelationID";
36  
37  	public static final String JMSDeliveryMode = "JMSDeliveryMode";
38  
39  	public static final String JMSDestination = "JMSDestination";
40  
41  	public static final String JMSReplyTo = "JMSReplyTo";
42  
43  	public static final String JMSExpiration = "JMSExpiration";
44  
45  	public static final String JMSMessageID = "JMSMessageID";
46  
47  	public static final String JMSPriority = "JMSPriority";
48  
49  	public static final String JMSRedelivered = "JMSRedelivered";
50  
51  	public static final String JMSTimestamp = "JMSTimestamp";
52  
53  	public static final String JMSType = "JMSType";
54  	
55  	protected Map<String, String> properties=new HashMap<String, String>();
56  	
57  	protected Map<String, String> headers=new HashMap<String, String>();
58  
59  	protected Destination jmsDestination;
60  
61  	protected Destination jmsReplyTo;
62  	
63  	/* ()(non-Javadoc)
64  	 * @see javax.jms.Message#acknowledge()
65  	 */
66  	public void acknowledge() throws JMSException {
67  		_acknowledge();
68  	}
69  
70  	public void _acknowledge() throws JMSException {
71  		// TODO: should we try to go back to connection?
72  	}
73  
74  	/* (non-Javadoc)
75  	 * @see javax.jms.Message#clearBody()
76  	 */
77  	public void clearBody() throws JMSException {
78  		_clearBody();
79  	}
80  
81  	public void _clearBody() throws JMSException {
82  		throw new NotYetImplementedException();
83  	}
84  
85  	/* (non-Javadoc)
86  	 * @see javax.jms.Message#clearProperties()
87  	 */
88  	public void clearProperties() throws JMSException {
89  		_clearProperties();
90  		properties.clear();
91  	}
92  
93  	public void _clearProperties() throws JMSException {
94  	}
95  
96  	/* (non-Javadoc)
97  	 * @see javax.jms.Message#getBooleanProperty(java.lang.String)
98  	 */
99  	public boolean getBooleanProperty(String name) throws JMSException {
100 		_getBooleanProperty(name);
101 		return Boolean.parseBoolean(properties.get(name));
102 	}
103 
104 	public boolean _getBooleanProperty(String name) throws JMSException {
105 		return false;
106 	}
107 
108 	/* (non-Javadoc)
109 	 * @see javax.jms.Message#getByteProperty(java.lang.String)
110 	 */
111 	public byte getByteProperty(String name) throws JMSException {
112 		_getByteProperty(name);
113 		return Byte.parseByte(properties.get(name));
114 	}
115 
116 	public byte _getByteProperty(String name) throws JMSException {
117 		return 0;
118 	}
119 
120 	/* (non-Javadoc)
121 	 * @see javax.jms.Message#getDoubleProperty(java.lang.String)
122 	 */
123 	public double getDoubleProperty(String name) throws JMSException {
124 		_getDoubleProperty(name);
125 		return Double.parseDouble(properties.get(name));
126 	}
127 
128 	public double _getDoubleProperty(String name) throws JMSException {
129 		return 0;
130 	}
131 
132 	/* (non-Javadoc)
133 	 * @see javax.jms.Message#getFloatProperty(java.lang.String)
134 	 */
135 	public float getFloatProperty(String name) throws JMSException {
136 		_getFloatProperty(name);
137 		return Float.parseFloat(properties.get(name));
138 	}
139 
140 	public float _getFloatProperty(String name) throws JMSException {
141 		return 0;
142 	}
143 
144 	/* (non-Javadoc)
145 	 * @see javax.jms.Message#getIntProperty(java.lang.String)
146 	 */
147 	public int getIntProperty(String name) throws JMSException {
148 		_getIntProperty(name);
149 		return Integer.parseInt(properties.get(name));
150 	}
151 
152 	public int _getIntProperty(String name) throws JMSException {
153 		return 0;
154 	}
155 
156 	/* (non-Javadoc)
157 	 * @see javax.jms.Message#getJMSCorrelationID()
158 	 */
159 	public String getJMSCorrelationID() throws JMSException {
160 		_getJMSCorrelationID();
161 		if (!headers.containsKey(JMSCorrelationID)){
162 			throw new JMSException("no JMSCorrelationID");
163 		}
164 		return headers.get(JMSCorrelationID);
165 	}
166 
167 	public String _getJMSCorrelationID() throws JMSException {
168 		return null;
169 	}
170 
171 	/* (non-Javadoc)
172 	 * @see javax.jms.Message#getJMSCorrelationIDAsBytes()
173 	 */
174 	public byte[] getJMSCorrelationIDAsBytes() throws JMSException {
175 	    _getJMSCorrelationIDAsBytes();
176 		if (!headers.containsKey(JMSCorrelationID)){
177 			throw new JMSException("no JMSCorrelationID");
178 		}
179 		String s=headers.get(JMSCorrelationID);
180 	    if ((s.length() % 2) != 0){
181 	        throw new JMSException("JMSCorrelationID is odd length");	    	
182 	    }
183 	    final byte result[] = new byte[s.length()/2];
184 	    final char enc[] = s.toCharArray();
185 	    for (int i = 0; i < enc.length; i += 2) {
186 	        StringBuilder curr = new StringBuilder(2);
187 	        curr.append(enc[i]).append(enc[i + 1]);
188 	        result[i/2] = (byte) Integer.parseInt(curr.toString(), 16);
189 	    }
190 	    return result;	
191 	}
192 
193 	public byte[] _getJMSCorrelationIDAsBytes() throws JMSException {
194 		return null;
195 
196 	}
197 
198 	/* (non-Javadoc)
199 	 * @see javax.jms.Message#getJMSDeliveryMode()
200 	 */
201 	public int getJMSDeliveryMode() throws JMSException {
202 		_getJMSDeliveryMode();
203 		if (!headers.containsKey(JMSDeliveryMode)){
204 			return 0;			
205 		}
206 		return Integer.parseInt(headers.get(JMSDeliveryMode));
207 	}
208 
209 	public int _getJMSDeliveryMode() throws JMSException {
210 		return 0;
211 	}
212 
213 	/* (non-Javadoc)
214 	 * @see javax.jms.Message#getJMSDestination()
215 	 */
216 	public Destination getJMSDestination() throws JMSException {
217 		_getJMSDestination();
218 		if(!headers.containsKey(JMSDestination)){
219 			throw new JMSException("no JMSDestination");			
220 		}
221 		return jmsDestination;
222 	}
223 
224 	public Destination _getJMSDestination() throws JMSException {
225 		return null;
226 	}
227 
228 	/* (non-Javadoc)
229 	 * @see javax.jms.Message#getJMSExpiration()
230 	 */
231 	public long getJMSExpiration() throws JMSException {
232 		_getJMSExpiration();
233 		if(!headers.containsKey(JMSExpiration)){
234 			return 0;			
235 		}
236 		return Long.parseLong(headers.get(JMSExpiration));
237 	}
238 
239 	public long _getJMSExpiration() throws JMSException {
240 		return 0;
241 	}
242 
243 	/* (non-Javadoc)
244 	 * @see javax.jms.Message#getJMSMessageID()
245 	 */
246 	public String getJMSMessageID() throws JMSException {
247 		_getJMSMessageID();
248 		if(!headers.containsKey(JMSMessageID)){
249 			throw new JMSException("no JMSMessageID");			
250 		}
251 		return headers.get(JMSMessageID);
252 	}
253 
254 	public String _getJMSMessageID() throws JMSException {
255 		return null;
256 	}
257 
258 	/* (non-Javadoc)
259 	 * @see javax.jms.Message#getJMSPriority()
260 	 */
261 	public int getJMSPriority() throws JMSException {
262 		_getJMSPriority();
263 		if(!headers.containsKey(JMSPriority)){
264 			return 0;
265 		}
266 		return Integer.parseInt(headers.get(JMSPriority));
267 	}
268 
269 	public int _getJMSPriority() throws JMSException {
270 		return 0;
271 	}
272 
273 	/* (non-Javadoc)
274 	 * @see javax.jms.Message#getJMSRedelivered()
275 	 */
276 	public boolean getJMSRedelivered() throws JMSException {
277 		_getJMSRedelivered();
278 		if(!headers.containsKey(JMSRedelivered)){
279 			return false;
280 		}
281 		return Boolean.parseBoolean(headers.get(JMSRedelivered));
282 	}
283 
284 	public boolean _getJMSRedelivered() throws JMSException {
285 		return false;
286 	}
287 
288 	/* (non-Javadoc)
289 	 * @see javax.jms.Message#getJMSReplyTo()
290 	 */
291 	public Destination getJMSReplyTo() throws JMSException {
292 		_getJMSReplyTo();
293 		if(!headers.containsKey(JMSReplyTo)){
294 			throw new JMSException("no JMSReplyTo");			
295 		}
296 		return jmsReplyTo;
297 	}
298 
299 	public Destination _getJMSReplyTo() throws JMSException {
300 		return null;
301 	}
302 
303 	/* (non-Javadoc)
304 	 * @see javax.jms.Message#getJMSTimestamp()
305 	 */
306 	public long getJMSTimestamp() throws JMSException {
307 		_getJMSTimestamp();
308 		if(!headers.containsKey(JMSTimestamp)){
309 			return 0;
310 		}
311 		return Long.parseLong(headers.get(JMSTimestamp));
312 	}
313 
314 	public long _getJMSTimestamp() throws JMSException {
315 		return 0;
316 	}
317 
318 	/* (non-Javadoc)
319 	 * @see javax.jms.Message#getJMSType()
320 	 */
321 	public String getJMSType() throws JMSException {
322 		_getJMSType();
323 		return headers.get(JMSType);
324 	}
325 
326 	public String _getJMSType() throws JMSException {
327 		return null;
328 	}
329 
330 	/* (non-Javadoc)
331 	 * @see javax.jms.Message#getLongProperty(java.lang.String)
332 	 */
333 	public long getLongProperty(String name) throws JMSException {
334 		_getLongProperty(name);
335 		return Long.parseLong(properties.get(name));
336 	}
337 
338 	public long _getLongProperty(String name) throws JMSException {
339 		return 0;
340 	}
341 
342 	/* (non-Javadoc)
343 	 * @see javax.jms.Message#getObjectProperty(java.lang.String)
344 	 */
345 	public Object getObjectProperty(String name) throws JMSException {
346 		_getObjectProperty(name);
347 		return properties.get(name);
348 	}
349 
350 	public Object _getObjectProperty(String name) throws JMSException {
351 		return null;
352 	}
353 
354 	/* (non-Javadoc)
355 	 * @see javax.jms.Message#getPropertyNames()
356 	 */
357 	public Enumeration getPropertyNames() throws JMSException {
358 		_getPropertyNames();
359 		return Collections.enumeration(properties.keySet());
360 	}
361 
362 	public Enumeration _getPropertyNames() throws JMSException {
363 		return null;
364 	}
365 
366 	/* (non-Javadoc)
367 	 * @see javax.jms.Message#getShortProperty(java.lang.String)
368 	 */
369 	public short getShortProperty(String name) throws JMSException {
370 		_getShortProperty(name);
371 		return Short.parseShort(properties.get(name));
372 	}
373 
374 	public short _getShortProperty(String name) throws JMSException {
375 		return 0;
376 	}
377 
378 	/* (non-Javadoc)
379 	 * @see javax.jms.Message#getStringProperty(java.lang.String)
380 	 */
381 	public String getStringProperty(String name) throws JMSException {
382 		_getStringProperty(name);
383 		return properties.get(name);
384 	}
385 
386 	public String _getStringProperty(String name) throws JMSException {
387 		return null;
388 	}
389 
390 	/* (non-Javadoc)
391 	 * @see javax.jms.Message#propertyExists(java.lang.String)
392 	 */
393 	public boolean propertyExists(String name) throws JMSException {
394 		_propertyExists(name);
395 		return properties.containsKey(name);
396 	}
397 
398 	public boolean _propertyExists(String name) throws JMSException {
399 		return false;
400 	}
401 
402 	/* (non-Javadoc)
403 	 * @see javax.jms.Message#setBooleanProperty(java.lang.String, boolean)
404 	 */
405 	public void setBooleanProperty(String name, boolean value) throws JMSException {
406 		_setBooleanProperty(name, value);
407 		properties.put(name, Boolean.toString(value));
408 	}
409 
410 	public void _setBooleanProperty(String name, boolean value) throws JMSException {
411 	}
412 
413 	/* (non-Javadoc)
414 	 * @see javax.jms.Message#setByteProperty(java.lang.String, byte)
415 	 */
416 	public void setByteProperty(String name, byte value) throws JMSException {
417 		_setByteProperty(name, value);
418 		properties.put(name, Byte.toString(value));
419 	}
420 
421 	public void _setByteProperty(String name, byte value) throws JMSException {
422 	}
423 
424 	/* (non-Javadoc)
425 	 * @see javax.jms.Message#setDoubleProperty(java.lang.String, double)
426 	 */
427 	public void setDoubleProperty(String name, double value) throws JMSException {
428 		_setDoubleProperty(name, value);
429 		properties.put(name, Double.toString(value));
430 	}
431 
432 	public void _setDoubleProperty(String name, double value) throws JMSException {
433 	}
434 
435 	/* (non-Javadoc)
436 	 * @see javax.jms.Message#setFloatProperty(java.lang.String, float)
437 	 */
438 	public void setFloatProperty(String name, float value) throws JMSException {
439 		_setFloatProperty(name, value);
440 		properties.put(name, Float.toString(value));
441 	}
442 
443 	public void _setFloatProperty(String name, float value) throws JMSException {
444 	}
445 
446 	/* (non-Javadoc)
447 	 * @see javax.jms.Message#setIntProperty(java.lang.String, int)
448 	 */
449 	public void setIntProperty(String name, int value) throws JMSException {
450 		_setIntProperty(name, value);
451 		properties.put(name, Integer.toString(value));
452 	}
453 
454 	public void _setIntProperty(String name, int value) throws JMSException {
455 	}
456 
457 	/* (non-Javadoc)
458 	 * @see javax.jms.Message#setJMSCorrelationID(java.lang.String)
459 	 */
460 	public void setJMSCorrelationID(String correlationID) throws JMSException {
461 		_setJMSCorrelationID(correlationID);
462 		headers.put(JMSCorrelationID, correlationID);
463 	}
464 
465 	public void _setJMSCorrelationID(String correlationID) throws JMSException {
466 	}
467 
468 	/* (non-Javadoc)
469 	 * @see javax.jms.Message#setJMSCorrelationIDAsBytes(byte[])
470 	 */
471 	public void setJMSCorrelationIDAsBytes(byte[] correlationID) throws JMSException {
472 		_setJMSCorrelationIDAsBytes(correlationID);
473 		StringBuffer sb=new StringBuffer();
474 		for (byte b:correlationID){
475 		    String s=Integer.toString( ( b & 0xff ) + 0x100, 16).substring(1);
476 		    sb.append(s);
477 		}
478 		headers.put(JMSCorrelationID, sb.toString());
479 	}
480 
481 	public void _setJMSCorrelationIDAsBytes(byte[] correlationID) throws JMSException {
482 	}
483 
484 	/* (non-Javadoc)
485 	 * @see javax.jms.Message#setJMSDeliveryMode(int)
486 	 */
487 	public void setJMSDeliveryMode(int deliveryMode) throws JMSException {
488 		assertThat(deliveryMode, AnyOf.anyOf(Is.is(DeliveryMode.PERSISTENT),
489 				Is.is(DeliveryMode.NON_PERSISTENT)));
490         _setJMSDeliveryMode(deliveryMode);
491         headers.put(JMSDeliveryMode, Integer.toString(deliveryMode));
492 	}
493 
494 	public void _setJMSDeliveryMode(int deliveryMode) throws JMSException {
495 	}
496 
497 	/* (non-Javadoc)
498 	 * @see javax.jms.Message#setJMSDestination(javax.jms.Destination)
499 	 */
500 	public void setJMSDestination(Destination destination) throws JMSException {
501 		assertThat(destination, AnyOf.anyOf(IsInstanceOf.instanceOf(Topic.class),
502 				IsInstanceOf.instanceOf(Queue.class)));
503 		_setJMSDestination(destination);
504 		if (destination instanceof Topic){
505 			headers.put(JMSDestination, ((Topic)destination).getTopicName());
506 		}
507 		if (destination instanceof Queue){
508 			headers.put(JMSDestination, ((Queue)destination).getQueueName());
509 		}
510 		jmsDestination=destination;
511 	}
512 
513 	public void _setJMSDestination(Destination destination) throws JMSException {
514 	}
515 
516 	/* (non-Javadoc)
517 	 * @see javax.jms.Message#setJMSExpiration(long)
518 	 */
519 	public void setJMSExpiration(long expiration) throws JMSException {
520 		_setJMSExpiration(expiration);
521 		headers.put(JMSExpiration, Long.toString(expiration));
522 	}
523 
524 	public void _setJMSExpiration(long expiration) throws JMSException {
525 	}
526 
527 	/* (non-Javadoc)
528 	 * @see javax.jms.Message#setJMSMessageID(java.lang.String)
529 	 */
530 	public void setJMSMessageID(String id) throws JMSException {
531 		_setJMSMessageID(id);
532 		headers.put(JMSMessageID, id);
533 	}
534 
535 	public void _setJMSMessageID(String id) throws JMSException {
536 	}
537 
538 	/* (non-Javadoc)
539 	 * @see javax.jms.Message#setJMSPriority(int)
540 	 */
541 	public void setJMSPriority(int priority) throws JMSException {
542 		_setJMSPriority(priority);		
543 		Integer[] priorities={0,1,2,3,4,5,6,7,8,9};
544 		assertThat(priority, IsIn.isOneOf(priorities));
545 		headers.put(JMSPriority, Integer.toString(priority));		
546 	}
547 
548 	public void _setJMSPriority(int priority) throws JMSException {
549 	}
550 
551 	/* (non-Javadoc)
552 	 * @see javax.jms.Message#setJMSRedelivered(boolean)
553 	 */
554 	public void setJMSRedelivered(boolean redelivered) throws JMSException {
555 		_setJMSRedelivered(redelivered);		
556 		headers.put(JMSRedelivered, Boolean.toString(redelivered));		
557 	}
558 
559 	public void _setJMSRedelivered(boolean redelivered) throws JMSException {
560 	}
561 
562 	/* (non-Javadoc)
563 	 * @see javax.jms.Message#setJMSReplyTo(javax.jms.Destination)
564 	 */
565 	public void setJMSReplyTo(Destination destination) throws JMSException {
566 		assertThat(destination, AnyOf.anyOf(IsInstanceOf.instanceOf(Topic.class),
567 				IsInstanceOf.instanceOf(Queue.class)));
568 		_setJMSReplyTo(destination);
569 		if (destination instanceof Topic){
570 			headers.put(JMSReplyTo, ((Topic)destination).getTopicName());
571 		}
572 		if (destination instanceof Queue){
573 			headers.put(JMSReplyTo, ((Queue)destination).getQueueName());
574 		}
575 		jmsReplyTo=destination;
576 	}
577 
578 	public void _setJMSReplyTo(Destination destination) throws JMSException {
579 	}
580 
581 	/* (non-Javadoc)
582 	 * @see javax.jms.Message#setJMSTimestamp(long)
583 	 */
584 	public void setJMSTimestamp(long timestamp) throws JMSException {
585 		_setJMSTimestamp(timestamp);		
586 		headers.put(JMSTimestamp, Long.toString(timestamp));		
587 	}
588 
589 	public void _setJMSTimestamp(long timestamp) throws JMSException {
590 	}
591 
592 	/* (non-Javadoc)
593 	 * @see javax.jms.Message#setJMSType(java.lang.String)
594 	 */
595 	public void setJMSType(String type) throws JMSException {
596 		_setJMSType(type);		
597 		headers.put(JMSType, type);		
598 	}
599 
600 	public void _setJMSType(String type) throws JMSException {
601 	}
602 
603 	/* (non-Javadoc)
604 	 * @see javax.jms.Message#setLongProperty(java.lang.String, long)
605 	 */
606 	public void setLongProperty(String name, long value) throws JMSException {
607 		_setLongProperty(name, value);
608 		properties.put(name, Long.toString(value));
609 	}
610 
611 	public void _setLongProperty(String name, long value) throws JMSException {
612 	}
613 
614 	/* (non-Javadoc)
615 	 * @see javax.jms.Message#setObjectProperty(java.lang.String, java.lang.Object)
616 	 */
617 	public void setObjectProperty(String name, Object value) throws JMSException {
618 		// only Boolean, Byte, Short, Integer, Long, Float, Double, and String
619 		assertThat(value, AnyOf.anyOf(IsInstanceOf.instanceOf(Boolean.class),
620 				IsInstanceOf.instanceOf(Byte.class),
621 				IsInstanceOf.instanceOf(Short.class),
622 				IsInstanceOf.instanceOf(Integer.class),
623 				IsInstanceOf.instanceOf(Long.class),
624 				IsInstanceOf.instanceOf(Float.class),
625 				IsInstanceOf.instanceOf(Double.class),
626 				IsInstanceOf.instanceOf(String.class)));
627 		_setObjectProperty(name, value);
628 		properties.put(name, value.toString());
629 	}
630 
631 	public void _setObjectProperty(String name, Object value) throws JMSException {
632 	}
633 
634 	/* (non-Javadoc)
635 	 * @see javax.jms.Message#setShortProperty(java.lang.String, short)
636 	 */
637 	public void setShortProperty(String name, short value) throws JMSException {
638 		_setShortProperty(name, value);
639 		properties.put(name, Short.toString(value));
640 	}
641 
642 	public void _setShortProperty(String name, short value) throws JMSException {
643 	}
644 
645 	/* (non-Javadoc)
646 	 * @see javax.jms.Message#setStringProperty(java.lang.String, java.lang.String)
647 	 */
648 	public void setStringProperty(String name, String value) throws JMSException {
649 		_setStringProperty(name, value);
650 		properties.put(name, value);
651 	}
652 
653 	public void _setStringProperty(String name, String value) throws JMSException {
654 	}
655 	
656 	@Override
657 	public String toString(){
658 		return ReflectionToStringBuilder.toString(this, ToStringStyle.MULTI_LINE_STYLE);
659 	}
660 }