use of jakarta.jms.MapMessage in project spring-framework by spring-projects.
the class SimpleMessageConverter method createMessageForMap.
/**
* Create a JMS MapMessage for the given Map.
* @param map the Map to convert
* @param session current JMS session
* @return the resulting message
* @throws JMSException if thrown by JMS methods
* @see jakarta.jms.Session#createMapMessage
*/
protected MapMessage createMessageForMap(Map<?, ?> map, Session session) throws JMSException {
MapMessage message = session.createMapMessage();
for (Map.Entry<?, ?> entry : map.entrySet()) {
Object key = entry.getKey();
if (!(key instanceof String)) {
throw new MessageConversionException("Cannot convert non-String key of type [" + ObjectUtils.nullSafeClassName(key) + "] to JMS MapMessage entry");
}
message.setObject((String) key, entry.getValue());
}
return message;
}
use of jakarta.jms.MapMessage 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();
assertThatExceptionOfType(MessageConversionException.class).isThrownBy(() -> converter.toMessage(content, session));
}
use of jakarta.jms.MapMessage 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();
assertThatExceptionOfType(MessageConversionException.class).isThrownBy(() -> converter.toMessage(content, session));
}
use of jakarta.jms.MapMessage 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);
assertThat(converter.fromMessage(msg)).isEqualTo(content);
verify(message).setObject("key1", "value1");
verify(message).setObject("key2", "value2");
}
Aggregations