Search in sources :

Example 36 with Session

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

the class MessagingMessageListenerAdapterTests method replyWithCustomTimeToLive.

@Test
public void replyWithCustomTimeToLive() throws JMSException {
    Session session = mock(Session.class);
    Queue replyDestination = mock(Queue.class);
    given(session.createQueue("queueOut")).willReturn(replyDestination);
    MessageProducer messageProducer = mock(MessageProducer.class);
    TextMessage responseMessage = mock(TextMessage.class);
    given(session.createTextMessage("Response")).willReturn(responseMessage);
    given(session.createProducer(replyDestination)).willReturn(messageProducer);
    MessagingMessageListenerAdapter listener = getPayloadInstance("Response", "replyPayloadToQueue", Message.class);
    QosSettings settings = new QosSettings();
    settings.setTimeToLive(6000);
    listener.setResponseQosSettings(settings);
    listener.onMessage(mock(jakarta.jms.Message.class), session);
    verify(session).createQueue("queueOut");
    verify(session).createTextMessage("Response");
    verify(messageProducer).send(responseMessage, jakarta.jms.Message.DEFAULT_DELIVERY_MODE, jakarta.jms.Message.DEFAULT_PRIORITY, 6000);
    verify(messageProducer).close();
}
Also used : StubTextMessage(org.springframework.jms.StubTextMessage) Message(org.springframework.messaging.Message) TextMessage(jakarta.jms.TextMessage) QosSettings(org.springframework.jms.support.QosSettings) MessageProducer(jakarta.jms.MessageProducer) Queue(jakarta.jms.Queue) StubTextMessage(org.springframework.jms.StubTextMessage) TextMessage(jakarta.jms.TextMessage) Session(jakarta.jms.Session) Test(org.junit.jupiter.api.Test)

Example 37 with Session

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

the class SimpleMessageConverterTests method testMapConversionWhereMapHasNonStringTypesForKeys.

@Test
public void testMapConversionWhereMapHasNonStringTypesForKeys() throws JMSException {
    MapMessage message = mock(MapMessage.class);
    Session session = mock(Session.class);
    given(session.createMapMessage()).willReturn(message);
    Map<Integer, String> content = new HashMap<>(1);
    content.put(1, "value1");
    SimpleMessageConverter converter = new SimpleMessageConverter();
    assertThatExceptionOfType(MessageConversionException.class).isThrownBy(() -> converter.toMessage(content, session));
}
Also used : MessageConversionException(org.springframework.jms.support.converter.MessageConversionException) 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 38 with Session

use of jakarta.jms.Session 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);
}
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) TextMessage(jakarta.jms.TextMessage) Session(jakarta.jms.Session) Test(org.junit.jupiter.api.Test)

Example 39 with Session

use of jakarta.jms.Session 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);
}
Also used : Message(jakarta.jms.Message) BytesMessage(jakarta.jms.BytesMessage) TextMessage(jakarta.jms.TextMessage) MapMessage(jakarta.jms.MapMessage) ObjectMessage(jakarta.jms.ObjectMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) BytesMessage(jakarta.jms.BytesMessage) SimpleMessageConverter(org.springframework.jms.support.converter.SimpleMessageConverter) Session(jakarta.jms.Session) Test(org.junit.jupiter.api.Test)

Example 40 with Session

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

the class DynamicDestinationResolverTests method resolveWithPointToPointQueueSession.

@Test
public void resolveWithPointToPointQueueSession() throws Exception {
    Queue expectedDestination = new StubQueue();
    Session session = mock(QueueSession.class);
    given(session.createQueue(DESTINATION_NAME)).willReturn(expectedDestination);
    testResolveDestination(session, expectedDestination, false);
}
Also used : StubQueue(org.springframework.jms.StubQueue) StubQueue(org.springframework.jms.StubQueue) Queue(jakarta.jms.Queue) QueueSession(jakarta.jms.QueueSession) Session(jakarta.jms.Session) TopicSession(jakarta.jms.TopicSession) Test(org.junit.jupiter.api.Test)

Aggregations

Session (jakarta.jms.Session)84 Test (org.junit.jupiter.api.Test)71 Connection (jakarta.jms.Connection)30 Message (jakarta.jms.Message)27 TextMessage (jakarta.jms.TextMessage)26 StubTextMessage (org.springframework.jms.StubTextMessage)22 ConnectionFactory (jakarta.jms.ConnectionFactory)21 JMSException (jakarta.jms.JMSException)20 MessageProducer (jakarta.jms.MessageProducer)19 Destination (jakarta.jms.Destination)18 MessagingMessageListenerAdapter (org.springframework.jms.listener.adapter.MessagingMessageListenerAdapter)16 Queue (jakarta.jms.Queue)12 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)12 BDDMockito.given (org.mockito.BDDMockito.given)12 Mockito.mock (org.mockito.Mockito.mock)12 Mockito.verify (org.mockito.Mockito.verify)12 StubQueue (org.springframework.jms.StubQueue)12 ObjectMessage (jakarta.jms.ObjectMessage)11 QueueSession (jakarta.jms.QueueSession)11 SimpleMessageConverter (org.springframework.jms.support.converter.SimpleMessageConverter)11