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 c456920bdd378d5ed65e9d06a06e31a1b326a023
Author: Raphael Ouazana <[email protected]>
AuthorDate: Thu Feb 6 10:20:10 2020 +0100

    JAMES-3034 Fix IMAPMessageReader to read correctly UTF-8 emails
---
 .../apache/james/MailsShouldBeWellReceived.java    | 21 +--------------------
 .../org/apache/james/utils/IMAPMessageReader.java  | 22 +++++++++++++++++++---
 .../apache/james/utils/IMAPMessageReaderTest.java  | 18 +++++++++---------
 3 files changed, 29 insertions(+), 32 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 ff443ab..e34fb46 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
@@ -75,25 +75,6 @@ interface MailsShouldBeWellReceived {
     String SENDER = "[email protected]";
     String UNICODE_BODY = "Unicode €uro symbol.";
 
-
-    static String readFirstMessageJavax(int imapPort) throws Exception {
-        Session imapSession = Session.getDefaultInstance(new Properties());
-        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));
-            }
-        }
-    }
-
     static Message[] searchForAll(Folder inbox) throws MessagingException {
         return inbox.search(new FlagTerm(new Flags(), false));
     }
@@ -131,7 +112,7 @@ interface MailsShouldBeWellReceived {
                 .select(IMAPMessageReader.INBOX)
                 .awaitMessageCount(CALMLY_AWAIT, 1);
 
-            assertThat(readFirstMessageJavax(imapPort))
+            assertThat(reader.readFirstMessage())
                 .contains(UNICODE_BODY);
         }
     }
diff --git 
a/server/testing/src/main/java/org/apache/james/utils/IMAPMessageReader.java 
b/server/testing/src/main/java/org/apache/james/utils/IMAPMessageReader.java
index c9f841e..36a57e0 100644
--- a/server/testing/src/main/java/org/apache/james/utils/IMAPMessageReader.java
+++ b/server/testing/src/main/java/org/apache/james/utils/IMAPMessageReader.java
@@ -19,14 +19,18 @@
 
 package org.apache.james.utils;
 
+import java.io.BufferedWriter;
 import java.io.Closeable;
 import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
 import java.util.List;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 import java.util.stream.Stream;
 
 import org.apache.commons.net.imap.IMAPClient;
+import org.apache.commons.net.io.CRLFLineReader;
 import org.apache.james.core.Username;
 import org.assertj.core.api.Assertions;
 import org.awaitility.core.ConditionFactory;
@@ -39,19 +43,31 @@ import com.google.common.base.Splitter;
 
 public class IMAPMessageReader extends ExternalResource implements Closeable, 
AfterEachCallback {
 
+    public static class Utf8IMAPClient extends IMAPClient {
+        private static String UTF8_ENCODING = "UTF-8";
+
+        @Override
+        protected void _connectAction_() throws IOException {
+            super._connectAction_();
+            _reader = new CRLFLineReader(new InputStreamReader(_input_, 
UTF8_ENCODING));
+            __writer = new BufferedWriter(new OutputStreamWriter(_output_, 
UTF8_ENCODING));
+        }
+    }
+
     private static final Pattern EXAMINE_EXISTS = Pattern.compile("^\\* (\\d+) 
EXISTS$");
     private static final int MESSAGE_NUMBER_MATCHING_GROUP = 1;
     public static final String INBOX = "INBOX";
 
-    private final IMAPClient imapClient;
+    private final Utf8IMAPClient imapClient;
+
 
     @VisibleForTesting
-    IMAPMessageReader(IMAPClient imapClient) {
+    IMAPMessageReader(Utf8IMAPClient imapClient) {
         this.imapClient = imapClient;
     }
 
     public IMAPMessageReader() {
-        this(new IMAPClient());
+        this(new Utf8IMAPClient());
     }
 
     public IMAPMessageReader connect(String host, int port) throws IOException 
{
diff --git 
a/server/testing/src/test/java/org/apache/james/utils/IMAPMessageReaderTest.java
 
b/server/testing/src/test/java/org/apache/james/utils/IMAPMessageReaderTest.java
index 797e92f..8d8051c 100644
--- 
a/server/testing/src/test/java/org/apache/james/utils/IMAPMessageReaderTest.java
+++ 
b/server/testing/src/test/java/org/apache/james/utils/IMAPMessageReaderTest.java
@@ -21,15 +21,15 @@ package org.apache.james.utils;
 
 import static org.assertj.core.api.Assertions.assertThat;
 
-import org.apache.commons.net.imap.IMAPClient;
-import org.junit.Test;
+import org.apache.james.utils.IMAPMessageReader.Utf8IMAPClient;
+import org.junit.jupiter.api.Test;
 
-public class IMAPMessageReaderTest {
-    private static final IMAPClient NULL_IMAP_CLIENT = null;
-    private IMAPMessageReader testee = new IMAPMessageReader(NULL_IMAP_CLIENT);
+class IMAPMessageReaderTest {
+    static final Utf8IMAPClient NULL_IMAP_CLIENT = null;
+    IMAPMessageReader testee = new IMAPMessageReader(NULL_IMAP_CLIENT);
 
     @Test
-    public void 
userReceivedMessageWithFlagsInMailboxShouldReturnTrueWhenSingleFlag() throws 
Exception {
+    void userReceivedMessageWithFlagsInMailboxShouldReturnTrueWhenSingleFlag() 
throws Exception {
         String replyString = "* 1 FETCH (FLAGS (\\Flagged) )\n" +
             "AAAC OK FETCH completed.";
 
@@ -38,7 +38,7 @@ public class IMAPMessageReaderTest {
     }
 
     @Test
-    public void 
userReceivedMessageWithFlagsInMailboxShouldReturnFalseWhenCompletedButNoFlag() 
throws Exception {
+    void 
userReceivedMessageWithFlagsInMailboxShouldReturnFalseWhenCompletedButNoFlag() 
throws Exception {
         String replyString = "* 1 FETCH (FLAGS (\\Seen) )\n" +
             "AAAC OK FETCH completed.";
 
@@ -47,7 +47,7 @@ public class IMAPMessageReaderTest {
     }
 
     @Test
-    public void 
userReceivedMessageWithFlagsInMailboxShouldReturnTrueWhenSeveralFlags() throws 
Exception {
+    void 
userReceivedMessageWithFlagsInMailboxShouldReturnTrueWhenSeveralFlags() throws 
Exception {
         String replyString = "* 1 FETCH (FLAGS (\\Flagged \\Seen) )\n" +
             "AAAC OK FETCH completed.";
 
@@ -56,7 +56,7 @@ public class IMAPMessageReaderTest {
     }
 
     @Test
-    public void 
userReceivedMessageWithFlagsInMailboxShouldReturnTrueWhenSeveralFlagsInAnyOrder()
 throws Exception {
+    void 
userReceivedMessageWithFlagsInMailboxShouldReturnTrueWhenSeveralFlagsInAnyOrder()
 throws Exception {
         String replyString = "* 1 FETCH (FLAGS (\\Flagged \\Seen) )\n" +
             "AAAC OK FETCH completed.";
 


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

Reply via email to