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

The following examples show how to use org.springframework.jms.support.converter.SimpleMessageConverter. 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 File: SimpleMessageConverterTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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 #2
Source File: SimpleMessageConverterTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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 #3
Source File: JmsTemplateTests.java    From java-technology-stack with 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 #4
Source File: JmsMessagingTemplateTests.java    From java-technology-stack with 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 #5
Source File: SimpleMessageConverterTests.java    From java-technology-stack with 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 #6
Source File: SimpleMessageConverterTests.java    From java-technology-stack with 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 #7
Source File: SimpleMessageConverterTests.java    From spring4-understanding with Apache License 2.0 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 #8
Source File: SimpleMessageConverterTests.java    From java-technology-stack with 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 #9
Source File: SimpleMessageConverterTests.java    From java-technology-stack with 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 #10
Source File: JmsMessagingTemplateTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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 #11
Source File: JmsTemplateTests.java    From spring4-understanding with 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 #12
Source File: JmsTemplateTests.java    From spring-analysis-note with 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
Source File: JmsMessagingTemplateTests.java    From spring-analysis-note with 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 #14
Source File: SimpleMessageConverterTests.java    From spring-analysis-note with 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 #15
Source File: SimpleMessageConverterTests.java    From spring-analysis-note with 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 #16
Source File: SimpleMessageConverterTests.java    From spring-analysis-note with 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 #17
Source File: SimpleMessageConverterTests.java    From spring4-understanding with Apache License 2.0 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<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 #18
Source File: SimpleMessageConverterTests.java    From spring-analysis-note with 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 #19
Source File: JmsTemplateTests.java    From spring-analysis-note with 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 #20
Source File: SimpleMessageConverterTests.java    From spring4-understanding with Apache License 2.0 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 #21
Source File: SimpleMessageConverterTests.java    From spring4-understanding with Apache License 2.0 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 #22
Source File: MessageListenerAdapterTests.java    From spring4-understanding with Apache License 2.0 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 #23
Source File: JmsTemplateTests.java    From java-technology-stack with 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 #24
Source File: JmsTemplateTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
protected void doTestJmsException(JMSException original, Class<? extends JmsException> thrownExceptionClass) 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);

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

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

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

	verify(messageProducer).close();
	verify(session).close();
	verify(connection).close();
}
 
Example #25
Source File: MessageListenerAdapterTests.java    From spring-analysis-note with 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 #26
Source File: SimpleMessageConverterTests.java    From spring-analysis-note with 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 #27
Source File: SimpleMessageConverterTests.java    From java-technology-stack with 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 #28
Source File: SimpleMessageConverterTests.java    From java-technology-stack with 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 #29
Source File: SimpleMessageConverterTests.java    From spring4-understanding with Apache License 2.0 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 #30
Source File: SimpleMessageConverterTests.java    From spring-analysis-note with 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);
}