Search in sources :

Example 6 with SimpleMessageConverter

use of org.springframework.jms.support.converter.SimpleMessageConverter 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 = new Integer(5);
    given(session.createObjectMessage(content)).willReturn(message);
    given(message.getObject()).willReturn(content);
    SimpleMessageConverter converter = new SimpleMessageConverter();
    Message msg = converter.toMessage(content, session);
    assertEquals(content, converter.fromMessage(msg));
}
Also used : TextMessage(javax.jms.TextMessage) MapMessage(javax.jms.MapMessage) ObjectMessage(javax.jms.ObjectMessage) BytesMessage(javax.jms.BytesMessage) Message(javax.jms.Message) ObjectMessage(javax.jms.ObjectMessage) SimpleMessageConverter(org.springframework.jms.support.converter.SimpleMessageConverter) Session(javax.jms.Session) Test(org.junit.Test)

Example 7 with SimpleMessageConverter

use of org.springframework.jms.support.converter.SimpleMessageConverter 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);
    assertEquals(content, converter.fromMessage(msg));
    verify(message).setObject("key1", "value1");
    verify(message).setObject("key2", "value2");
}
Also used : TextMessage(javax.jms.TextMessage) MapMessage(javax.jms.MapMessage) ObjectMessage(javax.jms.ObjectMessage) BytesMessage(javax.jms.BytesMessage) Message(javax.jms.Message) HashMap(java.util.HashMap) MapMessage(javax.jms.MapMessage) SimpleMessageConverter(org.springframework.jms.support.converter.SimpleMessageConverter) Session(javax.jms.Session) Test(org.junit.Test)

Example 8 with SimpleMessageConverter

use of org.springframework.jms.support.converter.SimpleMessageConverter 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);
    assertSame(message, msg);
}
Also used : TextMessage(javax.jms.TextMessage) MapMessage(javax.jms.MapMessage) ObjectMessage(javax.jms.ObjectMessage) BytesMessage(javax.jms.BytesMessage) Message(javax.jms.Message) ObjectMessage(javax.jms.ObjectMessage) SimpleMessageConverter(org.springframework.jms.support.converter.SimpleMessageConverter) Session(javax.jms.Session) Test(org.junit.Test)

Example 9 with SimpleMessageConverter

use of org.springframework.jms.support.converter.SimpleMessageConverter 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);
    assertSame(message, msg);
}
Also used : TextMessage(javax.jms.TextMessage) MapMessage(javax.jms.MapMessage) ObjectMessage(javax.jms.ObjectMessage) BytesMessage(javax.jms.BytesMessage) Message(javax.jms.Message) SimpleMessageConverter(org.springframework.jms.support.converter.SimpleMessageConverter) Test(org.junit.Test)

Example 10 with SimpleMessageConverter

use of org.springframework.jms.support.converter.SimpleMessageConverter in project spring-framework by spring-projects.

the class JmsTemplateTests method testConverter.

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

Aggregations

SimpleMessageConverter (org.springframework.jms.support.converter.SimpleMessageConverter)12 Test (org.junit.Test)11 TextMessage (javax.jms.TextMessage)9 MapMessage (javax.jms.MapMessage)8 Session (javax.jms.Session)8 BytesMessage (javax.jms.BytesMessage)6 Message (javax.jms.Message)6 ObjectMessage (javax.jms.ObjectMessage)6 HashMap (java.util.HashMap)3 JMSException (javax.jms.JMSException)2 MessageProducer (javax.jms.MessageProducer)2 MessageConversionException (org.springframework.jms.support.converter.MessageConversionException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ExceptionListener (javax.jms.ExceptionListener)1 StringContains (org.hamcrest.core.StringContains)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 JmsException (org.springframework.jms.JmsException)1 StubTextMessage (org.springframework.jms.StubTextMessage)1 UncategorizedJmsException (org.springframework.jms.UncategorizedJmsException)1 Message (org.springframework.messaging.Message)1