You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by ro...@apache.org on 2017/01/11 09:26:06 UTC

[18/50] [abbrv] james-project git commit: MAILET-115 Introduce mail address utility to transform InternetAddresses/MailAddresses

MAILET-115 Introduce mail address utility to transform InternetAddresses/MailAddresses


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/3a3c9251
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/3a3c9251
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/3a3c9251

Branch: refs/heads/master
Commit: 3a3c92512e0f4fc9e145c36f482c0211541a2de1
Parents: b94bc28
Author: Antoine Duprat <ad...@apache.org>
Authored: Wed Nov 9 16:26:04 2016 +0100
Committer: Benoit Tellier <bt...@linagora.com>
Committed: Wed Jan 11 10:03:29 2017 +0700

----------------------------------------------------------------------
 .../james/transport/util/MailAddressUtils.java  |  66 ++++++++++++
 .../transport/util/MailAddressUtilsTest.java    | 101 +++++++++++++++++++
 2 files changed, 167 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/3a3c9251/server/mailet/mailets/src/main/java/org/apache/james/transport/util/MailAddressUtils.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/util/MailAddressUtils.java b/server/mailet/mailets/src/main/java/org/apache/james/transport/util/MailAddressUtils.java
new file mode 100644
index 0000000..cbd529b
--- /dev/null
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/util/MailAddressUtils.java
@@ -0,0 +1,66 @@
+/****************************************************************
+ * 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.util;
+
+import java.util.List;
+
+import javax.mail.internet.AddressException;
+import javax.mail.internet.InternetAddress;
+
+import org.apache.mailet.MailAddress;
+
+import com.google.common.base.Function;
+import com.google.common.collect.FluentIterable;
+import com.google.common.collect.ImmutableList;
+
+public class MailAddressUtils {
+
+    public static List<MailAddress> from(InternetAddress[] internetAddresses) throws AddressException {
+        return from(ImmutableList.copyOf(internetAddresses));
+    }
+
+    private static List<MailAddress> from(List<InternetAddress> internetAddresses) throws AddressException {
+        ImmutableList.Builder<MailAddress> builder = ImmutableList.builder();
+        for (InternetAddress internetAddress : internetAddresses) {
+            builder.add(new MailAddress(internetAddress));
+        }
+        return builder.build();
+    }
+
+    public static List<InternetAddress> toInternetAddresses(List<MailAddress> mailAddresses) {
+        return iterableOfInternetAddress(mailAddresses)
+            .toList();
+    }
+
+    public static InternetAddress[] toInternetAddressArray(List<MailAddress> mailAddresses) {
+        return iterableOfInternetAddress(mailAddresses)
+            .toArray(InternetAddress.class);
+    }
+
+    private static FluentIterable<InternetAddress> iterableOfInternetAddress(List<MailAddress> mailAddresses) {
+        return FluentIterable.from(mailAddresses)
+            .transform(new Function<MailAddress, InternetAddress>() {
+
+                @Override
+                public InternetAddress apply(MailAddress mailAddress) {
+                    return mailAddress.toInternetAddress();
+                }
+            });
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/3a3c9251/server/mailet/mailets/src/test/java/org/apache/james/transport/util/MailAddressUtilsTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/util/MailAddressUtilsTest.java b/server/mailet/mailets/src/test/java/org/apache/james/transport/util/MailAddressUtilsTest.java
new file mode 100644
index 0000000..820e1d3
--- /dev/null
+++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/util/MailAddressUtilsTest.java
@@ -0,0 +1,101 @@
+/****************************************************************
+ * 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.util;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.util.List;
+
+import javax.mail.internet.InternetAddress;
+
+import org.apache.mailet.MailAddress;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import com.google.common.collect.ImmutableList;
+
+public class MailAddressUtilsTest {
+
+    @Rule
+    public ExpectedException expectedException = ExpectedException.none();
+
+    @Test
+    public void fromShouldThrowWhenInternetAddressesIsNull() throws Exception {
+        expectedException.expect(NullPointerException.class);
+        MailAddressUtils.from(null);
+    }
+
+    @Test
+    public void fromShouldReturnOneMailAddressWhenOneInternetAddresse() throws Exception {
+        List<MailAddress> mailAddresses = MailAddressUtils.from(InternetAddress.parse("user@james.org"));
+
+        MailAddress expectedMailAddress = new MailAddress("user", "james.org");
+        assertThat(mailAddresses).containsOnly(expectedMailAddress);
+    }
+
+    @Test
+    public void fromShouldReturnMailAddressesWhenInternetAddresses() throws Exception {
+        List<MailAddress> mailAddresses = MailAddressUtils.from(InternetAddress.parse("user@james.org, user2@apache.org"));
+
+        MailAddress expectedMailAddress = new MailAddress("user", "james.org");
+        MailAddress expectedMailAddress2 = new MailAddress("user2", "apache.org");
+        assertThat(mailAddresses).containsOnly(expectedMailAddress, expectedMailAddress2);
+    }
+
+    @Test
+    public void toInternetAddressArrayShouldThrowWhenMailAddressesIsNull() {
+        expectedException.expect(NullPointerException.class);
+        MailAddressUtils.toInternetAddressArray(null);
+    }
+
+    @Test
+    public void toInternetAddressArrayShouldReturnOneInternetAddressWhenOneMailAddress() throws Exception {
+        InternetAddress[] internetAddresses = MailAddressUtils.toInternetAddressArray(ImmutableList.of(new MailAddress("user", "james.org")));
+
+        assertThat(internetAddresses).containsOnly(new InternetAddress("user@james.org"));
+    }
+
+    @Test
+    public void toInternetAddressArrayShouldReturnInternetAddressesWhenMailAddresses() throws Exception {
+        InternetAddress[] internetAddresses = MailAddressUtils.toInternetAddressArray(ImmutableList.of(new MailAddress("user", "james.org"), new MailAddress("user2", "apache.org")));
+
+        assertThat(internetAddresses).containsOnly(new InternetAddress("user@james.org"), new InternetAddress("user2@apache.org"));
+    }
+
+    @Test
+    public void toInternetAddressesShouldThrowWhenMailAddressesIsNull() {
+        expectedException.expect(NullPointerException.class);
+        MailAddressUtils.toInternetAddresses(null);
+    }
+
+    @Test
+    public void toInternetAddressesShouldReturnOneInternetAddressWhenOneMailAddress() throws Exception {
+        List<InternetAddress> internetAddresses = MailAddressUtils.toInternetAddresses(ImmutableList.of(new MailAddress("user", "james.org")));
+
+        assertThat(internetAddresses).containsOnly(new InternetAddress("user@james.org"));
+    }
+
+    @Test
+    public void toInternetAddressesShouldReturnInternetAddressesWhenMailAddresses() throws Exception {
+        List<InternetAddress> internetAddresses = MailAddressUtils.toInternetAddresses(ImmutableList.of(new MailAddress("user", "james.org"), new MailAddress("user2", "apache.org")));
+
+        assertThat(internetAddresses).containsOnly(new InternetAddress("user@james.org"), new InternetAddress("user2@apache.org"));
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org