Skip to content

Commit 12116ad

Browse files
garyrussellartembilan
authored andcommitted
GH-1313: Add Message CTOR with default Props
Resolves #1313 **cherry-pick to 2.2.x** * Fix tests; remove null test in `Message.toString()`. (cherry picked from commit 8de660b)
1 parent 4d5cdfc commit 12116ad

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

spring-amqp/src/main/java/org/springframework/amqp/core/Message.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -57,7 +57,23 @@ public class Message implements Serializable {
5757

5858
private final byte[] body;
5959

60+
/**
61+
* Construct an instance with the provided body and default {@link MessageProperties}.
62+
* @param body the body.
63+
* @since 2.2.17
64+
*/
65+
public Message(byte[] body) {
66+
this(body, new MessageProperties());
67+
}
68+
69+
/**
70+
* Construct an instance with the provided body and properties.
71+
* @param body the body.
72+
* @param messageProperties the properties.
73+
*/
6074
public Message(byte[] body, MessageProperties messageProperties) { //NOSONAR
75+
Assert.notNull(body, "'body' cannot be null");
76+
Assert.notNull(messageProperties, "'messageProperties' cannot be null");
6177
this.body = body; //NOSONAR
6278
this.messageProperties = messageProperties;
6379
}
@@ -103,9 +119,7 @@ public String toString() {
103119
StringBuilder buffer = new StringBuilder();
104120
buffer.append("(");
105121
buffer.append("Body:'").append(this.getBodyContentAsString()).append("'");
106-
if (this.messageProperties != null) {
107-
buffer.append(" ").append(this.messageProperties.toString());
108-
}
122+
buffer.append(" ").append(this.messageProperties.toString());
109123
buffer.append(")");
110124
return buffer.toString();
111125
}

spring-amqp/src/test/java/org/springframework/amqp/core/MessageTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -56,13 +56,13 @@ public void properEncoding() {
5656

5757
@Test
5858
public void toStringForNullMessageProperties() {
59-
Message message = new Message(new byte[0], null);
59+
Message message = new Message(new byte[0]);
6060
assertThat(message.toString()).isNotNull();
6161
}
6262

6363
@Test
6464
public void toStringForNonStringMessageBody() {
65-
Message message = new Message(SerializationUtils.serialize(new Date()), null);
65+
Message message = new Message(SerializationUtils.serialize(new Date()));
6666
assertThat(message.toString()).isNotNull();
6767
}
6868

0 commit comments

Comments
 (0)