Search in sources :

Example 1 with SimpleMessageConverter

use of org.springframework.jms.support.converter.SimpleMessageConverter 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);
    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) SimpleMessageConverter(org.springframework.jms.support.converter.SimpleMessageConverter) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) Test(org.junit.Test)

Example 2 with SimpleMessageConverter

use of org.springframework.jms.support.converter.SimpleMessageConverter 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(new Answer<Integer>() {

        @Override
        public Integer answer(InvocationOnMock invocation) throws Throwable {
            return byteArrayInputStream.read((byte[]) invocation.getArguments()[0]);
        }
    });
    SimpleMessageConverter converter = new SimpleMessageConverter();
    Message msg = converter.toMessage(content, session);
    assertEquals(content.length, ((byte[]) converter.fromMessage(msg)).length);
    verify(message).writeBytes(content);
}
Also used : TextMessage(javax.jms.TextMessage) MapMessage(javax.jms.MapMessage) ObjectMessage(javax.jms.ObjectMessage) BytesMessage(javax.jms.BytesMessage) Message(javax.jms.Message) ByteArrayInputStream(java.io.ByteArrayInputStream) InvocationOnMock(org.mockito.invocation.InvocationOnMock) BytesMessage(javax.jms.BytesMessage) SimpleMessageConverter(org.springframework.jms.support.converter.SimpleMessageConverter) Session(javax.jms.Session) Test(org.junit.Test)

Example 3 with SimpleMessageConverter

use of org.springframework.jms.support.converter.SimpleMessageConverter 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();
    try {
        converter.toMessage(content, session);
        fail("expected MessageConversionException");
    } catch (MessageConversionException ex) {
    /* expected */
    }
}
Also used : MessageConversionException(org.springframework.jms.support.converter.MessageConversionException) HashMap(java.util.HashMap) MapMessage(javax.jms.MapMessage) SimpleMessageConverter(org.springframework.jms.support.converter.SimpleMessageConverter) Session(javax.jms.Session) Test(org.junit.Test)

Example 4 with SimpleMessageConverter

use of org.springframework.jms.support.converter.SimpleMessageConverter in project camel by apache.

the class JmsEndpointConfigurationTest method testSettingEndpointOptions.

