KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.PrintWriter JavaDoc;
20 import java.io.StringWriter JavaDoc;
21
22 import javax.jms.Connection JavaDoc;
23 import javax.jms.ConnectionFactory JavaDoc;
24 import javax.jms.DeliveryMode JavaDoc;
25 import javax.jms.JMSException JavaDoc;
26 import javax.jms.Message JavaDoc;
27 import javax.jms.MessageConsumer JavaDoc;
28 import javax.jms.MessageProducer JavaDoc;
29 import javax.jms.Queue JavaDoc;
30 import javax.jms.Session JavaDoc;
31 import javax.jms.TextMessage JavaDoc;
32 import javax.naming.Context JavaDoc;
33 import javax.naming.NamingException JavaDoc;
34
35 import junit.framework.TestCase;
36 import org.easymock.MockControl;
37
38 import org.springframework.jms.JmsException;
39 import org.springframework.jms.support.JmsUtils;
40 import org.springframework.jms.support.converter.SimpleMessageConverter;
41 import org.springframework.jms.support.destination.JndiDestinationResolver;
42 import org.springframework.jndi.JndiTemplate;
43
44 /**
45  * Unit tests for the JmsTemplate implemented using JMS 1.1.
46  *
47  * @author Andre Biryukov
48  * @author Mark Pollack
49  */

