Search in sources :

Example 26 with Message

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

the class JmsTemplateTests method doTestReceive.

private void doTestReceive(boolean explicitDestination, boolean useDefaultDestination, boolean testConverter, boolean clientAcknowledge, boolean messageSelector, boolean noLocal, long timeout) throws Exception {
    JmsTemplate template = createTemplate();
    template.setConnectionFactory(this.connectionFactory);
    String destinationName = "testDestination";
    if (useDefaultDestination) {
        if (explicitDestination) {
            template.setDefaultDestination(this.queue);
        } else {
            template.setDefaultDestinationName(destinationName);
        }
    }
    if (noLocal) {
        template.setPubSubNoLocal(true);
    }
    template.setReceiveTimeout(timeout);
    MessageConsumer messageConsumer = mock(MessageConsumer.class);
    String selectorString = "selector";
    given(this.session.createConsumer(this.queue, messageSelector ? selectorString : null)).willReturn(messageConsumer);
    if (!useTransactedTemplate() && !useTransactedSession()) {
        given(this.session.getAcknowledgeMode()).willReturn(clientAcknowledge ? Session.CLIENT_ACKNOWLEDGE : Session.AUTO_ACKNOWLEDGE);
    }
    TextMessage textMessage = mock(TextMessage.class);
    if (testConverter) {
        given(textMessage.getText()).willReturn("Hello World!");
    }
    if (timeout == JmsTemplate.RECEIVE_TIMEOUT_NO_WAIT) {
        given(messageConsumer.receiveNoWait()).willReturn(textMessage);
    } else if (timeout == JmsTemplate.RECEIVE_TIMEOUT_INDEFINITE_WAIT) {
        given(messageConsumer.receive()).willReturn(textMessage);
    } else {
        given(messageConsumer.receive(timeout)).willReturn(textMessage);
    }
    Message message = null;
    String textFromMessage = null;
    if (useDefaultDestination) {
        if (testConverter) {
            textFromMessage = (String) (messageSelector ? template.receiveSelectedAndConvert(selectorString) : template.receiveAndConvert());
        } else {
            message = (messageSelector ? template.receiveSelected(selectorString) : template.receive());
        }
    } else if (explicitDestination) {
        if (testConverter) {
            textFromMessage = (String) (messageSelector ? template.receiveSelectedAndConvert(this.queue, selectorString) : template.receiveAndConvert(this.queue));
        } else {
            message = (messageSelector ? template.receiveSelected(this.queue, selectorString) : template.receive(this.queue));
        }
    } else {
        if (testConverter) {
            textFromMessage = (String) (messageSelector ? template.receiveSelectedAndConvert(destinationName, selectorString) : template.receiveAndConvert(destinationName));
        } else {
            message = (messageSelector ? template.receiveSelected(destinationName, selectorString) : template.receive(destinationName));
        }
    }
    if (testConverter) {
        assertThat(textFromMessage).as("Message text should be equal").isEqualTo("Hello World!");
    } else {
        assertThat(textMessage).as("Messages should refer to the same object").isEqualTo(message);
    }
    verify(this.connection).start();
    verify(this.connection).close();
    if (useTransactedTemplate()) {
        verify(this.session).commit();
    }
    verify(this.session).close();
    if (!useTransactedSession() && clientAcknowledge) {
        verify(textMessage).acknowledge();
    }
    verify(messageConsumer).close();
}
Also used : MessageConsumer(jakarta.jms.MessageConsumer) Message(jakarta.jms.Message) TextMessage(jakarta.jms.TextMessage) TextMessage(jakarta.jms.TextMessage)

Example 27 with Message

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

the class SimpleMessageConverterTests method testToMessageSimplyReturnsMessageAsIsIfSuppliedWithMessage.

@Test
public void testToMessageSimplyReturnsMessageAsIsIfSuppliedWithMessage() throws JMSException {
    Session session = mock(Session.class);
    ObjectMessage message = mock(ObjectMessage.class);
    SimpleMessageConverter converter = new SimpleMessageConverter();
    Message msg = converter.toMessage(message, session);
    assertThat(msg).isSameAs(message);
}
Also used : Message(jakarta.jms.Message) BytesMessage(jakarta.jms.BytesMessage) TextMessage(jakarta.jms.TextMessage) MapMessage(jakarta.jms.MapMessage) ObjectMessage(jakarta.jms.ObjectMessage) ObjectMessage(jakarta.jms.ObjectMessage) SimpleMessageConverter(org.springframework.jms.support.converter.SimpleMessageConverter) Session(jakarta.jms.Session) Test(org.junit.jupiter.api.Test)

