use of jakarta.jms.TextMessage in project spring-framework by spring-projects.
the class MessageListenerAdapterTests method testWithMessageContentsDelegateForTextMessage.
@Test
void testWithMessageContentsDelegateForTextMessage() throws Exception {
TextMessage textMessage = mock(TextMessage.class);
// TextMessage contents must be unwrapped...
given(textMessage.getText()).willReturn(TEXT);
MessageContentsDelegate delegate = mock(MessageContentsDelegate.class);
MessageListenerAdapter adapter = new MessageListenerAdapter(delegate);
adapter.onMessage(textMessage);
verify(delegate).handleMessage(TEXT);
}
use of jakarta.jms.TextMessage in project spring-framework by spring-projects.
the class MessageListenerAdapterTests method testThatAnExceptionThrownFromTheHandlingMethodIsSimplySwallowedByDefault.
@Test
void testThatAnExceptionThrownFromTheHandlingMethodIsSimplySwallowedByDefault() throws Exception {
final IllegalArgumentException exception = new IllegalArgumentException();
TextMessage textMessage = mock(TextMessage.class);
MessageDelegate delegate = mock(MessageDelegate.class);
willThrow(exception).given(delegate).handleMessage(textMessage);
MessageListenerAdapter adapter = new MessageListenerAdapter(delegate) {
@Override
protected void handleListenerException(Throwable ex) {
assertThat(ex).as("The Throwable passed to the handleListenerException(..) method must never be null.").isNotNull();
boolean condition = ex instanceof ListenerExecutionFailedException;
assertThat(condition).as("The Throwable passed to the handleListenerException(..) method must be of type [ListenerExecutionFailedException].").isTrue();
ListenerExecutionFailedException lefx = (ListenerExecutionFailedException) ex;
Throwable cause = lefx.getCause();
assertThat(cause).as("The cause of a ListenerExecutionFailedException must be preserved.").isNotNull();
assertThat(cause).isSameAs(exception);
}
};
// we DON'T want the default SimpleMessageConversion happening...
adapter.setMessageConverter(null);
adapter.onMessage(textMessage);
}
use of jakarta.jms.TextMessage in project spring-framework by spring-projects.
the class MessageListenerAdapterTests method testWithResponsiveMessageDelegateNoDefaultDestination_SendsReturnTextMessageWhenSessionSupplied.
@Test
void testWithResponsiveMessageDelegateNoDefaultDestination_SendsReturnTextMessageWhenSessionSupplied() throws Exception {
Queue destination = mock(Queue.class);
TextMessage sentTextMessage = mock(TextMessage.class);
// correlation ID is queried when response is being created...
given(sentTextMessage.getJMSCorrelationID()).willReturn(null);
given(sentTextMessage.getJMSMessageID()).willReturn(CORRELATION_ID);
// Reply-To is queried when response is being created...
given(sentTextMessage.getJMSReplyTo()).willReturn(destination);
TextMessage responseTextMessage = mock(TextMessage.class);
MessageProducer messageProducer = mock(MessageProducer.class);
Session session = mock(Session.class);
given(session.createTextMessage(RESPONSE_TEXT)).willReturn(responseTextMessage);
given(session.createProducer(destination)).willReturn(messageProducer);
ResponsiveMessageDelegate delegate = mock(ResponsiveMessageDelegate.class);
given(delegate.handleMessage(sentTextMessage)).willReturn(RESPONSE_TEXT);
MessageListenerAdapter adapter = new MessageListenerAdapter(delegate) {
@Override
protected Object extractMessage(Message message) {
return message;
}
};
adapter.onMessage(sentTextMessage, session);
verify(responseTextMessage).setJMSCorrelationID(CORRELATION_ID);
verify(messageProducer).send(responseTextMessage);
verify(messageProducer).close();
verify(delegate).handleMessage(sentTextMessage);
}
use of jakarta.jms.TextMessage in project spring-framework by spring-projects.
the class MessageListenerAdapterTests method testWithResponsiveMessageDelegateWithDefaultDestination_SendsReturnTextMessageWhenSessionSupplied.
@Test
void testWithResponsiveMessageDelegateWithDefaultDestination_SendsReturnTextMessageWhenSessionSupplied() throws Exception {
Queue destination = mock(Queue.class);
TextMessage sentTextMessage = mock(TextMessage.class);
// correlation ID is queried when response is being created...
given(sentTextMessage.getJMSCorrelationID()).willReturn(CORRELATION_ID);
// Reply-To is queried when response is being created...
// we want to fall back to the default...
given(sentTextMessage.getJMSReplyTo()).willReturn(null);
TextMessage responseTextMessage = mock(TextMessage.class);
QueueSender queueSender = mock(QueueSender.class);
Session session = mock(Session.class);
given(session.createTextMessage(RESPONSE_TEXT)).willReturn(responseTextMessage);
given(session.createProducer(destination)).willReturn(queueSender);
ResponsiveMessageDelegate delegate = mock(ResponsiveMessageDelegate.class);
given(delegate.handleMessage(sentTextMessage)).willReturn(RESPONSE_TEXT);
MessageListenerAdapter adapter = new MessageListenerAdapter(delegate) {
@Override
protected Object extractMessage(Message message) {
return message;
}
};
adapter.setDefaultResponseDestination(destination);
adapter.onMessage(sentTextMessage, session);
verify(responseTextMessage).setJMSCorrelationID(CORRELATION_ID);
verify(queueSender).send(responseTextMessage);
verify(queueSender).close();
verify(delegate).handleMessage(sentTextMessage);
}
use of jakarta.jms.TextMessage in project spring-framework by spring-projects.
the class MessagingMessageListenerAdapterTests method replyPayloadNoDestination.
@Test
public void replyPayloadNoDestination() throws JMSException {
Queue replyDestination = mock(Queue.class);
Session session = mock(Session.class);
MessageProducer messageProducer = mock(MessageProducer.class);
TextMessage responseMessage = mock(TextMessage.class);
given(session.createTextMessage("Response")).willReturn(responseMessage);
given(session.createProducer(replyDestination)).willReturn(messageProducer);
MessagingMessageListenerAdapter listener = getPayloadInstance("Response", "replyPayloadNoDestination", Message.class);
listener.setDefaultResponseDestination(replyDestination);
listener.onMessage(mock(jakarta.jms.Message.class), session);
verify(session, times(0)).createQueue(anyString());
verify(session).createTextMessage("Response");
verify(messageProducer).send(responseMessage);
verify(messageProducer).close();
}
Aggregations