This is an automated email from the ASF dual-hosted git repository. tabish pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/qpid-protonj2.git
The following commit(s) were added to refs/heads/main by this push: new ed5774c PROTON-2384 reset body tracking values are set to null consistently ed5774c is described below commit ed5774cf831a462788bf73e86e578328577d5ac4 Author: Timothy Bish <tabish...@gmail.com> AuthorDate: Tue May 18 14:11:26 2021 -0400 PROTON-2384 reset body tracking values are set to null consistently Fixes issue of not clearing the singular body section value when needed and updates some API docs. --- .../qpid/protonj2/client/AdvancedMessage.java | 7 +++--- .../qpid/protonj2/client/impl/ClientMessage.java | 14 +++++------- .../protonj2/client/impl/ClientMessageTest.java | 25 +++++++++++++++++++++- 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/protonj2-client/src/main/java/org/apache/qpid/protonj2/client/AdvancedMessage.java b/protonj2-client/src/main/java/org/apache/qpid/protonj2/client/AdvancedMessage.java index 78b9080..13aa103 100644 --- a/protonj2-client/src/main/java/org/apache/qpid/protonj2/client/AdvancedMessage.java +++ b/protonj2-client/src/main/java/org/apache/qpid/protonj2/client/AdvancedMessage.java @@ -232,10 +232,11 @@ public interface AdvancedMessage<E> extends Message<E> { AdvancedMessage<E> bodySections(Collection<Section<?>> sections) throws ClientException; /** - * Create and return a {@link Collection} that contains the {@link Section} instances currently - * assigned to this message. Changes to the returned Collection are not reflected in the Message. + * Create and return an unmodifiable {@link Collection} that contains the {@link Section} instances + * currently assigned to this message. Changes to this message body after calling this will not be + * reflected in the returned collection. * - * @return a {@link Collection} that is a copy of the current sections assigned to this message. + * @return an unmodifiable {@link Collection} that is a view of the current sections assigned to this message. * * @throws ClientException if an error occurs while retrieving the message data. */ diff --git a/protonj2-client/src/main/java/org/apache/qpid/protonj2/client/impl/ClientMessage.java b/protonj2-client/src/main/java/org/apache/qpid/protonj2/client/impl/ClientMessage.java index cd51185..3fece03 100644 --- a/protonj2-client/src/main/java/org/apache/qpid/protonj2/client/impl/ClientMessage.java +++ b/protonj2-client/src/main/java/org/apache/qpid/protonj2/client/impl/ClientMessage.java @@ -506,15 +506,8 @@ public class ClientMessage<E> implements AdvancedMessage<E> { @Override public ClientMessage<E> body(E value) { - if (bodySections != null) { - if (value != null) { - bodySections.set(0, ClientMessageSupport.createSectionFromValue(value)); - } else { - bodySections = null; - } - } else { - body = ClientMessageSupport.createSectionFromValue(value); - } + clearBodySections(); + body = ClientMessageSupport.createSectionFromValue(value); return this; } @@ -662,6 +655,7 @@ public class ClientMessage<E> implements AdvancedMessage<E> { public ClientMessage<E> bodySections(Collection<Section<?>> sections) { if (sections == null || sections.isEmpty()) { bodySections = null; + body = null; } else { List<Section<?>> result = new ArrayList<>(sections.size()); sections.forEach(section -> result.add(validateBodySections(messageFormat, result, section))); @@ -676,6 +670,8 @@ public class ClientMessage<E> implements AdvancedMessage<E> { public Collection<Section<?>> bodySections() { if (bodySections == null && body == null) { return Collections.EMPTY_LIST; + } else if (body != null) { + return Collections.singletonList(body); } else { final Collection<Section<?>> result = new ArrayList<>(); forEachBodySection(section -> result.add(section)); diff --git a/protonj2-client/src/test/java/org/apache/qpid/protonj2/client/impl/ClientMessageTest.java b/protonj2-client/src/test/java/org/apache/qpid/protonj2/client/impl/ClientMessageTest.java index d80b50f..e1fee22 100644 --- a/protonj2-client/src/test/java/org/apache/qpid/protonj2/client/impl/ClientMessageTest.java +++ b/protonj2-client/src/test/java/org/apache/qpid/protonj2/client/impl/ClientMessageTest.java @@ -322,6 +322,28 @@ class ClientMessageTest { } @Test + public void testSetMultipleBodySectionsWithNullClearsOldSingleBodySection() { + ClientMessage<String> message = ClientMessage.create(); + + assertNull(message.body()); + assertNotNull(message.bodySections()); + assertTrue(message.bodySections().isEmpty()); + + message.body("test"); + + assertNotNull(message.body()); + assertNotNull(message.bodySections()); + assertFalse(message.bodySections().isEmpty()); + + message.bodySections(null); + + assertNull(message.body()); + assertNotNull(message.bodySections()); + assertTrue(message.bodySections().isEmpty()); + assertEquals(0, message.bodySections().size()); + } + + @Test public void testAddMultipleBodySectionsPreservesOriginal() { ClientMessage<byte[]> message = ClientMessage.create(); @@ -398,9 +420,10 @@ class ClientMessageTest { message.addBodySection(value); } + // setting a single body value should clear any previous sections. assertEquals(expected.size(), message.bodySections().size()); message.body(new byte[] { 3 }); - assertEquals(expected.size(), message.bodySections().size()); + assertEquals(1, message.bodySections().size()); expected.set(0, new Data(new byte[] { 3 })); Iterator<?> expectations = expected.iterator(); --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@qpid.apache.org For additional commands, e-mail: commits-h...@qpid.apache.org