JAMES-2553 Rewrite GuiceJamesServerTest test in JUNIT 5

Needs for a way to not start automatically James server extension


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

Branch: refs/heads/master
Commit: 8080ee48c03433eb0fc447356999877ac93b5637
Parents: b03b2a6
Author: Benoit Tellier <[email protected]>
Authored: Mon Oct 1 10:35:55 2018 +0700
Committer: Benoit Tellier <[email protected]>
Committed: Thu Oct 4 17:48:43 2018 +0700

----------------------------------------------------------------------
 .../org/apache/james/GuiceJamesServerTest.java  | 156 ++++++++-----------
 1 file changed, 66 insertions(+), 90 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/8080ee48/server/container/guice/memory-guice/src/test/java/org/apache/james/GuiceJamesServerTest.java
----------------------------------------------------------------------
diff --git 
a/server/container/guice/memory-guice/src/test/java/org/apache/james/GuiceJamesServerTest.java
 
b/server/container/guice/memory-guice/src/test/java/org/apache/james/GuiceJamesServerTest.java
index 0752504..7edf781 100644
--- 
a/server/container/guice/memory-guice/src/test/java/org/apache/james/GuiceJamesServerTest.java
+++ 
b/server/container/guice/memory-guice/src/test/java/org/apache/james/GuiceJamesServerTest.java
@@ -1,127 +1,103 @@
 package org.apache.james;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
 
-import java.io.IOException;
 import java.util.List;
 
 import org.apache.james.lifecycle.api.Configurable;
+import org.apache.james.mailbox.extractor.TextExtractor;
+import org.apache.james.mailbox.store.search.PDFTextExtractor;
+import org.apache.james.modules.TestJMAPServerModule;
 import org.apache.james.utils.ConfigurationPerformer;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
+import org.junit.jupiter.api.Nested;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.google.common.collect.ImmutableList;
 import com.google.inject.multibindings.Multibinder;
 
