james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject james-project git commit: JAMES-2340 Copying messages out of an empty mailbox should not throw
Date Wed, 02 May 2018 03:35:00 GMT
Repository: james-project
Updated Branches:
  refs/heads/master 560fd9051 -> 64f9aceb6


JAMES-2340 Copying messages out of an empty mailbox should not throw


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

Branch: refs/heads/master
Commit: 64f9aceb60850dcb579eecd1293162d286f2c27d
Parents: 560fd90
Author: benwa <btellier@linagora.com>
Authored: Mon Apr 23 15:11:49 2018 +0700
Committer: benwa <btellier@linagora.com>
Committed: Wed May 2 10:32:51 2018 +0700

----------------------------------------------------------------------
 .../james/mailbox/MailboxManagerTest.java       | 11 ++++++
 .../mailbox/store/StoreMessageManager.java      | 35 +++++++++-----------
 .../store/event/MailboxEventDispatcher.java     |  5 ++-
 .../mailbox/store/event/MessageMoveEvent.java   |  5 ++-
 .../store/event/MessageMoveEventTest.java       | 26 ++++++++++++---
 5 files changed, 56 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/64f9aceb/mailbox/api/src/test/java/org/apache/james/mailbox/MailboxManagerTest.java
----------------------------------------------------------------------
diff --git a/mailbox/api/src/test/java/org/apache/james/mailbox/MailboxManagerTest.java b/mailbox/api/src/test/java/org/apache/james/mailbox/MailboxManagerTest.java
index 87338ca..93573f1 100644
--- a/mailbox/api/src/test/java/org/apache/james/mailbox/MailboxManagerTest.java
+++ b/mailbox/api/src/test/java/org/apache/james/mailbox/MailboxManagerTest.java
@@ -951,4 +951,15 @@ public abstract class MailboxManagerTest {
         assertThatCode(() -> mailboxManager.moveMessages(MessageRange.all(), inbox, inbox,
session))
             .doesNotThrowAnyException();
     }
