JAMES-2578 Do not allow nulls
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/c1f4ba5c Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/c1f4ba5c Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/c1f4ba5c Branch: refs/heads/master Commit: c1f4ba5cda6c31fb6d22c351dce9c868dc67d64e Parents: dbee6f7 Author: Gautier DI FOLCO <gdifo...@linagora.com> Authored: Mon Oct 29 14:44:29 2018 +0100 Committer: Benoit Tellier <btell...@linagora.com> Committed: Thu Nov 1 11:02:43 2018 +0700 ---------------------------------------------------------------------- .../java/org/apache/mailet/AttributeValue.java | 12 +++ .../org/apache/mailet/AttributeValueTest.java | 107 ++++++------------- 2 files changed, 43 insertions(+), 76 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/c1f4ba5c/mailet/api/src/main/java/org/apache/mailet/AttributeValue.java ---------------------------------------------------------------------- diff --git a/mailet/api/src/main/java/org/apache/mailet/AttributeValue.java b/mailet/api/src/main/java/org/apache/mailet/AttributeValue.java index 4a548b3..6bf3a6e 100644 --- a/mailet/api/src/main/java/org/apache/mailet/AttributeValue.java +++ b/mailet/api/src/main/java/org/apache/mailet/AttributeValue.java @@ -36,6 +36,7 @@ import com.fasterxml.jackson.databind.node.JsonNodeFactory; import com.fasterxml.jackson.databind.node.ObjectNode; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.MoreObjects; +import com.google.common.base.Preconditions; /** * Strong typing for attribute value, which represents the value of an attribute stored in a mail. @@ -46,44 +47,54 @@ public class AttributeValue<T> { private static final Logger LOGGER = LoggerFactory.getLogger(AttributeValue.class); public static AttributeValue<Boolean> of(Boolean value) { + Preconditions.checkNotNull(value, "value should not be null"); return new AttributeValue<>(value, Serializer.BOOLEAN_SERIALIZER); } public static AttributeValue<String> of(String value) { + Preconditions.checkNotNull(value, "value should not be null"); return new AttributeValue<>(value, Serializer.STRING_SERIALIZER); } public static AttributeValue<Integer> of(Integer value) { + Preconditions.checkNotNull(value, "value should not be null"); return new AttributeValue<>(value, Serializer.INT_SERIALIZER); } public static AttributeValue<Long> of(Long value) { + Preconditions.checkNotNull(value, "value should not be null"); return new AttributeValue<>(value, Serializer.LONG_SERIALIZER); } public static AttributeValue<Float> of(Float value) { + Preconditions.checkNotNull(value, "value should not be null"); return new AttributeValue<>(value, Serializer.FLOAT_SERIALIZER); } public static AttributeValue<Double> of(Double value) { + Preconditions.checkNotNull(value, "value should not be null"); return new AttributeValue<>(value, Serializer.DOUBLE_SERIALIZER); } public static <T extends ArbitrarySerializable<T>> AttributeValue<T> of(T value) { + Preconditions.checkNotNull(value, "value should not be null"); return new AttributeValue<>(value, new Serializer.ArbitrarySerializableSerializer<>()); } public static AttributeValue<URL> of(URL value) { + Preconditions.checkNotNull(value, "value should not be null"); return new AttributeValue<>(value, Serializer.URL_SERIALIZER); } @SuppressWarnings({ "unchecked", "rawtypes" }) public static AttributeValue<Collection<AttributeValue<?>>> of(Collection<AttributeValue<?>> value) { + Preconditions.checkNotNull(value, "value should not be null"); return new AttributeValue<>(value, new Serializer.CollectionSerializer()); } @SuppressWarnings({ "unchecked", "rawtypes" }) public static AttributeValue<Map<String, AttributeValue<?>>> of(Map<String, AttributeValue<?>> value) { + Preconditions.checkNotNull(value, "value should not be null"); return new AttributeValue<>(value, new Serializer.MapSerializer()); } @@ -93,6 +104,7 @@ public class AttributeValue<T> { @SuppressWarnings("unchecked") public static AttributeValue<?> ofAny(Object value) { + Preconditions.checkNotNull(value, "value should not be null"); if (value instanceof Boolean) { return of((Boolean) value); } http://git-wip-us.apache.org/repos/asf/james-project/blob/c1f4ba5c/mailet/api/src/test/java/org/apache/mailet/AttributeValueTest.java ---------------------------------------------------------------------- diff --git a/mailet/api/src/test/java/org/apache/mailet/AttributeValueTest.java b/mailet/api/src/test/java/org/apache/mailet/AttributeValueTest.java index 45cfede..dd3f43f 100644 --- a/mailet/api/src/test/java/org/apache/mailet/AttributeValueTest.java +++ b/mailet/api/src/test/java/org/apache/mailet/AttributeValueTest.java @@ -20,6 +20,7 @@ package org.apache.mailet; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatIllegalStateException; +import static org.assertj.core.api.Assertions.assertThatNullPointerException; import static org.assertj.core.api.Assertions.assertThatThrownBy; import java.net.MalformedURLException; @@ -29,7 +30,6 @@ import java.util.Map; import java.util.Objects; import java.util.Optional; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; @@ -47,10 +47,9 @@ class AttributeValueTest { } @Test - void ofShouldAcceptNullValue() { - AttributeValue<String> attributeValue = AttributeValue.of((String) null); - - assertThat(attributeValue.getValue()).isNull(); + void ofShouldThrowAnExceptionOnNullValue() { + assertThatNullPointerException(). + isThrownBy(() -> AttributeValue.of((String) null)); } @Nested @@ -75,15 +74,10 @@ class AttributeValueTest { assertThat(actual).isEqualTo(expected); } - @Disabled("Failing!") @Test - void nullStringShouldBeSerializedAndBack() { - AttributeValue<String> expected = AttributeValue.of((String) null); - - JsonNode json = expected.toJson(); - AttributeValue<?> actual = AttributeValue.fromJson(json); - - assertThat(actual).isEqualTo(expected); + void nullStringShouldThrowAnException() { + assertThatNullPointerException(). + isThrownBy(() -> AttributeValue.of((String) null)); } @Test @@ -124,15 +118,10 @@ class AttributeValueTest { assertThat(actual).isEqualTo(expected); } - @Disabled("Failing") @Test - void nullBooleanShouldBeSerializedAndBack() { - AttributeValue<Boolean> expected = AttributeValue.of((Boolean) null); - - JsonNode json = expected.toJson(); - AttributeValue<?> actual = AttributeValue.fromJson(json); - - assertThat(actual).isEqualTo(expected); + void nullBooleanShouldThrowAnException() { + assertThatNullPointerException(). + isThrownBy(() -> AttributeValue.of((Boolean) null)); } @Test @@ -163,15 +152,10 @@ class AttributeValueTest { assertThat(actual).isEqualTo(expected); } - @Disabled("Failing") @Test - void nullIntShouldBeSerializedAndBack() { - AttributeValue<Integer> expected = AttributeValue.of((Integer) null); - - JsonNode json = expected.toJson(); - AttributeValue<?> actual = AttributeValue.fromJson(json); - - assertThat(actual).isEqualTo(expected); + void nullIntShouldThrowAnException() { + assertThatNullPointerException(). + isThrownBy(() -> AttributeValue.of((Integer) null)); } @Test @@ -202,15 +186,10 @@ class AttributeValueTest { assertThat(actual).isEqualTo(expected); } - @Disabled("Failing") @Test - void nullLongShouldBeSerializedAndBack() { - AttributeValue<Long> expected = AttributeValue.of((Long) null); - - JsonNode json = expected.toJson(); - AttributeValue<?> actual = AttributeValue.fromJson(json); - - assertThat(actual).isEqualTo(expected); + void nullLongShouldThrowAnException() { + assertThatNullPointerException(). + isThrownBy(() -> AttributeValue.of((Long) null)); } @Test @@ -241,15 +220,10 @@ class AttributeValueTest { assertThat(actual).isEqualTo(expected); } - @Disabled("Failing") @Test - void nullFloatShouldBeSerializedAndBack() { - AttributeValue<Float> expected = AttributeValue.of((Float) null); - - JsonNode json = expected.toJson(); - AttributeValue<?> actual = AttributeValue.fromJson(json); - - assertThat(actual).isEqualTo(expected); + void nullFloatShouldThrowAnException() { + assertThatNullPointerException(). + isThrownBy(() -> AttributeValue.of((Float) null)); } @Test @@ -280,15 +254,10 @@ class AttributeValueTest { assertThat(actual).isEqualTo(expected); } - @Disabled("Failing") @Test - void nullDoubleShouldBeSerializedAndBack() { - AttributeValue<Double> expected = AttributeValue.of((Double) null); - - JsonNode json = expected.toJson(); - AttributeValue<?> actual = AttributeValue.fromJson(json); - - assertThat(actual).isEqualTo(expected); + void nullDoubleShouldThrowAnException() { + assertThatNullPointerException(). + isThrownBy(() -> AttributeValue.of((Double) null)); } @Test @@ -342,15 +311,10 @@ class AttributeValueTest { assertThat(actual).isEqualTo(expected); } - @Disabled("Failing!") @Test - void nullURLShouldBeSerializedAndBack() { - AttributeValue<URL> expected = AttributeValue.of((URL) null); - - JsonNode json = expected.toJson(); - AttributeValue<?> actual = AttributeValue.fromJson(json); - - assertThat(actual).isEqualTo(expected); + void nullURLShouldThrowAnException() { + assertThatNullPointerException(). + isThrownBy(() -> AttributeValue.of((URL) null)); } @Test @@ -377,15 +341,10 @@ class AttributeValueTest { @Nested class ListSerialization { - @Disabled("Failing!") @Test - void nullStringListShouldBeSerializedAndBack() { - AttributeValue<?> expected = AttributeValue.ofAny((List<String>) null); - - JsonNode json = expected.toJson(); - AttributeValue<?> actual = AttributeValue.fromJson(json); - - assertThat(actual).isEqualTo(expected); + void nullStringListShouldThrowAnException() { + assertThatNullPointerException(). + isThrownBy(() -> AttributeValue.ofAny((List<String>) null)); } @Test @@ -435,14 +394,10 @@ class AttributeValueTest { @Nested class MapSerialization { - @Disabled("Failing!") @Test - void nullMapShouldBeSerializedAndBack() { - AttributeValue<?> expected = AttributeValue.of((Map) null); - JsonNode json = expected.toJson(); - AttributeValue<?> actual = AttributeValue.fromJson(json); - - assertThat(actual).isEqualTo(expected); + void nullMapShouldThrowAnException() { + assertThatNullPointerException(). + isThrownBy(() -> AttributeValue.of((Map) null)); } @Test --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org