Search in sources :

Example 51 with TextMessage

use of jakarta.jms.TextMessage 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 52 with TextMessage

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

the class MappingJackson2MessageConverterTests method fromTextMessage.

@Test
void fromTextMessage() throws Exception {
    TextMessage textMessageMock = mock(TextMessage.class);
    MyBean unmarshalled = new MyBean("bar");
    String text = "{\"foo\":\"bar\"}";
    given(textMessageMock.getStringProperty("__typeid__")).willReturn(MyBean.class.getName());
    given(textMessageMock.getText()).willReturn(text);
    MyBean result = (MyBean) converter.fromMessage(textMessageMock);
    assertThat(unmarshalled).as("Invalid result").isEqualTo(result);
}
Also used : TextMessage(jakarta.jms.TextMessage) Test(org.junit.jupiter.api.Test)

Example 53 with TextMessage

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

the class MappingJackson2MessageConverterTests method fromTextMessageWithUnknownProperty.

@Test
void fromTextMessageWithUnknownProperty() throws Exception {
    TextMessage textMessageMock = mock(TextMessage.class);
    MyBean unmarshalled = new MyBean("bar");
    String text = "{\"foo\":\"bar\", \"unknownProperty\":\"value\"}";
    given(textMessageMock.getStringProperty("__typeid__")).willReturn(MyBean.class.getName());
    given(textMessageMock.getText()).willReturn(text);
    MyBean result = (MyBean) converter.fromMessage(textMessageMock);
    assertThat(unmarshalled).as("Invalid result").isEqualTo(result);
}
Also used : TextMessage(jakarta.jms.TextMessage) Test(org.junit.jupiter.api.Test)

Example 54 with TextMessage

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

the class MappingJackson2MessageConverterTests method toTextMessageWithMap.

@Test
void toTextMessageWithMap() throws Exception {
    converter.setTargetType(MessageType.TEXT);
    TextMessage textMessageMock = mock(TextMessage.class);
    Map<String, String> toBeMarshalled = new HashMap<>();
    toBeMarshalled.put("foo", "bar");
    given(sessionMock.createTextMessage(isA(String.class))).willReturn(textMessageMock);
    converter.toMessage(toBeMarshalled, sessionMock);
    verify(textMessageMock).setStringProperty("__typeid__", HashMap.class.getName());
}
Also used : HashMap(java.util.HashMap) TextMessage(jakarta.jms.TextMessage) 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