1 16 17 package org.springframework.jms.core; 18 19 import javax.jms.DeliveryMode ; 20 import javax.jms.Destination ; 21 import javax.jms.JMSException ; 22 import javax.jms.Message ; 23 import javax.jms.MessageConsumer ; 24 import javax.jms.MessageProducer ; 25 import javax.jms.Queue ; 26 import javax.jms.QueueConnection ; 27 import javax.jms.QueueConnectionFactory ; 28 import javax.jms.QueueReceiver ; 29 import javax.jms.QueueSender ; 30 import javax.jms.QueueSession ; 31 import javax.jms.Session ; 32 import javax.jms.TextMessage ; 33 import javax.jms.Topic ; 34 import javax.jms.TopicConnection ; 35 import javax.jms.TopicConnectionFactory ; 36 import javax.jms.TopicPublisher ; 37 import javax.jms.TopicSession ; 38 import javax.jms.TopicSubscriber ; 39 import javax.naming.Context ; 40 import javax.naming.NamingException ; 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 55 public class JmsTemplate102Tests extends TestCase { 56 57 private Context mockJndiContext; 58 private MockControl mockJndiControl; 59 60 private MockControl queueConnectionFactoryControl; 61 private QueueConnectionFactory mockQueueConnectionFactory; 62 63 private MockControl queueConnectionControl; 64 private QueueConnection mockQueueConnection; 65 66 private MockControl queueSessionControl; 67 private QueueSession mockQueueSession; 68 69 private MockControl queueControl; 70 private Queue mockQueue; 71 72 private MockControl topicConnectionFactoryControl; 73 private TopicConnectionFactory mockTopicConnectionFactory; 74 75 private MockControl topicConnectionControl; 76 private TopicConnection mockTopicConnection; 77 78 private MockControl topicSessionControl; 79 private TopicSession mockTopicSession; 80 81 private MockControl topicControl; 82 private Topic mockTopic; 83 84 private int deliveryMode = DeliveryMode.PERSISTENT; 85 private int priority = 9; 86 private int timeToLive = 10000; 87 88 89 92 protected void setUp() throws Exception { 93 mockJndiControl = MockControl.createControl(Context .class); 94 mockJndiContext = (Context ) this.mockJndiControl.getMock(); 95 96 createMockForQueues(); 97 createMockForTopics(); 98 99 mockJndiContext.close(); 100 mockJndiControl.replay(); 101 } 102 103 private void createMockForTopics() throws JMSException , NamingException { 104 topicConnectionFactoryControl = MockControl.createControl(TopicConnectionFactory .class); 105 mockTopicConnectionFactory = (TopicConnectionFactory ) topicConnectionFactoryControl.getMock(); 106 107 topicConnectionControl = MockControl.createControl(TopicConnection .class); 108 mockTopicConnection = (TopicConnection ) topicConnectionControl.getMock(); 109 110 topicControl = MockControl.createControl(Topic .class); 111 mockTopic = (Topic ) topicControl.getMock(); 112 113 topicSessionControl = MockControl.createControl(TopicSession .class); 114 mockTopicSession = (TopicSession ) 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 , NamingException { 130 queueConnectionFactoryControl = MockControl.createControl(QueueConnectionFactory .class); 131 mockQueueConnectionFactory = (QueueConnectionFactory ) queueConnectionFactoryControl.getMock(); 132 133 queueConnectionControl = MockControl.createControl(QueueConnection .class); 134 mockQueueConnection = (QueueConnection ) queueConnectionControl.getMock(); 135 136 queueControl = MockControl.createControl(Queue .class); 137 mockQueue = (Queue ) queueControl.getMock(); 138 139 queueSessionControl = MockControl.createControl(QueueSession .class); 140 mockQueueSession = (QueueSession ) 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 createInitialContext() throws NamingException { 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 { 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 doInJms(Session session) throws JMSException { 194 boolean b = session.getTransacted(); 195 return null; 196 } 197 }); 198 199 topicConnectionFactoryControl.verify(); 200 topicConnectionControl.verify(); 201 topicSessionControl.verify(); 202 } 203 204 207 public void testTopicProducerCallback() throws Exception { 208 JmsTemplate102 template = createTemplate(); 209 template.setPubSubDomain(true); 210 template.setConnectionFactory(mockTopicConnectionFactory); 211 template.afterPropertiesSet(); 212 213 MockControl topicPublisherControl = MockControl.createControl(TopicPublisher .class); 214 TopicPublisher mockTopicPublisher = (TopicPublisher ) 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 doInJms(Session session, MessageProducer msgProducer) throws JMSException { 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 249 public void testTopicProducerCallbackWithIdAndTimestampDisabled() throws Exception { 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 .class); 258 TopicPublisher mockTopicPublisher = (TopicPublisher ) 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 doInJms(Session session, MessageProducer msgProducer) throws JMSException { 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 298 public void testQueueSessionCallback() throws Exception { 299 JmsTemplate102 template = createTemplate(); 300 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 doInJms(Session session) throws JMSException { 315 boolean b = session.getTransacted(); 316 return null; 317 } 318 }); 319 320 queueConnectionFactoryControl.verify(); 321 queueConnectionControl.verify(); 322 queueSessionControl.verify(); 323 } 324 325 328 public void testQueueProducerCallback() throws Exception { 329 JmsTemplate102 template = createTemplate(); 330 template.setConnectionFactory(mockQueueConnectionFactory); 332 template.afterPropertiesSet(); 333 334 MockControl queueSenderControl = MockControl.createControl(QueueSender .class); 335 QueueSender mockQueueSender = (QueueSender ) 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 doInJms(Session session, MessageProducer msgProducer) 356 throws JMSException { 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 { 369 JmsTemplate102 template = createTemplate(); 370 template.setConnectionFactory(mockQueueConnectionFactory); 372 template.setMessageIdEnabled(false); 373 template.setMessageTimestampEnabled(false); 374 template.afterPropertiesSet(); 375 376 MockControl queueSenderControl = MockControl.createControl(QueueSender .class); 377 QueueSender mockQueueSender = (QueueSender ) 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 doInJms(Session session, MessageProducer msgProducer) throws JMSException { 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 416 public void testBeanProperties() throws Exception { 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 ex) { 428 assertEquals("Exception message not matching", "connectionFactory is required", ex.getMessage()); 429 } 430 431 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 ex) { 440 } 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 ex) { 451 } 453 } 454 455 459 public void testSendStringQueue() throws Exception { 460 sendQueue(true, false, false, true); 461 } 462 463 467 public void testSendStringQueueWithQOS() throws Exception { 468 sendQueue(false, false, false, false); 469 } 470 471 474 public void testSendDefaultDestinationQueue() throws Exception { 475 sendQueue(true, false, true, true); 476 } 477 478 481 public void testSendDefaultDestinationQueueWithQOS() throws Exception { 482 sendQueue(false, false, true, false); 483 } 484 485 489 public void testSendStringTopic() throws Exception { 490 sendTopic(true, false); 491 } 492 493 497 public void testSendStringTopicWithQOS() throws Exception { 498 sendTopic(false, false); 499 } 500 501 505 public void testSendQueue() throws Exception { 506 sendQueue(true, false, false, true); 507 } 508 509 513 public void testSendQueueWithQOS() throws Exception { 514 sendQueue(false, false, false, false); 515 } 516 517 521 public void testSendTopic() throws Exception { 522 sendTopic(true, false); 523 } 524 525 529 public void testSendTopicWithQOS() throws Exception { 530 sendQueue(false, false, false, true); 531 } 532 533 537 private void sendQueue( 538 boolean ignoreQOS, boolean explicitQueue, boolean useDefaultDestination, boolean disableIdAndTimestamp) 539 throws Exception { 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 .class); 554 QueueSender mockQueueSender = (QueueSender ) queueSenderControl.getMock(); 555 556 MockControl messageControl = MockControl.createControl(TextMessage .class); 557 TextMessage mockMessage = (TextMessage ) 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 createMessage(Session session) throws JMSException { 602 return session.createTextMessage("just testing"); 603 } 604 }); 605 } 606 else { 607 if (explicitQueue) { 608 template.send(mockQueue, new MessageCreator() { 609 public Message createMessage(Session session) throws JMSException { 610 return session.createTextMessage("just testing"); 611 } 612 }); 613 } 614 else { 615 template.send("testQueue", new MessageCreator() { 616 public Message createMessage(Session session) 617 throws JMSException { 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 { 631 JmsTemplate102 template = createTemplate(); 632 template.setPubSubDomain(true); 633 template.setConnectionFactory(mockTopicConnectionFactory); 634 template.afterPropertiesSet(); 635 636 MockControl topicPublisherControl = MockControl.createControl(TopicPublisher .class); 637 TopicPublisher mockTopicPublisher = (TopicPublisher ) topicPublisherControl.getMock(); 638 639 MockControl messageControl = MockControl.createControl(TextMessage .class); 640 TextMessage mockMessage = (TextMessage ) 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 createMessage(Session session) 680 throws JMSException { 681 return session.createTextMessage("just testing"); 682 } 683 }); 684 } 685 else { 686 template.send("testTopic", new MessageCreator() { 687 public Message createMessage(Session session) 688 throws JMSException { 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 { 701 JmsTemplate102 template = createTemplate(); 702 template.setConnectionFactory(mockQueueConnectionFactory); 703 template.setMessageConverter(new SimpleMessageConverter()); 704 String s = "Hello world"; 705 706 MockControl queueSenderControl = MockControl.createControl(QueueSender .class); 707 QueueSender mockQueueSender = (QueueSender ) queueSenderControl.getMock(); 708 709 MockControl messageControl = MockControl.createControl(TextMessage .class); 710 TextMessage mockMessage = (TextMessage ) 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 { 745 doTestReceive(false, false, true, false, false, false, false, false); 746 } 747 748 public void testQueueReceiveDestination() throws Exception { 749 doTestReceive(false, true, false, false, false, false, true, false); 750 } 751 752 public void testQueueReceiveDestinationWithClientAcknowledge() throws Exception { 753 doTestReceive(false, true, false, false, true, false, false, true); 754 } 755 756 public void testQueueReceiveStringDestination() throws Exception { 757 doTestReceive(false, false, false, false, false, false, true, true); 758 } 759 760 public void testQueueReceiveDefaultDestinationWithSelector() throws Exception { 761 doTestReceive(false, false, true, false, false, true, true, true); 762 } 763 764 public void testQueueReceiveDestinationWithSelector() throws Exception { 765 doTestReceive(false, true, false, false, false, true, false, true); 766 } 767 768 public void testQueueReceiveDestinationWithClientAcknowledgeWithSelector() throws Exception { 769 doTestReceive(false, true, false, false, true, true, true, false); 770 } 771 772 public void testQueueReceiveStringDestinationWithSelector() throws Exception { 773 doTestReceive(false, false, false, false, false, true, false, false); 774 } 775 776 public void testQueueReceiveAndConvertDefaultDestination() throws Exception { 777 doTestReceive(false, false, true, true, false, false, false, true); 778 } 779 780 public void testQueueReceiveAndConvertStringDestination() throws Exception { 781 doTestReceive(false, false, false, true, false, false, true, false); 782 } 783 784 public void testQueueReceiveAndConvertDestination() throws Exception { 785 doTestReceive(false, true, false, true, false, false, true, true); 786 } 787 788 public void testQueueReceiveAndConvertDefaultDestinationWithSelector() throws Exception { 789 doTestReceive(false, false, true, true, false, true, true, true); 790 } 791 792 public void testQueueReceiveAndConvertStringDestinationWithSelector() throws Exception { 793 doTestReceive(false, false, false, true, false, true, true, false); 794 } 795 796 public void testQueueReceiveAndConvertDestinationWithSelector() throws Exception { 797 doTestReceive(false, true, false, true, false, true, false, true); 798 } 799 800 public void testTopicReceiveDefaultDestination() throws Exception { 801 doTestReceive(true, false, true, false, false, false, false, false); 802 } 803 804 public void testTopicReceiveDestination() throws Exception { 805 doTestReceive(true, true, false, false, false, false, true, false); 806 } 807 808 public void testTopicReceiveDestinationWithClientAcknowledge() throws Exception { 809 doTestReceive(true, true, false, false, true, false, false, true); 810 } 811 812 public void testTopicReceiveStringDestination() throws Exception { 813 doTestReceive(true, false, false, false, false, false, true, true); 814 } 815 816 public void testTopicReceiveDefaultDestinationWithSelector() throws Exception { 817 doTestReceive(true, false, true, false, false, true, true, true); 818 } 819 820 public void testTopicReceiveDestinationWithSelector() throws Exception { 821 doTestReceive(true, true, false, false, false, true, false, true); 822 } 823 824 public void testTopicReceiveDestinationWithClientAcknowledgeWithSelector() throws Exception { 825 doTestReceive(true, true, false, false, true, true, true, false); 826 } 827 828 public void testTopicReceiveStringDestinationWithSelector() throws Exception { 829 doTestReceive(true, false, false, false, false, true, false, false); 830 } 831 832 public void testTopicReceiveAndConvertDefaultDestination() throws Exception { 833 doTestReceive(true, false, true, true, false, false, false, true); 834 } 835 836 public void testTopicReceiveAndConvertStringDestination() throws Exception { 837 doTestReceive(true, false, false, true, false, false, true, false); 838 } 839 840 public void testTopicReceiveAndConvertDestination() throws Exception { 841 doTestReceive(true, true, false, true, false, false, true, true); 842 } 843 844 public void testTopicReceiveAndConvertDefaultDestinationWithSelector() throws Exception { 845 doTestReceive(true, false, true, true, false, true, true, true); 846 } 847 848 public void testTopicReceiveAndConvertStringDestinationWithSelector() throws Exception { 849 doTestReceive(true, false, false, true, false, true, true, false); 850 } 851 852 public void testTopicReceiveAndConvertDestinationWithSelector() throws Exception { 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 { 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 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 dest = pubSub ? (Destination ) mockTopic : (Destination ) 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 selectorString = "selector"; 924 MockControl messageConsumerControl = null; 925 MessageConsumer mockMessageConsumer = null; 926 927 if (pubSub) { 928 messageConsumerControl = MockControl.createControl(TopicSubscriber .class); 929 TopicSubscriber mockTopicSubscriber = (TopicSubscriber ) 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 .class); 946 QueueReceiver mockQueueReceiver = (QueueReceiver ) 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 .class); 978 TextMessage mockMessage = (TextMessage ) 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 message = null; 1012 String textFromMessage = null; 1013 1014 if (useDefaultDestination) { 1015 if (testConverter) { 1016 textFromMessage = (String ) 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 ) 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 destinationName = (pubSub ? "testTopic" : "testQueue"); 1037 if (testConverter) { 1038 textFromMessage = (String ) 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 |