@SuppressWarnings("deprecation")
@Test
public void testSettingEndpointOptions() throws Exception {
    JmsEndpoint endpoint = resolveMandatoryEndpoint("jms:queue:Foo", JmsEndpoint.class);
    endpoint.setAcceptMessagesWhileStopping(true);
    assertTrue(endpoint.isAcceptMessagesWhileStopping());
    endpoint.setAllowReplyManagerQuickStop(true);
    assertTrue(endpoint.isAllowReplyManagerQuickStop());
    endpoint.setAcknowledgementMode(2);
    assertEquals(2, endpoint.getAcknowledgementMode());
    endpoint.setAcknowledgementModeName("CLIENT_ACKNOWLEDGE");
    assertEquals("CLIENT_ACKNOWLEDGE", endpoint.getAcknowledgementModeName());
    endpoint.setAlwaysCopyMessage(true);
    assertTrue(endpoint.isAlwaysCopyMessage());
    endpoint.setCacheLevel(2);
    assertEquals(2, endpoint.getCacheLevel());
    endpoint.setCacheLevelName("foo");
    assertEquals("foo", endpoint.getCacheLevelName());
    endpoint.setClientId("bar");
    assertEquals("bar", endpoint.getClientId());
    endpoint.setConcurrentConsumers(5);
    assertEquals(5, endpoint.getConcurrentConsumers());
    endpoint.setDeliveryPersistent(true);
    assertTrue(endpoint.isDeliveryPersistent());
    endpoint.setDestinationName("cool");
    assertEquals("cool", endpoint.getDestinationName());
    endpoint.setDisableReplyTo(true);
    assertTrue(endpoint.isDisableReplyTo());
    endpoint.setEagerLoadingOfProperties(true);
    assertTrue(endpoint.isEagerLoadingOfProperties());
    endpoint.setExceptionListener(new ExceptionListener() {

        public void onException(JMSException exception) {
        }
    });
    assertNotNull(endpoint.getExceptionListener());
    endpoint.setErrorHandler(new ErrorHandler() {

        public void handleError(Throwable t) {
        }
    });
    assertNotNull(endpoint.getErrorHandler());
    endpoint.setExplicitQosEnabled(true);
    assertTrue(endpoint.isExplicitQosEnabled());
    endpoint.setExposeListenerSession(true);
    assertTrue(endpoint.isExposeListenerSession());
    endpoint.setIdleTaskExecutionLimit(5);
    assertEquals(5, endpoint.getIdleTaskExecutionLimit());
    endpoint.setIdleConsumerLimit(5);
    assertEquals(5, endpoint.getIdleConsumerLimit());
    endpoint.setMaxConcurrentConsumers(4);
    assertEquals(4, endpoint.getMaxConcurrentConsumers());
    endpoint.setMaxMessagesPerTask(9);
    assertEquals(9, endpoint.getMaxMessagesPerTask());
    endpoint.setMessageConverter(new SimpleMessageConverter());
    assertNotNull(endpoint.getMessageConverter());
    endpoint.setMessageIdEnabled(true);
    assertTrue(endpoint.isMessageIdEnabled());
    endpoint.setMessageTimestampEnabled(true);
    assertTrue(endpoint.isMessageTimestampEnabled());
    endpoint.setPreserveMessageQos(true);
    assertTrue(endpoint.isPreserveMessageQos());
    endpoint.setPriority(6);
    assertEquals(6, endpoint.getPriority());
    endpoint.setPubSubNoLocal(true);
    assertTrue(endpoint.isPubSubNoLocal());
    endpoint.setPubSubNoLocal(true);
    assertTrue(endpoint.isPubSubNoLocal());
    assertFalse(endpoint.isPubSubDomain());
    endpoint.setReceiveTimeout(5000);
    assertEquals(5000, endpoint.getReceiveTimeout());
    endpoint.setRecoveryInterval(6000);
    assertEquals(6000, endpoint.getRecoveryInterval());
    endpoint.setReplyTo("bar");
    assertEquals("bar", endpoint.getReplyTo());
    endpoint.setReplyToDeliveryPersistent(true);
    assertTrue(endpoint.isReplyToDeliveryPersistent());
    endpoint.setReplyToDestinationSelectorName("me");
    assertEquals("me", endpoint.getReplyToDestinationSelectorName());
    endpoint.setRequestTimeout(3000);
    assertEquals(3000, endpoint.getRequestTimeout());
    endpoint.setSelector("you");
    assertEquals("you", endpoint.getSelector());
    endpoint.setTimeToLive(4000);
    assertEquals(4000, endpoint.getTimeToLive());
    endpoint.setTransacted(true);
    assertTrue(endpoint.isTransacted());
    endpoint.setTransactedInOut(true);
    assertTrue(endpoint.isTransactedInOut());
    endpoint.setTransferExchange(true);
    assertTrue(endpoint.isTransferExchange());
    endpoint.setTransferException(true);
    assertTrue(endpoint.isTransferException());
    endpoint.setJmsMessageType(JmsMessageType.Text);
    assertEquals(JmsMessageType.Text, endpoint.getJmsMessageType());
}
Also used : ErrorHandler(org.springframework.util.ErrorHandler) ExceptionListener(javax.jms.ExceptionListener) JMSException(javax.jms.JMSException) SimpleMessageConverter(org.springframework.jms.support.converter.SimpleMessageConverter) Test(org.junit.Test)

Example 5 with SimpleMessageConverter

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

the class SimpleMessageConverterTests method testMapConversionWhereMapHasNNullForKey.

@Test
public void testMapConversionWhereMapHasNNullForKey() throws JMSException {
    MapMessage message = mock(MapMessage.class);
    Session session = mock(Session.class);
    given(session.createMapMessage()).willReturn(message);
    Map<Object, String> content = new HashMap<>(1);
    content.put(null, "value1");
    SimpleMessageConverter converter = new SimpleMessageConverter();
    try {
        converter.toMessage(content, session);
        fail("expected MessageConversionException");
    } catch (MessageConversionException ex) {
    /* expected */
    }
}
Also used : MessageConversionException(org.springframework.jms.support.converter.MessageConversionException) HashMap(java.util.HashMap) MapMessage(javax.jms.MapMessage) SimpleMessageConverter(org.springframework.jms.support.converter.SimpleMessageConverter) Session(javax.jms.Session) 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