MAILET-121 Provide a tool to read emails from matcher condition Code factorization and testing between SenderIs and RecipientIs
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/1a13bf3d Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/1a13bf3d Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/1a13bf3d Branch: refs/heads/master Commit: 1a13bf3d4ef598f9ee636c7c76863a041fca4b01 Parents: d410b7e Author: Benoit Tellier <[email protected]> Authored: Wed Aug 31 14:00:11 2016 +0700 Committer: Benoit Tellier <[email protected]> Committed: Fri Sep 2 13:22:36 2016 +0700 ---------------------------------------------------------------------- .../utils/MailAddressCollectionReader.java | 53 ++++++++++++ .../util/MailAddressCollectionReaderTest.java | 85 ++++++++++++++++++++ 2 files changed, 138 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/1a13bf3d/mailet/standard/src/main/java/org/apache/james/transport/matchers/utils/MailAddressCollectionReader.java ---------------------------------------------------------------------- diff --git a/mailet/standard/src/main/java/org/apache/james/transport/matchers/utils/MailAddressCollectionReader.java b/mailet/standard/src/main/java/org/apache/james/transport/matchers/utils/MailAddressCollectionReader.java new file mode 100644 index 0000000..0c936ed --- /dev/null +++ b/mailet/standard/src/main/java/org/apache/james/transport/matchers/utils/MailAddressCollectionReader.java @@ -0,0 +1,53 @@ +/**************************************************************** + * 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.transport.matchers.utils; + +import java.util.Set; + +import javax.mail.internet.AddressException; + +import org.apache.mailet.MailAddress; + +import com.google.common.base.Function; +import com.google.common.base.Preconditions; +import com.google.common.base.Splitter; +import com.google.common.base.Strings; +import com.google.common.base.Throwables; +import com.google.common.collect.FluentIterable; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterables; + +public class MailAddressCollectionReader { + + public static Set<MailAddress> read(String condition) { + Preconditions.checkArgument(!Strings.isNullOrEmpty(condition)); + return FluentIterable.from(Splitter.on(", ").split(condition)).transform(new Function<String, MailAddress>() { + @Override + public MailAddress apply(String s) { + try { + return new MailAddress(s); + } catch (AddressException e) { + throw Throwables.propagate(e); + } + } + }).toSet(); + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/1a13bf3d/mailet/standard/src/test/java/org/apache/james/transport/matchers/util/MailAddressCollectionReaderTest.java ---------------------------------------------------------------------- diff --git a/mailet/standard/src/test/java/org/apache/james/transport/matchers/util/MailAddressCollectionReaderTest.java b/mailet/standard/src/test/java/org/apache/james/transport/matchers/util/MailAddressCollectionReaderTest.java new file mode 100644 index 0000000..116a031 --- /dev/null +++ b/mailet/standard/src/test/java/org/apache/james/transport/matchers/util/MailAddressCollectionReaderTest.java @@ -0,0 +1,85 @@ +/**************************************************************** + * 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.transport.matchers.util; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.apache.james.transport.matchers.utils.MailAddressCollectionReader; +import org.apache.mailet.MailAddress; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +public class MailAddressCollectionReaderTest { + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + @Test + public void readShouldThrowOnNullInput() { + expectedException.expect(IllegalArgumentException.class); + MailAddressCollectionReader.read(null); + } + + @Test + public void readShouldThrowOnEmptyInput() { + expectedException.expect(IllegalArgumentException.class); + MailAddressCollectionReader.read(""); + } + + @Test + public void readShouldThrowOnInvalidEmail() { + expectedException.expect(RuntimeException.class); + MailAddressCollectionReader.read("not_valid"); + } + + @Test + public void readShouldThrowOnInvalidEmailOnSecondPosition() { + expectedException.expect(RuntimeException.class); + MailAddressCollectionReader.read("[email protected], not_valid"); + } + + @Test + public void readShouldParseOneEmail() throws Exception { + MailAddress mailAddress = new MailAddress("[email protected]"); + + assertThat(MailAddressCollectionReader.read(mailAddress.toString())) + .containsExactly(mailAddress); + } + + @Test + public void readShouldParseTwoEmail() throws Exception { + MailAddress mailAddress1 = new MailAddress("[email protected]"); + MailAddress mailAddress2 = new MailAddress("[email protected]"); + + assertThat(MailAddressCollectionReader.read(mailAddress1.toString() + ", " + mailAddress2.toString())) + .containsExactly(mailAddress1, mailAddress2); + } + + @Test + public void readShouldRemoveDuplicates() throws Exception { + MailAddress mailAddress = new MailAddress("[email protected]"); + + assertThat(MailAddressCollectionReader.read(mailAddress.toString() + ", " + mailAddress.toString())) + .containsExactly(mailAddress); + } + + +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
