Search in sources :

Example 46 with TextMessage

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

the class JmsTemplateTests method testConverter.

@Test
void testConverter() throws Exception {
    JmsTemplate template = createTemplate();
    template.setConnectionFactory(this.connectionFactory);
    template.setMessageConverter(new SimpleMessageConverter());
    String s = "Hello world";
    MessageProducer messageProducer = mock(MessageProducer.class);
    TextMessage textMessage = mock(TextMessage.class);
    given(this.session.createProducer(this.queue)).willReturn(messageProducer);
    given(this.session.createTextMessage("Hello world")).willReturn(textMessage);
    template.convertAndSend(this.queue, s);
    verify(messageProducer).send(textMessage);
    verify(messageProducer).close();
    if (useTransactedTemplate()) {
        verify(this.session).commit();
    }
    verify(this.session).close();
    verify(this.connection).close();
}
Also used : SimpleMessageConverter(org.springframework.jms.support.converter.SimpleMessageConverter) MessageProducer(jakarta.jms.MessageProducer) TextMessage(jakarta.jms.TextMessage) Test(org.junit.jupiter.api.Test)

Example 47 with TextMessage

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

the class MessageListenerAdapterTests method testWithResponsiveMessageDelegateDoesNotSendReturnTextMessageWhenSessionSupplied_AndListenerMethodThrowsException.

@Test
void testWithResponsiveMessageDelegateDoesNotSendReturnTextMessageWhenSessionSupplied_AndListenerMethodThrowsException() throws Exception {
    final TextMessage message = mock(TextMessage.class);
    final QueueSession session = mock(QueueSession.class);
    ResponsiveMessageDelegate delegate = mock(ResponsiveMessageDelegate.class);
    willThrow(new IllegalArgumentException("Doe!")).given(delegate).handleMessage(message);
    final MessageListenerAdapter adapter = new MessageListenerAdapter(delegate) {

        @Override
        protected Object extractMessage(Message message) {
            return message;
        }
    };
    assertThatExceptionOfType(ListenerExecutionFailedException.class).isThrownBy(() -> adapter.onMessage(message, session));
}
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 48 with TextMessage

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

the class MessageListenerAdapterTests method testRainyDayWithNoApplicableHandlingMethods.

@Test
void testRainyDayWithNoApplicableHandlingMethods() throws Exception {
    TextMessage textMessage = mock(TextMessage.class);
    // TextMessage contents must be unwrapped...
    given(textMessage.getText()).willReturn(TEXT);
    StubMessageListenerAdapter adapter = new StubMessageListenerAdapter();
    adapter.setDefaultListenerMethod("walnutsRock");
    adapter.onMessage(textMessage);
    assertThat(adapter.wasCalled()).isFalse();
}
Also used : TextMessage(jakarta.jms.TextMessage) Test(org.junit.jupiter.api.Test)

Example 49 with TextMessage

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

the class MarshallingMessageConverterTests method fromTextMessage.

@Test
public void fromTextMessage() throws Exception {
    TextMessage textMessageMock = mock(TextMessage.class);
    Object unmarshalled = new Object();
    String text = "foo";
    given(textMessageMock.getText()).willReturn(text);
    given(unmarshallerMock.unmarshal(isA(Source.class))).willReturn(unmarshalled);
    Object result = converter.fromMessage(textMessageMock);
    assertThat(unmarshalled).as("Invalid result").isEqualTo(result);
}
Also used : TextMessage(jakarta.jms.TextMessage) Source(javax.xml.transform.Source) Test(org.junit.jupiter.api.Test)

Example 50 with TextMessage

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

the class JmsTemplateTests method doTestJmsException.

protected void doTestJmsException(JMSException original, Class<? extends JmsException> thrownExceptionClass) throws Exception {
    JmsTemplate template = createTemplate();
    template.setConnectionFactory(this.connectionFactory);
    template.setMessageConverter(new SimpleMessageConverter());
    String s = "Hello world";
    MessageProducer messageProducer = mock(MessageProducer.class);
    TextMessage textMessage = mock(TextMessage.class);
    reset(this.session);
    given(this.session.createProducer(this.queue)).willReturn(messageProducer);
    given(this.session.createTextMessage("Hello world")).willReturn(textMessage);
    willThrow(original).given(messageProducer).send(textMessage);
    assertThatExceptionOfType(thrownExceptionClass).isThrownBy(() -> template.convertAndSend(this.queue, s)).withCause(original);
    verify(messageProducer).close();
    verify(this.session).close();
    verify(this.connection).close();
}
Also used : SimpleMessageConverter(org.springframework.jms.support.converter.SimpleMessageConverter) MessageProducer(jakarta.jms.MessageProducer) TextMessage(jakarta.jms.TextMessage)

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