KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jms > core > JmsTemplate102Tests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.jms.core;
18
19 import javax.jms.DeliveryMode JavaDoc;
20 import javax.jms.Destination JavaDoc;
21 import javax.jms.JMSException JavaDoc;
22 import javax.jms.Message JavaDoc;
23 import javax.jms.MessageConsumer JavaDoc;
24 import javax.jms.MessageProducer JavaDoc;
25 import javax.jms.Queue JavaDoc;
26 import javax.jms.QueueConnection JavaDoc;
27 import javax.jms.QueueConnectionFactory JavaDoc;
28 import javax.jms.QueueReceiver JavaDoc;
29 import javax.jms.QueueSender JavaDoc;
30 import javax.jms.QueueSession JavaDoc;
31 import javax.jms.Session JavaDoc;
32 import javax.jms.TextMessage JavaDoc;
33 import javax.jms.Topic JavaDoc;
34 import javax.jms.TopicConnection JavaDoc;
35 import javax.jms.TopicConnectionFactory JavaDoc;
36 import javax.jms.TopicPublisher JavaDoc;
37 import javax.jms.TopicSession JavaDoc;
38 import javax.jms.TopicSubscriber JavaDoc;
39 import javax.naming.Context JavaDoc;
40 import javax.naming.NamingException JavaDoc;
41
42 import junit.framework.TestCase;
43 import org.easymock.MockControl;
44
45 import org.springframework.jms.support.converter.SimpleMessageConverter;
46 import org.springframework.jms.support.destination.JndiDestinationResolver;
47 import org.springframework.jndi.JndiTemplate;
48
49 /**
50  * Unit tests for the JmsTemplate implemented using JMS 1.0.2.
51  *
52  * @author Andre Biryukov
53  * @author Mark Pollack
54  */

