Skip to content

Commit

Permalink
tests: use local embedded tomcat in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mjeanroy committed Nov 28, 2023
1 parent da565fd commit c7eff79
Show file tree
Hide file tree
Showing 4 changed files with 389 additions and 325 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
Expand All @@ -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() {
Expand All @@ -217,4 +205,32 @@ private static EmbeddedTomcatConfiguration.Builder defaultConfigurationBuilder()
throw new RuntimeException(ex);
}
}

private static void run(Consumer<EmbeddedTomcat> testFn) {
EmbeddedTomcat tomcat = new EmbeddedTomcat();
doRun(tomcat, () ->
testFn.accept(tomcat)
);
}

private static void run(EmbeddedTomcatConfiguration configuration, Consumer<EmbeddedTomcat> 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;
}
}
Loading

0 comments on commit c7eff79

Please sign in to comment.