3636import org .junit .jupiter .api .BeforeEach ;
3737import org .junit .jupiter .api .Test ;
3838
39+ import org .mockito .ArgumentCaptor ;
3940import org .springframework .jms .InvalidClientIDException ;
4041import org .springframework .jms .InvalidDestinationException ;
4142import org .springframework .jms .InvalidSelectorException ;
6263
6364import static org .assertj .core .api .Assertions .assertThat ;
6465import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
66+ import static org .mockito .ArgumentMatchers .anyString ;
67+ import static org .mockito .ArgumentMatchers .eq ;
6568import static org .mockito .BDDMockito .given ;
6669import static org .mockito .BDDMockito .willThrow ;
6770import static org .mockito .Mockito .mock ;
@@ -648,7 +651,7 @@ private void doTestSendAndReceive(boolean explicitDestination, boolean useDefaul
648651 given (localSession .createTemporaryQueue ()).willReturn (replyDestination );
649652
650653 MessageConsumer messageConsumer = mock ();
651- given (localSession .createConsumer (replyDestination )).willReturn (messageConsumer );
654+ given (localSession .createConsumer (replyDestination , null )).willReturn (messageConsumer );
652655
653656
654657 TextMessage request = mock ();
@@ -715,7 +718,8 @@ private void doTestSendAndReceiveWithResponseQueue(boolean explicitDestination,
715718 given (localSession .createProducer (this .queue )).willReturn (messageProducer );
716719
717720 MessageConsumer messageConsumer = mock ();
718- given (localSession .createConsumer (responseQueue )).willReturn (messageConsumer );
721+ // Default sendAndReceive with responseQueue uses CORRELATION_ID selector
722+ given (localSession .createConsumer (eq (responseQueue ), anyString ())).willReturn (messageConsumer );
719723
720724 TextMessage request = mock ();
721725 MessageCreator messageCreator = mock ();
@@ -742,6 +746,8 @@ else if (timeout == JmsTemplate.RECEIVE_TIMEOUT_INDEFINITE_WAIT) {
742746
743747 // replyTO set on the request
744748 verify (request ).setJMSReplyTo (responseQueue );
749+ // Correlation ID set on request for selector
750+ verify (request ).setJMSCorrelationID (anyString ());
745751 assertThat (message ).as ("Reply message not received" ).isSameAs (reply );
746752 verify (this .connection ).start ();
747753 verify (this .connection ).close ();
@@ -750,6 +756,113 @@ else if (timeout == JmsTemplate.RECEIVE_TIMEOUT_INDEFINITE_WAIT) {
750756 verify (messageProducer ).close ();
751757 }
752758
759+ @ Test
760+ void testSendAndReceiveDestinationWithResponseQueueAndCorrelationIdSelector () throws Exception {
761+ doTestSendAndReceiveWithResponseQueueAndSelectorType (true , 1000L , JmsOperations .SelectorType .CORRELATION_ID );
762+ }
763+
764+ @ Test
765+ void testSendAndReceiveDestinationNameWithResponseQueueNameAndCorrelationIdSelector () throws Exception {
766+ doTestSendAndReceiveWithResponseQueueAndSelectorType (false , 1000L , JmsOperations .SelectorType .CORRELATION_ID );
767+ }
768+
769+ @ Test
770+ void testSendAndReceiveDestinationWithResponseQueueAndMessageIdSelector () throws Exception {
771+ doTestSendAndReceiveWithResponseQueueAndSelectorType (true , 1000L , JmsOperations .SelectorType .MESSAGE_ID );
772+ }
773+
774+ @ Test
775+ void testSendAndReceiveDestinationNameWithResponseQueueNameAndMessageIdSelector () throws Exception {
776+ doTestSendAndReceiveWithResponseQueueAndSelectorType (false , 1000L , JmsOperations .SelectorType .MESSAGE_ID );
777+ }
778+
779+ @ Test
780+ void testSendAndReceiveDestinationWithResponseQueueAndNoSelector () throws Exception {
781+ doTestSendAndReceiveWithResponseQueueAndSelectorType (true , 1000L , JmsOperations .SelectorType .NONE );
782+ }
783+
784+ @ Test
785+ void testSendAndReceiveDestinationNameWithResponseQueueNameAndNoSelector () throws Exception {
786+ doTestSendAndReceiveWithResponseQueueAndSelectorType (false , 1000L , JmsOperations .SelectorType .NONE );
787+ }
788+
789+ private void doTestSendAndReceiveWithResponseQueueAndSelectorType (boolean explicitDestination , long timeout ,
790+ JmsOperations .SelectorType selectorType ) throws Exception {
791+
792+ JmsTemplate template = createTemplate ();
793+ template .setConnectionFactory (this .connectionFactory );
794+ template .setReceiveTimeout (timeout );
795+
796+ String destinationName = "testDestination" ;
797+ String responseQueueName = "responseQueue" ;
798+
799+ Queue responseQueue = mock ();
800+ given (this .jndiContext .lookup (responseQueueName )).willReturn (responseQueue );
801+
802+ Session localSession = getLocalSession ();
803+ MessageProducer messageProducer = mock ();
804+ given (localSession .createProducer (this .queue )).willReturn (messageProducer );
805+
806+ MessageConsumer messageConsumer = mock ();
807+ if (selectorType == JmsOperations .SelectorType .NONE ) {
808+ given (localSession .createConsumer (responseQueue , null )).willReturn (messageConsumer );
809+ }
810+ else {
811+ given (localSession .createConsumer (eq (responseQueue ), anyString ()))
812+ .willReturn (messageConsumer );
813+ }
814+
815+ TextMessage request = mock ();
816+ MessageCreator messageCreator = mock ();
817+ given (messageCreator .createMessage (localSession )).willReturn (request );
818+
819+ if (selectorType == JmsOperations .SelectorType .MESSAGE_ID ) {
820+ given (request .getJMSMessageID ()).willReturn ("ID:test-message-id-12345" );
821+ }
822+
823+ TextMessage reply = mock ();
824+ if (timeout == JmsTemplate .RECEIVE_TIMEOUT_NO_WAIT ) {
825+ given (messageConsumer .receiveNoWait ()).willReturn (reply );
826+ }
827+ else if (timeout == JmsTemplate .RECEIVE_TIMEOUT_INDEFINITE_WAIT ) {
828+ given (messageConsumer .receive ()).willReturn (reply );
829+ }
830+ else {
831+ given (messageConsumer .receive (timeout )).willReturn (reply );
832+ }
833+
834+ Message message ;
835+ if (explicitDestination ) {
836+ message = template .sendAndReceive (this .queue , responseQueue , messageCreator , selectorType );
837+ }
838+ else {
839+ message = template .sendAndReceive (destinationName , responseQueueName , messageCreator , selectorType );
840+ }
841+
842+ // replyTO set on the request
843+ verify (request ).setJMSReplyTo (responseQueue );
844+ assertThat (message ).as ("Reply message not received" ).isSameAs (reply );
845+ verify (this .connection ).start ();
846+ verify (this .connection ).close ();
847+ verify (localSession ).close ();
848+ verify (messageConsumer ).close ();
849+ verify (messageProducer ).close ();
850+
851+ // Verify selector-specific behavior
852+ if (selectorType == JmsOperations .SelectorType .CORRELATION_ID ) {
853+ ArgumentCaptor <String > captor = ArgumentCaptor .forClass (String .class );
854+ verify (request ).setJMSCorrelationID (captor .capture ());
855+ verify (localSession ).createConsumer (eq (responseQueue ), eq ("JMSCorrelationID = '" + captor .getValue () + "'" ));
856+ }
857+ else if (selectorType == JmsOperations .SelectorType .MESSAGE_ID ) {
858+ verify (request ).getJMSMessageID ();
859+ verify (localSession ).createConsumer (eq (responseQueue ), eq ("JMSCorrelationID = 'ID:test-message-id-12345'" ));
860+ }
861+ else {
862+ verify (localSession ).createConsumer (responseQueue , null );
863+ }
864+ }
865+
753866 @ Test
754867 void testIllegalStateException () throws Exception {
755868 doTestJmsException (new jakarta .jms .IllegalStateException ("" ), org .springframework .jms .IllegalStateException .class );
0 commit comments