Search in sources :

Example 31 with TextMessage

use of jakarta.jms.TextMessage in project spring-framework by spring-projects.

the class JmsMessagingTemplateTests method assertTextMessage.

private void assertTextMessage(MessageCreator messageCreator) {
    try {
        TextMessage jmsMessage = createTextMessage(messageCreator);
        assertThat(jmsMessage.getText()).as("Wrong body message").isEqualTo("Hello");
        assertThat(jmsMessage.getStringProperty("foo")).as("Invalid foo property").isEqualTo("bar");
    } catch (JMSException e) {
        throw new IllegalStateException("Wrong text message", e);
    }
}
Also used : Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) JMSException(jakarta.jms.JMSException) StubTextMessage(org.springframework.jms.StubTextMessage) TextMessage(jakarta.jms.TextMessage)

Example 32 with TextMessage

use of jakarta.jms.TextMessage in project spring-framework by spring-projects.

the class JmsMessagingTemplateTests method convertAndSendPayload.

@Test
public void convertAndSendPayload() throws JMSException {
    Destination destination = new Destination() {
    };
    this.messagingTemplate.convertAndSend(destination, "my Payload");
    verify(this.jmsTemplate).send(eq(destination), this.messageCreator.capture());
    TextMessage textMessage = createTextMessage(this.messageCreator.getValue());
    assertThat(textMessage.getText()).isEqualTo("my Payload");
}
Also used : Destination(jakarta.jms.Destination) StubTextMessage(org.springframework.jms.StubTextMessage) TextMessage(jakarta.jms.TextMessage) Test(org.junit.jupiter.api.Test)

Example 33 with TextMessage

use of jakarta.jms.TextMessage in project spring-framework by spring-projects.

the class JmsNamespaceHandlerTests method testListeners.

@Test
public void testListeners() throws Exception {
    TestBean testBean1 = context.getBean("testBean1", TestBean.class);
    TestBean testBean2 = context.getBean("testBean2", TestBean.class);
    TestMessageListener testBean3 = context.getBean("testBean3", TestMessageListener.class);
    assertThat(testBean1.getName()).isNull();
    assertThat(testBean2.getName()).isNull();
    assertThat(testBean3.message).isNull();
    TextMessage message1 = mock(TextMessage.class);
    given(message1.getText()).willReturn("Test1");
    MessageListener listener1 = getListener("listener1");
    listener1.onMessage(message1);
    assertThat(testBean1.getName()).isEqualTo("Test1");
    TextMessage message2 = mock(TextMessage.class);
    given(message2.getText()).willReturn("Test2");
    MessageListener listener2 = getListener("listener2");
    listener2.onMessage(message2);
    assertThat(testBean2.getName()).isEqualTo("Test2");
    TextMessage message3 = mock(TextMessage.class);
    MessageListener listener3 = getListener(DefaultMessageListenerContainer.class.getName() + "#0");
    listener3.onMessage(message3);
    assertThat(testBean3.message).isSameAs(message3);
}
Also used : TestBean(org.springframework.beans.testfixture.beans.TestBean) MessageListener(jakarta.jms.MessageListener) TextMessage(jakarta.jms.TextMessage) Test(org.junit.jupiter.api.Test)

Example 34 with TextMessage

use of jakarta.jms.TextMessage in project spring-framework by spring-projects.

the class MessageListenerAdapterTests method testWithResponsiveMessageDelegateNoDefaultDestinationAndNoReplyToDestination_SendsReturnTextMessageWhenSessionSupplied.

