JAMES-2368 Define an interface for MailboxListenersLoader

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

Branch: refs/heads/master
Commit: 6e64fcd71fc32d4aad65531bcc071ba025354d9d
Parents: fdc3795
Author: Antoine Duprat <adup...@linagora.com>
Authored: Wed May 2 14:50:10 2018 +0200
Committer: benwa <btell...@linagora.com>
Committed: Fri May 4 13:39:07 2018 +0700

----------------------------------------------------------------------
 .../modules/mailbox/DefaultEventModule.java     |  7 ++--
 .../modules/mailbox/MailboxListenersLoader.java | 28 +++++++++++++++
 .../mailbox/MailboxListenersLoaderImpl.java     | 36 +++++++++++---------
 .../mailbox/MailboxListenersLoaderImplTest.java | 25 +++++++-------
 4 files changed, 64 insertions(+), 32 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/6e64fcd7/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/DefaultEventModule.java
----------------------------------------------------------------------
diff --git 
a/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/DefaultEventModule.java
 
b/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/DefaultEventModule.java
index c5895a9..98c118f 100644
--- 
a/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/DefaultEventModule.java
+++ 
b/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/DefaultEventModule.java
@@ -57,17 +57,18 @@ public class DefaultEventModule extends AbstractModule {
         bind(ListeningCurrentQuotaUpdater.class).in(Scopes.SINGLETON);
         bind(MailboxAnnotationListener.class).in(Scopes.SINGLETON);
 
-        bind(MailboxListenersLoader.class).in(Scopes.SINGLETON);
+        bind(MailboxListenersLoaderImpl.class).in(Scopes.SINGLETON);
+        
bind(MailboxListenersLoader.class).to(MailboxListenersLoaderImpl.class);
         Multibinder.newSetBinder(binder(), MailboxListener.class);
     }
 
     @Singleton
     public static class ListenerRegistrationPerformer implements 
