Java Code Examples for org.springframework.jms.support.converter.SimpleMessageConverter#toMessage()

The following examples show how to use org.springframework.jms.support.converter.SimpleMessageConverter#toMessage() . 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 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 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: 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 4
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 5
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 6
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 7
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 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: 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 11
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 12
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 13
Source File: SimpleMessageConverterTests.java    From java-technology-stack 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 14
Source File: SimpleMessageConverterTests.java    From java-technology-stack with 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 15
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 16
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 17
Source File: SimpleMessageConverterTests.java    From spring-analysis-note 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 18
Source File: SimpleMessageConverterTests.java    From spring-analysis-note with 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 19
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 20
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));
}