Search in sources :

Example 1 with BytesMessage

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());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Message(jakarta.jms.Message) SimpleMessageConverter(org.springframework.jms.support.converter.SimpleMessageConverter) BDDMockito.willThrow(org.mockito.BDDMockito.willThrow) BytesMessage(jakarta.jms.BytesMessage) MessageProducer(jakarta.jms.MessageProducer) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) QueueSender(jakarta.jms.QueueSender) JMSException(jakarta.jms.JMSException) QueueSession(jakarta.jms.QueueSession) Queue(jakarta.jms.Queue) Session(jakarta.jms.Session) Serializable(java.io.Serializable) Mockito.verify(org.mockito.Mockito.verify) InvalidDestinationException(jakarta.jms.InvalidDestinationException) Test(org.junit.jupiter.api.Test) Answer(org.mockito.stubbing.Answer) ByteArrayInputStream(java.io.ByteArrayInputStream) BDDMockito.given(org.mockito.BDDMockito.given) TextMessage(jakarta.jms.TextMessage) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) MessageConversionException(org.springframework.jms.support.converter.MessageConversionException) ObjectMessage(jakarta.jms.ObjectMessage) Mockito.mock(org.mockito.Mockito.mock) ByteArrayInputStream(java.io.ByteArrayInputStream) BytesMessage(jakarta.jms.BytesMessage) Test(org.junit.jupiter.api.Test)

Example 2 with BytesMessage

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);
}
Also used : Message(jakarta.jms.Message) BytesMessage(jakarta.jms.BytesMessage) TextMessage(jakarta.jms.TextMessage) MapMessage(jakarta.jms.MapMessage) ObjectMessage(jakarta.jms.ObjectMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) BytesMessage(jakarta.jms.BytesMessage) SimpleMessageConverter(org.springframework.jms.support.converter.SimpleMessageConverter) Session(jakarta.jms.Session) Test(org.junit.jupiter.api.Test)

Example 3 with BytesMessage

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;
}
Also used : BytesMessage(jakarta.jms.BytesMessage)

Example 4 with BytesMessage

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));
}
Also used : BytesMessage(jakarta.jms.BytesMessage) Result(javax.xml.transform.Result) Test(org.junit.jupiter.api.Test)

Example 5 with BytesMessage

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);
}
Also used : BytesMessage(jakarta.jms.BytesMessage) Source(javax.xml.transform.Source) Test(org.junit.jupiter.api.Test)

Aggregations

BytesMessage (jakarta.jms.BytesMessage)7 Test (org.junit.jupiter.api.Test)6 Session (jakarta.jms.Session)3 TextMessage (jakarta.jms.TextMessage)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 JMSException (jakarta.jms.JMSException)2 Message (jakarta.jms.Message)2 ObjectMessage (jakarta.jms.ObjectMessage)2 Date (java.util.Date)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)2 BDDMockito.given (org.mockito.BDDMockito.given)2 Mockito.mock (org.mockito.Mockito.mock)2 Mockito.verify (org.mockito.Mockito.verify)2 Answer (org.mockito.stubbing.Answer)2 SimpleMessageConverter (org.springframework.jms.support.converter.SimpleMessageConverter)2 JsonView (com.fasterxml.jackson.annotation.JsonView)1 InvalidDestinationException (jakarta.jms.InvalidDestinationException)1 MapMessage (jakarta.jms.MapMessage)1 MessageProducer (jakarta.jms.MessageProducer)1