diff --git a/junit-servers-tomcat-10/src/test/java/com/github/mjeanroy/junit/servers/tomcat10/EmbeddedTomcatTest.java b/junit-servers-tomcat-10/src/test/java/com/github/mjeanroy/junit/servers/tomcat10/EmbeddedTomcatTest.java index 8afbe6c4..43885158 100644 --- a/junit-servers-tomcat-10/src/test/java/com/github/mjeanroy/junit/servers/tomcat10/EmbeddedTomcatTest.java +++ b/junit-servers-tomcat-10/src/test/java/com/github/mjeanroy/junit/servers/tomcat10/EmbeddedTomcatTest.java @@ -29,12 +29,12 @@ import org.apache.catalina.Container; import org.apache.catalina.Context; import org.apache.catalina.LifecycleState; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; import java.io.File; import java.nio.file.Files; +import java.util.function.Consumer; import static com.github.mjeanroy.junit.servers.testing.HttpTestUtils.get; import static com.github.mjeanroy.junit.servers.testing.HttpTestUtils.localhost; @@ -46,41 +46,31 @@ class EmbeddedTomcatTest { private static final String PATH = "junit-servers-tomcat-10/"; - private EmbeddedTomcat tomcat; - - @AfterEach - void tearDown() { - if (tomcat != null) { - tomcat.stop(); - } - } - @Test void it_should_start_tomcat() { - tomcat = new EmbeddedTomcat(defaultConfiguration()); - tomcat.start(); - - assertThat(tomcat.isStarted()).isTrue(); - assertThat(tomcat.getPort()).isNotZero(); - assertThat(tomcat.getScheme()).isEqualTo("http"); - assertThat(tomcat.getHost()).isEqualTo("localhost"); - assertThat(tomcat.getPath()).isEqualTo("/"); - assertThat(tomcat.getUrl()).isEqualTo(localhost(tomcat.getPort())); + run(defaultConfiguration(), (tomcat) -> { + assertThat(tomcat.isStarted()).isTrue(); + assertThat(tomcat.getPort()).isNotZero(); + assertThat(tomcat.getScheme()).isEqualTo("http"); + assertThat(tomcat.getHost()).isEqualTo("localhost"); + assertThat(tomcat.getPath()).isEqualTo("/"); + assertThat(tomcat.getUrl()).isEqualTo(localhost(tomcat.getPort())); + }); } @Test void it_should_stop_tomcat() { - tomcat = new EmbeddedTomcat(defaultConfiguration()); - tomcat.start(); + EmbeddedTomcat tomcat = new EmbeddedTomcat(defaultConfiguration()); - assertThat(tomcat.isStarted()).isTrue(); - assertThat(tomcat.getPort()).isNotZero(); - assertThat(tomcat.getScheme()).isEqualTo("http"); - assertThat(tomcat.getHost()).isEqualTo("localhost"); - assertThat(tomcat.getPath()).isEqualTo("/"); - assertThat(tomcat.getUrl()).isEqualTo(localhost(tomcat.getPort())); + doRun(tomcat, () -> { + assertThat(tomcat.isStarted()).isTrue(); + assertThat(tomcat.getPort()).isNotZero(); + assertThat(tomcat.getScheme()).isEqualTo("http"); + assertThat(tomcat.getHost()).isEqualTo("localhost"); + assertThat(tomcat.getPath()).isEqualTo("/"); + assertThat(tomcat.getUrl()).isEqualTo(localhost(tomcat.getPort())); + }); - tomcat.stop(); assertThat(tomcat.isStarted()).isFalse(); assertThat(tomcat.getPort()).isZero(); assertThat(tomcat.getScheme()).isEqualTo("http"); @@ -91,109 +81,107 @@ void it_should_stop_tomcat() { @Test void it_should_destroy_context_on_stop() { - tomcat = new EmbeddedTomcat(defaultConfiguration()); + EmbeddedTomcat tomcat = new EmbeddedTomcat(defaultConfiguration()); assertThat((Context) readPrivate(tomcat, "context")).isNull(); - tomcat.start(); + WrappedContext startedContext = new WrappedContext(); - Context ctx = readPrivate(tomcat, "context"); - assertThat(ctx).isNotNull(); - assertThat(ctx.getState()).isEqualTo(LifecycleState.STARTED); + doRun(tomcat, () -> { + startedContext.ctx = readPrivate(tomcat, "context"); + assertThat(startedContext.ctx).isNotNull(); + assertThat(startedContext.ctx.getState()).isEqualTo(LifecycleState.STARTED); + }); - tomcat.stop(); assertThat((Context) readPrivate(tomcat, "context")).isNull(); - assertThat(ctx.getState()).isEqualTo(LifecycleState.DESTROYED); + assertThat(startedContext.ctx.getState()).isEqualTo(LifecycleState.DESTROYED); } @Test void it_should_delete_base_dir_on_stop() { - tomcat = new EmbeddedTomcat(defaultConfigurationBuilder().deleteBaseDir().build()); - File baseDir = new File(tomcat.getConfiguration().getBaseDir()); + EmbeddedTomcatConfiguration configuration = defaultConfigurationBuilder().deleteBaseDir().build(); + File baseDir = new File(configuration.getBaseDir()); - assertThat(baseDir).exists(); - - tomcat.start(); - tomcat.stop(); + run(configuration, (tomcat) -> + assertThat(baseDir).exists() + ); assertThat(baseDir).doesNotExist(); } @Test void it_should_keep_base_dir_on_stop() { - tomcat = new EmbeddedTomcat(defaultConfigurationBuilder().keepBaseDir().build()); - File baseDir = new File(tomcat.getConfiguration().getBaseDir()); + EmbeddedTomcatConfiguration configuration = defaultConfigurationBuilder().keepBaseDir().build(); + File baseDir = new File(configuration.getBaseDir()); - assertThat(baseDir).exists(); - - tomcat.start(); - tomcat.stop(); + run(configuration, (tomcat) -> + assertThat(baseDir).exists() + ); assertThat(baseDir).exists(); } @Test void it_should_get_servlet_context() { - tomcat = new EmbeddedTomcat(defaultConfiguration()); - tomcat.start(); - assertThat(tomcat.getServletContext()).isNotNull(); + run(defaultConfiguration(), (tomcat) -> + assertThat(tomcat.getServletContext()).isNotNull() + ); } @Test void it_should_get_original_tomcat() { - tomcat = new EmbeddedTomcat(); - assertThat(tomcat.getDelegate()).isNotNull(); + run((tomcat) -> + assertThat(tomcat.getDelegate()).isNotNull() + ); } @Test - void it_should_create_meta_inf_directory_if_it_does_not_exist(@TempDir File baseDir) { - final File metaInf = new File(baseDir, "META-INF"); - - tomcat = new EmbeddedTomcat(EmbeddedTomcatConfiguration.builder() + void it_should_create_meta_inf_directory_if_it_does_not_exist(@TempDir File baseDir) throws Exception { + File metaInf = new File(baseDir, "META-INF"); + EmbeddedTomcatConfiguration configuration = EmbeddedTomcatConfiguration.builder() .withClasspath(baseDir.getAbsolutePath()) .withWebapp(baseDir) .enableForceMetaInf() - .build()); + .build(); assertThat(metaInf).doesNotExist(); - tomcat.start(); - assertThat(metaInf).exists(); + run(configuration, (tomcat) -> + assertThat(metaInf).exists() + ); } @Test void it_should_add_parent_classloader(@TempDir File tmpDir) throws Exception { - final File tmpFile = Files.createTempFile(tmpDir.toPath(), null, null).toFile(); - final File dir = tmpFile.getParentFile(); - - tomcat = new EmbeddedTomcat(EmbeddedTomcatConfiguration.builder() + File tmpFile = Files.createTempFile(tmpDir.toPath(), null, null).toFile(); + File dir = tmpFile.getParentFile(); + EmbeddedTomcatConfiguration configuration = EmbeddedTomcatConfiguration.builder() .withWebapp(dir) .withParentClasspath(dir.toURI().toURL()) - .build()); - - tomcat.start(); + .build(); - final Container[] containers = tomcat.getDelegate().getHost().findChildren(); - final ClassLoader cl = containers[0].getParentClassLoader(); + run(configuration, (tomcat) -> { + Container[] containers = tomcat.getDelegate().getHost().findChildren(); + ClassLoader cl = containers[0].getParentClassLoader(); - assertThat(cl).isNotNull(); - assertThat(cl.getResource("hello-world.html")).isNotNull(); - assertThat(cl.getResource(tmpFile.getName())).isNotNull(); + assertThat(cl).isNotNull(); + assertThat(cl.getResource("hello-world.html")).isNotNull(); + assertThat(cl.getResource(tmpFile.getName())).isNotNull(); + }); } @Test void it_should_override_web_xml() { File descriptor = getFileFromClasspath("/custom-web.xml"); - - tomcat = new EmbeddedTomcat(defaultConfigurationBuilder() + EmbeddedTomcatConfiguration configuration = defaultConfigurationBuilder() .withOverrideDescriptor(descriptor.getAbsolutePath()) - .build()); - - tomcat.start(); - - HttpResponse rsp = get(tomcat.getUrl()); - assertThat(rsp).isNotNull(); - assertThat(rsp.getStatusCode()).isEqualTo(200); - assertThat(rsp.getResponseBody()).isNotEmpty().contains("Hello World"); + .build(); + + run(configuration, (tomcat) -> { + HttpResponse rsp = get(tomcat.getUrl()); + assertThat(rsp).isNotNull(); + assertThat(rsp.getStatusCode()).isEqualTo(200); + assertThat(rsp.getResponseBody()).isNotEmpty().contains("Hello World"); + }); } private static EmbeddedTomcatConfiguration defaultConfiguration() { @@ -217,4 +205,32 @@ private static EmbeddedTomcatConfiguration.Builder defaultConfigurationBuilder() throw new RuntimeException(ex); } } + + private static void run(Consumer testFn) { + EmbeddedTomcat tomcat = new EmbeddedTomcat(); + doRun(tomcat, () -> + testFn.accept(tomcat) + ); + } + + private static void run(EmbeddedTomcatConfiguration configuration, Consumer testFn) { + EmbeddedTomcat tomcat = new EmbeddedTomcat(configuration); + doRun(tomcat, () -> + testFn.accept(tomcat) + ); + } + + private static void doRun(EmbeddedTomcat tomcat, Runnable testFn) { + tomcat.start(); + try { + testFn.run(); + } + finally { + tomcat.stop(); + } + } + + private static final class WrappedContext { + Context ctx = null; + } } diff --git a/junit-servers-tomcat-8/src/test/java/com/github/mjeanroy/junit/servers/tomcat8/EmbeddedTomcatTest.java b/junit-servers-tomcat-8/src/test/java/com/github/mjeanroy/junit/servers/tomcat8/EmbeddedTomcatTest.java index d49c598b..d2852d66 100644 --- a/junit-servers-tomcat-8/src/test/java/com/github/mjeanroy/junit/servers/tomcat8/EmbeddedTomcatTest.java +++ b/junit-servers-tomcat-8/src/test/java/com/github/mjeanroy/junit/servers/tomcat8/EmbeddedTomcatTest.java @@ -24,19 +24,19 @@ package com.github.mjeanroy.junit.servers.tomcat8; -import com.github.mjeanroy.junit.servers.testing.HttpTestUtils; import com.github.mjeanroy.junit.servers.testing.HttpTestUtils.HttpResponse; import com.github.mjeanroy.junit.servers.tomcat.EmbeddedTomcatConfiguration; import org.apache.catalina.Container; import org.apache.catalina.Context; import org.apache.catalina.LifecycleState; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; import java.io.File; import java.nio.file.Files; +import java.util.function.Consumer; +import static com.github.mjeanroy.junit.servers.testing.HttpTestUtils.get; import static com.github.mjeanroy.junit.servers.testing.HttpTestUtils.localhost; import static com.github.mjeanroy.junit.servers.testing.IoTestUtils.getFileFromClasspath; import static com.github.mjeanroy.junit.servers.tomcat8.tests.commons.Fields.readPrivate; @@ -46,41 +46,31 @@ class EmbeddedTomcatTest { private static final String PATH = "junit-servers-tomcat-8/"; - private EmbeddedTomcat tomcat; - - @AfterEach - void tearDown() { - if (tomcat != null) { - tomcat.stop(); - } - } - @Test void it_should_start_tomcat() { - tomcat = new EmbeddedTomcat(defaultConfiguration()); - tomcat.start(); - - assertThat(tomcat.isStarted()).isTrue(); - assertThat(tomcat.getPort()).isNotZero(); - assertThat(tomcat.getScheme()).isEqualTo("http"); - assertThat(tomcat.getHost()).isEqualTo("localhost"); - assertThat(tomcat.getPath()).isEqualTo("/"); - assertThat(tomcat.getUrl()).isEqualTo(localhost(tomcat.getPort())); + run(defaultConfiguration(), (tomcat) -> { + assertThat(tomcat.isStarted()).isTrue(); + assertThat(tomcat.getPort()).isNotZero(); + assertThat(tomcat.getScheme()).isEqualTo("http"); + assertThat(tomcat.getHost()).isEqualTo("localhost"); + assertThat(tomcat.getPath()).isEqualTo("/"); + assertThat(tomcat.getUrl()).isEqualTo(localhost(tomcat.getPort())); + }); } @Test void it_should_stop_tomcat() { - tomcat = new EmbeddedTomcat(defaultConfiguration()); - tomcat.start(); + EmbeddedTomcat tomcat = new EmbeddedTomcat(defaultConfiguration()); - assertThat(tomcat.isStarted()).isTrue(); - assertThat(tomcat.getPort()).isNotZero(); - assertThat(tomcat.getScheme()).isEqualTo("http"); - assertThat(tomcat.getHost()).isEqualTo("localhost"); - assertThat(tomcat.getPath()).isEqualTo("/"); - assertThat(tomcat.getUrl()).isEqualTo(localhost(tomcat.getPort())); + doRun(tomcat, () -> { + assertThat(tomcat.isStarted()).isTrue(); + assertThat(tomcat.getPort()).isNotZero(); + assertThat(tomcat.getScheme()).isEqualTo("http"); + assertThat(tomcat.getHost()).isEqualTo("localhost"); + assertThat(tomcat.getPath()).isEqualTo("/"); + assertThat(tomcat.getUrl()).isEqualTo(localhost(tomcat.getPort())); + }); - tomcat.stop(); assertThat(tomcat.isStarted()).isFalse(); assertThat(tomcat.getPort()).isZero(); assertThat(tomcat.getScheme()).isEqualTo("http"); @@ -91,109 +81,107 @@ void it_should_stop_tomcat() { @Test void it_should_destroy_context_on_stop() { - tomcat = new EmbeddedTomcat(defaultConfiguration()); + EmbeddedTomcat tomcat = new EmbeddedTomcat(defaultConfiguration()); assertThat((Context) readPrivate(tomcat, "context")).isNull(); - tomcat.start(); + WrappedContext startedContext = new WrappedContext(); - Context ctx = readPrivate(tomcat, "context"); - assertThat(ctx).isNotNull(); - assertThat(ctx.getState()).isEqualTo(LifecycleState.STARTED); + doRun(tomcat, () -> { + startedContext.ctx = readPrivate(tomcat, "context"); + assertThat(startedContext.ctx).isNotNull(); + assertThat(startedContext.ctx.getState()).isEqualTo(LifecycleState.STARTED); + }); - tomcat.stop(); assertThat((Context) readPrivate(tomcat, "context")).isNull(); - assertThat(ctx.getState()).isEqualTo(LifecycleState.DESTROYED); + assertThat(startedContext.ctx.getState()).isEqualTo(LifecycleState.DESTROYED); } @Test void it_should_delete_base_dir_on_stop() { - tomcat = new EmbeddedTomcat(defaultConfigurationBuilder().deleteBaseDir().build()); - File baseDir = new File(tomcat.getConfiguration().getBaseDir()); + EmbeddedTomcatConfiguration configuration = defaultConfigurationBuilder().deleteBaseDir().build(); + File baseDir = new File(configuration.getBaseDir()); - assertThat(baseDir).exists(); - - tomcat.start(); - tomcat.stop(); + run(configuration, (tomcat) -> + assertThat(baseDir).exists() + ); assertThat(baseDir).doesNotExist(); } @Test void it_should_keep_base_dir_on_stop() { - tomcat = new EmbeddedTomcat(defaultConfigurationBuilder().keepBaseDir().build()); - File baseDir = new File(tomcat.getConfiguration().getBaseDir()); + EmbeddedTomcatConfiguration configuration = defaultConfigurationBuilder().keepBaseDir().build(); + File baseDir = new File(configuration.getBaseDir()); - assertThat(baseDir).exists(); - - tomcat.start(); - tomcat.stop(); + run(configuration, (tomcat) -> + assertThat(baseDir).exists() + ); assertThat(baseDir).exists(); } @Test void it_should_get_servlet_context() { - tomcat = new EmbeddedTomcat(defaultConfiguration()); - tomcat.start(); - assertThat(tomcat.getServletContext()).isNotNull(); + run(defaultConfiguration(), (tomcat) -> + assertThat(tomcat.getServletContext()).isNotNull() + ); } @Test void it_should_get_original_tomcat() { - tomcat = new EmbeddedTomcat(); - assertThat(tomcat.getDelegate()).isNotNull(); + run((tomcat) -> + assertThat(tomcat.getDelegate()).isNotNull() + ); } @Test void it_should_create_meta_inf_directory_if_it_does_not_exist(@TempDir File baseDir) throws Exception { - final File metaInf = new File(baseDir, "META-INF"); - - tomcat = new EmbeddedTomcat(EmbeddedTomcatConfiguration.builder() + File metaInf = new File(baseDir, "META-INF"); + EmbeddedTomcatConfiguration configuration = EmbeddedTomcatConfiguration.builder() .withClasspath(baseDir.getAbsolutePath()) .withWebapp(baseDir) .enableForceMetaInf() - .build()); + .build(); assertThat(metaInf).doesNotExist(); - tomcat.start(); - assertThat(metaInf).exists(); + run(configuration, (tomcat) -> + assertThat(metaInf).exists() + ); } @Test void it_should_add_parent_classloader(@TempDir File tmpDir) throws Exception { - final File tmpFile = Files.createTempFile(tmpDir.toPath(), null, null).toFile(); - final File dir = tmpFile.getParentFile(); - - tomcat = new EmbeddedTomcat(EmbeddedTomcatConfiguration.builder() + File tmpFile = Files.createTempFile(tmpDir.toPath(), null, null).toFile(); + File dir = tmpFile.getParentFile(); + EmbeddedTomcatConfiguration configuration = EmbeddedTomcatConfiguration.builder() .withWebapp(dir) .withParentClasspath(dir.toURI().toURL()) - .build()); - - tomcat.start(); + .build(); - final Container[] containers = tomcat.getDelegate().getHost().findChildren(); - final ClassLoader cl = containers[0].getParentClassLoader(); + run(configuration, (tomcat) -> { + Container[] containers = tomcat.getDelegate().getHost().findChildren(); + ClassLoader cl = containers[0].getParentClassLoader(); - assertThat(cl).isNotNull(); - assertThat(cl.getResource("hello-world.html")).isNotNull(); - assertThat(cl.getResource(tmpFile.getName())).isNotNull(); + assertThat(cl).isNotNull(); + assertThat(cl.getResource("hello-world.html")).isNotNull(); + assertThat(cl.getResource(tmpFile.getName())).isNotNull(); + }); } @Test void it_should_override_web_xml() { File descriptor = getFileFromClasspath("/custom-web.xml"); - - tomcat = new EmbeddedTomcat(defaultConfigurationBuilder() + EmbeddedTomcatConfiguration configuration = defaultConfigurationBuilder() .withOverrideDescriptor(descriptor.getAbsolutePath()) - .build()); - - tomcat.start(); - - HttpResponse rsp = HttpTestUtils.get(tomcat.getUrl()); - assertThat(rsp).isNotNull(); - assertThat(rsp.getStatusCode()).isEqualTo(200); - assertThat(rsp.getResponseBody()).isNotEmpty().contains("Hello World"); + .build(); + + run(configuration, (tomcat) -> { + HttpResponse rsp = get(tomcat.getUrl()); + assertThat(rsp).isNotNull(); + assertThat(rsp.getStatusCode()).isEqualTo(200); + assertThat(rsp.getResponseBody()).isNotEmpty().contains("Hello World"); + }); } private static EmbeddedTomcatConfiguration defaultConfiguration() { @@ -217,4 +205,32 @@ private static EmbeddedTomcatConfiguration.Builder defaultConfigurationBuilder() throw new RuntimeException(ex); } } + + private static void run(Consumer testFn) { + EmbeddedTomcat tomcat = new EmbeddedTomcat(); + doRun(tomcat, () -> + testFn.accept(tomcat) + ); + } + + private static void run(EmbeddedTomcatConfiguration configuration, Consumer testFn) { + EmbeddedTomcat tomcat = new EmbeddedTomcat(configuration); + doRun(tomcat, () -> + testFn.accept(tomcat) + ); + } + + private static void doRun(EmbeddedTomcat tomcat, Runnable testFn) { + tomcat.start(); + try { + testFn.run(); + } + finally { + tomcat.stop(); + } + } + + private static final class WrappedContext { + Context ctx = null; + } } diff --git a/junit-servers-tomcat-9/src/test/java/com/github/mjeanroy/junit/servers/tomcat9/EmbeddedTomcatTest.java b/junit-servers-tomcat-9/src/test/java/com/github/mjeanroy/junit/servers/tomcat9/EmbeddedTomcatTest.java index 7df3373f..d7ab1b56 100644 --- a/junit-servers-tomcat-9/src/test/java/com/github/mjeanroy/junit/servers/tomcat9/EmbeddedTomcatTest.java +++ b/junit-servers-tomcat-9/src/test/java/com/github/mjeanroy/junit/servers/tomcat9/EmbeddedTomcatTest.java @@ -24,19 +24,19 @@ package com.github.mjeanroy.junit.servers.tomcat9; -import com.github.mjeanroy.junit.servers.testing.HttpTestUtils; import com.github.mjeanroy.junit.servers.testing.HttpTestUtils.HttpResponse; import com.github.mjeanroy.junit.servers.tomcat.EmbeddedTomcatConfiguration; import org.apache.catalina.Container; import org.apache.catalina.Context; import org.apache.catalina.LifecycleState; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; import java.io.File; import java.nio.file.Files; +import java.util.function.Consumer; +import static com.github.mjeanroy.junit.servers.testing.HttpTestUtils.get; import static com.github.mjeanroy.junit.servers.testing.HttpTestUtils.localhost; import static com.github.mjeanroy.junit.servers.testing.IoTestUtils.getFileFromClasspath; import static com.github.mjeanroy.junit.servers.tomcat9.tests.commons.Fields.readPrivate; @@ -46,41 +46,31 @@ class EmbeddedTomcatTest { private static final String PATH = "junit-servers-tomcat-9/"; - private EmbeddedTomcat tomcat; - - @AfterEach - void tearDown() { - if (tomcat != null) { - tomcat.stop(); - } - } - @Test void it_should_start_tomcat() { - tomcat = new EmbeddedTomcat(defaultConfiguration()); - tomcat.start(); - - assertThat(tomcat.isStarted()).isTrue(); - assertThat(tomcat.getPort()).isNotZero(); - assertThat(tomcat.getScheme()).isEqualTo("http"); - assertThat(tomcat.getHost()).isEqualTo("localhost"); - assertThat(tomcat.getPath()).isEqualTo("/"); - assertThat(tomcat.getUrl()).isEqualTo(localhost(tomcat.getPort())); + run(defaultConfiguration(), (tomcat) -> { + assertThat(tomcat.isStarted()).isTrue(); + assertThat(tomcat.getPort()).isNotZero(); + assertThat(tomcat.getScheme()).isEqualTo("http"); + assertThat(tomcat.getHost()).isEqualTo("localhost"); + assertThat(tomcat.getPath()).isEqualTo("/"); + assertThat(tomcat.getUrl()).isEqualTo(localhost(tomcat.getPort())); + }); } @Test void it_should_stop_tomcat() { - tomcat = new EmbeddedTomcat(defaultConfiguration()); - tomcat.start(); + EmbeddedTomcat tomcat = new EmbeddedTomcat(defaultConfiguration()); - assertThat(tomcat.isStarted()).isTrue(); - assertThat(tomcat.getPort()).isNotZero(); - assertThat(tomcat.getScheme()).isEqualTo("http"); - assertThat(tomcat.getHost()).isEqualTo("localhost"); - assertThat(tomcat.getPath()).isEqualTo("/"); - assertThat(tomcat.getUrl()).isEqualTo(localhost(tomcat.getPort())); + doRun(tomcat, () -> { + assertThat(tomcat.isStarted()).isTrue(); + assertThat(tomcat.getPort()).isNotZero(); + assertThat(tomcat.getScheme()).isEqualTo("http"); + assertThat(tomcat.getHost()).isEqualTo("localhost"); + assertThat(tomcat.getPath()).isEqualTo("/"); + assertThat(tomcat.getUrl()).isEqualTo(localhost(tomcat.getPort())); + }); - tomcat.stop(); assertThat(tomcat.isStarted()).isFalse(); assertThat(tomcat.getPort()).isZero(); assertThat(tomcat.getScheme()).isEqualTo("http"); @@ -91,109 +81,107 @@ void it_should_stop_tomcat() { @Test void it_should_destroy_context_on_stop() { - tomcat = new EmbeddedTomcat(defaultConfiguration()); + EmbeddedTomcat tomcat = new EmbeddedTomcat(defaultConfiguration()); assertThat((Context) readPrivate(tomcat, "context")).isNull(); - tomcat.start(); + WrappedContext startedContext = new WrappedContext(); - Context ctx = readPrivate(tomcat, "context"); - assertThat(ctx).isNotNull(); - assertThat(ctx.getState()).isEqualTo(LifecycleState.STARTED); + doRun(tomcat, () -> { + startedContext.ctx = readPrivate(tomcat, "context"); + assertThat(startedContext.ctx).isNotNull(); + assertThat(startedContext.ctx.getState()).isEqualTo(LifecycleState.STARTED); + }); - tomcat.stop(); assertThat((Context) readPrivate(tomcat, "context")).isNull(); - assertThat(ctx.getState()).isEqualTo(LifecycleState.DESTROYED); + assertThat(startedContext.ctx.getState()).isEqualTo(LifecycleState.DESTROYED); } @Test void it_should_delete_base_dir_on_stop() { - tomcat = new EmbeddedTomcat(defaultConfigurationBuilder().deleteBaseDir().build()); - File baseDir = new File(tomcat.getConfiguration().getBaseDir()); + EmbeddedTomcatConfiguration configuration = defaultConfigurationBuilder().deleteBaseDir().build(); + File baseDir = new File(configuration.getBaseDir()); - assertThat(baseDir).exists(); - - tomcat.start(); - tomcat.stop(); + run(configuration, (tomcat) -> + assertThat(baseDir).exists() + ); assertThat(baseDir).doesNotExist(); } @Test void it_should_keep_base_dir_on_stop() { - tomcat = new EmbeddedTomcat(defaultConfigurationBuilder().keepBaseDir().build()); - File baseDir = new File(tomcat.getConfiguration().getBaseDir()); + EmbeddedTomcatConfiguration configuration = defaultConfigurationBuilder().keepBaseDir().build(); + File baseDir = new File(configuration.getBaseDir()); - assertThat(baseDir).exists(); - - tomcat.start(); - tomcat.stop(); + run(configuration, (tomcat) -> + assertThat(baseDir).exists() + ); assertThat(baseDir).exists(); } @Test void it_should_get_servlet_context() { - tomcat = new EmbeddedTomcat(defaultConfiguration()); - tomcat.start(); - assertThat(tomcat.getServletContext()).isNotNull(); + run(defaultConfiguration(), (tomcat) -> + assertThat(tomcat.getServletContext()).isNotNull() + ); } @Test void it_should_get_original_tomcat() { - tomcat = new EmbeddedTomcat(); - assertThat(tomcat.getDelegate()).isNotNull(); + run((tomcat) -> + assertThat(tomcat.getDelegate()).isNotNull() + ); } @Test void it_should_create_meta_inf_directory_if_it_does_not_exist(@TempDir File baseDir) throws Exception { - final File metaInf = new File(baseDir, "META-INF"); - - tomcat = new EmbeddedTomcat(EmbeddedTomcatConfiguration.builder() + File metaInf = new File(baseDir, "META-INF"); + EmbeddedTomcatConfiguration configuration = EmbeddedTomcatConfiguration.builder() .withClasspath(baseDir.getAbsolutePath()) .withWebapp(baseDir) .enableForceMetaInf() - .build()); + .build(); assertThat(metaInf).doesNotExist(); - tomcat.start(); - assertThat(metaInf).exists(); + run(configuration, (tomcat) -> + assertThat(metaInf).exists() + ); } @Test void it_should_add_parent_classloader(@TempDir File tmpDir) throws Exception { - final File tmpFile = Files.createTempFile(tmpDir.toPath(), null, null).toFile(); - final File dir = tmpFile.getParentFile(); - - tomcat = new EmbeddedTomcat(EmbeddedTomcatConfiguration.builder() + File tmpFile = Files.createTempFile(tmpDir.toPath(), null, null).toFile(); + File dir = tmpFile.getParentFile(); + EmbeddedTomcatConfiguration configuration = EmbeddedTomcatConfiguration.builder() .withWebapp(dir) .withParentClasspath(dir.toURI().toURL()) - .build()); - - tomcat.start(); + .build(); - final Container[] containers = tomcat.getDelegate().getHost().findChildren(); - final ClassLoader cl = containers[0].getParentClassLoader(); + run(configuration, (tomcat) -> { + Container[] containers = tomcat.getDelegate().getHost().findChildren(); + ClassLoader cl = containers[0].getParentClassLoader(); - assertThat(cl).isNotNull(); - assertThat(cl.getResource("hello-world.html")).isNotNull(); - assertThat(cl.getResource(tmpFile.getName())).isNotNull(); + assertThat(cl).isNotNull(); + assertThat(cl.getResource("hello-world.html")).isNotNull(); + assertThat(cl.getResource(tmpFile.getName())).isNotNull(); + }); } @Test void it_should_override_web_xml() { File descriptor = getFileFromClasspath("/custom-web.xml"); - - tomcat = new EmbeddedTomcat(defaultConfigurationBuilder() + EmbeddedTomcatConfiguration configuration = defaultConfigurationBuilder() .withOverrideDescriptor(descriptor.getAbsolutePath()) - .build()); - - tomcat.start(); - - HttpResponse rsp = HttpTestUtils.get(tomcat.getUrl()); - assertThat(rsp).isNotNull(); - assertThat(rsp.getStatusCode()).isEqualTo(200); - assertThat(rsp.getResponseBody()).isNotEmpty().contains("Hello World"); + .build(); + + run(configuration, (tomcat) -> { + HttpResponse rsp = get(tomcat.getUrl()); + assertThat(rsp).isNotNull(); + assertThat(rsp.getStatusCode()).isEqualTo(200); + assertThat(rsp.getResponseBody()).isNotEmpty().contains("Hello World"); + }); } private static EmbeddedTomcatConfiguration defaultConfiguration() { @@ -217,4 +205,32 @@ private static EmbeddedTomcatConfiguration.Builder defaultConfigurationBuilder() throw new RuntimeException(ex); } } + + private static void run(Consumer testFn) { + EmbeddedTomcat tomcat = new EmbeddedTomcat(); + doRun(tomcat, () -> + testFn.accept(tomcat) + ); + } + + private static void run(EmbeddedTomcatConfiguration configuration, Consumer testFn) { + EmbeddedTomcat tomcat = new EmbeddedTomcat(configuration); + doRun(tomcat, () -> + testFn.accept(tomcat) + ); + } + + private static void doRun(EmbeddedTomcat tomcat, Runnable testFn) { + tomcat.start(); + try { + testFn.run(); + } + finally { + tomcat.stop(); + } + } + + private static final class WrappedContext { + Context ctx = null; + } } diff --git a/junit-servers-tomcat/src/test/java/com/github/mjeanroy/junit/servers/tomcat/EmbeddedTomcatTest.java b/junit-servers-tomcat/src/test/java/com/github/mjeanroy/junit/servers/tomcat/EmbeddedTomcatTest.java index 7e0cbbdd..0e6610bf 100644 --- a/junit-servers-tomcat/src/test/java/com/github/mjeanroy/junit/servers/tomcat/EmbeddedTomcatTest.java +++ b/junit-servers-tomcat/src/test/java/com/github/mjeanroy/junit/servers/tomcat/EmbeddedTomcatTest.java @@ -24,18 +24,18 @@ package com.github.mjeanroy.junit.servers.tomcat; -import com.github.mjeanroy.junit.servers.testing.HttpTestUtils; import com.github.mjeanroy.junit.servers.testing.HttpTestUtils.HttpResponse; import org.apache.catalina.Container; import org.apache.catalina.Context; import org.apache.catalina.LifecycleState; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; import java.io.File; import java.nio.file.Files; +import java.util.function.Consumer; +import static com.github.mjeanroy.junit.servers.testing.HttpTestUtils.get; import static com.github.mjeanroy.junit.servers.testing.HttpTestUtils.localhost; import static com.github.mjeanroy.junit.servers.testing.IoTestUtils.getFileFromClasspath; import static com.github.mjeanroy.junit.servers.tomcat.tests.commons.Fields.readPrivate; @@ -46,41 +46,31 @@ class EmbeddedTomcatTest { private static final String PATH = "junit-servers-tomcat/"; - private EmbeddedTomcat tomcat; - - @AfterEach - void tearDown() { - if (tomcat != null) { - tomcat.stop(); - } - } - @Test void it_should_start_tomcat() { - tomcat = new EmbeddedTomcat(defaultConfiguration()); - tomcat.start(); - - assertThat(tomcat.isStarted()).isTrue(); - assertThat(tomcat.getPort()).isNotZero(); - assertThat(tomcat.getScheme()).isEqualTo("http"); - assertThat(tomcat.getHost()).isEqualTo("localhost"); - assertThat(tomcat.getPath()).isEqualTo("/"); - assertThat(tomcat.getUrl()).isEqualTo(localhost(tomcat.getPort())); + run(defaultConfiguration(), (tomcat) -> { + assertThat(tomcat.isStarted()).isTrue(); + assertThat(tomcat.getPort()).isNotZero(); + assertThat(tomcat.getScheme()).isEqualTo("http"); + assertThat(tomcat.getHost()).isEqualTo("localhost"); + assertThat(tomcat.getPath()).isEqualTo("/"); + assertThat(tomcat.getUrl()).isEqualTo(localhost(tomcat.getPort())); + }); } @Test void it_should_stop_tomcat() { - tomcat = new EmbeddedTomcat(defaultConfiguration()); - tomcat.start(); + EmbeddedTomcat tomcat = new EmbeddedTomcat(defaultConfiguration()); - assertThat(tomcat.isStarted()).isTrue(); - assertThat(tomcat.getPort()).isNotZero(); - assertThat(tomcat.getScheme()).isEqualTo("http"); - assertThat(tomcat.getHost()).isEqualTo("localhost"); - assertThat(tomcat.getPath()).isEqualTo("/"); - assertThat(tomcat.getUrl()).isEqualTo(localhost(tomcat.getPort())); + doRun(tomcat, () -> { + assertThat(tomcat.isStarted()).isTrue(); + assertThat(tomcat.getPort()).isNotZero(); + assertThat(tomcat.getScheme()).isEqualTo("http"); + assertThat(tomcat.getHost()).isEqualTo("localhost"); + assertThat(tomcat.getPath()).isEqualTo("/"); + assertThat(tomcat.getUrl()).isEqualTo(localhost(tomcat.getPort())); + }); - tomcat.stop(); assertThat(tomcat.isStarted()).isFalse(); assertThat(tomcat.getPort()).isZero(); assertThat(tomcat.getScheme()).isEqualTo("http"); @@ -91,109 +81,107 @@ void it_should_stop_tomcat() { @Test void it_should_destroy_context_on_stop() { - tomcat = new EmbeddedTomcat(defaultConfiguration()); + EmbeddedTomcat tomcat = new EmbeddedTomcat(defaultConfiguration()); assertThat((Context) readPrivate(tomcat, "context")).isNull(); - tomcat.start(); + WrappedContext startedContext = new WrappedContext(); - Context ctx = readPrivate(tomcat, "context"); - assertThat(ctx).isNotNull(); - assertThat(ctx.getState()).isEqualTo(LifecycleState.STARTED); + doRun(tomcat, () -> { + startedContext.ctx = readPrivate(tomcat, "context"); + assertThat(startedContext.ctx).isNotNull(); + assertThat(startedContext.ctx.getState()).isEqualTo(LifecycleState.STARTED); + }); - tomcat.stop(); assertThat((Context) readPrivate(tomcat, "context")).isNull(); - assertThat(ctx.getState()).isEqualTo(LifecycleState.DESTROYED); + assertThat(startedContext.ctx.getState()).isEqualTo(LifecycleState.DESTROYED); } @Test void it_should_delete_base_dir_on_stop() { - tomcat = new EmbeddedTomcat(defaultConfigurationBuilder().deleteBaseDir().build()); - File baseDir = new File(tomcat.getConfiguration().getBaseDir()); + EmbeddedTomcatConfiguration configuration = defaultConfigurationBuilder().deleteBaseDir().build(); + File baseDir = new File(configuration.getBaseDir()); - assertThat(baseDir).exists(); - - tomcat.start(); - tomcat.stop(); + run(configuration, (tomcat) -> + assertThat(baseDir).exists() + ); assertThat(baseDir).doesNotExist(); } @Test void it_should_keep_base_dir_on_stop() { - tomcat = new EmbeddedTomcat(defaultConfigurationBuilder().keepBaseDir().build()); - File baseDir = new File(tomcat.getConfiguration().getBaseDir()); + EmbeddedTomcatConfiguration configuration = defaultConfigurationBuilder().keepBaseDir().build(); + File baseDir = new File(configuration.getBaseDir()); - assertThat(baseDir).exists(); - - tomcat.start(); - tomcat.stop(); + run(configuration, (tomcat) -> + assertThat(baseDir).exists() + ); assertThat(baseDir).exists(); } @Test void it_should_get_servlet_context() { - tomcat = new EmbeddedTomcat(defaultConfiguration()); - tomcat.start(); - assertThat(tomcat.getServletContext()).isNotNull(); + run(defaultConfiguration(), (tomcat) -> + assertThat(tomcat.getServletContext()).isNotNull() + ); } @Test void it_should_get_original_tomcat() { - tomcat = new EmbeddedTomcat(); - assertThat(tomcat.getDelegate()).isNotNull(); + run((tomcat) -> + assertThat(tomcat.getDelegate()).isNotNull() + ); } @Test void it_should_create_meta_inf_directory_if_it_does_not_exist(@TempDir File baseDir) throws Exception { - final File metaInf = new File(baseDir, "META-INF"); - - tomcat = new EmbeddedTomcat(EmbeddedTomcatConfiguration.builder() + File metaInf = new File(baseDir, "META-INF"); + EmbeddedTomcatConfiguration configuration = EmbeddedTomcatConfiguration.builder() .withClasspath(baseDir.getAbsolutePath()) .withWebapp(baseDir) .enableForceMetaInf() - .build()); + .build(); assertThat(metaInf).doesNotExist(); - tomcat.start(); - assertThat(metaInf).exists(); + run(configuration, (tomcat) -> + assertThat(metaInf).exists() + ); } @Test void it_should_add_parent_classloader(@TempDir File tmpDir) throws Exception { - final File tmpFile = Files.createTempFile(tmpDir.toPath(), null, null).toFile(); - final File dir = tmpFile.getParentFile(); - - tomcat = new EmbeddedTomcat(EmbeddedTomcatConfiguration.builder() + File tmpFile = Files.createTempFile(tmpDir.toPath(), null, null).toFile(); + File dir = tmpFile.getParentFile(); + EmbeddedTomcatConfiguration configuration = EmbeddedTomcatConfiguration.builder() .withWebapp(dir) .withParentClasspath(dir.toURI().toURL()) - .build()); - - tomcat.start(); + .build(); - final Container[] containers = tomcat.getDelegate().getHost().findChildren(); - final ClassLoader cl = containers[0].getParentClassLoader(); + run(configuration, (tomcat) -> { + Container[] containers = tomcat.getDelegate().getHost().findChildren(); + ClassLoader cl = containers[0].getParentClassLoader(); - assertThat(cl).isNotNull(); - assertThat(cl.getResource("hello-world.html")).isNotNull(); - assertThat(cl.getResource(tmpFile.getName())).isNotNull(); + assertThat(cl).isNotNull(); + assertThat(cl.getResource("hello-world.html")).isNotNull(); + assertThat(cl.getResource(tmpFile.getName())).isNotNull(); + }); } @Test void it_should_override_web_xml() { - final File descriptor = getFileFromClasspath("/custom-web.xml"); - - tomcat = new EmbeddedTomcat(defaultConfigurationBuilder() + File descriptor = getFileFromClasspath("/custom-web.xml"); + EmbeddedTomcatConfiguration configuration = defaultConfigurationBuilder() .withOverrideDescriptor(descriptor.getAbsolutePath()) - .build()); - - tomcat.start(); - - HttpResponse rsp = HttpTestUtils.get(tomcat.getUrl()); - assertThat(rsp).isNotNull(); - assertThat(rsp.getStatusCode()).isEqualTo(200); - assertThat(rsp.getResponseBody()).isNotEmpty().contains("Hello World"); + .build(); + + run(configuration, (tomcat) -> { + HttpResponse rsp = get(tomcat.getUrl()); + assertThat(rsp).isNotNull(); + assertThat(rsp.getStatusCode()).isEqualTo(200); + assertThat(rsp.getResponseBody()).isNotEmpty().contains("Hello World"); + }); } private static EmbeddedTomcatConfiguration defaultConfiguration() { @@ -217,4 +205,32 @@ private static EmbeddedTomcatConfiguration.Builder defaultConfigurationBuilder() throw new RuntimeException(ex); } } + + private static void run(Consumer testFn) { + EmbeddedTomcat tomcat = new EmbeddedTomcat(); + doRun(tomcat, () -> + testFn.accept(tomcat) + ); + } + + private static void run(EmbeddedTomcatConfiguration configuration, Consumer testFn) { + EmbeddedTomcat tomcat = new EmbeddedTomcat(configuration); + doRun(tomcat, () -> + testFn.accept(tomcat) + ); + } + + private static void doRun(EmbeddedTomcat tomcat, Runnable testFn) { + tomcat.start(); + try { + testFn.run(); + } + finally { + tomcat.stop(); + } + } + + private static final class WrappedContext { + Context ctx = null; + } }