Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JAMES-2553 Rewrite GuiceJamesServerTest test in JUNIT 5
Needs for a way to not start automatically James server extension
- Loading branch information
Showing
1 changed file
with
66 additions
and
90 deletions.
There are no files selected for viewing
156 changes: 66 additions & 90 deletions
156
server/container/guice/memory-guice/src/test/java/org/apache/james/GuiceJamesServerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
} |