james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From adup...@apache.org
Subject [5/9] james-project git commit: PROTOCOL-117 refactor ListProcessorTest to make it clear what the test covers
Date Wed, 08 Nov 2017 07:29:52 GMT
PROTOCOL-117 refactor ListProcessorTest to make it clear what the test covers


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

Branch: refs/heads/master
Commit: 7ed93d9ec3df3a0a71f6aa1370c6e6ec98993b78
Parents: ae155b8
Author: Matthieu Baechler <matthieu@apache.org>
Authored: Mon Nov 6 17:49:13 2017 +0100
Committer: Matthieu Baechler <matthieu@apache.org>
Committed: Tue Nov 7 17:55:48 2017 +0100

----------------------------------------------------------------------
 .../james/imap/processor/ListProcessor.java     |  18 +-
 .../james/imap/processor/ListProcessorTest.java | 188 ++++++++++---------
 2 files changed, 111 insertions(+), 95 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/7ed93d9e/protocols/imap/src/main/java/org/apache/james/imap/processor/ListProcessor.java
----------------------------------------------------------------------
diff --git a/protocols/imap/src/main/java/org/apache/james/imap/processor/ListProcessor.java
b/protocols/imap/src/main/java/org/apache/james/imap/processor/ListProcessor.java
index b91ad75..b23f649 100644
--- a/protocols/imap/src/main/java/org/apache/james/imap/processor/ListProcessor.java
+++ b/protocols/imap/src/main/java/org/apache/james/imap/processor/ListProcessor.java
@@ -36,6 +36,7 @@ import org.apache.james.imap.api.process.MailboxType;
 import org.apache.james.imap.api.process.MailboxTyper;
 import org.apache.james.imap.main.PathConverter;
 import org.apache.james.imap.message.request.ListRequest;
+import org.apache.james.imap.message.response.AbstractListingResponse;
 import org.apache.james.imap.message.response.ListResponse;
 import org.apache.james.mailbox.MailboxManager;
 import org.apache.james.mailbox.MailboxSession;
@@ -52,6 +53,8 @@ import org.apache.james.util.MDCBuilder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.google.common.annotations.VisibleForTesting;
+
 public class ListProcessor extends AbstractMailboxProcessor<ListRequest> {
     private static final Logger LOGGER = LoggerFactory.getLogger(ListProcessor.class);
 
@@ -194,14 +197,19 @@ public class ListProcessor extends AbstractMailboxProcessor<ListRequest>
{
         }
     }
 