50 public class JmsTemplateTests extends TestCase {
51
52     private Context JavaDoc mockJndiContext;
53     private MockControl mockJndiControl;
54
55     private MockControl connectionFactoryControl;
56     private ConnectionFactory JavaDoc mockConnectionFactory;
57
58     private MockControl connectionControl;
59     private Connection JavaDoc mockConnection;
60
61     private MockControl sessionControl;
62     private Session JavaDoc mockSession;
63
64     private MockControl queueControl;
65     private Queue JavaDoc mockQueue;
66
67     private int deliveryMode = DeliveryMode.PERSISTENT;
68     private int priority = 9;
69     private int timeToLive = 10000;
70
71
72     /**
73      * Create the mock objects for testing.
74      */

75     protected void setUp() throws Exception JavaDoc {
76         mockJndiControl = MockControl.createControl(Context JavaDoc.class);
77         mockJndiContext = (Context JavaDoc) this.mockJndiControl.getMock();
78
79         createMockforDestination();
80
81         mockJndiContext.close();
82         mockJndiControl.replay();
83     }
84
85     private void createMockforDestination() throws JMSException JavaDoc, NamingException JavaDoc {
86         connectionFactoryControl =
87                 MockControl.createControl(ConnectionFactory JavaDoc.class);
88         mockConnectionFactory =
89                 (ConnectionFactory JavaDoc) connectionFactoryControl.getMock();
90
91         connectionControl = MockControl.createControl(Connection JavaDoc.class);
92         mockConnection = (Connection JavaDoc) connectionControl.getMock();
93
94         sessionControl = MockControl.createControl(Session JavaDoc.class);
95         mockSession = (Session JavaDoc) sessionControl.getMock();
96
97         queueControl = MockControl.createControl(Queue JavaDoc.class);
98         mockQueue = (Queue JavaDoc) queueControl.getMock();
99
100         mockConnectionFactory.createConnection();
101         connectionFactoryControl.setReturnValue(mockConnection);
102         connectionFactoryControl.replay();
103
104         mockConnection.createSession(useTransactedTemplate(), Session.AUTO_ACKNOWLEDGE);
105         connectionControl.setReturnValue(mockSession);
106         mockSession.getTransacted();
107         sessionControl.setReturnValue(useTransactedSession());
108
109         mockJndiContext.lookup("testDestination");
110         mockJndiControl.setReturnValue(mockQueue);
111     }
112
113     private JmsTemplate createTemplate() {
114         JmsTemplate template = new JmsTemplate();
115         JndiDestinationResolver destMan = new JndiDestinationResolver();
116         destMan.setJndiTemplate(new JndiTemplate() {
117             protected Context JavaDoc createInitialContext() throws NamingException JavaDoc {
118                 return mockJndiContext;
119             }
120         });
121         template.setDestinationResolver(destMan);
122         template.setSessionTransacted(useTransactedTemplate());
123         return template;
124     }
125
126     protected boolean useTransactedSession() {
127         return false;
128     }
129
130     protected boolean useTransactedTemplate() {
131         return false;
132     }
133
134
135     public void testExceptionStackTrace() {
136         JMSException JavaDoc jmsEx = new JMSException JavaDoc("could not connect");
137         Exception JavaDoc innerEx = new Exception JavaDoc("host not found");
138         jmsEx.setLinkedException(innerEx);
139         JmsException springJmsEx = JmsUtils.convertJmsAccessException(jmsEx);
140         StringWriter JavaDoc sw = new StringWriter JavaDoc();
141         PrintWriter JavaDoc out = new PrintWriter JavaDoc(sw);
142         springJmsEx.printStackTrace(out);
143         String JavaDoc trace = sw.toString();
144         assertTrue("inner jms exception not found", trace.indexOf("host not found") > 0);
145     }
146
147     public void testProducerCallback() throws Exception JavaDoc {
148         JmsTemplate template = createTemplate();
149         template.setConnectionFactory(mockConnectionFactory);
150
151         MockControl messageProducerControl = MockControl.createControl(MessageProducer JavaDoc.class);
152         MessageProducer JavaDoc mockMessageProducer = (MessageProducer JavaDoc) messageProducerControl.getMock();
153
154         mockSession.createProducer(null);
155         sessionControl.setReturnValue(mockMessageProducer);
156
157         mockMessageProducer.getPriority();
158         messageProducerControl.setReturnValue(4);
159
160         mockMessageProducer.close();
161         messageProducerControl.setVoidCallable(1);
162         mockSession.close();
163         sessionControl.setVoidCallable(1);
164         mockConnection.close();
165         connectionControl.setVoidCallable(1);
166
167         messageProducerControl.replay();
168         sessionControl.replay();
169         connectionControl.replay();
170
171         template.execute(new ProducerCallback() {
172             public Object JavaDoc doInJms(Session JavaDoc session, MessageProducer JavaDoc msgProducer) throws JMSException JavaDoc {
173                 boolean b = session.getTransacted();
174                 int i = msgProducer.getPriority();
175                 return null;
176             }
177         });
178
179         connectionFactoryControl.verify();
180         connectionControl.verify();
181         sessionControl.verify();
182     }
183
184     public void testProducerCallbackWithIdAndTimestampDisabled() throws Exception JavaDoc {
185         JmsTemplate template = createTemplate();
186         template.setConnectionFactory(mockConnectionFactory);
187         template.setMessageIdEnabled(false);
188         template.setMessageTimestampEnabled(false);
189
190         MockControl messageProducerControl = MockControl.createControl(MessageProducer JavaDoc.class);
191         MessageProducer JavaDoc mockMessageProducer = (MessageProducer JavaDoc) messageProducerControl.getMock();
192
193         mockSession.createProducer(null);
194         sessionControl.setReturnValue(mockMessageProducer);
195
196         mockMessageProducer.setDisableMessageID(true);
197         messageProducerControl.setVoidCallable(1);
198         mockMessageProducer.setDisableMessageTimestamp(true);
199         messageProducerControl.setVoidCallable(1);
200         mockMessageProducer.getPriority();
201         messageProducerControl.setReturnValue(4);
202
203         mockMessageProducer.close();
204         messageProducerControl.setVoidCallable(1);
205         mockSession.close();
206         sessionControl.setVoidCallable(1);
207         mockConnection.close();
208         connectionControl.setVoidCallable(1);
209
210         messageProducerControl.replay();
211         sessionControl.replay();
212         connectionControl.replay();
213
214         template.execute(new ProducerCallback() {
215             public Object JavaDoc doInJms(Session JavaDoc session, MessageProducer JavaDoc msgProducer) throws JMSException JavaDoc {
216                 boolean b = session.getTransacted();
217                 int i = msgProducer.getPriority();
218                 return null;
219             }
220         });
221
222         connectionFactoryControl.verify();
223         connectionControl.verify();
224         sessionControl.verify();
225     }
226
227     /**
228      * Test the method execute(SessionCallback action).
229      */

230     public void testSessionCallback() throws Exception JavaDoc {
231         JmsTemplate template = createTemplate();
232         template.setConnectionFactory(mockConnectionFactory);
233
234         mockSession.close();
235         sessionControl.setVoidCallable(1);
236
237         mockConnection.close();
238         connectionControl.setVoidCallable(1);
239
240         sessionControl.replay();
241         connectionControl.replay();
242
243         template.execute(new SessionCallback() {
244             public Object JavaDoc doInJms(Session JavaDoc session) throws JMSException JavaDoc {
245                 boolean b = session.getTransacted();
246                 return null;
247             }
248         });
249
250         connectionFactoryControl.verify();
251         connectionControl.verify();
252         sessionControl.verify();
253     }
254
255     /**
256      * Test sending to a destination using the method
257      * send(Destination d, MessageCreator messageCreator)
258      */

259     public void testSendDestination() throws Exception JavaDoc {
260         doTestSendDestination(true, false, true, false);
261     }
262
263     /**
264      * Test seding to a destination using the method
265      * send(String d, MessageCreator messageCreator)
266      */

267     public void testSendDestinationName() throws Exception JavaDoc {
268         doTestSendDestination(false, false, true, false);
269     }
270
271     /**
272      * Test sending to a destination using the method
273      * send(Destination d, MessageCreator messageCreator) using QOS parameters.
274      */

275     public void testSendDestinationWithQOS() throws Exception JavaDoc {
276         doTestSendDestination(true, false, false, true);
277     }
278
279     /**
280      * Test sending to a destination using the method
281      * send(String d, MessageCreator messageCreator) using QOS parameters.
282      */

283     public void testSendDestinationNameWithQOS() throws Exception JavaDoc {
284         doTestSendDestination(false, false, false, true);
285     }
286
287     /**
288      * Test sending to the default destination.
289      */

290     public void testSendDefaultDestination() throws Exception JavaDoc {
291         doTestSendDestination(true, true, true, true);
292     }
293
294     /**
295      * Test sending to the default destination name.
296      */

297     public void testSendDefaultDestinationName() throws Exception JavaDoc {
298         doTestSendDestination(false, true, true, true);
299     }
300
301     /**
302      * Test sending to the default destination using explicit QOS parameters.
303      */

304     public void testSendDefaultDestinationWithQOS() throws Exception JavaDoc {
305         doTestSendDestination(true, true, false, false);
306     }
307
308     /**
309      * Test sending to the default destination name using explicit QOS parameters.
310      */

311     public void testSendDefaultDestinationNameWithQOS() throws Exception JavaDoc {
312         doTestSendDestination(false, true, false, false);
313     }
314
315     /**
316      * Common method for testing a send method that uses the MessageCreator
317      * callback but with different QOS options.
318      * @param ignoreQOS test using default QOS options.
319      */

320     private void doTestSendDestination(
321             boolean explicitDestination, boolean useDefaultDestination,
322             boolean ignoreQOS, boolean disableIdAndTimestamp) throws Exception JavaDoc {
323
324         JmsTemplate template = createTemplate();
325         template.setConnectionFactory(mockConnectionFactory);
326
327         String JavaDoc destinationName = "testDestination";
328
329         if (useDefaultDestination) {
330             if (explicitDestination) {
331                 template.setDefaultDestination(mockQueue);
332             }
333             else {
334                 template.setDefaultDestinationName(destinationName);
335             }
336         }
337         if (disableIdAndTimestamp) {
338             template.setMessageIdEnabled(false);
339             template.setMessageTimestampEnabled(false);
340         }
341
342         MockControl messageProducerControl = MockControl.createControl(MessageProducer JavaDoc.class);
343         MessageProducer JavaDoc mockMessageProducer = (MessageProducer JavaDoc) messageProducerControl.getMock();
344
345         MockControl messageControl = MockControl.createControl(TextMessage JavaDoc.class);
346         TextMessage JavaDoc mockMessage = (TextMessage JavaDoc) messageControl.getMock();
347
348         mockSession.createProducer(mockQueue);
349         sessionControl.setReturnValue(mockMessageProducer);
350         mockSession.createTextMessage("just testing");
351         sessionControl.setReturnValue(mockMessage);
352
353         if (useTransactedTemplate()) {
354             mockSession.commit();
355             sessionControl.setVoidCallable(1);
356         }
357
358         if (disableIdAndTimestamp) {
359             mockMessageProducer.setDisableMessageID(true);
360             messageProducerControl.setVoidCallable(1);
361             mockMessageProducer.setDisableMessageTimestamp(true);
362             messageProducerControl.setVoidCallable(1);
363         }
364
365         if (ignoreQOS) {
366             mockMessageProducer.send(mockMessage);
367         }
368         else {
369             template.setExplicitQosEnabled(true);
370             template.setDeliveryMode(deliveryMode);
371             template.setPriority(priority);
372             template.setTimeToLive(timeToLive);
373             mockMessageProducer.send(mockMessage, deliveryMode, priority, timeToLive);
374         }
375         messageProducerControl.setVoidCallable(1);
376
377         mockMessageProducer.close();
378         messageProducerControl.setVoidCallable(1);
379         mockSession.close();
380         sessionControl.setVoidCallable(1);
381         mockConnection.close();
382         connectionControl.setVoidCallable(1);
383
384         messageProducerControl.replay();
385         sessionControl.replay();
386         connectionControl.replay();
387
388         if (useDefaultDestination) {
389             template.send(new MessageCreator() {
390                 public Message JavaDoc createMessage(Session JavaDoc session) throws JMSException JavaDoc {
391                     return session.createTextMessage("just testing");
392                 }
393             });
394         }
395         else {
396             if (explicitDestination) {
397                 template.send(mockQueue, new MessageCreator() {
398                     public Message JavaDoc createMessage(Session JavaDoc session)
399                             throws JMSException JavaDoc {
400                         return session.createTextMessage("just testing");
401                     }
402                 });
403             }
404             else {
405                 template.send(destinationName, new MessageCreator() {
406                     public Message JavaDoc createMessage(Session JavaDoc session)
407                             throws JMSException JavaDoc {
408                         return session.createTextMessage("just testing");
409                     }
410                 });
411             }
412         }
413
414         connectionFactoryControl.verify();
415         connectionControl.verify();
416         sessionControl.verify();
417         messageProducerControl.verify();
418     }
419
420     public void testConverter() throws Exception JavaDoc {
421         JmsTemplate template = createTemplate();
422         template.setConnectionFactory(mockConnectionFactory);
423         template.setMessageConverter(new SimpleMessageConverter());
424         String JavaDoc s = "Hello world";
425
426         MockControl messageProducerControl = MockControl.createControl(MessageProducer JavaDoc.class);
427         MessageProducer JavaDoc mockMessageProducer = (MessageProducer JavaDoc) messageProducerControl.getMock();
428
429         MockControl messageControl = MockControl.createControl(TextMessage JavaDoc.class);
430         TextMessage JavaDoc mockMessage = (TextMessage JavaDoc) messageControl.getMock();
431
432         mockSession.createProducer(mockQueue);
433         sessionControl.setReturnValue(mockMessageProducer);
434         mockSession.createTextMessage("Hello world");
435         sessionControl.setReturnValue(mockMessage);
436
437         mockMessageProducer.send(mockMessage);
438         messageProducerControl.setVoidCallable(1);
439         mockMessageProducer.close();
440         messageProducerControl.setVoidCallable(1);
441
442         if (useTransactedTemplate()) {
443             mockSession.commit();
444             sessionControl.setVoidCallable(1);
445         }
446
447         mockSession.close();
448         sessionControl.setVoidCallable(1);
449         mockConnection.close();
450         connectionControl.setVoidCallable(1);
451
452         messageProducerControl.replay();
453         sessionControl.replay();
454         connectionControl.replay();
455
456         template.convertAndSend(mockQueue, s);
457
458         messageProducerControl.verify();
459         sessionControl.verify();
460         connectionControl.verify();
461         connectionFactoryControl.verify();
462     }
463
464     public void testReceiveDefaultDestination() throws Exception JavaDoc {
465         doTestReceive(true, true, false, false, false, false, false);
466     }
467
468     public void testReceiveDefaultDestinationName() throws Exception JavaDoc {
469         doTestReceive(false, true, false, false, false, false, false);
470     }
471
472     public void testReceiveDestination() throws Exception JavaDoc {
473         doTestReceive(true, false, false, false, false, true, false);
474     }
475
476     public void testReceiveDestinationWithClientAcknowledge() throws Exception JavaDoc {
477         doTestReceive(true, false, false, true, false, false, true);
478     }
479
480     public void testReceiveDestinationName() throws Exception JavaDoc {
481         doTestReceive(false, false, false, false, false, true, true);
482     }
483
484     public void testReceiveDefaultDestinationWithSelector() throws Exception JavaDoc {
485         doTestReceive(true, true, false, false, true, true, true);
486     }
487
488     public void testReceiveDefaultDestinationNameWithSelector() throws Exception JavaDoc {
489         doTestReceive(false, true, false, false, true, true, true);
490     }
491
492     public void testReceiveDestinationWithSelector() throws Exception JavaDoc {
493         doTestReceive(true, false, false, false, true, false, true);
494     }
495
496     public void testReceiveDestinationWithClientAcknowledgeWithSelector() throws Exception JavaDoc {
497         doTestReceive(true, false, false, true, true, true, false);
498     }
499
500     public void testReceiveDestinationNameWithSelector() throws Exception JavaDoc {
501         doTestReceive(false, false, false, false, true, false, false);
502     }
503
504     public void testReceiveAndConvertDefaultDestination() throws Exception JavaDoc {
505         doTestReceive(true, true, true, false, false, false, true);
506     }
507
508     public void testReceiveAndConvertDefaultDestinationName() throws Exception JavaDoc {
509         doTestReceive(false, true, true, false, false, false, true);
510     }
511
512     public void testReceiveAndConvertDestinationName() throws Exception JavaDoc {
513         doTestReceive(false, false, true, false, false, true, false);
514     }
515
516     public void testReceiveAndConvertDestination() throws Exception JavaDoc {
517         doTestReceive(true, false, true, false, false, true, true);
518     }
519
520     public void testReceiveAndConvertDefaultDestinationWithSelector() throws Exception JavaDoc {
521         doTestReceive(true, true, true, false, true, true, true);
522     }
523
524     public void testReceiveAndConvertDestinationNameWithSelector() throws Exception JavaDoc {
525         doTestReceive(false, false, true, false, true, true, false);
526     }
527
528     public void testReceiveAndConvertDestinationWithSelector() throws Exception JavaDoc {
529         doTestReceive(true, false, true, false, true, false, true);
530     }
531
532     private void doTestReceive(
533             boolean explicitDestination, boolean useDefaultDestination, boolean testConverter,
534             boolean clientAcknowledge, boolean messageSelector, boolean noLocal, boolean timeout)
535             throws Exception JavaDoc {
536
537         JmsTemplate template = createTemplate();
538         template.setConnectionFactory(mockConnectionFactory);
539
540         String JavaDoc destinationName = "testDestination";
541
542         if (useDefaultDestination) {
543             if (explicitDestination) {
544                 template.setDefaultDestination(mockQueue);
545             }
546             else {
547                 template.setDefaultDestinationName(destinationName);
548             }
549         }
550         if (noLocal) {
551             template.setPubSubNoLocal(true);
552         }
553         if (timeout) {
554             template.setReceiveTimeout(1000);
555         }
556
557         mockConnection.start();
558         connectionControl.setVoidCallable(1);
559         mockConnection.close();
560         connectionControl.setVoidCallable(1);
561
562         MockControl messageConsumerControl = MockControl.createControl(MessageConsumer JavaDoc.class);
563         MessageConsumer JavaDoc mockMessageConsumer = (MessageConsumer JavaDoc) messageConsumerControl.getMock();
564
565         String JavaDoc selectorString = "selector";
566         if (messageSelector) {
567             if (noLocal) {
568                 mockSession.createConsumer(mockQueue, selectorString, true);
569             }
570             else {
571                 mockSession.createConsumer(mockQueue, selectorString);
572             }
573         }
574         else {
575             if (noLocal) {
576                 mockSession.createConsumer(mockQueue, null, true);
577             }
578             else {
579                 mockSession.createConsumer(mockQueue);
580             }
581         }
582         sessionControl.setReturnValue(mockMessageConsumer);
583
584         if (useTransactedTemplate()) {
585             mockSession.commit();
586             sessionControl.setVoidCallable(1);
587         }
588         else if (!useTransactedSession()) {
589             mockSession.getAcknowledgeMode();
590             if (clientAcknowledge) {
591                 sessionControl.setReturnValue(Session.CLIENT_ACKNOWLEDGE, 1);
592             }
593             else {
594                 sessionControl.setReturnValue(Session.AUTO_ACKNOWLEDGE, 1);
595             }
596         }
597
598         mockSession.close();
599         sessionControl.setVoidCallable(1);
600
601         MockControl messageControl = MockControl.createControl(TextMessage JavaDoc.class);
602         TextMessage JavaDoc mockMessage = (TextMessage JavaDoc) messageControl.getMock();
603
604         if (testConverter) {
605             mockMessage.getText();
606             messageControl.setReturnValue("Hello World!");
607         }
608         if (!useTransactedSession() && clientAcknowledge) {
609             mockMessage.acknowledge();
610             messageControl.setVoidCallable(1);
611         }
612
613         sessionControl.replay();
614         connectionControl.replay();
615         messageControl.replay();
616
617         if (timeout) {
618             mockMessageConsumer.receive(1000);
619         }
620         else {
621             mockMessageConsumer.receive();
622         }
623         messageConsumerControl.setReturnValue(mockMessage);
624         mockMessageConsumer.close();
625         messageConsumerControl.setVoidCallable(1);
626
627         messageConsumerControl.replay();
628
629         Message JavaDoc message = null;
630         String JavaDoc textFromMessage = null;
631
632         if (useDefaultDestination) {
633             if (testConverter) {
634                 textFromMessage = (String JavaDoc)
635                         (messageSelector ? template.receiveSelectedAndConvert(selectorString) :
636                         template.receiveAndConvert());
637             }
638             else {
639                 message = (messageSelector ? template.receiveSelected(selectorString) : template.receive());
640             }
641         }
642         else if (explicitDestination) {
643             if (testConverter) {
644                 textFromMessage = (String JavaDoc)
645                         (messageSelector ? template.receiveSelectedAndConvert(mockQueue, selectorString) :
646                         template.receiveAndConvert(mockQueue));
647             }
648             else {
649                 message = (messageSelector ? template.receiveSelected(mockQueue, selectorString) :
650                         template.receive(mockQueue));
651             }
652         }
653         else {
654             if (testConverter) {
655                 textFromMessage = (String JavaDoc)
656                         (messageSelector ? template.receiveSelectedAndConvert(destinationName, selectorString) :
657                         template.receiveAndConvert(destinationName));
658             }
659             else {
660                 message = (messageSelector ? template.receiveSelected(destinationName, selectorString) :
661                         template.receive(destinationName));
662             }
663         }
664
665         connectionFactoryControl.verify();
666         connectionControl.verify();
667         sessionControl.verify();
668         messageConsumerControl.verify();
669         messageControl.verify();
670
671         if (testConverter) {
672             assertEquals("Message text should be equal", "Hello World!", textFromMessage);
673         }
674         else {
675             assertEquals("Messages should refer to the same object", message, mockMessage);
676         }
677     }
678
679 }
680
Popular Tags