Java Code Examples for org.springframework.jms.support.converter.SimpleMessageConverter

The following examples show how to use org.springframework.jms.support.converter.SimpleMessageConverter. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: spring-analysis-note   Source File: SimpleMessageConverterTests.java    License: MIT License 6 votes vote down vote up
@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);
}
 
Example 2
Source Project: spring-analysis-note   Source File: SimpleMessageConverterTests.java    License: MIT License 6 votes vote down vote up
@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");
}
 
Example 3
Source Project: spring-analysis-note   Source File: SimpleMessageConverterTests.java    License: MIT License 6 votes vote down vote up
@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 */ }
}
 
Example 4
Source Project: spring-analysis-note   Source File: SimpleMessageConverterTests.java    License: MIT License 6 votes vote down vote up
@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 */ }
}
 
Example 5
Source Project: spring-analysis-note   Source File: JmsMessagingTemplateTests.java    License: MIT License 6 votes vote down vote up
@Test
public void convertAndSendCustomJmsMessageConverter() throws JMSException {
	this.messagingTemplate.setJmsMessageConverter(new SimpleMessageConverter() {
		@Override
		public javax.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");
}
 
Example 6
Source Project: spring-analysis-note   Source File: JmsTemplateTests.java    License: MIT License 6 votes vote down vote up
@Test
public void testConverter() throws Exception {
	JmsTemplate template = createTemplate();
	template.setConnectionFactory(this.connectionFactory);
	template.setMessageConverter(new SimpleMessageConverter());
	String s = "Hello world";

	MessageProducer messageProducer = mock(MessageProducer.class);
	TextMessage textMessage = mock(TextMessage.class);

	given(this.session.createProducer(this.queue)).willReturn(messageProducer);
	given(this.session.createTextMessage("Hello world")).willReturn(textMessage);

	template.convertAndSend(this.queue, s);

	verify(messageProducer).send(textMessage);
	verify(messageProducer).close();
	if (useTransactedTemplate()) {
		verify(this.session).commit();
	}
	verify(this.session).close();
	verify(this.connection).close();
}
 
Example 7
@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);
}
 
Example 8
@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");
}
 
Example 9
@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 */ }
}
 
Example 10
@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 */ }
}
 
Example 11
Source Project: java-technology-stack   Source File: JmsMessagingTemplateTests.java    License: MIT License 6 votes vote down vote up
@Test
public void convertAndSendCustomJmsMessageConverter() throws JMSException {
	this.messagingTemplate.setJmsMessageConverter(new SimpleMessageConverter() {
		@Override
		public javax.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());

	this.thrown.expect(org.springframework.messaging.converter.MessageConversionException.class);
	this.thrown.expectMessage(new StringContains("Test exception"));
	this.messageCreator.getValue().createMessage(mock(Session.class));
}
 
Example 12
Source Project: java-technology-stack   Source File: JmsTemplateTests.java    License: MIT License 6 votes vote down vote up
@Test
public void testConverter() throws Exception {
	JmsTemplate template = createTemplate();
	template.setConnectionFactory(this.connectionFactory);
	template.setMessageConverter(new SimpleMessageConverter());
	String s = "Hello world";

	MessageProducer messageProducer = mock(MessageProducer.class);
	TextMessage textMessage = mock(TextMessage.class);

	given(this.session.createProducer(this.queue)).willReturn(messageProducer);
	given(this.session.createTextMessage("Hello world")).willReturn(textMessage);

	template.convertAndSend(this.queue, s);

	verify(messageProducer).send(textMessage);
	verify(messageProducer).close();
	if (useTransactedTemplate()) {
		verify(this.session).commit();
	}
	verify(this.session).close();
	verify(this.connection).close();
}
 
Example 13
@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);
}
 
Example 14
@Test
public void testMapConversion() throws JMSException {

	Session session = mock(Session.class);
	MapMessage message = mock(MapMessage.class);

	Map<String, String> content = new HashMap<String, String>(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");
}
 
Example 15
@Test
public void testMapConversionWhereMapHasNonStringTypesForKeys() throws JMSException {

	MapMessage message = mock(MapMessage.class);
	final Session session = mock(Session.class);
	given(session.createMapMessage()).willReturn(message);

	final Map<Integer, String> content = new HashMap<Integer, String>(1);
	content.put(1, "value1");

	final SimpleMessageConverter converter = new SimpleMessageConverter();
	try {
		converter.toMessage(content, session);
		fail("expected MessageConversionException");
	} catch (MessageConversionException ex) { /* expected */ }
}
 
Example 16
@Test
public void testMapConversionWhereMapHasNNullForKey() throws JMSException {

	MapMessage message = mock(MapMessage.class);
	final Session session = mock(Session.class);
	given(session.createMapMessage()).willReturn(message);

	final Map<Object, String> content = new HashMap<Object, String>(1);
	content.put(null, "value1");

	final SimpleMessageConverter converter = new SimpleMessageConverter();
	try {
		converter.toMessage(content, session);
		fail("expected MessageConversionException");
	} catch (MessageConversionException ex) { /* expected */ }
}
 
Example 17
@Test
public void convertAndSendCustomJmsMessageConverter() throws JMSException {
	messagingTemplate.setJmsMessageConverter(new SimpleMessageConverter() {
		@Override
		public javax.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");
		}
	});

	messagingTemplate.convertAndSend("myQueue", "msg to convert");
	verify(jmsTemplate).send(eq("myQueue"), messageCreator.capture());

	thrown.expect(org.springframework.messaging.converter.MessageConversionException.class);
	thrown.expectMessage(new StringContains("Test exception"));
	messageCreator.getValue().createMessage(mock(Session.class));
}
 