@Test
void testWithResponsiveMessageDelegateNoDefaultDestinationAndNoReplyToDestination_SendsReturnTextMessageWhenSessionSupplied() throws Exception {
    final 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...
    given(sentTextMessage.getJMSReplyTo()).willReturn(null);
    TextMessage responseTextMessage = mock(TextMessage.class);
    final QueueSession session = mock(QueueSession.class);
    given(session.createTextMessage(RESPONSE_TEXT)).willReturn(responseTextMessage);
    ResponsiveMessageDelegate delegate = mock(ResponsiveMessageDelegate.class);
    given(delegate.handleMessage(sentTextMessage)).willReturn(RESPONSE_TEXT);
    final MessageListenerAdapter adapter = new MessageListenerAdapter(delegate) {

        @Override
        protected Object extractMessage(Message message) {
            return message;
        }
    };
    assertThatExceptionOfType(ReplyFailureException.class).isThrownBy(() -> adapter.onMessage(sentTextMessage, session)).withCauseExactlyInstanceOf(InvalidDestinationException.class);
    verify(responseTextMessage).setJMSCorrelationID(CORRELATION_ID);
    verify(delegate).handleMessage(sentTextMessage);
}
Also used : Message(jakarta.jms.Message) BytesMessage(jakarta.jms.BytesMessage) TextMessage(jakarta.jms.TextMessage) ObjectMessage(jakarta.jms.ObjectMessage) TextMessage(jakarta.jms.TextMessage) QueueSession(jakarta.jms.QueueSession) Test(org.junit.jupiter.api.Test)

Example 35 with TextMessage

use of jakarta.jms.TextMessage in project spring-framework by spring-projects.

the class MessageListenerAdapterTests method testWithResponsiveMessageDelegateNoDefaultDestination_SendsReturnTextMessageWhenSessionSupplied_AndSendingThrowsJMSException.

@Test
void testWithResponsiveMessageDelegateNoDefaultDestination_SendsReturnTextMessageWhenSessionSupplied_AndSendingThrowsJMSException() throws Exception {
    Queue destination = mock(Queue.class);
    final 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...
    given(sentTextMessage.getJMSReplyTo()).willReturn(destination);
    TextMessage responseTextMessage = mock(TextMessage.class);
    MessageProducer messageProducer = mock(MessageProducer.class);
    willThrow(new JMSException("Doe!")).given(messageProducer).send(responseTextMessage);
    final QueueSession session = mock(QueueSession.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);
    final MessageListenerAdapter adapter = new MessageListenerAdapter(delegate) {

        @Override
        protected Object extractMessage(Message message) {
            return message;
        }
    };
    assertThatExceptionOfType(ReplyFailureException.class).isThrownBy(() -> adapter.onMessage(sentTextMessage, session)).withCauseExactlyInstanceOf(JMSException.class);
    verify(responseTextMessage).setJMSCorrelationID(CORRELATION_ID);
    verify(messageProducer).close();
    verify(delegate).handleMessage(sentTextMessage);
}
Also used : Message(jakarta.jms.Message) BytesMessage(jakarta.jms.BytesMessage) TextMessage(jakarta.jms.TextMessage) ObjectMessage(jakarta.jms.ObjectMessage) JMSException(jakarta.jms.JMSException) MessageProducer(jakarta.jms.MessageProducer) Queue(jakarta.jms.Queue) TextMessage(jakarta.jms.TextMessage) QueueSession(jakarta.jms.QueueSession) Test(org.junit.jupiter.api.Test)

Aggregations

TextMessage (jakarta.jms.TextMessage)54 Test (org.junit.jupiter.api.Test)47 StubTextMessage (org.springframework.jms.StubTextMessage)22 Session (jakarta.jms.Session)20 MessageProducer (jakarta.jms.MessageProducer)13 Message (jakarta.jms.Message)12 Queue (jakarta.jms.Queue)10 ObjectMessage (jakarta.jms.ObjectMessage)9 Message (org.springframework.messaging.Message)9 BytesMessage (jakarta.jms.BytesMessage)8 Destination (jakarta.jms.Destination)7 QueueSession (jakarta.jms.QueueSession)7 SimpleMessageConverter (org.springframework.jms.support.converter.SimpleMessageConverter)5 JMSException (jakarta.jms.JMSException)4 QueueSender (jakarta.jms.QueueSender)4 MessageConsumer (jakarta.jms.MessageConsumer)3 HashMap (java.util.HashMap)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 QosSettings (org.springframework.jms.support.QosSettings)3 ConnectionFactory (jakarta.jms.ConnectionFactory)2