use of jakarta.jms.BytesMessage in project spring-framework by spring-projects.
the class MessageListenerAdapterTests method testWithMessageContentsDelegateForBytesMessage.
@Test
void testWithMessageContentsDelegateForBytesMessage() throws Exception {
BytesMessage bytesMessage = mock(BytesMessage.class);
// BytesMessage contents must be unwrapped...
given(bytesMessage.getBodyLength()).willReturn(Long.valueOf(TEXT.getBytes().length));
given(bytesMessage.readBytes(any(byte[].class))).willAnswer((Answer<Integer>) invocation -> {
byte[] bytes = (byte[]) invocation.getArguments()[0];
ByteArrayInputStream inputStream = new ByteArrayInputStream(TEXT.getBytes());
return inputStream.read(bytes);
});
MessageContentsDelegate delegate = mock(MessageContentsDelegate.class);
MessageListenerAdapter adapter = new MessageListenerAdapter(delegate);
adapter.onMessage(bytesMessage);
verify(delegate).handleMessage(TEXT.getBytes());
}
use of jakarta.jms.BytesMessage in project spring-framework by spring-projects.
the class SimpleMessageConverterTests method testByteArrayConversion.
@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(invocation -> byteArrayInputStream.read((byte[]) invocation.getArguments()[0]));
SimpleMessageConverter converter = new SimpleMessageConverter();
Message msg = converter.toMessage(content, session);
assertThat(((byte[]) converter.fromMessage(msg)).length).isEqualTo(content.length);
verify(message).writeBytes(content);
}
use of jakarta.jms.BytesMessage in project spring-framework by spring-projects.
the class SimpleMessageConverter method createMessageForByteArray.
/**
* Create a JMS BytesMessage for the given byte array.
* @param bytes the byte array to convert
* @param session current JMS session
* @return the resulting message
* @throws JMSException if thrown by JMS methods
* @see jakarta.jms.Session#createBytesMessage
*/
protected BytesMessage createMessageForByteArray(byte[] bytes, Session session) throws JMSException {
BytesMessage message = session.createBytesMessage();
message.writeBytes(bytes);
return message;
}
use of jakarta.jms.BytesMessage in project spring-framework by spring-projects.
the class MarshallingMessageConverterTests method toBytesMessage.
@Test
public void toBytesMessage() throws Exception {
BytesMessage bytesMessageMock = mock(BytesMessage.class);
Object toBeMarshalled = new Object();
given(sessionMock.createBytesMessage()).willReturn(bytesMessageMock);
converter.toMessage(toBeMarshalled, sessionMock);
verify(marshallerMock).marshal(eq(toBeMarshalled), isA(Result.class));
verify(bytesMessageMock).writeBytes(isA(byte[].class));
}
use of jakarta.jms.BytesMessage in project spring-framework by spring-projects.
the class MarshallingMessageConverterTests method fromBytesMessage.
@Test
public void fromBytesMessage() throws Exception {
BytesMessage bytesMessageMock = mock(BytesMessage.class);
Object unmarshalled = new Object();
given(bytesMessageMock.getBodyLength()).willReturn(10L);
given(bytesMessageMock.readBytes(isA(byte[].class))).willReturn(0);
given(unmarshallerMock.unmarshal(isA(Source.class))).willReturn(unmarshalled);
Object result = converter.fromMessage(bytesMessageMock);
assertThat(unmarshalled).as("Invalid result").isEqualTo(result);
}
Aggregations