+
+    @Test
+    public void copyMessagesShouldNotThrowWhenMovingAllMessagesOfAnEmptyMailbox() throws
Exception {
+        session = mailboxManager.createSystemSession(USER_1);
+
+        MailboxPath inbox = MailboxPath.inbox(session);
+        mailboxManager.createMailbox(inbox, session);
+
+        assertThatCode(() -> mailboxManager.copyMessages(MessageRange.all(), inbox, inbox,
session))
+            .doesNotThrowAnyException();
+    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/64f9aceb/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMessageManager.java
----------------------------------------------------------------------
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMessageManager.java
b/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMessageManager.java
index 3109f32..84bc92f 100644
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMessageManager.java
+++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMessageManager.java
@@ -89,7 +89,6 @@ import org.slf4j.LoggerFactory;
 
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.ImmutableSortedMap;
 
 /**
  * Base class for {@link org.apache.james.mailbox.MessageManager}
@@ -734,7 +733,7 @@ public class StoreMessageManager implements org.apache.james.mailbox.MessageMana
         SortedMap<MessageUid, MessageMetaData> copiedUids = collectMetadata(to.copy(originalRows,
session));
 
         ImmutableMap.Builder<MessageUid, MailboxMessage> messagesMap = ImmutableMap.builder();
-        for (MailboxMessage message: originalRows.getEntriesSeen()) {
+        for (MailboxMessage message : originalRows.getEntriesSeen()) {
             messagesMap.put(message.getUid(), immutableMailboxMessageFactory.from(to.getMailboxEntity().getMailboxId(),
message));
         }
         dispatcher.added(session, copiedUids, to.getMailboxEntity(), messagesMap.build());
@@ -744,32 +743,28 @@ public class StoreMessageManager implements org.apache.james.mailbox.MessageMana
                 .targetMailboxIds(to.getMailboxEntity().getMailboxId(), getMailboxEntity().getMailboxId())
                 .build(),
             messagesMap.build());
-
         return copiedUids;
     }
 
     private SortedMap<MessageUid, MessageMetaData> move(MessageRange set, StoreMessageManager
to, MailboxSession session) throws MailboxException {
         IteratorWrapper<MailboxMessage> originalRows = new IteratorWrapper<>(retrieveOriginalRows(set,
session));
 
-        if (originalRows.hasNext()) {
-            MoveResult moveResult = to.move(originalRows, session);
-            SortedMap<MessageUid, MessageMetaData> moveUids = collectMetadata(moveResult.getMovedMessages());
+        MoveResult moveResult = to.move(originalRows, session);
+        SortedMap<MessageUid, MessageMetaData> moveUids = collectMetadata(moveResult.getMovedMessages());
 
-            ImmutableMap.Builder<MessageUid, MailboxMessage> messagesMap = ImmutableMap.builder();
-            for (MailboxMessage message : originalRows.getEntriesSeen()) {
-                messagesMap.put(message.getUid(), immutableMailboxMessageFactory.from(to.getMailboxEntity().getMailboxId(),
message));
-            }
-            dispatcher.added(session, moveUids, to.getMailboxEntity(), messagesMap.build());
-            dispatcher.expunged(session, collectMetadata(moveResult.getOriginalMessages()),
getMailboxEntity());
-            dispatcher.moved(session,
-                MessageMoves.builder()
-                    .previousMailboxIds(getMailboxEntity().getMailboxId())
-                    .targetMailboxIds(to.getMailboxEntity().getMailboxId())
-                    .build(),
-                messagesMap.build());
-            return moveUids;
+        ImmutableMap.Builder<MessageUid, MailboxMessage> messagesMap = ImmutableMap.builder();
+        for (MailboxMessage message : originalRows.getEntriesSeen()) {
+            messagesMap.put(message.getUid(), immutableMailboxMessageFactory.from(to.getMailboxEntity().getMailboxId(),
message));
         }
-        return ImmutableSortedMap.of();
+        dispatcher.added(session, moveUids, to.getMailboxEntity(), messagesMap.build());
+        dispatcher.expunged(session, collectMetadata(moveResult.getOriginalMessages()), getMailboxEntity());
+        dispatcher.moved(session,
+            MessageMoves.builder()
+                .previousMailboxIds(getMailboxEntity().getMailboxId())
+                .targetMailboxIds(to.getMailboxEntity().getMailboxId())
+                .build(),
+            messagesMap.build());
+        return moveUids;
     }
 
     private Iterator<MailboxMessage> retrieveOriginalRows(MessageRange set, MailboxSession
session) throws MailboxException {

http://git-wip-us.apache.org/repos/asf/james-project/blob/64f9aceb/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/MailboxEventDispatcher.java
----------------------------------------------------------------------
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/MailboxEventDispatcher.java
b/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/MailboxEventDispatcher.java
index c4fb3c9..37779cb 100644
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/MailboxEventDispatcher.java
+++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/MailboxEventDispatcher.java
@@ -160,7 +160,10 @@ public class MailboxEventDispatcher {
     }
 
     public void moved(MailboxSession session, MessageMoves messageMoves, Map<MessageUid,
MailboxMessage> messages) {
-        listener.event(eventFactory.moved(session, messageMoves, messages));
+        MessageMoveEvent moveEvent = eventFactory.moved(session, messageMoves, messages);
+        if (!moveEvent.isNoop()) {
+            listener.event(moveEvent);
+        }
     }
 
     public void quota(MailboxSession session, QuotaRoot quotaRoot, Quota<QuotaCount>
countQuota, Quota<QuotaSize> sizeQuota) {

http://git-wip-us.apache.org/repos/asf/james-project/blob/64f9aceb/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/MessageMoveEvent.java
----------------------------------------------------------------------
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/MessageMoveEvent.java
b/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/MessageMoveEvent.java
index 8fe8c55..06221c9 100644
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/MessageMoveEvent.java
+++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/MessageMoveEvent.java
@@ -67,7 +67,6 @@ public class MessageMoveEvent implements Event {
             Preconditions.checkNotNull(messageMoves, "'messageMoves' is mandatory");
 
             ImmutableMap<MessageUid, MailboxMessage> messages = messagesBuilder.build();
-            Preconditions.checkArgument(!messages.isEmpty(), "'messages' should not be empty");
 
             return new MessageMoveEvent(session, messageMoves, messages);
         }
@@ -84,6 +83,10 @@ public class MessageMoveEvent implements Event {
         this.messages = messages;
     }
 
+    public boolean isNoop() {
+        return messages.isEmpty();
+    }
+
     @Override
     public MailboxSession getSession() {
         return session;

http://git-wip-us.apache.org/repos/asf/james-project/blob/64f9aceb/mailbox/store/src/test/java/org/apache/james/mailbox/store/event/MessageMoveEventTest.java
----------------------------------------------------------------------
diff --git a/mailbox/store/src/test/java/org/apache/james/mailbox/store/event/MessageMoveEventTest.java
b/mailbox/store/src/test/java/org/apache/james/mailbox/store/event/MessageMoveEventTest.java
index 5052ff8..33239d4 100644
--- a/mailbox/store/src/test/java/org/apache/james/mailbox/store/event/MessageMoveEventTest.java
+++ b/mailbox/store/src/test/java/org/apache/james/mailbox/store/event/MessageMoveEventTest.java
@@ -56,15 +56,33 @@ public class MessageMoveEventTest {
     }
 
     @Test
-    public void builderShouldThrowWhenMessagesIsEmpty() {
-        assertThatThrownBy(() -> MessageMoveEvent.builder()
+    public void builderShouldReturnNoopWhenMessagesIsEmpty() {
+        assertThat(MessageMoveEvent.builder()
                 .session(new MockMailboxSession("user@james.org"))
                 .messageMoves(MessageMoves.builder()
                     .previousMailboxIds(TestId.of(1))
                     .targetMailboxIds(TestId.of(2))
                     .build())
-                .build())
-            .isInstanceOf(IllegalArgumentException.class);
+                .build()
+            .isNoop()).isTrue();
+    }
+
+    @Test
+    public void builderShouldNotBeNoopWhenFieldsAreGiven() {
+        MockMailboxSession session = new MockMailboxSession("user@james.org");
+        MessageMoves messageMoves = MessageMoves.builder()
+            .targetMailboxIds(TestId.of(2))
+            .previousMailboxIds(TestId.of(1))
+            .build();
+        Map<MessageUid, MailboxMessage> messages = ImmutableMap.of(MessageUid.of(1),
mock(MailboxMessage.class));
+
+        MessageMoveEvent event = MessageMoveEvent.builder()
+            .session(session)
+            .messageMoves(messageMoves)
+            .messages(messages)
+            .build();
+
+        assertThat(event.isNoop()).isFalse();
     }
 
     @Test


---------------------------------------------------------------------
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