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
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
64
65
66 public void acknowledge() throws JMSException {
67 _acknowledge();
68 }
69
70 public void _acknowledge() throws JMSException {
71
72 }
73
74
75
76
77 public void clearBody() throws JMSException {
78 _clearBody();
79 }
80
81 public void _clearBody() throws JMSException {
82 throw new NotYetImplementedException();
83 }
84
85
86
87
88 public void clearProperties() throws JMSException {
89 _clearProperties();
90 properties.clear();
91 }
92
93 public void _clearProperties() throws JMSException {
94 }
95
96
97
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
109
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
121
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
133
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
145
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
157
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
172
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
199
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
214
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
229
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
244
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
259
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
274
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
289
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
304
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
319
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
331
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
343
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
355
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
367
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
379
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
391
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
403
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
414
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
425
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
436
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
447
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
458
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
469
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
485
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
498
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
517
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
528
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
539
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
552
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
563
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
582
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
593
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
604
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
615
616
617 public void setObjectProperty(String name, Object value) throws JMSException {
618
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
635
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
646
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 }