This is an automated email from the ASF dual-hosted git repository.

rcordier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit c171f206ac367ddd47be58463160d3bb56b7386c
Author: Tran Tien Duc <[email protected]>
AuthorDate: Tue Feb 4 14:03:04 2020 +0700

    JAMES-3034 Switch to use James SMTPMessageSender
---
 .../apache/james/MailsShouldBeWellReceived.java    | 86 ++++++++++------------
 .../eml/mail-containing-unicode-characters.eml     | 28 +++++++
 2 files changed, 68 insertions(+), 46 deletions(-)

diff --git 
a/server/container/guice/guice-common/src/test/java/org/apache/james/MailsShouldBeWellReceived.java
 
b/server/container/guice/guice-common/src/test/java/org/apache/james/MailsShouldBeWellReceived.java
index 3fa2d00..ff443ab 100644
--- 
a/server/container/guice/guice-common/src/test/java/org/apache/james/MailsShouldBeWellReceived.java
+++ 
b/server/container/guice/guice-common/src/test/java/org/apache/james/MailsShouldBeWellReceived.java
@@ -22,33 +22,33 @@ package org.apache.james;
 import static org.assertj.core.api.Assertions.assertThat;
 
 import java.io.IOException;
+import java.io.InputStream;
 import java.nio.charset.StandardCharsets;
 import java.util.Properties;
 import java.util.UUID;
 
-import javax.mail.Authenticator;
 import javax.mail.Flags;
 import javax.mail.Folder;
 import javax.mail.Message;
 import javax.mail.MessagingException;
-import javax.mail.PasswordAuthentication;
 import javax.mail.Session;
 import javax.mail.Store;
-import javax.mail.Transport;
-import javax.mail.internet.InternetAddress;
 import javax.mail.internet.MimeMessage;
 import javax.mail.search.FlagTerm;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.james.core.Domain;
 import org.apache.james.mailbox.DefaultMailboxes;
 import org.apache.james.modules.MailboxProbeImpl;
 import org.apache.james.modules.protocols.ImapGuiceProbe;
 import org.apache.james.modules.protocols.SmtpGuiceProbe;
+import org.apache.james.util.MimeMessageUtil;
 import org.apache.james.util.Port;
 import org.apache.james.utils.DataProbeImpl;
 import org.apache.james.utils.IMAPMessageReader;
 import org.apache.james.utils.SMTPMessageSender;
 import org.apache.james.utils.SpoolerProbe;
+import org.apache.mailet.base.test.FakeMail;
 import org.awaitility.Awaitility;
 import org.awaitility.Duration;
 import org.awaitility.core.ConditionFactory;
