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 bt...@apache.org on 2018/01/05 02:56:36 UTC

[17/44] james-project git commit: JAMES-2267 ImapMessageReader should be a test rule

http://git-wip-us.apache.org/repos/asf/james-project/blob/456972f7/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SetMessagesMethodTest.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SetMessagesMethodTest.java b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SetMessagesMethodTest.java
index 93a7da5..504593c 100644
--- a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SetMessagesMethodTest.java
+++ b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SetMessagesMethodTest.java
@@ -4527,8 +4527,10 @@ public abstract class SetMessagesMethodTest {
 
         calmlyAwait.atMost(30, TimeUnit.SECONDS).until(() -> isAnyMessageFoundInInbox(accessToken));
 
-        try (IMAPMessageReader imapMessageReader = new IMAPMessageReader(LOCALHOST_IP, IMAP_PORT);) {
-            assertThat(imapMessageReader.readFirstMessageHeadersInInbox(USERNAME, PASSWORD))
+        try (IMAPMessageReader imapMessageReader = new IMAPMessageReader()
+                .connect(LOCALHOST_IP, IMAP_PORT)
+                .select(MailboxConstants.INBOX)) {
+            assertThat(imapMessageReader.readFirstMessage())
                 .contains("X-MY-MULTIVALUATED-HEADER: first value")
                 .contains("X-MY-MULTIVALUATED-HEADER: second value");
         }

http://git-wip-us.apache.org/repos/asf/james-project/blob/456972f7/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/cucumber/ImapStepdefs.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/cucumber/ImapStepdefs.java b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/cucumber/ImapStepdefs.java
index 744eee3..048e469 100644
--- a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/cucumber/ImapStepdefs.java
+++ b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/cucumber/ImapStepdefs.java
@@ -53,58 +53,61 @@ public class ImapStepdefs {
 
     @Then("^the user has a IMAP message in mailbox \"([^\"]*)\"$")
     public void hasMessageInMailbox(String mailbox) throws Throwable {
-        try (IMAPMessageReader imapMessageReader = new IMAPMessageReader(LOCALHOST, IMAP_PORT)) {
-            assertThat(
-                imapMessageReader.userReceivedMessageInMailbox(userStepdefs.getConnectedUser(),
-                    userStepdefs.getUserPassword(userStepdefs.getConnectedUser()),
-                    mailbox))
-                .isTrue();
+        try (IMAPMessageReader imapMessageReader = new IMAPMessageReader()
+            .connect(LOCALHOST, IMAP_PORT)
+            .login(userStepdefs.getConnectedUser(),
+                userStepdefs.getUserPassword(userStepdefs.getConnectedUser()))
+            .select(mailbox)) {
+            assertThat(imapMessageReader.hasAMessage()).isTrue();
         }
     }
 
     @Then("^the message has IMAP flag \"([^\"]*)\" in mailbox \"([^\"]*)\" for \"([^\"]*)\"$")
     public void hasMessageWithFlagInMailbox(String flags, String mailbox, String username) throws Throwable {
-        try (IMAPMessageReader imapMessageReader = new IMAPMessageReader(LOCALHOST, IMAP_PORT)) {
-            assertThat(
-                imapMessageReader.userReceivedMessageWithFlagsInMailbox(username,
-                    userStepdefs.getUserPassword(username),
-                    mailbox,
-                    flags))
+        try (IMAPMessageReader imapMessageReader = new IMAPMessageReader()
+            .connect(LOCALHOST, IMAP_PORT)
+            .login(userStepdefs.getConnectedUser(),
+                userStepdefs.getUserPassword(username))
+            .select(mailbox)) {
+            assertThat(imapMessageReader.hasAMessageWithFlags(flags))
                 .isTrue();
         }
     }
 
     @Then("^the user has a IMAP notification about (\\d+) new message when selecting mailbox \"([^\"]*)\"$")
     public void hasANotificationAboutNewMessagesInMailbox(int numOfNewMessage, String mailbox) throws Throwable {
-        try (IMAPMessageReader imapMessageReader = new IMAPMessageReader(LOCALHOST, IMAP_PORT)) {
+        try (IMAPMessageReader imapMessageReader = new IMAPMessageReader()
+            .connect(LOCALHOST, IMAP_PORT)
+            .login(userStepdefs.getConnectedUser(),
+                userStepdefs.getUserPassword(userStepdefs.getConnectedUser()))
+            .select(mailbox)) {
             assertThat(
-                imapMessageReader.userGetNotifiedForNewMessagesWhenSelectingMailbox(userStepdefs.getConnectedUser(),
-                    userStepdefs.getUserPassword(userStepdefs.getConnectedUser()),
-                    numOfNewMessage, mailbox))
+                imapMessageReader.userGetNotifiedForNewMessagesWhenSelectingMailbox(numOfNewMessage))
                 .isTrue();
         }
     }
 
     @Then("^the user does not have a IMAP message in mailbox \"([^\"]*)\"$")
     public void hasNoMessageInMailbox(String mailbox) throws Throwable {
-        try (IMAPMessageReader imapMessageReader = new IMAPMessageReader(LOCALHOST, IMAP_PORT)) {
-            assertThat(
-                imapMessageReader.userDoesNotReceiveMessageInMailbox(userStepdefs.getConnectedUser(),
-                    userStepdefs.getUserPassword(userStepdefs.getConnectedUser()),
-                    mailbox))
+        try (IMAPMessageReader imapMessageReader = new IMAPMessageReader()
+            .connect(LOCALHOST, IMAP_PORT)
+            .login(userStepdefs.getConnectedUser(),
+                userStepdefs.getUserPassword(userStepdefs.getConnectedUser()))
+            .select(mailbox)) {
+            assertThat(imapMessageReader.userDoesNotReceiveMessage())
                 .isTrue();
         }
     }
 
     @Given("^the user has an open IMAP connection with mailbox \"([^\"]*)\" selected")
     public void openImapConnectionAndSelectMailbox(String mailbox) throws Throwable {
-        IMAPMessageReader imapMessageReader = new IMAPMessageReader(LOCALHOST, IMAP_PORT);
-
         String login = userStepdefs.getConnectedUser();
         String password = userStepdefs.getUserPassword(login);
 
-        imapMessageReader.connectAndSelect(login, password, mailbox);
-        imapConnections.put(mailbox, imapMessageReader);
+        imapConnections.put(mailbox, new IMAPMessageReader()
+            .connect(LOCALHOST, IMAP_PORT)
+            .login(login, password)
+            .select(mailbox));
     }
 
     @Then("^the user set flags via IMAP to \"([^\"]*)\" for all messages in mailbox \"([^\"]*)\"$")
@@ -123,12 +126,13 @@ public class ImapStepdefs {
 
     @When("^the user copy by IMAP first message of \"([^\"]*)\" to mailbox \"([^\"]*)\"$")
     public void copyAMessageByIMAP(String srcMailbox, String destMailbox) throws Throwable {
-        IMAPMessageReader imapMessageReader = new IMAPMessageReader(LOCALHOST, IMAP_PORT);
-
         String login = userStepdefs.getConnectedUser();
         String password = userStepdefs.getUserPassword(login);
 
-        imapMessageReader.connectAndSelect(login, password, srcMailbox);
+        IMAPMessageReader imapMessageReader = new IMAPMessageReader()
+            .connect(LOCALHOST, IMAP_PORT)
+            .login(login, password)
+            .select(srcMailbox);
         assertThat(imapMessageReader).isNotNull();
         imapMessageReader.copyFirstMessage(destMailbox);
         mainStepdefs.awaitMethod.run();

http://git-wip-us.apache.org/repos/asf/james-project/blob/456972f7/server/testing/src/main/java/org/apache/james/utils/IMAPMessageReader.java
----------------------------------------------------------------------
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 4b28106..2d27da0 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
@@ -23,14 +23,17 @@ import java.io.Closeable;
 import java.io.IOException;
 import java.util.List;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.commons.net.imap.IMAPClient;
+import org.junit.rules.ExternalResource;
 
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Splitter;
+import com.jayway.awaitility.core.ConditionFactory;
 
-public class IMAPMessageReader implements Closeable {
+public class IMAPMessageReader extends ExternalResource implements Closeable {
 
-    private static final String INBOX = "INBOX";
+    public static final String INBOX = "INBOX";
 
     private final IMAPClient imapClient;
 
@@ -39,40 +42,41 @@ public class IMAPMessageReader implements Closeable {
         this.imapClient = imapClient;
     }
 
-    public IMAPMessageReader(String host, int port) throws IOException {
-        imapClient = new IMAPClient();
+    public IMAPMessageReader() {
+        this(new IMAPClient());
+    }
+
+    public IMAPMessageReader connect(String host, int port) throws IOException {
         imapClient.connect(host, port);
+        return this;
     }
 
-    public void connectAndSelect(String user, String password, String mailbox) throws IOException{
+    public IMAPMessageReader login(String user, String password) throws IOException{
         imapClient.login(user, password);
-        imapClient.select(mailbox);
+        return this;
     }
 
-    public boolean countReceivedMessage(String user, String password, int numberOfMessages) throws IOException {
-        return countReceivedMessageInMailbox(user, password, INBOX, numberOfMessages);
+    public IMAPMessageReader select(String mailbox) throws IOException {
+        imapClient.select(mailbox);
+        return this;
     }
 
-    public boolean countReceivedMessageInMailbox(String user, String password, String mailbox, int numberOfMessages) throws IOException {
-        connectAndSelect(user, password, mailbox);
-
+    public boolean hasMessageCount(int numberOfMessages) throws IOException {
         return imapClient.getReplyString()
             .contains(numberOfMessages + " EXISTS");
     }
 
-    public boolean userReceivedMessage(String user, String password) throws IOException {
-        return userReceivedMessageInMailbox(user, password, INBOX);
-    }
-
-    public boolean userReceivedMessageInMailbox(String user, String password, String mailbox) throws IOException {
-        connectAndSelect(user, password, mailbox);
+    public boolean hasAMessage() throws IOException {
         imapClient.fetch("1:1", "ALL");
         return imapClient.getReplyString()
             .contains("OK FETCH completed");
     }
 
-    public boolean userReceivedMessageWithFlagsInMailbox(String user, String password, String mailbox, String flags) throws IOException {
-        connectAndSelect(user, password, mailbox);
+    public void awaitMessage(ConditionFactory conditionFactory) throws IOException {
+        conditionFactory.until(this::hasAMessage);
+    }
+
+    public boolean hasAMessageWithFlags(String flags) throws IOException {
         imapClient.fetch("1:1", "ALL");
         String replyString = imapClient.getReplyString();
         return isCompletedWithFlags(flags, replyString);
@@ -87,34 +91,22 @@ public class IMAPMessageReader implements Closeable {
             .allMatch(s -> replyString.contains(s));
     }
 
-    public boolean userGetNotifiedForNewMessagesWhenSelectingMailbox(String user, String password, int numOfNewMessage, String mailboxName) throws IOException {
-        connectAndSelect(user, password, mailboxName);
-
+    public boolean userGetNotifiedForNewMessagesWhenSelectingMailbox(int numOfNewMessage) throws IOException {
         return imapClient.getReplyString().contains("OK [UNSEEN " + numOfNewMessage +"]");
     }
 
-    public boolean userDoesNotReceiveMessage(String user, String password) throws IOException {
-        return userDoesNotReceiveMessageInMailbox(user, password, INBOX);
-    }
-
-    public boolean userDoesNotReceiveMessageInMailbox(String user, String password, String mailboxName) throws IOException {
-        connectAndSelect(user, password, mailboxName);
+    public boolean userDoesNotReceiveMessage() throws IOException {
         imapClient.fetch("1:1", "ALL");
         return imapClient.getReplyString()
              .contains("BAD FETCH failed. Invalid messageset");
     }
 
-    public String readFirstMessageInInbox(String user, String password) throws IOException {
-
-        return readFirstMessageInMailbox(user, password, "(BODY[])", INBOX);
-    }
-
-    public String readFirstMessageHeadersInMailbox(String user, String password, String mailboxName) throws IOException {
-        return readFirstMessageInMailbox(user, password, "(RFC822.HEADER)", mailboxName);
+    public String readFirstMessage() throws IOException {
+        return readFirstMessageInMailbox("(BODY[])");
     }
 
-    public String readFirstMessageHeadersInInbox(String user, String password) throws IOException {
-        return readFirstMessageInMailbox(user, password, "(RFC822.HEADER)", INBOX);
+    public String readFirstMessageHeaders() throws IOException {
+        return readFirstMessageInMailbox("(RFC822.HEADER)");
     }
 
     public String setFlagsForAllMessagesInMailbox(String flag) throws IOException {
@@ -122,9 +114,7 @@ public class IMAPMessageReader implements Closeable {
         return imapClient.getReplyString();
     }
 
-    private String readFirstMessageInMailbox(String user, String password, String parameters, String mailboxName) throws IOException {
-        imapClient.login(user, password);
-        imapClient.select(mailboxName);
+    private String readFirstMessageInMailbox(String parameters) throws IOException {
         imapClient.fetch("1:1", parameters);
         return imapClient.getReplyString();
     }
@@ -159,7 +149,14 @@ public class IMAPMessageReader implements Closeable {
 
     @Override
     public void close() throws IOException {
-        imapClient.close();
+        if (imapClient.isConnected()) {
+            imapClient.close();
+        }
+    }
+
+    @Override
+    protected void after() {
+        IOUtils.closeQuietly(this);
     }
 
     public void copyFirstMessage(String destMailbox) throws IOException {


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