-    void processResult(Responder responder, boolean relative, MailboxMetaData listResult,
MailboxType mailboxType) {
+    private void processResult(Responder responder, boolean relative, MailboxMetaData listResult,
MailboxType mailboxType) {
+        ImapResponseMessage response = convertMetadataToListResponse(relative, listResult,
mailboxType);
+        responder.respond(response);
+    }
+
+    @VisibleForTesting <T extends AbstractListingResponse & ImapResponseMessage>
T convertMetadataToListResponse(boolean relative, MailboxMetaData listResult, MailboxType
mailboxType) {
         final char delimiter = listResult.getHierarchyDelimiter();
         final String mailboxName = mailboxName(relative, listResult.getPath(), delimiter);
 
         final Children inferiors = listResult.inferiors();
-        final boolean noInferior = MailboxMetaData.Children.NO_INFERIORS.equals(inferiors);
-        final boolean hasChildren = MailboxMetaData.Children.HAS_CHILDREN.equals(inferiors);
-        final boolean hasNoChildren = MailboxMetaData.Children.HAS_NO_CHILDREN.equals(inferiors);
+        final boolean noInferior = Children.NO_INFERIORS.equals(inferiors);
+        final boolean hasChildren = Children.HAS_CHILDREN.equals(inferiors);
+        final boolean hasNoChildren = Children.HAS_NO_CHILDREN.equals(inferiors);
         boolean noSelect = false;
         boolean marked = false;
         boolean unmarked = false;
@@ -218,7 +226,7 @@ public class ListProcessor extends AbstractMailboxProcessor<ListRequest>
{
         default:
             break;
         }
-        responder.respond(createResponse(noInferior, noSelect, marked, unmarked, hasChildren,
hasNoChildren, mailboxName, delimiter, mailboxType));
+        return (T)createResponse(noInferior, noSelect, marked, unmarked, hasChildren, hasNoChildren,
mailboxName, delimiter, mailboxType);
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/james-project/blob/7ed93d9e/protocols/imap/src/test/java/org/apache/james/imap/processor/ListProcessorTest.java
----------------------------------------------------------------------
diff --git a/protocols/imap/src/test/java/org/apache/james/imap/processor/ListProcessorTest.java
b/protocols/imap/src/test/java/org/apache/james/imap/processor/ListProcessorTest.java
index 2d48f28..4c546b9 100644
--- a/protocols/imap/src/test/java/org/apache/james/imap/processor/ListProcessorTest.java
+++ b/protocols/imap/src/test/java/org/apache/james/imap/processor/ListProcessorTest.java
@@ -19,130 +19,138 @@
 
 package org.apache.james.imap.processor;
 
-import org.apache.james.imap.api.ImapCommand;
+import static org.assertj.core.api.Assertions.assertThat;
+
 import org.apache.james.imap.api.message.response.StatusResponseFactory;
 import org.apache.james.imap.api.process.ImapProcessor;
-import org.apache.james.imap.api.process.ImapSession;
 import org.apache.james.imap.api.process.MailboxType;
 import org.apache.james.imap.message.response.ListResponse;
 import org.apache.james.mailbox.MailboxManager;
-import org.apache.james.mailbox.model.MailboxConstants;
 import org.apache.james.mailbox.model.MailboxMetaData;
 import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.mailbox.store.SimpleMailboxMetaData;
 import org.apache.james.metrics.api.NoopMetricFactory;
-import org.jmock.Expectations;
-import org.jmock.Mockery;
-import org.jmock.integration.junit4.JUnit4Mockery;
 import org.junit.Before;
 import org.junit.Test;
 
 public class ListProcessorTest  {
 
-    ListProcessor processor;
-
-    ImapProcessor next;
-
-    MailboxManager manager;
-
-    ImapProcessor.Responder responder;
-
-    MailboxMetaData result;
-
-    ImapSession session;
-
-    ImapCommand command;
-
-    StatusResponseFactory serverResponseFactory;
-
     MailboxPath inboxPath = new MailboxPath("", "", "INBOX");
+    ListProcessor processor;
 
-    private Mockery mockery = new JUnit4Mockery();
-    
     @Before
     public void setUp() throws Exception {
-        serverResponseFactory = mockery.mock(StatusResponseFactory.class);
-        session = mockery.mock(ImapSession.class);
-        command = ImapCommand.anyStateCommand("Command");
-        next = mockery.mock(ImapProcessor.class);
-        responder = mockery.mock(ImapProcessor.Responder.class);
-        result = mockery.mock(MailboxMetaData.class);
-        manager = mockery.mock(MailboxManager.class);
-        processor = createProcessor(next, manager, serverResponseFactory);
-    }
-
-    ListProcessor createProcessor(ImapProcessor next,
-            MailboxManager manager, StatusResponseFactory factory) {
-        return new ListProcessor(next, manager, factory, new NoopMetricFactory());
+        StatusResponseFactory serverResponseFactory = null;
+        ImapProcessor next = null;
+        MailboxManager manager = null;
+        processor = new ListProcessor(next, manager, serverResponseFactory, new NoopMetricFactory());
     }
 
-    ListResponse createResponse(boolean noinferior, boolean noselect,
-            boolean marked, boolean unmarked, boolean hasChildren,
-            boolean hasNoChildren, char hierarchyDelimiter, String mailboxName) {
-        return new ListResponse(noinferior, noselect, marked, unmarked,
-                hasChildren, hasNoChildren, MailboxConstants.USER_NAMESPACE + MailboxConstants.DEFAULT_DELIMITER
+ mailboxName, hierarchyDelimiter);
-    }
-
-    void setUpResult(final MailboxMetaData.Children children, final MailboxMetaData.Selectability
selectability,
-            final char hierarchyDelimiter, final MailboxPath path) {
-        mockery.checking(new Expectations() {{
-            oneOf(result).inferiors();will(returnValue(children));
-            oneOf(result).getSelectability();will(returnValue(selectability));
-            oneOf(result).getHierarchyDelimiter();will(returnValue(hierarchyDelimiter));
-            oneOf(result).getPath();will(returnValue(path));
-        }});
-    }
-    
     @Test
-    public void testHasChildren() throws Exception {
-        setUpResult(MailboxMetaData.Children.HAS_CHILDREN, MailboxMetaData.Selectability.NONE,
'.', inboxPath);
-        mockery.checking(new Expectations() {{
-            oneOf(responder).respond(with(equal(createResponse(false, false, false, false,
true, false, '.', "INBOX"))));
-        }});
-        processor.processResult(responder, false, result,MailboxType.OTHER);
+    public void convertHasChildrenShouldHaveHasChildrenFlagOnly() throws Exception {
+        ListResponse actual = processor.convertMetadataToListResponse(false,
+            new SimpleMailboxMetaData(inboxPath, null, '.', MailboxMetaData.Children.HAS_CHILDREN,
MailboxMetaData.Selectability.NONE),
+            MailboxType.OTHER);
+        assertThat(actual).isEqualTo(
+            new ListResponse(
+                false,
+                false,
+                false,
+                false,
+                true,
+                false,
+                inboxPath.getFullName('.'),
+                '.')
+        );
     }
 
     @Test
-    public void testHasNoChildren() throws Exception {
-        setUpResult(MailboxMetaData.Children.HAS_NO_CHILDREN, MailboxMetaData.Selectability.NONE,
'.', inboxPath);
-        mockery.checking(new Expectations() {{
-            oneOf(responder).respond(with(equal(createResponse(false, false, false, false,
false, true, '.', "INBOX"))));
-        }});
-        processor.processResult(responder, false, result,MailboxType.OTHER);
+    public void convertHasNoChildrenShouldHaveHasNoChildrenFlagOnly() throws Exception {
+        ListResponse actual = processor.convertMetadataToListResponse(false,
+            new SimpleMailboxMetaData(inboxPath, null, '.', MailboxMetaData.Children.HAS_NO_CHILDREN,
MailboxMetaData.Selectability.NONE),
+            MailboxType.OTHER);
+        assertThat(actual).isEqualTo(
+            new ListResponse(
+                false,
+                false,
+                false,
+                false,
+                false,
+                true,
+                inboxPath.getFullName('.'),
+                '.')
+        );
     }
     
     @Test
-    public void testNoInferiors() throws Exception {
-        setUpResult(MailboxMetaData.Children.NO_INFERIORS, MailboxMetaData.Selectability.NONE,
'.', inboxPath);
-        mockery.checking(new Expectations() {{
-            oneOf(responder).respond(with(equal(createResponse(true, false, false, false,
false, false, '.', "INBOX"))));
-        }});
-        processor.processResult(responder, false, result,MailboxType.OTHER);
+    public void convertNoInferiorShouldHaveNoInferiorFlagOnly() throws Exception {
+        ListResponse actual = processor.convertMetadataToListResponse(false,
+            new SimpleMailboxMetaData(inboxPath, null, '.', MailboxMetaData.Children.NO_INFERIORS,
MailboxMetaData.Selectability.NONE),
+            MailboxType.OTHER);
+        assertThat(actual).isEqualTo(
+            new ListResponse(
+                true,
+                false,
+                false,
+                false,
+                false,
+                false,
+                inboxPath.getFullName('.'),
+                '.')
+        );
     }
 
     @Test
-    public void testNoSelect() throws Exception {
-        setUpResult(MailboxMetaData.Children.CHILDREN_ALLOWED_BUT_UNKNOWN, MailboxMetaData.Selectability.NOSELECT,
'.', inboxPath);
-        mockery.checking(new Expectations() {{
-            oneOf(responder).respond(with(equal(createResponse(false, true, false, false,
false, false, '.', "INBOX"))));
-        }});
-        processor.processResult(responder, false, result,MailboxType.OTHER);
+    public void convertNoSelectUnknownChildrenShouldHaveNoSelectFlagOnly() throws Exception
{
+        ListResponse actual = processor.convertMetadataToListResponse(false,
+            new SimpleMailboxMetaData(inboxPath, null, '.', MailboxMetaData.Children.CHILDREN_ALLOWED_BUT_UNKNOWN,
MailboxMetaData.Selectability.NOSELECT),
+            MailboxType.OTHER);
+        assertThat(actual).isEqualTo(
+            new ListResponse(
+                false,
+                true,
+                false,
+                false,
+                false,
+                false,
+                inboxPath.getFullName('.'),
+                '.')
+        );
     }
 
     @Test
-    public void testUnMarked() throws Exception {
-        setUpResult(MailboxMetaData.Children.CHILDREN_ALLOWED_BUT_UNKNOWN, MailboxMetaData.Selectability.UNMARKED,
'.', inboxPath);
-        mockery.checking(new Expectations() {{
-            oneOf(responder).respond(with(equal(createResponse(false, false, false, true,
false, false, '.', "INBOX"))));
-        }});
-        processor.processResult(responder, false, result,MailboxType.OTHER);
+    public void convertUnmarkedUnknownChildrenShouldHaveUnmarkedFlagOnly() throws Exception
{
+        ListResponse actual = processor.convertMetadataToListResponse(false,
+            new SimpleMailboxMetaData(inboxPath, null, '.', MailboxMetaData.Children.CHILDREN_ALLOWED_BUT_UNKNOWN,
MailboxMetaData.Selectability.UNMARKED),
+            MailboxType.OTHER);
+        assertThat(actual).isEqualTo(
+            new ListResponse(
+                false,
+                false,
+                false,
+                true,
+                false,
+                false,
+                inboxPath.getFullName('.'),
+                '.')
+        );
     }
 
     @Test
-    public void testMarked() throws Exception {
-        setUpResult(MailboxMetaData.Children.CHILDREN_ALLOWED_BUT_UNKNOWN, MailboxMetaData.Selectability.MARKED,
'.', inboxPath);
-        mockery.checking(new Expectations() {{
-            oneOf(responder).respond(with(equal(createResponse(false, false, true, false,
false, false, '.', "INBOX"))));
-        }});
-        processor.processResult(responder, false, result,MailboxType.OTHER);
+    public void convertMarkedUnknownChildrenShouldHaveMarkedFlagOnly() throws Exception {
+        ListResponse actual = processor.convertMetadataToListResponse(false,
+            new SimpleMailboxMetaData(inboxPath, null, '.', MailboxMetaData.Children.CHILDREN_ALLOWED_BUT_UNKNOWN,
MailboxMetaData.Selectability.MARKED),
+            MailboxType.OTHER);
+        assertThat(actual).isEqualTo(
+            new ListResponse(
+                false,
+                false,
+                true,
+                false,
+                false,
+                false,
+                inboxPath.getFullName('.'),
+                '.')
+        );
     }
 }


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


Mime
View raw message