use of jakarta.jms.Message 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.Message 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.Message in project spring-framework by spring-projects.
the class SimpleMessageConverterTests method testStringConversion.
@Test
public void testStringConversion() throws JMSException {
Session session = mock(Session.class);
TextMessage message = mock(TextMessage.class);
String content = "test";
given(session.createTextMessage(content)).willReturn(message);
given(message.getText()).willReturn(content);
SimpleMessageConverter converter = new SimpleMessageConverter();
Message msg = converter.toMessage(content, session);
assertThat(converter.fromMessage(msg)).isEqualTo(content);
}
use of jakarta.jms.Message in project spring-framework by spring-projects.
the class SimpleMessageConverterTests method testByteArrayConversion.
@Test
public void testByteArrayConversion() throws JMSException {
Session session = mock(Session.class);
BytesMessage message = mock(BytesMessage.class);
byte[] content = "test".getBytes();
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(content);
given(session.createBytesMessage()).willReturn(message);
given(message.getBodyLength()).willReturn((long) content.length);
given(message.readBytes(any(byte[].class))).willAnswer(invocation -> byteArrayInputStream.read((byte[]) invocation.getArguments()[0]));
SimpleMessageConverter converter = new SimpleMessageConverter();
Message msg = converter.toMessage(content, session);
assertThat(((byte[]) converter.fromMessage(msg)).length).isEqualTo(content.length);
verify(message).writeBytes(content);
}
use of jakarta.jms.Message in project spring-framework by spring-projects.
the class JmsTransactionManagerTests method testTransactionCommitWithMessageProducer.
@Test
public void testTransactionCommitWithMessageProducer() throws JMSException {
Destination dest = new StubQueue();
ConnectionFactory cf = mock(ConnectionFactory.class);
Connection con = mock(Connection.class);
Session session = mock(Session.class);
MessageProducer producer = mock(MessageProducer.class);
final Message message = mock(Message.class);
given(cf.createConnection()).willReturn(con);
given(con.createSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(session);
given(session.createProducer(dest)).willReturn(producer);
given(session.getTransacted()).willReturn(true);
JmsTransactionManager tm = new JmsTransactionManager(cf);
TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
JmsTemplate jt = new JmsTemplate(cf);
jt.send(dest, sess -> message);
tm.commit(ts);
verify(producer).send(message);
verify(session).commit();
verify(producer).close();
verify(session).close();
verify(con).close();
}
Aggregations