-public class GuiceJamesServerTest {
+class GuiceJamesServerTest {
     private static final Logger LOGGER = 
LoggerFactory.getLogger(GuiceJamesServerTest.class);
 
-    public static final ConfigurationPerformer 
THROWING_CONFIGURATION_PERFORMER = new ConfigurationPerformer() {
-        @Override
-        public void initModule() {
-            throw new RuntimeException();
-        }
-
-        @Override
-        public List<Class<? extends Configurable>> forClasses() {
-            return ImmutableList.of();
-        }
-    };
-    @Rule
-    public ExpectedException expectedException = ExpectedException.none();
-
-    @Rule
-    public MemoryJmapTestRule memoryJmapTestRule = new MemoryJmapTestRule();
-    private GuiceJamesServer guiceJamesServer;
-
+    private static final int LIMIT_TO_10_MESSAGES = 10;
 
-    @Before
-    public void setUp() throws IOException {
-        guiceJamesServer = memoryJmapTestRule.jmapServer();
-    }
-
-    @After
-    public void tearDown() {
-        guiceJamesServer.stop();
-    }
+    @Nested
+    class NormalBehaviour {
+        @RegisterExtension
+        JamesServerExtension jamesServerExtension = new 
JamesServerExtensionBuilder()
+            .server(configuration -> 
GuiceJamesServer.forConfiguration(configuration)
+                
.combineWith(MemoryJamesServerMain.IN_MEMORY_SERVER_AGGREGATE_MODULE)
+                .overrideWith(new TestJMAPServerModule(LIMIT_TO_10_MESSAGES))
+                .overrideWith(binder -> 
binder.bind(TextExtractor.class).to(PDFTextExtractor.class)))
+            .disableAutoStart()
+            .build();
 
-    @Test
-    public void serverShouldBeStartedAfterCallingStart() throws Exception {
-        guiceJamesServer.start();
-
-        assertThat(guiceJamesServer.isStarted()).isTrue();
-    }
+        @Test
+        void serverShouldBeStartedAfterCallingStart(GuiceJamesServer server) 
throws Exception {
+            server.start();
 
-    @Test
-    public void serverShouldNotBeStartedAfterCallingStop() throws Exception {
-        guiceJamesServer.start();
+            assertThat(server.isStarted()).isTrue();
+        }
 
-        guiceJamesServer.stop();
+        @Test
+        void serverShouldNotBeStartedAfterCallingStop(GuiceJamesServer server) 
throws Exception {
+            server.start();
 
-        assertThat(guiceJamesServer.isStarted()).isFalse();
-    }
+            server.stop();
 
-    @Test
-    public void serverShouldNotBeStartedBeforeCallingStart() throws Exception {
-        assertThat(guiceJamesServer.isStarted()).isFalse();
-    }
+            assertThat(server.isStarted()).isFalse();
+        }
 
-    @Test
-    public void serverShouldPropagateUncaughtConfigurationException() throws 
Exception {
-        expectedException.expect(RuntimeException.class);
-
-        GuiceJamesServer overWrittenServer = null;
-
-        try {
-            overWrittenServer = this.guiceJamesServer.overrideWith(
-                binder -> Multibinder.newSetBinder(binder, 
ConfigurationPerformer.class).addBinding().toInstance(
-                    new ConfigurationPerformer() {
-                        @Override
-                        public void initModule() {
-                            throw new RuntimeException();
-                        }
-
-                        @Override
-                        public List<Class<? extends Configurable>> 
forClasses() {
-                            return ImmutableList.of();
-                        }
-                    }
-                )
-            );
-            overWrittenServer.start();
-        } finally {
-            if (overWrittenServer != null) {
-                overWrittenServer.stop();
-            }
+        @Test
+        void serverShouldNotBeStartedBeforeCallingStart(GuiceJamesServer 
server) {
+            assertThat(server.isStarted()).isFalse();
         }
     }
 
-    @Test
-    public void serverShouldNotBeStartedOnUncaughtException() throws Exception 
{
-        GuiceJamesServer overWrittenServer = null;
+    @Nested
+    class InitFailed {
+        private final ConfigurationPerformer THROWING_CONFIGURATION_PERFORMER 
= new ConfigurationPerformer() {
+            @Override
+            public void initModule() {
+                throw new RuntimeException();
+            }
 
-        try {
-            overWrittenServer = this.guiceJamesServer.overrideWith(
-                binder -> Multibinder.newSetBinder(binder, 
ConfigurationPerformer.class)
+            @Override
+            public List<Class<? extends Configurable>> forClasses() {
+                return ImmutableList.of();
+            }
+        };
+
+        @RegisterExtension
+        JamesServerExtension jamesServerExtension = new 
JamesServerExtensionBuilder()
+            .server(configuration -> 
GuiceJamesServer.forConfiguration(configuration)
+                
.combineWith(MemoryJamesServerMain.IN_MEMORY_SERVER_AGGREGATE_MODULE)
+                .overrideWith(new TestJMAPServerModule(LIMIT_TO_10_MESSAGES))
+                .overrideWith(binder -> 
binder.bind(TextExtractor.class).to(PDFTextExtractor.class))
+                .overrideWith(binder -> Multibinder.newSetBinder(binder, 
ConfigurationPerformer.class)
                     .addBinding()
-                    .toInstance(THROWING_CONFIGURATION_PERFORMER));
+                    .toInstance(THROWING_CONFIGURATION_PERFORMER)))
+            .disableAutoStart()
+            .build();
+
+        @Test
+        void 
serverShouldPropagateUncaughtConfigurationException(GuiceJamesServer server) {
+            assertThatThrownBy(server::start)
+                .isInstanceOf(RuntimeException.class);
+        }
 
+        @Test
+        void serverShouldNotBeStartedOnUncaughtException(GuiceJamesServer 
server) throws Exception {
             try {
-                overWrittenServer.start();
+                server.start();
             } catch (RuntimeException e) {
                 LOGGER.info("Ignored expected exception", e);
             }
 
-            assertThat(overWrittenServer.isStarted()).isFalse();
-        } finally {
-            if (overWrittenServer != null) {
-                overWrittenServer.stop();
-            }
+            assertThat(server.isStarted()).isFalse();
         }
     }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to