|
35 | 35 | import jakarta.jms.TextMessage; |
36 | 36 | import org.junit.jupiter.api.BeforeEach; |
37 | 37 | import org.junit.jupiter.api.Test; |
| 38 | +import org.mockito.ArgumentCaptor; |
38 | 39 |
|
39 | 40 | import org.springframework.jms.InvalidClientIDException; |
40 | 41 | import org.springframework.jms.InvalidDestinationException; |
|
62 | 63 |
|
63 | 64 | import static org.assertj.core.api.Assertions.assertThat; |
64 | 65 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
| 66 | +import static org.mockito.ArgumentMatchers.anyString; |
| 67 | +import static org.mockito.ArgumentMatchers.eq; |
65 | 68 | import static org.mockito.BDDMockito.given; |
66 | 69 | import static org.mockito.BDDMockito.willThrow; |
67 | 70 | import static org.mockito.Mockito.mock; |
@@ -648,7 +651,7 @@ private void doTestSendAndReceive(boolean explicitDestination, boolean useDefaul |
648 | 651 | given(localSession.createTemporaryQueue()).willReturn(replyDestination); |
649 | 652 |
|
650 | 653 | MessageConsumer messageConsumer = mock(); |
651 | | - given(localSession.createConsumer(replyDestination)).willReturn(messageConsumer); |
| 654 | + given(localSession.createConsumer(replyDestination, null)).willReturn(messageConsumer); |
652 | 655 |
|
653 | 656 |
|
654 | 657 | TextMessage request = mock(); |
@@ -715,7 +718,8 @@ private void doTestSendAndReceiveWithResponseQueue(boolean explicitDestination, |
715 | 718 | given(localSession.createProducer(this.queue)).willReturn(messageProducer); |
716 | 719 |
|
717 | 720 | MessageConsumer messageConsumer = mock(); |
718 | | - given(localSession.createConsumer(responseQueue)).willReturn(messageConsumer); |
| 721 | + // Default sendAndReceive with responseQueue uses MESSAGE_ID selector |
| 722 | + given(localSession.createConsumer(eq(responseQueue), anyString())).willReturn(messageConsumer); |
719 | 723 |
|
720 | 724 | TextMessage request = mock(); |
721 | 725 | MessageCreator messageCreator = mock(); |
@@ -750,6 +754,113 @@ else if (timeout == JmsTemplate.RECEIVE_TIMEOUT_INDEFINITE_WAIT) { |
750 | 754 | verify(messageProducer).close(); |
751 | 755 | } |
752 | 756 |
|
| 757 | + @Test |
| 758 | + void testSendAndReceiveDestinationWithResponseQueueAndCorrelationIdSelector() throws Exception { |
| 759 | + doTestSendAndReceiveWithResponseQueueAndSelectorType(true, 1000L, JmsOperations.SelectorType.CORRELATION_ID); |
| 760 | + } |
| 761 | + |
| 762 | + @Test |
| 763 | + void testSendAndReceiveDestinationNameWithResponseQueueNameAndCorrelationIdSelector() throws Exception { |
| 764 | + doTestSendAndReceiveWithResponseQueueAndSelectorType(false, 1000L, JmsOperations.SelectorType.CORRELATION_ID); |
| 765 | + } |
| 766 | + |
| 767 | + @Test |
| 768 | + void testSendAndReceiveDestinationWithResponseQueueAndMessageIdSelector() throws Exception { |
| 769 | + doTestSendAndReceiveWithResponseQueueAndSelectorType(true, 1000L, JmsOperations.SelectorType.MESSAGE_ID); |
| 770 | + } |
| 771 | + |
| 772 | + @Test |
| 773 | + void testSendAndReceiveDestinationNameWithResponseQueueNameAndMessageIdSelector() throws Exception { |
| 774 | + doTestSendAndReceiveWithResponseQueueAndSelectorType(false, 1000L, JmsOperations.SelectorType.MESSAGE_ID); |
| 775 | + } |
| 776 | + |
| 777 | + @Test |
| 778 | + void testSendAndReceiveDestinationWithResponseQueueAndNoSelector() throws Exception { |
| 779 | + doTestSendAndReceiveWithResponseQueueAndSelectorType(true, 1000L, JmsOperations.SelectorType.NONE); |
| 780 | + } |
| 781 | + |
| 782 | + @Test |
| 783 | + void testSendAndReceiveDestinationNameWithResponseQueueNameAndNoSelector() throws Exception { |
| 784 | + doTestSendAndReceiveWithResponseQueueAndSelectorType(false, 1000L, JmsOperations.SelectorType.NONE); |
| 785 | + } |
| 786 | + |
| 787 | + private void doTestSendAndReceiveWithResponseQueueAndSelectorType(boolean explicitDestination, long timeout, |
| 788 | + JmsOperations.SelectorType selectorType) throws Exception { |
| 789 | + |
| 790 | + JmsTemplate template = createTemplate(); |
| 791 | + template.setConnectionFactory(this.connectionFactory); |
| 792 | + template.setReceiveTimeout(timeout); |
| 793 | + |
| 794 | + String destinationName = "testDestination"; |
| 795 | + String responseQueueName = "responseQueue"; |
| 796 | + |
| 797 | + Queue responseQueue = mock(); |
| 798 | + given(this.jndiContext.lookup(responseQueueName)).willReturn(responseQueue); |
| 799 | + |
| 800 | + Session localSession = getLocalSession(); |
| 801 | + MessageProducer messageProducer = mock(); |
| 802 | + given(localSession.createProducer(this.queue)).willReturn(messageProducer); |
| 803 | + |
| 804 | + MessageConsumer messageConsumer = mock(); |
| 805 | + if (selectorType == JmsOperations.SelectorType.NONE) { |
| 806 | + given(localSession.createConsumer(responseQueue, null)).willReturn(messageConsumer); |
| 807 | + } |
| 808 | + else { |
| 809 | + given(localSession.createConsumer(eq(responseQueue), anyString())) |
| 810 | + .willReturn(messageConsumer); |
| 811 | + } |
| 812 | + |
| 813 | + TextMessage request = mock(); |
| 814 | + MessageCreator messageCreator = mock(); |
| 815 | + given(messageCreator.createMessage(localSession)).willReturn(request); |
| 816 | + |
| 817 | + if (selectorType == JmsOperations.SelectorType.MESSAGE_ID) { |
| 818 | + given(request.getJMSMessageID()).willReturn("ID:test-message-id-12345"); |
| 819 | + } |
| 820 | + |
| 821 | + TextMessage reply = mock(); |
| 822 | + if (timeout == JmsTemplate.RECEIVE_TIMEOUT_NO_WAIT) { |
| 823 | + given(messageConsumer.receiveNoWait()).willReturn(reply); |
| 824 | + } |
| 825 | + else if (timeout == JmsTemplate.RECEIVE_TIMEOUT_INDEFINITE_WAIT) { |
| 826 | + given(messageConsumer.receive()).willReturn(reply); |
| 827 | + } |
| 828 | + else { |
| 829 | + given(messageConsumer.receive(timeout)).willReturn(reply); |
| 830 | + } |
| 831 | + |
| 832 | + Message message; |
| 833 | + if (explicitDestination) { |
| 834 | + message = template.sendAndReceive(this.queue, responseQueue, messageCreator, selectorType); |
| 835 | + } |
| 836 | + else { |
| 837 | + message = template.sendAndReceive(destinationName, responseQueueName, messageCreator, selectorType); |
| 838 | + } |
| 839 | + |
| 840 | + // replyTO set on the request |
| 841 | + verify(request).setJMSReplyTo(responseQueue); |
| 842 | + assertThat(message).as("Reply message not received").isSameAs(reply); |
| 843 | + verify(this.connection).start(); |
| 844 | + verify(this.connection).close(); |
| 845 | + verify(localSession).close(); |
| 846 | + verify(messageConsumer).close(); |
| 847 | + verify(messageProducer).close(); |
| 848 | + |
| 849 | + // Verify selector-specific behavior |
| 850 | + if (selectorType == JmsOperations.SelectorType.CORRELATION_ID) { |
| 851 | + ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); |
| 852 | + verify(request).setJMSCorrelationID(captor.capture()); |
| 853 | + verify(localSession).createConsumer(eq(responseQueue), eq("JMSCorrelationID = '" + captor.getValue() + "'")); |
| 854 | + } |
| 855 | + else if (selectorType == JmsOperations.SelectorType.MESSAGE_ID) { |
| 856 | + verify(request).getJMSMessageID(); |
| 857 | + verify(localSession).createConsumer(eq(responseQueue), eq("JMSCorrelationID = 'ID:test-message-id-12345'")); |
| 858 | + } |
| 859 | + else { |
| 860 | + verify(localSession).createConsumer(responseQueue, null); |
| 861 | + } |
| 862 | + } |
| 863 | + |
753 | 864 | @Test |
754 | 865 | void testIllegalStateException() throws Exception { |
755 | 866 | doTestJmsException(new jakarta.jms.IllegalStateException(""), org.springframework.jms.IllegalStateException.class); |
|
0 commit comments