@@ -73,49 +73,29 @@ interface MailsShouldBeWellReceived {
 
     ConditionFactory CALMLY_AWAIT_FIVE_MINUTE = 
CALMLY_AWAIT.timeout(Duration.FIVE_MINUTES);
     String SENDER = "[email protected]";
-    String UNICODE_BODY = "unicode character 'Ð'";
+    String UNICODE_BODY = "Unicode €uro symbol.";
 
 
-    static Message readFirstMessageJavax(int imapPort) throws 
MessagingException {
+    static String readFirstMessageJavax(int imapPort) throws Exception {
         Session imapSession = Session.getDefaultInstance(new Properties());
-        Store store = imapSession.getStore("imap");
-        store.connect("localhost", imapPort, JAMES_USER, PASSWORD);
-        Folder inbox = store.getFolder(IMAPMessageReader.INBOX);
-        inbox.open(Folder.READ_ONLY);
-
-        CALMLY_AWAIT.untilAsserted(() ->
-            assertThat(searchForUnSeen(inbox))
-                .hasSize(1));
-
-        return searchForUnSeen(inbox)[0];
-    }
-
-    static Message[] searchForUnSeen(Folder inbox) throws MessagingException {
-        return inbox.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false));
-    }
-
-    static void sendMessageJavax(GuiceJamesServer server) throws 
MessagingException {
-        Port smtpPort = server.getProbe(SmtpGuiceProbe.class).getSmtpPort();
-
-        Properties props = new Properties();
-        props.put("mail.smtp.host", "localhost");
-        props.put("mail.smtp.port", smtpPort.getValue() + "");
-        props.put("mail.smtp.auth", "true");
-        props.put("mail.smtp.allow8bitmime", "true"); //force to use 
8bit(UTF-8), otherwise, it automatically converts into 7bit
-
-        Authenticator auth = new Authenticator() {
-            protected PasswordAuthentication getPasswordAuthentication() {
-                return new PasswordAuthentication(SENDER, PASSWORD);
+        try (Store store = imapSession.getStore("imap")) {
+            store.connect("localhost", imapPort, JAMES_USER, PASSWORD);
+            Folder inbox = store.getFolder(IMAPMessageReader.INBOX);
+            inbox.open(Folder.READ_ONLY);
+
+            CALMLY_AWAIT.untilAsserted(() ->
+                assertThat(searchForAll(inbox))
+                    .hasSize(1));
+
+            try (InputStream inputStream = 
searchForAll(inbox)[0].getInputStream()) {
+                return MimeMessageUtil.asString(
+                    MimeMessageUtil.mimeMessageFromStream(inputStream));
             }
-        };
-
-        Session session = Session.getInstance(props, auth);
-        Message message = new MimeMessage(session);
-        message.setFrom(new InternetAddress(SENDER));
-        message.setRecipients(Message.RecipientType.TO, 
InternetAddress.parse(JAMES_USER));
-        message.setText(UNICODE_BODY);
+        }
+    }
 
-        Transport.send(message);
+    static Message[] searchForAll(Folder inbox) throws MessagingException {
+        return inbox.search(new FlagTerm(new Flags(), false));
     }
 
     @Test
@@ -127,7 +107,20 @@ interface MailsShouldBeWellReceived {
         MailboxProbeImpl mailboxProbe = 
server.getProbe(MailboxProbeImpl.class);
         mailboxProbe.createMailbox("#private", JAMES_USER, 
DefaultMailboxes.INBOX);
 
-        sendMessageJavax(server);
+        Port smtpPort = server.getProbe(SmtpGuiceProbe.class).getSmtpPort();
+        try (SMTPMessageSender sender = new 
SMTPMessageSender(Domain.LOCALHOST.asString())) {
+            sender.connect(JAMES_SERVER_HOST, smtpPort);
+            MimeMessage mimeMessage = MimeMessageUtil.mimeMessageFromStream(
+                
ClassLoader.getSystemResourceAsStream("eml/mail-containing-unicode-characters.eml"));
+
+            FakeMail.Builder mail = FakeMail.builder()
+                .name("test-unicode-body")
+                .sender(SENDER)
+                .recipient(JAMES_USER)
+                .mimeMessage(mimeMessage);
+
+            sender.sendMessage(mail);
+        }
 
         CALMLY_AWAIT.until(() -> 
server.getProbe(SpoolerProbe.class).processingFinished());
 
@@ -135,10 +128,11 @@ interface MailsShouldBeWellReceived {
             int imapPort = server.getProbe(ImapGuiceProbe.class).getImapPort();
             reader.connect(JAMES_SERVER_HOST, imapPort)
                 .login(JAMES_USER, PASSWORD)
-                .select(IMAPMessageReader.INBOX);
+                .select(IMAPMessageReader.INBOX)
+                .awaitMessageCount(CALMLY_AWAIT, 1);
 
-            assertThat(readFirstMessageJavax(imapPort).getInputStream())
-                .hasContent(UNICODE_BODY);
+            assertThat(readFirstMessageJavax(imapPort))
+                .contains(UNICODE_BODY);
         }
     }
 
diff --git 
a/server/container/guice/guice-common/src/test/resources/eml/mail-containing-unicode-characters.eml
 
b/server/container/guice/guice-common/src/test/resources/eml/mail-containing-unicode-characters.eml
new file mode 100644
index 0000000..54e04b1
--- /dev/null
+++ 
b/server/container/guice/guice-common/src/test/resources/eml/mail-containing-unicode-characters.eml
@@ -0,0 +1,28 @@
+Return-Path: <[email protected]>
+MIME-Version: 1.0
+Delivered-To: [email protected]
+Received: from 10.233.68.83 (EHLO incoming.james.org) ([10.233.68.83])
+          by james-0 (JAMES SMTP Server ) with ESMTP ID -1705393842
+          for <[email protected]>;
+          Wed, 22 Jan 2020 04:11:54 +0000 (UTC)
+Received: from smtp.james.org (unknown [10.233.65.0])
+       by incoming.james.org (Postfix) with ESMTPS id CCCA943
+       for <[email protected]>; Wed, 22 Jan 2020 04:11:54 +0000 (UTC)
+Received: from [10.116.29.102] (unknown [1.54.162.156])
+       (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits))
+       (No client certificate requested)
+       by smtp.james.org (Postfix) with ESMTPSA id 2B7D13F393
+       for <[email protected]>; Wed, 22 Jan 2020 05:11:53 +0100 (CET)
+To: [email protected]
+X-LINAGORA-Copy-Delivery-Done: 1
+From: Uncle Bob <[email protected]>
+Subject: quoted
+Message-ID: <[email protected]>
+Date: Wed, 22 Jan 2020 11:11:38 +0700
+User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101
+ Thunderbird/60.2.1
+Content-Type: text/plain; charset=utf-8; format=flowed
+Content-Transfer-Encoding: 8bit
+Content-Language: en-US
+
+Unicode €uro symbol.


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to