This is an automated email from the ASF dual-hosted git repository. btellier pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
The following commit(s) were added to refs/heads/master by this push: new 3ba7c42399 JAMES-3922: Enforce non-null Property contract and add missing null-check in Prope (#1619) 3ba7c42399 is described below commit 3ba7c423991b0d47a8cc38cb902bca14a38b36c1 Author: Wojtek <woj-...@users.noreply.github.com> AuthorDate: Fri Jun 30 04:22:40 2023 -0400 JAMES-3922: Enforce non-null Property contract and add missing null-check in Prope (#1619) --- .../james/mailbox/store/mail/model/Property.java | 10 ++++++---- .../mailbox/store/mail/model/impl/PropertyBuilder.java | 8 ++++++-- .../store/mail/model/impl/PropertyBuilderTest.java | 18 ++++++++++++++++++ 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/model/Property.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/model/Property.java index 2e0e7a3ce1..350a17ef7d 100644 --- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/model/Property.java +++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/model/Property.java @@ -22,6 +22,8 @@ import java.util.Objects; import com.google.common.base.MoreObjects; +import reactor.util.annotation.NonNull; + /** * <p>Values a namespaced property.</p> * <p> @@ -51,11 +53,11 @@ public class Property { * @param localName not null * @param value not null */ - public Property(String namespace, String localName, String value) { + public Property(@NonNull String namespace, @NonNull String localName, @NonNull String value) { super(); - this.namespace = namespace; - this.localName = localName; - this.value = value; + this.namespace = Objects.requireNonNull(namespace); + this.localName = Objects.requireNonNull(localName); + this.value = Objects.requireNonNull(value); } public Property(Property property) { diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/model/impl/PropertyBuilder.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/model/impl/PropertyBuilder.java index 74f5409822..4356add478 100644 --- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/model/impl/PropertyBuilder.java +++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/model/impl/PropertyBuilder.java @@ -111,7 +111,9 @@ public class PropertyBuilder { properties.removeIf(property -> property.isNamed(namespace, localName)); if (values != null) { for (String value:values) { - properties.add(new Property(namespace, localName, value)); + if (value != null) { + properties.add(new Property(namespace, localName, value)); + } } } } @@ -126,7 +128,9 @@ public class PropertyBuilder { private void setProperties(String namespace, Map<String,String> valuesByLocalName) { properties.removeIf(property -> property.isInSpace(namespace)); for (Map.Entry<String, String> valueByLocalName:valuesByLocalName.entrySet()) { - properties.add(new Property(namespace, valueByLocalName.getKey().toLowerCase(Locale.US), valueByLocalName.getValue())); + if (valueByLocalName.getValue() != null) { + properties.add(new Property(namespace, valueByLocalName.getKey().toLowerCase(Locale.US), valueByLocalName.getValue())); + } } } diff --git a/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/impl/PropertyBuilderTest.java b/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/impl/PropertyBuilderTest.java index 1e10873a07..20054f7b68 100644 --- a/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/impl/PropertyBuilderTest.java +++ b/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/impl/PropertyBuilderTest.java @@ -26,12 +26,30 @@ import static org.assertj.core.api.Assertions.assertThat; import org.apache.james.mailbox.store.mail.model.Property; import org.junit.jupiter.api.Test; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Map; + class PropertyBuilderTest { @Test void emptyPropertyBuilderShouldCreateEmptyProperties() { assertThat(new PropertyBuilder().toProperties()).isEmpty(); } + @Test + void nullValuePropertyBuilderShouldCreateEmptyProperties() { + List<String> listOfNulls = Arrays.asList(null, null, null); + Map <String,String> mapWithNullValues = Collections.singletonMap("k1", null); + + PropertyBuilder builder = new PropertyBuilder(); + builder.setContentLanguage(listOfNulls); + builder.setContentTypeParameters(mapWithNullValues); + builder.setCharset(null); + + assertThat(builder.toProperties()).isEmpty(); + } + @Test void setContentMD5ShouldAddMd5Property() { PropertyBuilder propertyBuilder = new PropertyBuilder(); --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@james.apache.org For additional commands, e-mail: notifications-h...@james.apache.org