Example 18
Source Project: spring4-understanding   Source File: JmsTemplateTests.java    License: Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example 19
Source Project: spring-analysis-note   Source File: MessageListenerAdapterTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testThatTheDefaultMessageConverterisIndeedTheSimpleMessageConverter() throws Exception {
	MessageListenerAdapter adapter = new MessageListenerAdapter();
	assertNotNull("The default [MessageConverter] must never be null.", adapter.getMessageConverter());
	assertTrue("The default [MessageConverter] must be of the type [SimpleMessageConverter]",
			adapter.getMessageConverter() instanceof SimpleMessageConverter);
}
 
Example 20
Source Project: spring-analysis-note   Source File: SimpleMessageConverterTests.java    License: MIT License 5 votes vote down vote up
@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));
}
 
Example 21
Source Project: spring-analysis-note   Source File: SimpleMessageConverterTests.java    License: MIT License 5 votes vote down vote up
@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));
}
 
Example 22
Source Project: spring-analysis-note   Source File: SimpleMessageConverterTests.java    License: MIT License 5 votes vote down vote up
@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);
}
 
Example 23
Source Project: spring-analysis-note   Source File: SimpleMessageConverterTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testFromMessageSimplyReturnsMessageAsIsIfSuppliedWithMessage() throws JMSException {
	Message message = mock(Message.class);

	SimpleMessageConverter converter = new SimpleMessageConverter();
	Object msg = converter.fromMessage(message);
	assertSame(message, msg);
}
 
Example 24
Source Project: spring-analysis-note   Source File: JmsTemplateTests.java    License: MIT License 5 votes vote down vote up
protected void doTestJmsException(JMSException original, Class<? extends JmsException> thrownExceptionClass) throws Exception {
	JmsTemplate template = createTemplate();
	template.setConnectionFactory(this.connectionFactory);
	template.setMessageConverter(new SimpleMessageConverter());
	String s = "Hello world";

	MessageProducer messageProducer = mock(MessageProducer.class);
	TextMessage textMessage = mock(TextMessage.class);

	reset(this.session);
	given(this.session.createProducer(this.queue)).willReturn(messageProducer);
	given(this.session.createTextMessage("Hello world")).willReturn(textMessage);

	willThrow(original).given(messageProducer).send(textMessage);

	try {
		template.convertAndSend(this.queue, s);
		fail("Should have thrown JmsException");
	}
	catch (JmsException wrappedEx) {
		// expected
		assertEquals(thrownExceptionClass, wrappedEx.getClass());
		assertEquals(original, wrappedEx.getCause());
	}

	verify(messageProducer).close();
	verify(this.session).close();
	verify(this.connection).close();
}
 
Example 25
@Test
public void testThatTheDefaultMessageConverterisIndeedTheSimpleMessageConverter() throws Exception {
	MessageListenerAdapter adapter = new MessageListenerAdapter();
	assertNotNull("The default [MessageConverter] must never be null.", adapter.getMessageConverter());
	assertTrue("The default [MessageConverter] must be of the type [SimpleMessageConverter]",
			adapter.getMessageConverter() instanceof SimpleMessageConverter);
}
 
Example 26
@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));
}
 
Example 27
@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));
}
 
Example 28
@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);
}
 
Example 29
@Test
public void testFromMessageSimplyReturnsMessageAsIsIfSuppliedWithMessage() throws JMSException {
	Message message = mock(Message.class);

	SimpleMessageConverter converter = new SimpleMessageConverter();
	Object msg = converter.fromMessage(message);
	assertSame(message, msg);
}
 
Example 30
Source Project: java-technology-stack   Source File: JmsTemplateTests.java    License: MIT License 5 votes vote down vote up
protected void doTestJmsException(JMSException original, Class<? extends JmsException> thrownExceptionClass) throws Exception {
	JmsTemplate template = createTemplate();
	template.setConnectionFactory(this.connectionFactory);
	template.setMessageConverter(new SimpleMessageConverter());
	String s = "Hello world";

	MessageProducer messageProducer = mock(MessageProducer.class);
	TextMessage textMessage = mock(TextMessage.class);

	reset(this.session);
	given(this.session.createProducer(this.queue)).willReturn(messageProducer);
	given(this.session.createTextMessage("Hello world")).willReturn(textMessage);

	willThrow(original).given(messageProducer).send(textMessage);

	try {
		template.convertAndSend(this.queue, s);
		fail("Should have thrown JmsException");
	}
	catch (JmsException wrappedEx) {
		// expected
		assertEquals(thrownExceptionClass, wrappedEx.getClass());
		assertEquals(original, wrappedEx.getCause());
	}

	verify(messageProducer).close();
	verify(this.session).close();
	verify(this.connection).close();
}