55 public class JmsTemplate102Tests extends TestCase {
56
57     private Context JavaDoc mockJndiContext;
58     private MockControl mockJndiControl;
59
60     private MockControl queueConnectionFactoryControl;
61     private QueueConnectionFactory JavaDoc mockQueueConnectionFactory;
62
63     private MockControl queueConnectionControl;
64     private QueueConnection JavaDoc mockQueueConnection;
65
66     private MockControl queueSessionControl;
67     private QueueSession JavaDoc mockQueueSession;
68
69     private MockControl queueControl;
70     private Queue JavaDoc mockQueue;
71
72     private MockControl topicConnectionFactoryControl;
73     private TopicConnectionFactory JavaDoc mockTopicConnectionFactory;
74
75     private MockControl topicConnectionControl;
76     private TopicConnection JavaDoc mockTopicConnection;
77
78     private MockControl topicSessionControl;
79     private TopicSession JavaDoc mockTopicSession;
80
81     private MockControl topicControl;
82     private Topic JavaDoc mockTopic;
83
84     private int deliveryMode = DeliveryMode.PERSISTENT;
85     private int priority = 9;
86     private int timeToLive = 10000;
87
88
89     /**
90      * Create the mock objects for testing.
91      */

92     protected void setUp() throws Exception JavaDoc {
93         mockJndiControl = MockControl.createControl(Context JavaDoc.class);
94         mockJndiContext = (Context JavaDoc) this.mockJndiControl.getMock();
95
96         createMockForQueues();
97         createMockForTopics();
98
99         mockJndiContext.close();
100         mockJndiControl.replay();
101     }
102
103     private void createMockForTopics() throws JMSException JavaDoc, NamingException JavaDoc {
104         topicConnectionFactoryControl = MockControl.createControl(TopicConnectionFactory JavaDoc.class);
105         mockTopicConnectionFactory = (TopicConnectionFactory JavaDoc) topicConnectionFactoryControl.getMock();
106
107         topicConnectionControl = MockControl.createControl(TopicConnection JavaDoc.class);
108         mockTopicConnection = (TopicConnection JavaDoc) topicConnectionControl.getMock();
109
110         topicControl = MockControl.createControl(Topic JavaDoc.class);
111         mockTopic = (Topic JavaDoc) topicControl.getMock();
112
113         topicSessionControl = MockControl.createControl(TopicSession JavaDoc.class);
114         mockTopicSession = (TopicSession JavaDoc) topicSessionControl.getMock();
115
116         mockTopicConnectionFactory.createTopicConnection();
117         topicConnectionFactoryControl.setReturnValue(mockTopicConnection);
118         topicConnectionFactoryControl.replay();
119
120         mockTopicConnection.createTopicSession(useTransactedTemplate(), Session.AUTO_ACKNOWLEDGE);
121         topicConnectionControl.setReturnValue(mockTopicSession);
122         mockTopicSession.getTransacted();
123         topicSessionControl.setReturnValue(useTransactedSession());
124
125         mockJndiContext.lookup("testTopic");
126         mockJndiControl.setReturnValue(mockTopic);
127     }
128
129     private void createMockForQueues() throws JMSException JavaDoc, NamingException JavaDoc {
130         queueConnectionFactoryControl = MockControl.createControl(QueueConnectionFactory JavaDoc.class);
131         mockQueueConnectionFactory = (QueueConnectionFactory JavaDoc) queueConnectionFactoryControl.getMock();
132
133         queueConnectionControl = MockControl.createControl(QueueConnection JavaDoc.class);
134         mockQueueConnection = (QueueConnection JavaDoc) queueConnectionControl.getMock();
135
136         queueControl = MockControl.createControl(Queue JavaDoc.class);
137         mockQueue = (Queue JavaDoc) queueControl.getMock();
138
139         queueSessionControl = MockControl.createControl(QueueSession JavaDoc.class);
140         mockQueueSession = (QueueSession JavaDoc) queueSessionControl.getMock();
141
142         mockQueueConnectionFactory.createQueueConnection();
143         queueConnectionFactoryControl.setReturnValue(mockQueueConnection);
144         queueConnectionFactoryControl.replay();
145
146         mockQueueConnection.createQueueSession(useTransactedTemplate(), Session.AUTO_ACKNOWLEDGE);
147         queueConnectionControl.setReturnValue(mockQueueSession);
148         mockQueueSession.getTransacted();
149         queueSessionControl.setReturnValue(useTransactedSession());
150
151         mockJndiContext.lookup("testQueue");
152         mockJndiControl.setReturnValue(mockQueue);
153     }
154
155     private JmsTemplate102 createTemplate() {
156         JmsTemplate102 template = new JmsTemplate102();
157         JndiDestinationResolver destMan = new JndiDestinationResolver();
158         destMan.setJndiTemplate(new JndiTemplate() {
159             protected Context JavaDoc createInitialContext() throws NamingException JavaDoc {
160                 return mockJndiContext;
161             }
162         });
163         template.setDestinationResolver(destMan);
164         template.setSessionTransacted(useTransactedTemplate());
165         return template;
166     }
167
168     protected boolean useTransactedSession() {
169         return false;
170     }
171
172     protected boolean useTransactedTemplate() {
173         return false;
174     }
175
176
177     public void testTopicSessionCallback() throws Exception JavaDoc {
178         JmsTemplate102 template = createTemplate();
179         template.setPubSubDomain(true);
180         template.setConnectionFactory(mockTopicConnectionFactory);
181         template.afterPropertiesSet();
182
183         mockTopicSession.close();
184         topicSessionControl.setVoidCallable(1);
185
186         mockTopicConnection.close();
187         topicConnectionControl.setVoidCallable(1);
188
189         topicSessionControl.replay();
190         topicConnectionControl.replay();
191
192         template.execute(new SessionCallback() {
193             public Object JavaDoc doInJms(Session JavaDoc session) throws JMSException JavaDoc {
194                 boolean b = session.getTransacted();
195                 return null;
196             }
197         });
198
199         topicConnectionFactoryControl.verify();
200         topicConnectionControl.verify();
201         topicSessionControl.verify();
202     }
203
204     /**
205      * Test the execute(ProducerCallback) using a topic.
206      */

207     public void testTopicProducerCallback() throws Exception JavaDoc {
208         JmsTemplate102 template = createTemplate();
209         template.setPubSubDomain(true);
210         template.setConnectionFactory(mockTopicConnectionFactory);
211         template.afterPropertiesSet();
212
213         MockControl topicPublisherControl = MockControl.createControl(TopicPublisher JavaDoc.class);
214         TopicPublisher JavaDoc mockTopicPublisher = (TopicPublisher JavaDoc) topicPublisherControl.getMock();
215
216         mockTopicSession.createPublisher(null);
217         topicSessionControl.setReturnValue(mockTopicPublisher);
218
219         mockTopicPublisher.getPriority();
220         topicPublisherControl.setReturnValue(4);
221
222         mockTopicPublisher.close();
223         topicPublisherControl.setVoidCallable(1);
224         mockTopicSession.close();
225         topicSessionControl.setVoidCallable(1);
226         mockTopicConnection.close();
227         topicConnectionControl.setVoidCallable(1);
228
229         topicPublisherControl.replay();
230         topicSessionControl.replay();
231         topicConnectionControl.replay();
232
233         template.execute(new ProducerCallback() {
234             public Object JavaDoc doInJms(Session JavaDoc session, MessageProducer JavaDoc msgProducer) throws JMSException JavaDoc {
235                 boolean b = session.getTransacted();
236                 int i = msgProducer.getPriority();
237                 return null;
238             }
239         });
240
241         topicConnectionFactoryControl.verify();
242         topicConnectionControl.verify();
243         topicSessionControl.verify();
244     }
245
246     /**
247      * Test the execute(ProducerCallback) using a topic.
248      */

249     public void testTopicProducerCallbackWithIdAndTimestampDisabled() throws Exception JavaDoc {
250         JmsTemplate102 template = createTemplate();
251         template.setPubSubDomain(true);
252         template.setConnectionFactory(mockTopicConnectionFactory);
253         template.setMessageIdEnabled(false);
254         template.setMessageTimestampEnabled(false);
255         template.afterPropertiesSet();
256
257         MockControl topicPublisherControl = MockControl.createControl(TopicPublisher JavaDoc.class);
258         TopicPublisher JavaDoc mockTopicPublisher = (TopicPublisher JavaDoc) topicPublisherControl.getMock();
259
260         mockTopicSession.createPublisher(null);
261         topicSessionControl.setReturnValue(mockTopicPublisher);
262
263         mockTopicPublisher.setDisableMessageID(true);
264         topicPublisherControl.setVoidCallable(1);
265         mockTopicPublisher.setDisableMessageTimestamp(true);
266         topicPublisherControl.setVoidCallable(1);
267         mockTopicPublisher.getPriority();
268         topicPublisherControl.setReturnValue(4);
269
270         mockTopicPublisher.close();
271         topicPublisherControl.setVoidCallable(1);
272         mockTopicSession.close();
273         topicSessionControl.setVoidCallable(1);
274         mockTopicConnection.close();
275         topicConnectionControl.setVoidCallable(1);
276
277         topicPublisherControl.replay();
278         topicSessionControl.replay();
279         topicConnectionControl.replay();
280
281         template.execute(new ProducerCallback() {
282             public Object JavaDoc doInJms(Session JavaDoc session, MessageProducer JavaDoc msgProducer) throws JMSException JavaDoc {
283                 boolean b = session.getTransacted();
284                 int i = msgProducer.getPriority();
285                 return null;
286             }
287         });
288
289         topicConnectionFactoryControl.verify();
290         topicConnectionControl.verify();
291         topicSessionControl.verify();
292     }
293
294     /**
295      * Test the method execute(SessionCallback action) with using the
296      * point to point domain as specified by the value of isPubSubDomain = false.
297      */

298     public void testQueueSessionCallback() throws Exception JavaDoc {
299         JmsTemplate102 template = createTemplate();
300         // Point-to-Point (queues) are the default domain
301
template.setConnectionFactory(mockQueueConnectionFactory);
302         template.afterPropertiesSet();
303
304         mockQueueSession.close();
305         queueSessionControl.setVoidCallable(1);
306
307         mockQueueConnection.close();
308         queueConnectionControl.setVoidCallable(1);
309
310         queueSessionControl.replay();
311         queueConnectionControl.replay();
312
313         template.execute(new SessionCallback() {
314             public Object JavaDoc doInJms(Session JavaDoc session) throws JMSException JavaDoc {
315                 boolean b = session.getTransacted();
316                 return null;
317             }
318         });
319
320         queueConnectionFactoryControl.verify();
321         queueConnectionControl.verify();
322         queueSessionControl.verify();
323     }
324
325     /**
326      * Test the method execute(ProducerCallback) with a Queue.
327      */

328     public void testQueueProducerCallback() throws Exception JavaDoc {
329         JmsTemplate102 template = createTemplate();
330         // Point-to-Point (queues) are the default domain.
331
template.setConnectionFactory(mockQueueConnectionFactory);
332         template.afterPropertiesSet();
333
334         MockControl queueSenderControl = MockControl.createControl(QueueSender JavaDoc.class);
335         QueueSender JavaDoc mockQueueSender = (QueueSender JavaDoc) queueSenderControl.getMock();
336
337         mockQueueSession.createSender(null);
338         queueSessionControl.setReturnValue(mockQueueSender);
339
340         mockQueueSender.getPriority();
341         queueSenderControl.setReturnValue(4);
342
343         mockQueueSender.close();
344         queueSenderControl.setVoidCallable(1);
345         mockQueueSession.close();
346         queueSessionControl.setVoidCallable(1);
347         mockQueueConnection.close();
348         queueConnectionControl.setVoidCallable(1);
349
350         queueSenderControl.replay();
351         queueSessionControl.replay();
352         queueConnectionControl.replay();
353
354         template.execute(new ProducerCallback() {
355             public Object JavaDoc doInJms(Session JavaDoc session, MessageProducer JavaDoc msgProducer)
356                 throws JMSException JavaDoc {
357                 boolean b = session.getTransacted();
358                 int i = msgProducer.getPriority();
359                 return null;
360             }
361         });
362
363         queueConnectionFactoryControl.verify();
364         queueConnectionControl.verify();
365         queueSessionControl.verify();
366     }
367
368     public void testQueueProducerCallbackWithIdAndTimestampDisabled() throws Exception JavaDoc {
369         JmsTemplate102 template = createTemplate();
370         // Point-to-Point (queues) are the default domain.
371
template.setConnectionFactory(mockQueueConnectionFactory);
372         template.setMessageIdEnabled(false);
373         template.setMessageTimestampEnabled(false);
374         template.afterPropertiesSet();
375
376         MockControl queueSenderControl = MockControl.createControl(QueueSender JavaDoc.class);
377         QueueSender JavaDoc mockQueueSender = (QueueSender JavaDoc) queueSenderControl.getMock();
378
379         mockQueueSession.createSender(null);
380         queueSessionControl.setReturnValue(mockQueueSender);
381
382         mockQueueSender.setDisableMessageID(true);
383         queueSenderControl.setVoidCallable(1);
384         mockQueueSender.setDisableMessageTimestamp(true);
385         queueSenderControl.setVoidCallable(1);
386         mockQueueSender.getPriority();
387         queueSenderControl.setReturnValue(4);
388
389         mockQueueSender.close();
390         queueSenderControl.setVoidCallable(1);
391         mockQueueSession.close();
392         queueSessionControl.setVoidCallable(1);
393         mockQueueConnection.close();
394         queueConnectionControl.setVoidCallable(1);
395
396         queueSenderControl.replay();
397         queueSessionControl.replay();
398         queueConnectionControl.replay();
399
400         template.execute(new ProducerCallback() {
401             public Object JavaDoc doInJms(Session JavaDoc session, MessageProducer JavaDoc msgProducer) throws JMSException JavaDoc {
402                 boolean b = session.getTransacted();
403                 int i = msgProducer.getPriority();
404                 return null;
405             }
406         });
407
408         queueConnectionFactoryControl.verify();
409         queueConnectionControl.verify();
410         queueSessionControl.verify();
411     }
412
413     /**
414      * Test the setting of the JmsTemplate properties.
415      */

416     public void testBeanProperties() throws Exception JavaDoc {
417         JmsTemplate102 template = createTemplate();
418         template.setConnectionFactory(mockQueueConnectionFactory);
419
420         assertTrue("connection factory ok", template.getConnectionFactory() == mockQueueConnectionFactory);
421
422         JmsTemplate102 s102 = createTemplate();
423         try {
424             s102.afterPropertiesSet();
425             fail("IllegalArgumentException not thrown. ConnectionFactory should be set");
426         }
427         catch (IllegalArgumentException JavaDoc ex) {
428             assertEquals("Exception message not matching", "connectionFactory is required", ex.getMessage());
429         }
430
431         // The default is for the JmsTemplate102 to send to queues.
432
// Test to make sure exeception is thrown and has reasonable message.
433
s102 = createTemplate();
434         s102.setConnectionFactory(mockTopicConnectionFactory);
435         try {
436             s102.afterPropertiesSet();
437             fail("IllegalArgumentException not thrown. Mismatch of Destination and ConnectionFactory types.");
438         }
439         catch (IllegalArgumentException JavaDoc ex) {
440             // expected
441
}
442
443         s102 = createTemplate();
444         s102.setConnectionFactory(mockQueueConnectionFactory);
445         s102.setPubSubDomain(true);
446         try {
447             s102.afterPropertiesSet();
448             fail("IllegalArgumentException not thrown. Mismatch of Destination and ConnectionFactory types.");
449         }
450         catch (IllegalArgumentException JavaDoc ex) {
451             // expected
452
}
453     }
454
455     /**
456      * Test the method send(String destination, MessgaeCreator c) using
457      * a queue and default QOS values.
458      */

459     public void testSendStringQueue() throws Exception JavaDoc {
460         sendQueue(true, false, false, true);
461     }
462
463     /**
464      * Test the method send(String destination, MessageCreator c) when
465      * explicit QOS parameters are enabled, using a queue.
466      */

467     public void testSendStringQueueWithQOS() throws Exception JavaDoc {
468         sendQueue(false, false, false, false);
469     }
470
471     /**
472      * Test the method send(MessageCreator c) using default QOS values.
473      */

474     public void testSendDefaultDestinationQueue() throws Exception JavaDoc {
475         sendQueue(true, false, true, true);
476     }
477
478     /**
479      * Test the method send(MessageCreator c) using explicit QOS values.
480      */

481     public void testSendDefaultDestinationQueueWithQOS() throws Exception JavaDoc {
482         sendQueue(false, false, true, false);
483     }
484
485     /**
486      * Test the method send(String destination, MessageCreator c) using
487      * a topic and default QOS values.
488      */

489     public void testSendStringTopic() throws Exception JavaDoc {
490         sendTopic(true, false);
491     }
492
493     /**
494      * Test the method send(String destination, MessageCreator c) using explicit
495      * QOS values.
496      */

497     public void testSendStringTopicWithQOS() throws Exception JavaDoc {
498         sendTopic(false, false);
499     }
500
501     /**
502      * Test the method send(Destination queue, MessgaeCreator c) using
503      * a queue and default QOS values.
504      */

505     public void testSendQueue() throws Exception JavaDoc {
506         sendQueue(true, false, false, true);
507     }
508
509     /**
510      * Test the method send(Destination queue, MessageCreator c) sing explicit
511      * QOS values.
512      */

513     public void testSendQueueWithQOS() throws Exception JavaDoc {
514         sendQueue(false, false, false, false);
515     }
516
517     /**
518      * Test the method send(Destination queue, MessgaeCreator c) using
519      * a topic and default QOS values.
520      */

521     public void testSendTopic() throws Exception JavaDoc {
522         sendTopic(true, false);
523     }
524
525     /**
526      * Test the method send(Destination queue, MessageCreator c) using explicity
527      * QOS values.
528      */

529     public void testSendTopicWithQOS() throws Exception JavaDoc {
530         sendQueue(false, false, false, true);
531     }
532
533     /**
534      * Common method for testing a send method that uses the MessageCreator
535      * callback but with different QOS options.
536      */

537     private void sendQueue(
538             boolean ignoreQOS, boolean explicitQueue, boolean useDefaultDestination, boolean disableIdAndTimestamp)
539             throws Exception JavaDoc {
540
541         JmsTemplate102 template = createTemplate();
542         template.setConnectionFactory(mockQueueConnectionFactory);
543         template.afterPropertiesSet();
544
545         if (useDefaultDestination) {
546             template.setDefaultDestination(mockQueue);
547         }
548         if (disableIdAndTimestamp) {
549             template.setMessageIdEnabled(false);
550             template.setMessageTimestampEnabled(false);
551         }
552
553         MockControl queueSenderControl = MockControl.createControl(QueueSender JavaDoc.class);
554         QueueSender JavaDoc mockQueueSender = (QueueSender JavaDoc) queueSenderControl.getMock();
555
556         MockControl messageControl = MockControl.createControl(TextMessage JavaDoc.class);
557         TextMessage JavaDoc mockMessage = (TextMessage JavaDoc) messageControl.getMock();
558
559         if (disableIdAndTimestamp) {
560             mockQueueSender.setDisableMessageID(true);
561             queueSenderControl.setVoidCallable(1);
562             mockQueueSender.setDisableMessageTimestamp(true);
563             queueSenderControl.setVoidCallable(1);
564         }
565
566         mockQueueSession.createSender(this.mockQueue);
567         queueSessionControl.setReturnValue(mockQueueSender);
568         mockQueueSession.createTextMessage("just testing");
569         queueSessionControl.setReturnValue(mockMessage);
570
571         if (useTransactedTemplate()) {
572             mockQueueSession.commit();
573             queueSessionControl.setVoidCallable(1);
574         }
575
576         if (ignoreQOS) {
577             mockQueueSender.send(mockMessage);
578         }
579         else {
580             template.setExplicitQosEnabled(true);
581             template.setDeliveryMode(deliveryMode);
582             template.setPriority(priority);
583             template.setTimeToLive(timeToLive);
584             mockQueueSender.send(mockMessage, deliveryMode, priority, timeToLive);
585         }
586         queueSenderControl.setVoidCallable(1);
587
588         mockQueueSender.close();
589         queueSenderControl.setVoidCallable(1);
590         mockQueueSession.close();
591         queueSessionControl.setVoidCallable(1);
592         mockQueueConnection.close();
593         queueConnectionControl.setVoidCallable(1);
594
595         queueSenderControl.replay();
596         queueSessionControl.replay();
597         queueConnectionControl.replay();
598
599         if (useDefaultDestination) {
600             template.send(new MessageCreator() {
601                 public Message JavaDoc createMessage(Session JavaDoc session) throws JMSException JavaDoc {
602                     return session.createTextMessage("just testing");
603                 }
604             });
605         }
606         else {
607             if (explicitQueue) {
608                 template.send(mockQueue, new MessageCreator() {
609                     public Message JavaDoc createMessage(Session JavaDoc session) throws JMSException JavaDoc {
610                         return session.createTextMessage("just testing");
611                     }
612                 });
613             }
614             else {
615                 template.send("testQueue", new MessageCreator() {
616                     public Message JavaDoc createMessage(Session JavaDoc session)
617                         throws JMSException JavaDoc {
618                         return session.createTextMessage("just testing");
619                     }
620                 });
621             }
622         }
623
624         queueConnectionFactoryControl.verify();
625         queueConnectionControl.verify();
626         queueSessionControl.verify();
627         queueSenderControl.verify();
628     }
629
630     private void sendTopic(boolean ignoreQOS, boolean explicitTopic) throws Exception JavaDoc {
631         JmsTemplate102 template = createTemplate();
632         template.setPubSubDomain(true);
633         template.setConnectionFactory(mockTopicConnectionFactory);
634         template.afterPropertiesSet();
635
636         MockControl topicPublisherControl = MockControl.createControl(TopicPublisher JavaDoc.class);
637         TopicPublisher JavaDoc mockTopicPublisher = (TopicPublisher JavaDoc) topicPublisherControl.getMock();
638
639         MockControl messageControl = MockControl.createControl(TextMessage JavaDoc.class);
640         TextMessage JavaDoc mockMessage = (TextMessage JavaDoc) messageControl.getMock();
641
642         mockTopicSession.createPublisher(this.mockTopic);
643         topicSessionControl.setReturnValue(mockTopicPublisher);
644         mockTopicSession.createTextMessage("just testing");
645         topicSessionControl.setReturnValue(mockMessage);
646
647         if (useTransactedTemplate()) {
648             mockTopicSession.commit();
649             topicSessionControl.setVoidCallable(1);
650         }
651
652         mockTopicPublisher.close();
653         topicPublisherControl.setVoidCallable(1);
654         mockTopicSession.close();
655         topicSessionControl.setVoidCallable(1);
656         mockTopicConnection.close();
657         topicConnectionControl.setVoidCallable(1);
658
659
660         topicSessionControl.replay();
661         topicConnectionControl.replay();
662
663         if (ignoreQOS) {
664             mockTopicPublisher.publish(mockMessage);
665         }
666         else {
667             template.setExplicitQosEnabled(true);
668             template.setDeliveryMode(deliveryMode);
669             template.setPriority(priority);
670             template.setTimeToLive(timeToLive);
671             mockTopicPublisher.publish(mockMessage, deliveryMode, priority, timeToLive);
672         }
673         topicPublisherControl.replay();
674
675         template.setPubSubDomain(true);
676
677         if (explicitTopic) {
678             template.send(mockTopic, new MessageCreator() {
679                 public Message JavaDoc createMessage(Session JavaDoc session)
680                     throws JMSException JavaDoc {
681                     return session.createTextMessage("just testing");
682                 }
683             });
684         }
685         else {
686             template.send("testTopic", new MessageCreator() {
687                 public Message JavaDoc createMessage(Session JavaDoc session)
688                     throws JMSException JavaDoc {
689                     return session.createTextMessage("just testing");
690                 }
691             });
692         }
693
694         topicConnectionFactoryControl.verify();
695         topicConnectionControl.verify();
696         topicSessionControl.verify();
697         topicPublisherControl.verify();
698     }
699
700     public void testConverter() throws Exception JavaDoc {
701         JmsTemplate102 template = createTemplate();
702         template.setConnectionFactory(mockQueueConnectionFactory);
703         template.setMessageConverter(new SimpleMessageConverter());
704         String JavaDoc s = "Hello world";
705
706         MockControl queueSenderControl = MockControl.createControl(QueueSender JavaDoc.class);
707         QueueSender JavaDoc mockQueueSender = (QueueSender JavaDoc) queueSenderControl.getMock();
708
709         MockControl messageControl = MockControl.createControl(TextMessage JavaDoc.class);
710         TextMessage JavaDoc mockMessage = (TextMessage JavaDoc) messageControl.getMock();
711
712         mockQueueSession.createSender(this.mockQueue);
713         queueSessionControl.setReturnValue(mockQueueSender);
714         mockQueueSession.createTextMessage("Hello world");
715         queueSessionControl.setReturnValue(mockMessage);
716
717         if (useTransactedTemplate()) {
718             mockQueueSession.commit();
719             queueSessionControl.setVoidCallable(1);
720         }
721
722         mockQueueSender.send(mockMessage);
723         queueSenderControl.setVoidCallable(1);
724
725         mockQueueSender.close();
726         queueSenderControl.setVoidCallable(1);
727         mockQueueSession.close();
728         queueSessionControl.setVoidCallable(1);
729         mockQueueConnection.close();
730         queueConnectionControl.setVoidCallable(1);
731
732         queueSenderControl.replay();
733         queueSessionControl.replay();
734         queueConnectionControl.replay();
735
736         template.convertAndSend(mockQueue, s);
737
738         queueConnectionFactoryControl.verify();
739         queueConnectionControl.verify();
740         queueSessionControl.verify();
741         queueSenderControl.verify();
742     }
743
744     public void testQueueReceiveDefaultDestination() throws Exception JavaDoc {
745         doTestReceive(false, false, true, false, false, false, false, false);
746     }
747
748     public void testQueueReceiveDestination() throws Exception JavaDoc {
749         doTestReceive(false, true, false, false, false, false, true, false);
750     }
751
752     public void testQueueReceiveDestinationWithClientAcknowledge() throws Exception JavaDoc {
753         doTestReceive(false, true, false, false, true, false, false, true);
754     }
755
756     public void testQueueReceiveStringDestination() throws Exception JavaDoc {
757         doTestReceive(false, false, false, false, false, false, true, true);
758     }
759
760     public void testQueueReceiveDefaultDestinationWithSelector() throws Exception JavaDoc {
761         doTestReceive(false, false, true, false, false, true, true, true);
762     }
763
764     public void testQueueReceiveDestinationWithSelector() throws Exception JavaDoc {
765         doTestReceive(false, true, false, false, false, true, false, true);
766     }
767
768     public void testQueueReceiveDestinationWithClientAcknowledgeWithSelector() throws Exception JavaDoc {
769         doTestReceive(false, true, false, false, true, true, true, false);
770     }
771
772     public void testQueueReceiveStringDestinationWithSelector() throws Exception JavaDoc {
773         doTestReceive(false, false, false, false, false, true, false, false);
774     }
775
776     public void testQueueReceiveAndConvertDefaultDestination() throws Exception JavaDoc {
777         doTestReceive(false, false, true, true, false, false, false, true);
778     }
779
780     public void testQueueReceiveAndConvertStringDestination() throws Exception JavaDoc {
781         doTestReceive(false, false, false, true, false, false, true, false);
782     }
783
784     public void testQueueReceiveAndConvertDestination() throws Exception JavaDoc {
785         doTestReceive(false, true, false, true, false, false, true, true);
786     }
787
788     public void testQueueReceiveAndConvertDefaultDestinationWithSelector() throws Exception JavaDoc {
789         doTestReceive(false, false, true, true, false, true, true, true);
790     }
791
792     public void testQueueReceiveAndConvertStringDestinationWithSelector() throws Exception JavaDoc {
793         doTestReceive(false, false, false, true, false, true, true, false);
794     }
795
796     public void testQueueReceiveAndConvertDestinationWithSelector() throws Exception JavaDoc {
797         doTestReceive(false, true, false, true, false, true, false, true);
798     }
799
800     public void testTopicReceiveDefaultDestination() throws Exception JavaDoc {
801         doTestReceive(true, false, true, false, false, false, false, false);
802     }
803
804     public void testTopicReceiveDestination() throws Exception JavaDoc {
805         doTestReceive(true, true, false, false, false, false, true, false);
806     }
807
808     public void testTopicReceiveDestinationWithClientAcknowledge() throws Exception JavaDoc {
809         doTestReceive(true, true, false, false, true, false, false, true);
810     }
811
812     public void testTopicReceiveStringDestination() throws Exception JavaDoc {
813         doTestReceive(true, false, false, false, false, false, true, true);
814     }
815
816     public void testTopicReceiveDefaultDestinationWithSelector() throws Exception JavaDoc {
817         doTestReceive(true, false, true, false, false, true, true, true);
818     }
819
820     public void testTopicReceiveDestinationWithSelector() throws Exception JavaDoc {
821         doTestReceive(true, true, false, false, false, true, false, true);
822     }
823
824     public void testTopicReceiveDestinationWithClientAcknowledgeWithSelector() throws Exception JavaDoc {
825         doTestReceive(true, true, false, false, true, true, true, false);
826     }
827
828     public void testTopicReceiveStringDestinationWithSelector() throws Exception JavaDoc {
829         doTestReceive(true, false, false, false, false, true, false, false);
830     }
831
832     public void testTopicReceiveAndConvertDefaultDestination() throws Exception JavaDoc {
833         doTestReceive(true, false, true, true, false, false, false, true);
834     }
835
836     public void testTopicReceiveAndConvertStringDestination() throws Exception JavaDoc {
837         doTestReceive(true, false, false, true, false, false, true, false);
838     }
839
840     public void testTopicReceiveAndConvertDestination() throws Exception JavaDoc {
841         doTestReceive(true, true, false, true, false, false, true, true);
842     }
843
844     public void testTopicReceiveAndConvertDefaultDestinationWithSelector() throws Exception JavaDoc {
845         doTestReceive(true, false, true, true, false, true, true, true);
846     }
847
848     public void testTopicReceiveAndConvertStringDestinationWithSelector() throws Exception JavaDoc {
849         doTestReceive(true, false, false, true, false, true, true, false);
850     }
851
852     public void testTopicReceiveAndConvertDestinationWithSelector() throws Exception JavaDoc {
853         doTestReceive(true, true, false, true, false, true, false, true);
854     }
855
856     private void doTestReceive(
857             boolean pubSub,
858             boolean explicitDestination, boolean useDefaultDestination, boolean testConverter,
859             boolean clientAcknowledge, boolean messageSelector, boolean noLocal, boolean timeout)
860             throws Exception JavaDoc {
861
862         JmsTemplate102 template = createTemplate();
863         template.setPubSubDomain(pubSub);
864         if (pubSub) {
865             template.setConnectionFactory(mockTopicConnectionFactory);
866         }
867         else {
868             template.setConnectionFactory(mockQueueConnectionFactory);
869         }
870
871         // Override the default settings for client ack used in the test setup.
872
// Can't use Session.getAcknowledgeMode()
873
if (pubSub) {
874             topicConnectionControl.reset();
875             if (clientAcknowledge) {
876                 template.setSessionAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE);
877                 mockTopicConnection.createTopicSession(useTransactedTemplate(), Session.CLIENT_ACKNOWLEDGE);
878             }
879             else {
880                 template.setSessionAcknowledgeMode(Session.AUTO_ACKNOWLEDGE);
881                 mockTopicConnection.createTopicSession(useTransactedTemplate(), Session.AUTO_ACKNOWLEDGE);
882             }
883             topicConnectionControl.setReturnValue(mockTopicSession);
884         }
885         else {
886             queueConnectionControl.reset();
887             if (clientAcknowledge) {
888                 template.setSessionAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE);
889                 mockQueueConnection.createQueueSession(useTransactedTemplate(), Session.CLIENT_ACKNOWLEDGE);
890             }
891             else {
892                 template.setSessionAcknowledgeMode(Session.AUTO_ACKNOWLEDGE);
893                 mockQueueConnection.createQueueSession(useTransactedTemplate(), Session.AUTO_ACKNOWLEDGE);
894             }
895             queueConnectionControl.setReturnValue(mockQueueSession);
896         }
897
898         Destination JavaDoc dest = pubSub ? (Destination JavaDoc) mockTopic : (Destination JavaDoc) mockQueue;
899
900         if (useDefaultDestination) {
901             template.setDefaultDestination(dest);
902         }
903         if (noLocal) {
904             template.setPubSubNoLocal(true);
905         }
906         if (timeout) {
907             template.setReceiveTimeout(1000);
908         }
909
910         if (pubSub) {
911             mockTopicConnection.start();
912             topicConnectionControl.setVoidCallable(1);
913             mockTopicConnection.close();
914             topicConnectionControl.setVoidCallable(1);
915         }
916         else {
917             mockQueueConnection.start();
918             queueConnectionControl.setVoidCallable(1);
919             mockQueueConnection.close();
920             queueConnectionControl.setVoidCallable(1);
921         }
922
923         String JavaDoc selectorString = "selector";
924         MockControl messageConsumerControl = null;
925         MessageConsumer JavaDoc mockMessageConsumer = null;
926
927         if (pubSub) {
928             messageConsumerControl = MockControl.createControl(TopicSubscriber JavaDoc.class);
929             TopicSubscriber JavaDoc mockTopicSubscriber = (TopicSubscriber JavaDoc) messageConsumerControl.getMock();
930             mockMessageConsumer = mockTopicSubscriber;
931             if (messageSelector) {
932                 mockTopicSession.createSubscriber(mockTopic, selectorString, noLocal);
933             }
934             else {
935                 if (noLocal) {
936                     mockTopicSession.createSubscriber(mockTopic, null, true);
937                 }
938                 else {
939                     mockTopicSession.createSubscriber(mockTopic);
940                 }
941             }
942             topicSessionControl.setReturnValue(mockTopicSubscriber);
943         }
944         else {
945             messageConsumerControl = MockControl.createControl(QueueReceiver JavaDoc.class);
946             QueueReceiver JavaDoc mockQueueReceiver = (QueueReceiver JavaDoc) messageConsumerControl.getMock();
947             mockMessageConsumer = mockQueueReceiver;
948             if (messageSelector) {
949                 mockQueueSession.createReceiver(mockQueue, selectorString);
950             }
951             else {
952                 mockQueueSession.createReceiver(mockQueue);
953             }
954             queueSessionControl.setReturnValue(mockQueueReceiver);
955         }
956
957         if (useTransactedTemplate()) {
958             if (pubSub) {
959                 mockTopicSession.commit();
960                 topicSessionControl.setVoidCallable(1);
961             }
962             else {
963                 mockQueueSession.commit();
964                 queueSessionControl.setVoidCallable(1);
965             }
966         }
967
968         if (pubSub) {
969             mockTopicSession.close();
970             topicSessionControl.setVoidCallable(1);
971         }
972         else {
973             mockQueueSession.close();
974             queueSessionControl.setVoidCallable(1);
975         }
976
977         MockControl messageControl = MockControl.createControl(TextMessage JavaDoc.class);
978         TextMessage JavaDoc mockMessage = (TextMessage JavaDoc) messageControl.getMock();
979
980         if (testConverter) {
981             mockMessage.getText();
982             messageControl.setReturnValue("Hello World!");
983         }
984         if (!useTransactedSession() && clientAcknowledge) {
985             mockMessage.acknowledge();
986             messageControl.setVoidCallable(1);
987         }
988
989         if (pubSub) {
990             topicSessionControl.replay();
991             topicConnectionControl.replay();
992         }
993         else {
994             queueSessionControl.replay();
995             queueConnectionControl.replay();
996         }
997         messageControl.replay();
998
999         if (timeout) {
1000            mockMessageConsumer.receive(1000);
1001        }
1002        else {
1003            mockMessageConsumer.receive();
1004        }
1005        messageConsumerControl.setReturnValue(mockMessage);
1006        mockMessageConsumer.close();
1007        messageConsumerControl.setVoidCallable(1);
1008
1009        messageConsumerControl.replay();
1010
1011        Message JavaDoc message = null;
1012        String JavaDoc textFromMessage = null;
1013
1014        if (useDefaultDestination) {
1015            if (testConverter) {
1016                textFromMessage = (String JavaDoc)
1017                        (messageSelector ? template.receiveSelectedAndConvert(selectorString) :
1018                        template.receiveAndConvert());
1019            }
1020            else {
1021                message = (messageSelector ? template.receiveSelected(selectorString) : template.receive());
1022            }
1023        }
1024        else if (explicitDestination) {
1025            if (testConverter) {
1026                textFromMessage = (String JavaDoc)
1027                        (messageSelector ? template.receiveSelectedAndConvert(dest, selectorString) :
1028                        template.receiveAndConvert(dest));
1029            }
1030            else {
1031                message = (messageSelector ? template.receiveSelected(dest, selectorString) :
1032                        template.receive(dest));
1033            }
1034        }
1035        else {
1036            String JavaDoc destinationName = (pubSub ? "testTopic" : "testQueue");
1037            if (testConverter) {
1038                textFromMessage = (String JavaDoc)
1039                        (messageSelector ? template.receiveSelectedAndConvert(destinationName, selectorString) :
1040                        template.receiveAndConvert(destinationName));
1041            }
1042            else {
1043                message = (messageSelector ? template.receiveSelected(destinationName, selectorString) :
1044                        template.receive(destinationName));
1045            }
1046        }
1047
1048        if (pubSub) {
1049            topicConnectionFactoryControl.verify();
1050            topicConnectionControl.verify();
1051            topicSessionControl.verify();
1052        }
1053        else {
1054            queueConnectionFactoryControl.verify();
1055            queueConnectionControl.verify();
1056            queueSessionControl.verify();
1057        }
1058        messageConsumerControl.verify();
1059        messageControl.verify();
1060
1061        if (testConverter) {
1062            assertEquals("Message text should be equal", "Hello World!", textFromMessage);
1063        }
1064        else {
1065            assertEquals("Messages should refer to the same object", message, mockMessage);
1066        }
1067    }
1068
1069}
1070
Popular Tags