GH-1313: Add Message CTOR with default Props

Resolves https://github.com/spring-projects/spring-amqp/issues/1313

**cherry-pick to 2.2.x**

* Fix tests; remove null test in `Message.toString()`.
This commit is contained in:
Gary Russell
2021-03-30 11:52:45 -04:00
committed by GitHub
parent 03fca1d6a3
commit 8de660be7b
2 changed files with 21 additions and 7 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -57,7 +57,23 @@ public class Message implements Serializable {
private final byte[] body;
/**
* Construct an instance with the provided body and default {@link MessageProperties}.
* @param body the body.
* @since 2.2.17
*/
public Message(byte[] body) {
this(body, new MessageProperties());
}
/**
* Construct an instance with the provided body and properties.
* @param body the body.
* @param messageProperties the properties.
*/
public Message(byte[] body, MessageProperties messageProperties) { //NOSONAR
Assert.notNull(body, "'body' cannot be null");
Assert.notNull(messageProperties, "'messageProperties' cannot be null");
this.body = body; //NOSONAR
this.messageProperties = messageProperties;
}
@@ -103,9 +119,7 @@ public class Message implements Serializable {
StringBuilder buffer = new StringBuilder();
buffer.append("(");
buffer.append("Body:'").append(this.getBodyContentAsString()).append("'");
if (this.messageProperties != null) {
buffer.append(" ").append(this.messageProperties.toString());
}
buffer.append(" ").append(this.messageProperties.toString());
buffer.append(")");
return buffer.toString();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -56,13 +56,13 @@ public class MessageTests {
@Test
public void toStringForNullMessageProperties() {
Message message = new Message(new byte[0], null);
Message message = new Message(new byte[0]);
assertThat(message.toString()).isNotNull();
}
@Test
public void toStringForNonStringMessageBody() {
Message message = new Message(SerializationUtils.serialize(new Date()), null);
Message message = new Message(SerializationUtils.serialize(new Date()));
assertThat(message.toString()).isNotNull();
}