Search in sources :

Example 6 with EmbeddedServerRunner

use of com.github.mjeanroy.junit.servers.engine.EmbeddedServerRunner in project junit-servers by mjeanroy.

the class TomcatServerExtensionTest method it_should_start_server_with_default_configuration_before_all_tests.

@Test
void it_should_start_server_with_default_configuration_before_all_tests() {
    final TomcatServerExtension extension = new TomcatServerExtension();
    final FixtureClass testInstance = new FixtureClass();
    final FakeExtensionContext context = new FakeExtensionContext(testInstance);
    extension.beforeAll(context);
    final FakeStore store = context.getSingleStore();
    final EmbeddedServerRunner serverAdapter = store.get("serverAdapter", EmbeddedServerRunner.class);
    assertThat(serverAdapter).isNotNull();
    assertThat(serverAdapter.getServer()).isNotNull().isExactlyInstanceOf(EmbeddedTomcat.class);
    assertThat(serverAdapter.getServer().isStarted()).isTrue();
}
Also used : EmbeddedServerRunner(com.github.mjeanroy.junit.servers.engine.EmbeddedServerRunner) Test(org.junit.jupiter.api.Test)

Example 7 with EmbeddedServerRunner

use of com.github.mjeanroy.junit.servers.engine.EmbeddedServerRunner in project junit-servers by mjeanroy.

the class TomcatServerExtensionTest method it_should_start_tomcat_server_using_given_configuration_before_all_tests.

@Test
void it_should_start_tomcat_server_using_given_configuration_before_all_tests() {
    final EmbeddedTomcatConfiguration configuration = EmbeddedTomcatConfiguration.defaultConfiguration();
    final TomcatServerExtension extension = new TomcatServerExtension(configuration);
    final FixtureClass testInstance = new FixtureClass();
    final FakeExtensionContext context = new FakeExtensionContext(testInstance);
    extension.beforeAll(context);
    final FakeStore store = context.getSingleStore();
    final EmbeddedServerRunner serverAdapter = store.get("serverAdapter", EmbeddedServerRunner.class);
    assertThat(serverAdapter).isNotNull();
    assertThat(serverAdapter.getServer()).isNotNull().isExactlyInstanceOf(EmbeddedTomcat.class);
    assertThat(serverAdapter.getServer().getConfiguration()).isSameAs(configuration);
    assertThat(serverAdapter.getServer().isStarted()).isTrue();
}
Also used : EmbeddedServerRunner(com.github.mjeanroy.junit.servers.engine.EmbeddedServerRunner) EmbeddedTomcatConfiguration(com.github.mjeanroy.junit.servers.tomcat.EmbeddedTomcatConfiguration) Test(org.junit.jupiter.api.Test)

Example 8 with EmbeddedServerRunner

use of com.github.mjeanroy.junit.servers.engine.EmbeddedServerRunner in project junit-servers by mjeanroy.

the class JunitServerExtension method registerEmbeddedServer.

/**
 * Start and register the embedded server.
 *
 * The embedded server may be used in two modes:
 *
 * <ul>
 *   <li>Started before all tests, and stopped after all tests, this is the static mode (the extension has been used as a static extension).</li>
 *   <li>Started before each tests, and stopped after eacg tests, this is the non static mode (the extension has not been used as a static extension).</li>
 * </ul>
 *
 * @param context The test context.
 * @param staticMode {@code true} if the extension has been registered as a static extension, {@code false} otherwise.
 * @return The registered adapter.
 */
private EmbeddedServerRunner registerEmbeddedServer(ExtensionContext context, boolean staticMode) {
    log.debug("Register embedded server to junit extension context");
    Class<?> testClass = context.getRequiredTestClass();
    EmbeddedServer<?> server = this.server == null ? instantiateServer(testClass, configuration) : this.server;
    EmbeddedServerRunner serverAdapter = new EmbeddedServerRunner(server);
    serverAdapter.beforeAll();
    putEmbeddedServerAdapterInStore(context, serverAdapter, staticMode);
    return serverAdapter;
}
Also used : EmbeddedServerRunner(com.github.mjeanroy.junit.servers.engine.EmbeddedServerRunner)

Example 9 with EmbeddedServerRunner

