Search in sources :

Example 76 with Session

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

the class SimpleMessageListenerContainerTests method testContextRefreshedEventDoesNotStartTheConnectionIfAutoStartIsSetToFalse.

@Test
public void testContextRefreshedEventDoesNotStartTheConnectionIfAutoStartIsSetToFalse() throws Exception {
    MessageConsumer messageConsumer = mock(MessageConsumer.class);
    Session session = mock(Session.class);
    // Queue gets created in order to create MessageConsumer for that Destination...
    given(session.createQueue(DESTINATION_NAME)).willReturn(QUEUE_DESTINATION);
    // and then the MessageConsumer gets created...
    // no MessageSelector...
    given(session.createConsumer(QUEUE_DESTINATION, null)).willReturn(messageConsumer);
    Connection connection = mock(Connection.class);
    // session gets created in order to register MessageListener...
    given(connection.createSession(this.container.isSessionTransacted(), this.container.getSessionAcknowledgeMode())).willReturn(session);
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    given(connectionFactory.createConnection()).willReturn(connection);
    this.container.setConnectionFactory(connectionFactory);
    this.container.setDestinationName(DESTINATION_NAME);
    this.container.setMessageListener(new TestMessageListener());
    this.container.setAutoStartup(false);
    this.container.afterPropertiesSet();
    GenericApplicationContext context = new GenericApplicationContext();
    context.getBeanFactory().registerSingleton("messageListenerContainer", this.container);
    context.refresh();
    context.close();
    verify(connection).setExceptionListener(this.container);
}
Also used : MessageConsumer(jakarta.jms.MessageConsumer) ConnectionFactory(jakarta.jms.ConnectionFactory) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) Connection(jakarta.jms.Connection) Session(jakarta.jms.Session) Test(org.junit.jupiter.api.Test)

Example 77 with Session

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

the class MessagingMessageConverterTests method simpleObject.

@Test
public void simpleObject() throws Exception {
    Session session = mock(Session.class);
    Serializable payload = mock(Serializable.class);
    ObjectMessage jmsMessage = mock(ObjectMessage.class);
    given(session.createObjectMessage(payload)).willReturn(jmsMessage);
    this.converter.toMessage(MessageBuilder.withPayload(payload).build(), session);
    verify(session).createObjectMessage(payload);
}
Also used : Serializable(java.io.Serializable) ObjectMessage(jakarta.jms.ObjectMessage) Session(jakarta.jms.Session) Test(org.junit.jupiter.api.Test)

Example 78 with Session

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

the class DynamicDestinationResolverTests method resolveWithPubSubVanillaSession.

@Test
public void resolveWithPubSubVanillaSession() throws Exception {
    Topic expectedDestination = new StubTopic();
    Session session = mock(Session.class);
    given(session.createTopic(DESTINATION_NAME)).willReturn(expectedDestination);
    testResolveDestination(session, expectedDestination, true);
}
Also used : StubTopic(org.springframework.jms.StubTopic) Topic(jakarta.jms.Topic) StubTopic(org.springframework.jms.StubTopic) QueueSession(jakarta.jms.QueueSession) Session(jakarta.jms.Session) TopicSession(jakarta.jms.TopicSession) Test(org.junit.jupiter.api.Test)

Example 79 with Session

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

the class JndiDestinationResolverTests method testDelegatesToFallbackIfNotResolvedInJndi.

@Test
public void testDelegatesToFallbackIfNotResolvedInJndi() throws Exception {
    Session session = mock(Session.class);
    DestinationResolver dynamicResolver = mock(DestinationResolver.class);
    given(dynamicResolver.resolveDestinationName(session, DESTINATION_NAME, true)).willReturn(DESTINATION);
    JndiDestinationResolver resolver = new JndiDestinationResolver() {

        @Override
        protected <T> T lookup(String jndiName, Class<T> requiredClass) throws NamingException {
            throw new NamingException();
        }
    };
    resolver.setFallbackToDynamicDestination(true);
    resolver.setDynamicDestinationResolver(dynamicResolver);
    Destination destination = resolver.resolveDestinationName(session, DESTINATION_NAME, true);
    assertThat(destination).isNotNull();
    assertThat(destination).isSameAs(DESTINATION);
}
Also used : Destination(jakarta.jms.Destination) NamingException(javax.naming.NamingException) Session(jakarta.jms.Session) Test(org.junit.jupiter.api.Test)

Example 80 with Session

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

the class JmsMessagingTemplateTests method convertAndSendCustomJmsMessageConverter.

@Test
public void convertAndSendCustomJmsMessageConverter() throws JMSException {
    this.messagingTemplate.setJmsMessageConverter(new SimpleMessageConverter() {

        @Override
        public jakarta.jms.Message toMessage(Object object, Session session) throws JMSException, org.springframework.jms.support.converter.MessageConversionException {
            throw new org.springframework.jms.support.converter.MessageConversionException("Test exception");
        }
    });
    this.messagingTemplate.convertAndSend("myQueue", "msg to convert");
    verify(this.jmsTemplate).send(eq("myQueue"), this.messageCreator.capture());
    assertThatExceptionOfType(org.springframework.messaging.converter.MessageConversionException.class).isThrownBy(() -> this.messageCreator.getValue().createMessage(mock(Session.class))).withMessageContaining("Test exception");
}
Also used : StubTextMessage(org.springframework.jms.StubTextMessage) Message(org.springframework.messaging.Message) TextMessage(jakarta.jms.TextMessage) JMSException(jakarta.jms.JMSException) SimpleMessageConverter(org.springframework.jms.support.converter.SimpleMessageConverter) Session(jakarta.jms.Session) 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