Example 28 with Message

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

the class SimpleMessageConverterTests method testMapConversion.

@Test
public void testMapConversion() throws JMSException {
    Session session = mock(Session.class);
    MapMessage message = mock(MapMessage.class);
    Map<String, String> content = new HashMap<>(2);
    content.put("key1", "value1");
    content.put("key2", "value2");
    given(session.createMapMessage()).willReturn(message);
    given(message.getMapNames()).willReturn(Collections.enumeration(content.keySet()));
    given(message.getObject("key1")).willReturn("value1");
    given(message.getObject("key2")).willReturn("value2");
    SimpleMessageConverter converter = new SimpleMessageConverter();
    Message msg = converter.toMessage(content, session);
    assertThat(converter.fromMessage(msg)).isEqualTo(content);
    verify(message).setObject("key1", "value1");
    verify(message).setObject("key2", "value2");
}
Also used : Message(jakarta.jms.Message) BytesMessage(jakarta.jms.BytesMessage) TextMessage(jakarta.jms.TextMessage) MapMessage(jakarta.jms.MapMessage) ObjectMessage(jakarta.jms.ObjectMessage) HashMap(java.util.HashMap) MapMessage(jakarta.jms.MapMessage) SimpleMessageConverter(org.springframework.jms.support.converter.SimpleMessageConverter) Session(jakarta.jms.Session) Test(org.junit.jupiter.api.Test)

Example 29 with Message

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

the class SimpleMessageConverterTests method testSerializableConversion.

@Test
public void testSerializableConversion() throws JMSException {
    Session session = mock(Session.class);
    ObjectMessage message = mock(ObjectMessage.class);
    Integer content = 5;
    given(session.createObjectMessage(content)).willReturn(message);
    given(message.getObject()).willReturn(content);
    SimpleMessageConverter converter = new SimpleMessageConverter();
    Message msg = converter.toMessage(content, session);
    assertThat(converter.fromMessage(msg)).isEqualTo(content);
}
Also used : Message(jakarta.jms.Message) BytesMessage(jakarta.jms.BytesMessage) TextMessage(jakarta.jms.TextMessage) MapMessage(jakarta.jms.MapMessage) ObjectMessage(jakarta.jms.ObjectMessage) ObjectMessage(jakarta.jms.ObjectMessage) SimpleMessageConverter(org.springframework.jms.support.converter.SimpleMessageConverter) Session(jakarta.jms.Session) Test(org.junit.jupiter.api.Test)

Example 30 with Message

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

the class SimpleMessageConverterTests method testFromMessageSimplyReturnsMessageAsIsIfSuppliedWithMessage.

@Test
public void testFromMessageSimplyReturnsMessageAsIsIfSuppliedWithMessage() throws JMSException {
    Message message = mock(Message.class);
    SimpleMessageConverter converter = new SimpleMessageConverter();
    Object msg = converter.fromMessage(message);
    assertThat(msg).isSameAs(message);
}
Also used : Message(jakarta.jms.Message) BytesMessage(jakarta.jms.BytesMessage) TextMessage(jakarta.jms.TextMessage) MapMessage(jakarta.jms.MapMessage) ObjectMessage(jakarta.jms.ObjectMessage) SimpleMessageConverter(org.springframework.jms.support.converter.SimpleMessageConverter) Test(org.junit.jupiter.api.Test)

Aggregations

Message (jakarta.jms.Message)30 Test (org.junit.jupiter.api.Test)23 TextMessage (jakarta.jms.TextMessage)19 Session (jakarta.jms.Session)18 BytesMessage (jakarta.jms.BytesMessage)13 ObjectMessage (jakarta.jms.ObjectMessage)13 ConnectionFactory (jakarta.jms.ConnectionFactory)9 Connection (jakarta.jms.Connection)8 QueueSession (jakarta.jms.QueueSession)7 MapMessage (jakarta.jms.MapMessage)6 MessageConsumer (jakarta.jms.MessageConsumer)6 MessageProducer (jakarta.jms.MessageProducer)6 SimpleMessageConverter (org.springframework.jms.support.converter.SimpleMessageConverter)6 JMSException (jakarta.jms.JMSException)5 Queue (jakarta.jms.Queue)4 Nullable (org.springframework.lang.Nullable)4 ExceptionListener (jakarta.jms.ExceptionListener)3 HashSet (java.util.HashSet)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 StubQueue (org.springframework.jms.StubQueue)3