@Override public Message toMessage(Object object, Session session, @Nullable Object conversionHint) throws JMSException, MessageConversionException { return toMessage(object, session, getSerializationView(conversionHint)); }
/** * Convert a Java object to a JMS Message using the specified json view * and the supplied session to create the message object. * @param object the object to convert * @param session the Session to use for creating a JMS Message * @param jsonView the view to use to filter the content * @return the JMS Message * @throws javax.jms.JMSException if thrown by JMS API methods * @throws MessageConversionException in case of conversion failure * @since 4.3 */ public Message toMessage(Object object, Session session, @Nullable Class<?> jsonView) throws JMSException, MessageConversionException { if (jsonView != null) { return toMessage(object, session, this.objectMapper.writerWithView(jsonView)); } else { return toMessage(object, session, this.objectMapper.writer()); } }
@Test public void toTextMessageWithMap() throws Exception { converter.setTargetType(MessageType.TEXT); TextMessage textMessageMock = mock(TextMessage.class); Map<String, String> toBeMarshalled = new HashMap<>(); toBeMarshalled.put("foo", "bar"); given(sessionMock.createTextMessage(isA(String.class))).willReturn(textMessageMock); converter.toMessage(toBeMarshalled, sessionMock); verify(textMessageMock).setStringProperty("__typeid__", HashMap.class.getName()); }
@Test public void toTextMessageWithObject() throws Exception { converter.setTargetType(MessageType.TEXT); TextMessage textMessageMock = mock(TextMessage.class); Date toBeMarshalled = new Date(); given(sessionMock.createTextMessage(isA(String.class))).willReturn(textMessageMock); converter.toMessage(toBeMarshalled, sessionMock); verify(textMessageMock).setStringProperty("__typeid__", Date.class.getName()); }
private void testToTextMessageWithReturnType(MethodParameter returnType) throws JMSException, NoSuchMethodException { converter.setTargetType(MessageType.TEXT); TextMessage textMessageMock = mock(TextMessage.class); MyAnotherBean bean = new MyAnotherBean("test", "lengthy description"); given(sessionMock.createTextMessage(isA(String.class))).willReturn(textMessageMock); converter.toMessage(bean, sessionMock, returnType); verify(textMessageMock).setStringProperty("__typeid__", MyAnotherBean.class.getName()); }
@Test public void toTextMessageWithJsonViewClass() throws JMSException { converter.setTargetType(MessageType.TEXT); TextMessage textMessageMock = mock(TextMessage.class); MyAnotherBean bean = new MyAnotherBean("test", "lengthy description"); given(sessionMock.createTextMessage(isA(String.class))).willReturn(textMessageMock); converter.toMessage(bean, sessionMock, Summary.class); verify(textMessageMock).setStringProperty("__typeid__", MyAnotherBean.class.getName()); verify(sessionMock).createTextMessage("{\"name\":\"test\"}"); }
@Test public void toTextMessageWithAnotherJsonViewClass() throws JMSException { converter.setTargetType(MessageType.TEXT); TextMessage textMessageMock = mock(TextMessage.class); MyAnotherBean bean = new MyAnotherBean("test", "lengthy description"); given(sessionMock.createTextMessage(isA(String.class))).willReturn(textMessageMock); converter.toMessage(bean, sessionMock, Full.class); verify(textMessageMock).setStringProperty("__typeid__", MyAnotherBean.class.getName()); verify(sessionMock).createTextMessage("{\"name\":\"test\",\"description\":\"lengthy description\"}"); }
@Test public void toBytesMessage() throws Exception { BytesMessage bytesMessageMock = mock(BytesMessage.class); Date toBeMarshalled = new Date(); given(sessionMock.createBytesMessage()).willReturn(bytesMessageMock); converter.toMessage(toBeMarshalled, sessionMock); verify(bytesMessageMock).setStringProperty("__encoding__", "UTF-8"); verify(bytesMessageMock).setStringProperty("__typeid__", Date.class.getName()); verify(bytesMessageMock).writeBytes(isA(byte[].class)); }
@Test public void testJsonHeaderMapping() throws JMSException { MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter(); converter.setTargetType(MessageType.TEXT); converter.setTypeIdPropertyName("javatype"); Session session = Mockito.mock(Session.class); Mockito.doAnswer(invocation -> new StubTextMessage(invocation.getArgument(0))).when(session) .createTextMessage(Mockito.anyString()); javax.jms.Message request = converter.toMessage(new Foo(), session); DefaultJmsHeaderMapper mapper = new DefaultJmsHeaderMapper(); Map<String, Object> headers = mapper.toHeaders(request); javax.jms.Message reply = converter.toMessage("foo", session); mapper.fromHeaders(new MessageHeaders(headers), reply); Object result = converter.fromMessage(reply); assertThat(result, instanceOf(String.class)); }
@Override public Message toMessage(Object object, Session session, @Nullable Object conversionHint) throws JMSException, MessageConversionException { return toMessage(object, session, getSerializationView(conversionHint)); }
@Override public Message toMessage(Object object, Session session, Object conversionHint) throws JMSException, MessageConversionException { return toMessage(object, session, getSerializationView(conversionHint)); }
/** * Convert a Java object to a JMS Message using the specified json view * and the supplied session to create the message object. * @param object the object to convert * @param session the Session to use for creating a JMS Message * @param jsonView the view to use to filter the content * @return the JMS Message * @throws javax.jms.JMSException if thrown by JMS API methods * @throws MessageConversionException in case of conversion failure * @since 4.3 */ public Message toMessage(Object object, Session session, Class<?> jsonView) throws JMSException, MessageConversionException { if (jsonView != null) { return toMessage(object, session, this.objectMapper.writerWithView(jsonView)); } else { return toMessage(object, session, this.objectMapper.writer()); } }
/** * Convert a Java object to a JMS Message using the specified json view * and the supplied session to create the message object. * @param object the object to convert * @param session the Session to use for creating a JMS Message * @param jsonView the view to use to filter the content * @return the JMS Message * @throws javax.jms.JMSException if thrown by JMS API methods * @throws MessageConversionException in case of conversion failure * @since 4.3 */ public Message toMessage(Object object, Session session, @Nullable Class<?> jsonView) throws JMSException, MessageConversionException { if (jsonView != null) { return toMessage(object, session, this.objectMapper.writerWithView(jsonView)); } else { return toMessage(object, session, this.objectMapper.writer()); } }