JAMES-2557 Propose an object wrapping a potentially missing sender This object can encapsulate behaviour like transforming such senders as string.
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/33d3ea3a Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/33d3ea3a Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/33d3ea3a Branch: refs/heads/master Commit: 33d3ea3a96449c33ad9b9a3bacf47b7a6730abd7 Parents: 4231ebf Author: Benoit Tellier <[email protected]> Authored: Thu Oct 25 11:38:32 2018 +0700 Committer: Benoit Tellier <[email protected]> Committed: Tue Oct 30 09:39:04 2018 +0700 ---------------------------------------------------------------------- .../java/org/apache/james/core/MaybeSender.java | 99 ++++++++++++ .../org/apache/james/core/MaybeSenderTest.java | 158 +++++++++++++++++++ 2 files changed, 257 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/33d3ea3a/core/src/main/java/org/apache/james/core/MaybeSender.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/james/core/MaybeSender.java b/core/src/main/java/org/apache/james/core/MaybeSender.java new file mode 100644 index 0000000..6e91fce --- /dev/null +++ b/core/src/main/java/org/apache/james/core/MaybeSender.java @@ -0,0 +1,99 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.core; + +import java.util.NoSuchElementException; +import java.util.Objects; +import java.util.Optional; +import java.util.stream.Stream; + +import com.google.common.base.MoreObjects; +import com.google.common.collect.ImmutableList; + +public class MaybeSender { + public static MaybeSender nullSender() { + return new MaybeSender(Optional.empty()); + } + + public static MaybeSender of(MailAddress mailAddress) { + return new MaybeSender(Optional.ofNullable(mailAddress) + .filter(address -> !address.isNullSender())); + } + + private final Optional<MailAddress> mailAddress; + + private MaybeSender(Optional<MailAddress> mailAddress) { + this.mailAddress = mailAddress; + } + + public Optional<MailAddress> asOptional() { + return mailAddress; + } + + public Stream<MailAddress> asStream() { + return mailAddress.map(Stream::of).orElse(Stream.of()); + } + + public ImmutableList<MailAddress> asList() { + return mailAddress.map(ImmutableList::of).orElse(ImmutableList.of()); + } + + public MailAddress get() throws NoSuchElementException { + return mailAddress.get(); + } + + public String asString() { + return asString(MailAddress.NULL_SENDER_AS_STRING); + } + + public String asPrettyString() { + return mailAddress.map(MailAddress::asPrettyString).orElse(MailAddress.NULL_SENDER_AS_STRING); + } + + public boolean isNullSender() { + return !mailAddress.isPresent(); + } + + public String asString(String forNullValue) { + return mailAddress.map(MailAddress::asString).orElse(forNullValue); + } + + @Override + public final boolean equals(Object o) { + if (o instanceof MaybeSender) { + MaybeSender that = (MaybeSender) o; + + return Objects.equals(this.mailAddress, that.mailAddress); + } + return false; + } + + @Override + public final int hashCode() { + return Objects.hash(mailAddress); + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("mailAddress", mailAddress) + .toString(); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/33d3ea3a/core/src/test/java/org/apache/james/core/MaybeSenderTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/james/core/MaybeSenderTest.java b/core/src/test/java/org/apache/james/core/MaybeSenderTest.java new file mode 100644 index 0000000..627aacd --- /dev/null +++ b/core/src/test/java/org/apache/james/core/MaybeSenderTest.java @@ -0,0 +1,158 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.core; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.util.NoSuchElementException; + +import javax.mail.internet.AddressException; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import nl.jqno.equalsverifier.EqualsVerifier; + +class MaybeSenderTest { + private static final String MAIL_ADDRESS_STRING = "[email protected]"; + + private MailAddress mailAddress; + + @BeforeEach + void setUp() throws AddressException { + mailAddress = new MailAddress(MAIL_ADDRESS_STRING); + } + + @Test + void shouldMatchBeanContract() { + EqualsVerifier.forClass(MaybeSender.class) + .verify(); + } + + @Test + void ofShouldSanitizeNull() { + assertThat(MaybeSender.of(null)) + .isEqualTo(MaybeSender.nullSender()); + } + + @Test + void ofShouldSanitizeNullSender() { + assertThat(MaybeSender.of(MailAddress.nullSender())) + .isEqualTo(MaybeSender.nullSender()); + } + + @Test + void asOptionalShouldReturnWrappedValue() { + assertThat(MaybeSender.of(mailAddress).asOptional()) + .contains(mailAddress); + } + + @Test + void asOptionalShouldReturnEmptyWhenNullSender() { + assertThat(MaybeSender.nullSender().asOptional()) + .isEmpty(); + } + + @Test + void getShouldReturnWrappedValue() { + assertThat(MaybeSender.of(mailAddress).get()) + .isEqualTo(mailAddress); + } + + @Test + void getShouldThrowWhenNullSender() { + assertThatThrownBy(() -> MaybeSender.nullSender().get()) + .isInstanceOf(NoSuchElementException.class); + } + + @Test + void asListShouldReturnWrappedValue() { + assertThat(MaybeSender.of(mailAddress).asList()) + .contains(mailAddress); + } + + @Test + void asListShouldReturnEmptyWhenNullSender() { + assertThat(MaybeSender.nullSender().asList()) + .isEmpty(); + } + + @Test + void asStreamShouldReturnWrappedValue() { + assertThat(MaybeSender.of(mailAddress).asStream()) + .contains(mailAddress); + } + + @Test + void asStreamShouldReturnEmptyWhenNullSender() { + assertThat(MaybeSender.nullSender().asStream()) + .isEmpty(); + } + + @Test + void isNullSenderShouldReturnFalseWhenNotNullSender() { + assertThat(MaybeSender.of(mailAddress).isNullSender()) + .isFalse(); + } + + @Test + void isNullSenderShouldReturnTrueWhenNullSender() { + assertThat(MaybeSender.nullSender().isNullSender()) + .isTrue(); + } + + @Test + void asStringShouldReturnWrappedValue() { + assertThat(MaybeSender.of(mailAddress).asString()) + .isEqualTo(MAIL_ADDRESS_STRING); + } + + @Test + void asStringShouldReturnDefaultWhenNullSender() { + assertThat(MaybeSender.nullSender().asString()) + .isEqualTo(MailAddress.NULL_SENDER_AS_STRING); + } + + @Test + void asStringWithDefaultShouldReturnWrappedValue() { + assertThat(MaybeSender.of(mailAddress).asString("default")) + .isEqualTo(MAIL_ADDRESS_STRING); + } + + @Test + void asPrettyStringShouldReturnDefaultWhenNullSender() { + assertThat(MaybeSender.nullSender().asPrettyString()) + .isEqualTo(MailAddress.NULL_SENDER_AS_STRING); + } + + @Test + void asPrettyStringShouldReturnWrappedValue() { + assertThat(MaybeSender.of(mailAddress).asPrettyString()) + .isEqualTo("<" + MAIL_ADDRESS_STRING + ">"); + } + + @Test + void asStringWithDefaultShouldReturnDefaultWhenNullSender() { + assertThat(MaybeSender.nullSender().asString("default")) + .isEqualTo("default"); + } + +} \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