use of com.github.mjeanroy.junit.servers.engine.EmbeddedServerRunner in project junit-servers by mjeanroy.

the class JunitServerExtension method beforeEach.

@Override
public void beforeEach(ExtensionContext context) {
    EmbeddedServerRunner serverAdapter = findEmbeddedServerAdapterInStore(context);
    // The extension was not declared as a static extension.
    if (serverAdapter == null) {
        serverAdapter = registerEmbeddedServer(context, false);
    }
    EmbeddedServer<?> server = serverAdapter.getServer();
    AbstractConfiguration configuration = server.getConfiguration();
    AnnotationsHandlerRunner annotationsAdapter = new AnnotationsHandlerRunner(server, configuration);
    annotationsAdapter.beforeEach(context.getRequiredTestInstance());
    putAnnotationsHandlerAdapterInStore(context, annotationsAdapter);
}
Also used : AbstractConfiguration(com.github.mjeanroy.junit.servers.servers.AbstractConfiguration) EmbeddedServerRunner(com.github.mjeanroy.junit.servers.engine.EmbeddedServerRunner) AnnotationsHandlerRunner(com.github.mjeanroy.junit.servers.engine.AnnotationsHandlerRunner)

Example 10 with EmbeddedServerRunner

use of com.github.mjeanroy.junit.servers.engine.EmbeddedServerRunner in project junit-servers by mjeanroy.

the class JunitServerExtension method unregisterEmbeddedServer.

/**
 * Stop and remove from the store the started embedded server.
 *
 * @param context The test context.
 * @param staticMode {@code true} if the extension has been registered as a static extension, {@code false} otherwise.
 * @see #registerEmbeddedServer(ExtensionContext, boolean)
 */
private void unregisterEmbeddedServer(ExtensionContext context, boolean staticMode) {
    log.debug("Attempt to unregister embedded server to junit extension context");
    boolean registeredAsStatic = findInStore(context, SERVER_RUNNER_STATIC_MODE);
    if (registeredAsStatic == staticMode) {
        try {
            EmbeddedServerRunner serverAdapter = findEmbeddedServerAdapterInStore(context);
            serverAdapter.afterAll();
        } finally {
            removeEmbeddedServerAdapterFromStore(context);
        }
    }
}
Also used : EmbeddedServerRunner(com.github.mjeanroy.junit.servers.engine.EmbeddedServerRunner)

Aggregations

EmbeddedServerRunner (com.github.mjeanroy.junit.servers.engine.EmbeddedServerRunner)23 Test (org.junit.jupiter.api.Test)16 FixtureClass (com.github.mjeanroy.junit.servers.utils.fixtures.FixtureClass)10 EmbeddedServerMockBuilder (com.github.mjeanroy.junit.servers.utils.builders.EmbeddedServerMockBuilder)4 BeforeEach (org.junit.jupiter.api.BeforeEach)3 AnnotationsHandlerRunner (com.github.mjeanroy.junit.servers.engine.AnnotationsHandlerRunner)2 AbstractConfiguration (com.github.mjeanroy.junit.servers.servers.AbstractConfiguration)2 ParameterContext (org.junit.jupiter.api.extension.ParameterContext)2 EmbeddedJetty (com.github.mjeanroy.junit.servers.jetty.EmbeddedJetty)1 EmbeddedJettyConfiguration (com.github.mjeanroy.junit.servers.jetty.EmbeddedJettyConfiguration)1 EmbeddedJettyMockBuilder (com.github.mjeanroy.junit.servers.jetty.tests.EmbeddedJettyMockBuilder)1 EmbeddedTomcat (com.github.mjeanroy.junit.servers.tomcat.EmbeddedTomcat)1 EmbeddedTomcatConfiguration (com.github.mjeanroy.junit.servers.tomcat.EmbeddedTomcatConfiguration)1 EmbeddedTomcatMockBuilder (com.github.mjeanroy.junit.servers.tomcat.tests.builders.EmbeddedTomcatMockBuilder)1 FakeEmbeddedServerConfiguration (com.github.mjeanroy.junit.servers.utils.impl.FakeEmbeddedServerConfiguration)1 Parameter (java.lang.reflect.Parameter)1