ConfigurationPerformer {
         private final ConfigurationProvider configurationProvider;
-        private final MailboxListenersLoader listeners;
+        private final MailboxListenersLoaderImpl listeners;
 
         @Inject
-        public ListenerRegistrationPerformer(ConfigurationProvider 
configurationProvider, MailboxListenersLoader listeners) {
+        public ListenerRegistrationPerformer(ConfigurationProvider 
configurationProvider, MailboxListenersLoaderImpl listeners) {
             this.configurationProvider = configurationProvider;
             this.listeners = listeners;
         }

http://git-wip-us.apache.org/repos/asf/james-project/blob/6e64fcd7/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/MailboxListenersLoader.java
----------------------------------------------------------------------
diff --git 
a/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/MailboxListenersLoader.java
 
b/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/MailboxListenersLoader.java
new file mode 100644
index 0000000..46757a9
--- /dev/null
+++ 
b/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/MailboxListenersLoader.java
@@ -0,0 +1,28 @@
+/****************************************************************
+ * 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.modules.mailbox;
+
+import org.apache.james.mailbox.MailboxListener;
+
+public interface MailboxListenersLoader {
+
+    MailboxListener createListener(ListenerConfiguration configuration);
+
+    void register(MailboxListener listener);
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/6e64fcd7/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/MailboxListenersLoaderImpl.java
----------------------------------------------------------------------
diff --git 
a/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/MailboxListenersLoaderImpl.java
 
b/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/MailboxListenersLoaderImpl.java
index 60219b8..16b7608 100644
--- 
a/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/MailboxListenersLoaderImpl.java
+++ 
b/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/MailboxListenersLoaderImpl.java
@@ -29,13 +29,12 @@ import org.apache.james.utils.ExtendedClassLoader;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.annotations.VisibleForTesting;
 import com.google.inject.Inject;
 import com.google.inject.Injector;
 
-public class MailboxListenersLoader implements Configurable {
+public class MailboxListenersLoaderImpl implements Configurable, 
MailboxListenersLoader {
 
-    private static final Logger LOGGER = 
LoggerFactory.getLogger(MailboxListenersLoader.class);
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(MailboxListenersLoaderImpl.class);
 
     private final Injector injector;
     private final MailboxListenerRegistry registry;
@@ -43,7 +42,7 @@ public class MailboxListenersLoader implements Configurable {
     private final Set<MailboxListener> guiceDefinedListeners;
 
     @Inject
-    public MailboxListenersLoader(Injector injector, MailboxListenerRegistry 
registry,
+    public MailboxListenersLoaderImpl(Injector injector, 
MailboxListenerRegistry registry,
                                   ExtendedClassLoader classLoader, 
Set<MailboxListener> guiceDefinedListeners) {
         this.injector = injector;
         this.registry = registry;
@@ -58,29 +57,32 @@ public class MailboxListenersLoader implements Configurable 
{
         ListenersConfiguration listenersConfiguration = 
ListenersConfiguration.from(configuration);
 
         guiceDefinedListeners.forEach(this::register);
-        listenersConfiguration.getListenersConfiguration()
-            .forEach(this::configureListener);
+        listenersConfiguration.getListenersConfiguration().stream()
+            .map(this::createListener)
+            .forEach(this::register);
     }
 
-    @VisibleForTesting void configureListener(ListenerConfiguration 
configuration) {
+    @Override
+    public void register(MailboxListener listener) {
+        try {
+            registry.addGlobalListener(listener);
+        } catch (MailboxException e) {
+            LOGGER.error("Error while registering global listener {}", 
listener, e);
+            throw new RuntimeException(e);
+        }
+    }
+
+    @Override
+    public MailboxListener createListener(ListenerConfiguration configuration) 
{
         String listenerClass = configuration.getClazz();
         try {
             LOGGER.info("Loading user registered mailbox listener {}", 
listenerClass);
             Class<MailboxListener> clazz = 
classLoader.locateClass(listenerClass);
             MailboxListener listener = injector.getInstance(clazz);
-            register(listener);
+            return listener;
         } catch (ClassNotFoundException e) {
             LOGGER.error("Error while loading user registered global listener 
{}", listenerClass, e);
             throw new RuntimeException(e);
         }
     }
-
-    private void register(MailboxListener listener) {
-        try {
-            registry.addGlobalListener(listener);
-        } catch (MailboxException e) {
-            LOGGER.error("Error while registering global listener {}", 
listener, e);
-            throw new RuntimeException(e);
-        }
-    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/6e64fcd7/server/container/guice/mailbox/src/test/java/org/apache/james/modules/mailbox/MailboxListenersLoaderImplTest.java
----------------------------------------------------------------------
diff --git 
a/server/container/guice/mailbox/src/test/java/org/apache/james/modules/mailbox/MailboxListenersLoaderImplTest.java
 
b/server/container/guice/mailbox/src/test/java/org/apache/james/modules/mailbox/MailboxListenersLoaderImplTest.java
index d1673db..d243db4 100644
--- 
a/server/container/guice/mailbox/src/test/java/org/apache/james/modules/mailbox/MailboxListenersLoaderImplTest.java
+++ 
b/server/container/guice/mailbox/src/test/java/org/apache/james/modules/mailbox/MailboxListenersLoaderImplTest.java
@@ -31,6 +31,7 @@ import java.nio.charset.StandardCharsets;
 import org.apache.commons.configuration.ConfigurationException;
 import org.apache.commons.configuration.DefaultConfigurationBuilder;
 import org.apache.james.filesystem.api.FileSystem;
+import org.apache.james.mailbox.MailboxListener;
 import org.apache.james.mailbox.store.event.MailboxListenerRegistry;
 import org.apache.james.utils.ExtendedClassLoader;
 import org.junit.Before;
@@ -39,10 +40,10 @@ import org.junit.Test;
 import com.google.common.collect.ImmutableSet;
 import com.google.inject.Guice;
 
-public class MailboxListenersLoaderTest {
+public class MailboxListenersLoaderImplTest {
 
     private MailboxListenerRegistry registry;
-    private MailboxListenersLoader testee;
+    private MailboxListenersLoaderImpl testee;
 
     @Before
     public void setup() throws Exception {
@@ -51,41 +52,41 @@ public class MailboxListenersLoaderTest {
             .thenThrow(new FileNotFoundException());
 
         registry = new MailboxListenerRegistry();
-        testee = new MailboxListenersLoader(Guice.createInjector(), registry,
+        testee = new MailboxListenersLoaderImpl(Guice.createInjector(), 
registry,
             new ExtendedClassLoader(fileSystem), ImmutableSet.of());
     }
 
     @Test
-    public void configureListenerShouldThrowWhenClassCantBeLoaded() {
+    public void createListenerShouldThrowWhenClassCantBeLoaded() {
         ListenerConfiguration configuration = new 
ListenerConfiguration("MyUnknownClass");
 
-        assertThatThrownBy(() -> testee.configureListener(configuration))
+        assertThatThrownBy(() -> testee.createListener(configuration))
             .isInstanceOf(RuntimeException.class);
     }
 
     @Test
-    public void 
configureListenerShouldThrowWhenClassCantBeCastToMailboxListener() {
+    public void 
createListenerShouldThrowWhenClassCantBeCastToMailboxListener() {
         ListenerConfiguration configuration = new 
ListenerConfiguration("java.lang.String");
 
-        assertThatThrownBy(() -> testee.configureListener(configuration))
+        assertThatThrownBy(() -> testee.createListener(configuration))
             .isInstanceOf(RuntimeException.class);
     }
 
     @Test
-    public void configureListenerShouldThrowWhenNotFullClassName() {
+    public void createListenerShouldThrowWhenNotFullClassName() {
         ListenerConfiguration configuration = new 
ListenerConfiguration("NoopMailboxListener");
 
-        assertThatThrownBy(() -> testee.configureListener(configuration))
+        assertThatThrownBy(() -> testee.createListener(configuration))
             .isInstanceOf(RuntimeException.class);
     }
 
     @Test
-    public void 
configureListenerShouldAddMailboxListenerWhenConfigurationIsGood() {
+    public void 
createListenerShouldReturnMailboxListenerWhenConfigurationIsGood() {
         ListenerConfiguration configuration = new 
ListenerConfiguration("org.apache.james.modules.mailbox.NoopMailboxListener");
 
-        testee.configureListener(configuration);
+        MailboxListener listener = testee.createListener(configuration);
 
-        assertThat(registry.getGlobalListeners()).hasSize(1);
+        assertThat(listener).isInstanceOf(NoopMailboxListener.class);
     }
 
     @Test


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