Author: btellier
Date: Sat Nov 28 13:06:02 2015
New Revision: 1716962

URL: http://svn.apache.org/viewvc?rev=1716962&view=rev
Log:
MAILBOX-211 Distributed Delegating Mailbox Listener

Added:
    
james/project/trunk/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/distributed/DistributedDelegatingMailboxListener.java
    
james/project/trunk/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/distributed/RegisteredDelegatingMailboxListener.java
    
james/project/trunk/mailbox/store/src/test/java/org/apache/james/mailbox/store/event/distributed/RegisteredDelegatingMailboxListenerTest.java
Modified:
    
james/project/trunk/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/DefaultDelegatingMailboxListener.java

Modified: 
james/project/trunk/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/DefaultDelegatingMailboxListener.java
URL: 
http://svn.apache.org/viewvc/james/project/trunk/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/DefaultDelegatingMailboxListener.java?rev=1716962&r1=1716961&r2=1716962&view=diff
==============================================================================
--- 
james/project/trunk/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/DefaultDelegatingMailboxListener.java
 (original)
+++ 
james/project/trunk/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/DefaultDelegatingMailboxListener.java
 Sat Nov 28 13:06:02 2015
@@ -74,9 +74,9 @@ public class DefaultDelegatingMailboxLis
     @Override
     public void event(Event event) {
         Collection<MailboxListener> listenerSnapshot = 
registry.getLocalMailboxListeners(event.getMailboxPath());
-        if (event instanceof MailboxDeletion) {
+        if (event instanceof MailboxDeletion && listenerSnapshot.size() > 0) {
             registry.deleteRegistryFor(event.getMailboxPath());
-        } else if (event instanceof MailboxRenamed) {
+        } else if (event instanceof MailboxRenamed && listenerSnapshot.size() 
> 0) {
             MailboxRenamed renamed = (MailboxRenamed) event;
             registry.handleRename(renamed.getMailboxPath(), 
renamed.getNewPath());
         }

Added: 
james/project/trunk/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/distributed/DistributedDelegatingMailboxListener.java
URL: 
http://svn.apache.org/viewvc/james/project/trunk/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/distributed/DistributedDelegatingMailboxListener.java?rev=1716962&view=auto
==============================================================================
--- 
james/project/trunk/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/distributed/DistributedDelegatingMailboxListener.java
 (added)
+++ 
james/project/trunk/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/distributed/DistributedDelegatingMailboxListener.java
 Sat Nov 28 13:06:02 2015
@@ -0,0 +1,27 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.mailbox.store.event.distributed;
+
+import org.apache.james.mailbox.store.event.DelegatingMailboxListener;
+import org.apache.james.mailbox.store.publisher.MessageReceiver;
+
+public interface DistributedDelegatingMailboxListener extends 
DelegatingMailboxListener, MessageReceiver {
+
+}

Added: 
james/project/trunk/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/distributed/RegisteredDelegatingMailboxListener.java
URL: 
http://svn.apache.org/viewvc/james/project/trunk/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/distributed/RegisteredDelegatingMailboxListener.java?rev=1716962&view=auto
==============================================================================
--- 
james/project/trunk/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/distributed/RegisteredDelegatingMailboxListener.java
 (added)
+++ 
james/project/trunk/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/distributed/RegisteredDelegatingMailboxListener.java
 Sat Nov 28 13:06:02 2015
@@ -0,0 +1,174 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.mailbox.store.event.distributed;
+
+import org.apache.james.mailbox.MailboxListener;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.exception.MailboxException;
+import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.mailbox.store.event.EventSerializer;
+import org.apache.james.mailbox.store.event.MailboxListenerRegistry;
+import org.apache.james.mailbox.store.publisher.MessageConsumer;
+import org.apache.james.mailbox.store.publisher.Publisher;
+import org.apache.james.mailbox.store.publisher.Topic;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Collection;
+import java.util.Set;
+
+public class RegisteredDelegatingMailboxListener implements 
DistributedDelegatingMailboxListener {
+
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(RegisteredDelegatingMailboxListener.class);
+
+    private final MailboxListenerRegistry mailboxListenerRegistry;
+    private final MailboxPathRegister mailboxPathRegister;
+    private final Publisher publisher;
+    private final EventSerializer eventSerializer;
+
+    public RegisteredDelegatingMailboxListener(EventSerializer eventSerializer,
+                                               Publisher publisher,
+                                               MessageConsumer messageConsumer,
+                                               MailboxPathRegister 
mailboxPathRegister) throws Exception {
+        this.eventSerializer = eventSerializer;
+        this.publisher = publisher;
+        this.mailboxPathRegister = mailboxPathRegister;
+        this.mailboxListenerRegistry = new MailboxListenerRegistry();
+        messageConsumer.setMessageReceiver(this);
+        messageConsumer.init(mailboxPathRegister.getLocalTopic());
+    }
+
+    @Override
+    public ListenerType getType() {
+        return ListenerType.ONCE;
+    }
+
+    @Override
+    public void addListener(MailboxPath path, MailboxListener listener, 
MailboxSession session) throws MailboxException {
+        mailboxListenerRegistry.addListener(path, listener);
+        mailboxPathRegister.register(path);
+    }
+
+    @Override
+    public void addGlobalListener(MailboxListener listener, MailboxSession 
session) throws MailboxException {
+        if (listener.getType().equals(ListenerType.EACH_NODE)) {
+            throw new MailboxException("Attempt to register a global listener 
that need to be called on each node while using a non compatible delegating 
listeners");
+        }
+        mailboxListenerRegistry.addGlobalListener(listener);
+    }
+
+    @Override
+    public void removeListener(MailboxPath mailboxPath, MailboxListener 
listener, MailboxSession session) throws MailboxException {
+        mailboxListenerRegistry.removeListener(mailboxPath, listener);
+        mailboxPathRegister.unregister(mailboxPath);
+    }
+
+    @Override
+    public void removeGlobalListener(MailboxListener listener, MailboxSession 
session) throws MailboxException {
+        mailboxListenerRegistry.removeGlobalListener(listener);
+    }
+
+    @Override
+    public void event(Event event) {
+        try {
+            deliverEventToOnceGlobalListeners(event);
+            deliverToMailboxPathRegisteredListeners(event);
+            sendToRemoteJames(event);
+        } catch (Throwable t) {
+            event.getSession()
+                .getLog()
+                .error("Error while delegating event " + 
event.getClass().getCanonicalName(), t);
+        }
+    }
+
+    public void receiveSerializedEvent(byte[] serializedEvent) {
+        try {
+            Event event = eventSerializer.deSerializeEvent(serializedEvent);
+            deliverToMailboxPathRegisteredListeners(event);
+        } catch (Exception e) {
+            LOGGER.error("Error while receiving serialized event", e);
+        }
+    }
+
+    private void deliverToMailboxPathRegisteredListeners(Event event) throws 
MailboxException {
+        Collection<MailboxListener> listenerSnapshot = 
mailboxListenerRegistry.getLocalMailboxListeners(event.getMailboxPath());
+        if (event instanceof MailboxDeletion && listenerSnapshot.size() > 0) {
+            mailboxListenerRegistry.deleteRegistryFor(event.getMailboxPath());
+            mailboxPathRegister.doCompleteUnRegister(event.getMailboxPath());
+        } else if (event instanceof MailboxRenamed && listenerSnapshot.size() 
> 0) {
+            MailboxRenamed renamed = (MailboxRenamed) event;
+            mailboxListenerRegistry.handleRename(renamed.getMailboxPath(), 
renamed.getNewPath());
+            mailboxPathRegister.doRename(renamed.getMailboxPath(), 
renamed.getNewPath());
+        }
+        for (MailboxListener listener : listenerSnapshot) {
+            deliverEvent(event, listener);
+        }
+    }
+
+    private void deliverEventToOnceGlobalListeners(Event event) {
+        for (MailboxListener mailboxListener : 
mailboxListenerRegistry.getGlobalListeners()) {
+            if (mailboxListener.getType() == ListenerType.ONCE) {
+                deliverEvent(event, mailboxListener);
+            }
+        }
+    }
+
+    private void sendToRemoteJames(Event event) {
+        Set<Topic> topics = 
mailboxPathRegister.getTopics(event.getMailboxPath());
+        topics.remove(mailboxPathRegister.getLocalTopic());
+        if (topics.size() > 0) {
+            sendEventToRemotesJamesByTopic(event, topics);
+        }
+    }
+
+    private void sendEventToRemotesJamesByTopic(Event event, Set<Topic> 
topics) {
+        byte[] serializedEvent;
+        try {
+            serializedEvent = eventSerializer.serializeEvent(event);
+        } catch (Exception e) {
+            event.getSession()
+                .getLog()
+                .error("Unable to serialize " + 
event.getClass().getCanonicalName(), e);
+            return;
+        }
+        for (Topic topic : topics) {
+            try {
+                publisher.publish(topic, serializedEvent);
+            } catch (Throwable t) {
+                event.getSession().getLog().error("Unable to send serialized 
event to topic " + topic);
+            }
+        }
+    }
+
+    private void deliverEvent(Event event, MailboxListener listener) {
+        try {
+            listener.event(event);
+        } catch(Throwable throwable) {
+            event.getSession()
+                .getLog()
+                .error("Error while processing listener "
+                        + listener.getClass().getCanonicalName()
+                        + " for "
+                        + event.getClass().getCanonicalName(),
+                    throwable);
+        }
+    }
+
+}

Added: 
james/project/trunk/mailbox/store/src/test/java/org/apache/james/mailbox/store/event/distributed/RegisteredDelegatingMailboxListenerTest.java
URL: 
http://svn.apache.org/viewvc/james/project/trunk/mailbox/store/src/test/java/org/apache/james/mailbox/store/event/distributed/RegisteredDelegatingMailboxListenerTest.java?rev=1716962&view=auto
==============================================================================
--- 
james/project/trunk/mailbox/store/src/test/java/org/apache/james/mailbox/store/event/distributed/RegisteredDelegatingMailboxListenerTest.java
 (added)
+++ 
james/project/trunk/mailbox/store/src/test/java/org/apache/james/mailbox/store/event/distributed/RegisteredDelegatingMailboxListenerTest.java
 Sat Nov 28 13:06:02 2015
@@ -0,0 +1,278 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.mailbox.store.event.distributed;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.when;
+
+import com.google.common.collect.Sets;
+import org.apache.james.mailbox.MailboxListener;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.exception.MailboxException;
+import org.apache.james.mailbox.mock.MockMailboxSession;
+import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.mailbox.store.event.EventSerializer;
+import org.apache.james.mailbox.store.publisher.MessageConsumer;
+import org.apache.james.mailbox.store.publisher.Publisher;
+import org.apache.james.mailbox.store.publisher.Topic;
+import org.apache.james.mailbox.util.EventCollector;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+
+import java.util.Set;
+
+public class RegisteredDelegatingMailboxListenerTest {
+
+    private static final MailboxPath MAILBOX_PATH = new 
MailboxPath("namespace", "user", "name");
+    private static final MailboxPath MAILBOX_PATH_NEW = new 
MailboxPath("namespace_new", "user_new", "name_new");
+    private static final Topic TOPIC = new Topic("topic");
+    private static final Topic TOPIC_2 = new Topic("topic_2");
+    private static final byte[] BYTES = new byte[0];
+    private static final MailboxSession mailboxSession = new 
MockMailboxSession("benwa");
+    public static final MailboxListener.Event EVENT = new 
MailboxListener.Event(mailboxSession, MAILBOX_PATH) {};
+
+    private RegisteredDelegatingMailboxListener testee;
+    private MailboxPathRegister mockedMailboxPathRegister;
+    private EventSerializer mockedEventSerializer;
+    private Publisher mockedPublisher;
+    private EventCollector mailboxEventCollector;
+    private EventCollector eachEventCollector;
+    private EventCollector onceEventCollector;
+
+    @Before
+    public void setUp() throws Exception {
+        mockedEventSerializer = mock(EventSerializer.class);
+        mockedPublisher = mock(Publisher.class);
+        mockedMailboxPathRegister = mock(MailboxPathRegister.class);
+        MessageConsumer messageConsumer = mock(MessageConsumer.class);
+        testee = new 
RegisteredDelegatingMailboxListener(mockedEventSerializer, mockedPublisher, 
messageConsumer, mockedMailboxPathRegister);
+        mailboxEventCollector = new 
EventCollector(MailboxListener.ListenerType.MAILBOX);
+        eachEventCollector = new 
EventCollector(MailboxListener.ListenerType.EACH_NODE);
+        onceEventCollector = new 
EventCollector(MailboxListener.ListenerType.ONCE);
+    }
+
+    @Test
+    public void 
eventShouldBeLocallyDeliveredIfThereIsNoOtherRegisteredServers() throws 
Exception {
+        testee.addListener(MAILBOX_PATH, mailboxEventCollector, 
mailboxSession);
+        verify(mockedMailboxPathRegister).register(MAILBOX_PATH);
+        when(mockedMailboxPathRegister.getTopics(MAILBOX_PATH)).thenAnswer(new 
Answer<Set<Topic>>() {
+            @Override
+            public Set<Topic> answer(InvocationOnMock invocation) throws 
Throwable {
+                return Sets.newHashSet(TOPIC);
+            }
+        });
+        when(mockedMailboxPathRegister.getLocalTopic()).thenAnswer(new 
Answer<Topic>() {
+            @Override
+            public Topic answer(InvocationOnMock invocation) throws Throwable {
+                return TOPIC;
+            }
+        });
+        testee.event(EVENT);
+        assertThat(mailboxEventCollector.getEvents()).containsOnly(EVENT);
+        verify(mockedMailboxPathRegister, times(2)).getLocalTopic();
+        verify(mockedMailboxPathRegister).getTopics(MAILBOX_PATH);
+        verifyNoMoreInteractions(mockedEventSerializer);
+        verifyNoMoreInteractions(mockedPublisher);
+        verifyNoMoreInteractions(mockedMailboxPathRegister);
+    }
+
+    @Test
+    public void eventShouldBeRemotelySent() throws Exception {
+        testee.addListener(MAILBOX_PATH, mailboxEventCollector, 
mailboxSession);
+        verify(mockedMailboxPathRegister).register(MAILBOX_PATH);
+        when(mockedMailboxPathRegister.getTopics(MAILBOX_PATH)).thenAnswer(new 
Answer<Set<Topic>>() {
+            @Override
+            public Set<Topic> answer(InvocationOnMock invocation) throws 
Throwable {
+                return Sets.newHashSet(TOPIC, TOPIC_2);
+            }
+        });
+        when(mockedMailboxPathRegister.getLocalTopic()).thenAnswer(new 
Answer<Topic>() {
+            @Override
+            public Topic answer(InvocationOnMock invocation) throws Throwable {
+                return TOPIC;
+            }
+        });
+        when(mockedEventSerializer.serializeEvent(EVENT)).thenAnswer(new 
Answer<byte[]>() {
+            @Override
+            public byte[] answer(InvocationOnMock invocation) throws Throwable 
{
+                return BYTES;
+            }
+        });
+        testee.event(EVENT);
+        assertThat(mailboxEventCollector.getEvents()).containsOnly(EVENT);
+        verify(mockedMailboxPathRegister, times(2)).getLocalTopic();
+        verify(mockedMailboxPathRegister).getTopics(MAILBOX_PATH);
+        verify(mockedEventSerializer).serializeEvent(EVENT);
+        verify(mockedPublisher).publish(TOPIC_2, BYTES);
+        verifyNoMoreInteractions(mockedEventSerializer);
+        verifyNoMoreInteractions(mockedPublisher);
+        verifyNoMoreInteractions(mockedMailboxPathRegister);
+    }
+
+    @Test
+    public void onceListenersShouldBeTriggered() throws Exception {
+        MailboxListener.Event event = new 
MailboxListener.Event(mailboxSession, MAILBOX_PATH) {};
+        testee.addGlobalListener(onceEventCollector, mailboxSession);
+        when(mockedMailboxPathRegister.getTopics(MAILBOX_PATH)).thenAnswer(new 
Answer<Set<Topic>>() {
+            @Override
+            public Set<Topic> answer(InvocationOnMock invocation) throws 
Throwable {
+                return Sets.newHashSet(TOPIC);
+            }
+        });
+        when(mockedMailboxPathRegister.getLocalTopic()).thenAnswer(new 
Answer<Topic>() {
+            @Override
+            public Topic answer(InvocationOnMock invocation) throws Throwable {
+                return TOPIC;
+            }
+        });
+        testee.event(event);
+        assertThat(onceEventCollector.getEvents()).containsOnly(event);
+        verify(mockedMailboxPathRegister, times(2)).getLocalTopic();
+        verify(mockedMailboxPathRegister).getTopics(MAILBOX_PATH);
+        verifyNoMoreInteractions(mockedEventSerializer);
+        verifyNoMoreInteractions(mockedPublisher);
+        verifyNoMoreInteractions(mockedMailboxPathRegister);
+    }
+
+    @Test(expected = MailboxException.class)
+    public void eachNodeListenersShouldBeRejected() throws Exception {
+        testee.addGlobalListener(eachEventCollector, mailboxSession);
+    }
+
+    @Test
+    public void distantEventShouldBeLocallyDelivered() throws Exception {
+        testee.addListener(MAILBOX_PATH, mailboxEventCollector, 
mailboxSession);
+        verify(mockedMailboxPathRegister).register(MAILBOX_PATH);
+        when(mockedEventSerializer.deSerializeEvent(BYTES)).thenAnswer(new 
Answer<MailboxListener.Event>() {
+            @Override
+            public MailboxListener.Event answer(InvocationOnMock invocation) 
throws Throwable {
+                return EVENT;
+            }
+        });
+        testee.receiveSerializedEvent(BYTES);
+        assertThat(mailboxEventCollector.getEvents()).containsOnly(EVENT);
+        verify(mockedMailboxPathRegister).getLocalTopic();
+        verify(mockedEventSerializer).deSerializeEvent(BYTES);
+        verifyNoMoreInteractions(mockedEventSerializer);
+        verifyNoMoreInteractions(mockedPublisher);
+        verifyNoMoreInteractions(mockedMailboxPathRegister);
+    }
+
+
+    @Test
+    public void distantEventShouldNotBeDeliveredToOnceGlobalListeners() throws 
Exception {
+        testee.addGlobalListener(onceEventCollector, mailboxSession);
+        when(mockedEventSerializer.deSerializeEvent(BYTES)).thenAnswer(new 
Answer<MailboxListener.Event>() {
+            @Override
+            public MailboxListener.Event answer(InvocationOnMock invocation) 
throws Throwable {
+                return EVENT;
+            }
+        });
+        testee.receiveSerializedEvent(BYTES);
+        assertThat(onceEventCollector.getEvents()).isEmpty();
+        verify(mockedMailboxPathRegister).getLocalTopic();
+        verify(mockedEventSerializer).deSerializeEvent(BYTES);
+        verifyNoMoreInteractions(mockedEventSerializer);
+        verifyNoMoreInteractions(mockedPublisher);
+        verifyNoMoreInteractions(mockedMailboxPathRegister);
+    }
+
+    @Test
+    public void deletionEventsShouldBeWellHandled() throws Exception {
+        MailboxListener.Event event = new 
MailboxListener.MailboxDeletion(mailboxSession, MAILBOX_PATH);
+        testee.addListener(MAILBOX_PATH, mailboxEventCollector, 
mailboxSession);
+        verify(mockedMailboxPathRegister).register(MAILBOX_PATH);
+        when(mockedMailboxPathRegister.getTopics(MAILBOX_PATH)).thenAnswer(new 
Answer<Set<Topic>>() {
+            @Override
+            public Set<Topic> answer(InvocationOnMock invocation) throws 
Throwable {
+                return Sets.newHashSet(TOPIC, TOPIC_2);
+            }
+        });
+        when(mockedMailboxPathRegister.getLocalTopic()).thenAnswer(new 
Answer<Topic>() {
+            @Override
+            public Topic answer(InvocationOnMock invocation) throws Throwable {
+                return TOPIC;
+            }
+        });
+        when(mockedEventSerializer.serializeEvent(event)).thenAnswer(new 
Answer<byte[]>() {
+            @Override
+            public byte[] answer(InvocationOnMock invocation) throws Throwable 
{
+                return BYTES;
+            }
+        });
+        testee.event(event);
+        assertThat(mailboxEventCollector.getEvents()).containsOnly(event);
+        verify(mockedMailboxPathRegister, times(2)).getLocalTopic();
+        verify(mockedMailboxPathRegister).getTopics(MAILBOX_PATH);
+        verify(mockedMailboxPathRegister).doCompleteUnRegister(MAILBOX_PATH);
+        verify(mockedEventSerializer).serializeEvent(event);
+        verify(mockedPublisher).publish(TOPIC_2, BYTES);
+        verifyNoMoreInteractions(mockedEventSerializer);
+        verifyNoMoreInteractions(mockedPublisher);
+        verifyNoMoreInteractions(mockedMailboxPathRegister);
+    }
+
+    @Test
+    public void renameEventsShouldBeWellHandled() throws Exception {
+        MailboxListener.Event event = new 
MailboxListener.MailboxRenamed(mailboxSession, MAILBOX_PATH) {
+            @Override
+            public MailboxPath getNewPath() {
+                return MAILBOX_PATH_NEW;
+            }
+        };
+        testee.addListener(MAILBOX_PATH, mailboxEventCollector, 
mailboxSession);
+        verify(mockedMailboxPathRegister).register(MAILBOX_PATH);
+        when(mockedMailboxPathRegister.getTopics(MAILBOX_PATH)).thenAnswer(new 
Answer<Set<Topic>>() {
+            @Override
+            public Set<Topic> answer(InvocationOnMock invocation) throws 
Throwable {
+                return Sets.newHashSet(TOPIC, TOPIC_2);
+            }
+        });
+        when(mockedMailboxPathRegister.getLocalTopic()).thenAnswer(new 
Answer<Topic>() {
+            @Override
+            public Topic answer(InvocationOnMock invocation) throws Throwable {
+                return TOPIC;
+            }
+        });
+        when(mockedEventSerializer.serializeEvent(event)).thenAnswer(new 
Answer<byte[]>() {
+            @Override
+            public byte[] answer(InvocationOnMock invocation) throws Throwable 
{
+                return BYTES;
+            }
+        });
+        testee.event(event);
+        assertThat(mailboxEventCollector.getEvents()).containsOnly(event);
+        verify(mockedMailboxPathRegister, times(2)).getLocalTopic();
+        verify(mockedMailboxPathRegister).getTopics(MAILBOX_PATH);
+        verify(mockedMailboxPathRegister).doRename(MAILBOX_PATH, 
MAILBOX_PATH_NEW);
+        verify(mockedEventSerializer).serializeEvent(event);
+        verify(mockedPublisher).publish(TOPIC_2, BYTES);
+        verifyNoMoreInteractions(mockedEventSerializer);
+        verifyNoMoreInteractions(mockedPublisher);
+        verifyNoMoreInteractions(mockedMailboxPathRegister);
+    }
+}



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

Reply via email to