embed-apache-tomcat-test

Embed Apache Tomcat

License

License

Categories

Categories

Tomcat Container Application Servers
GroupId

GroupId

org.pidster
ArtifactId

ArtifactId

embed-apache-tomcat-test
Last Version

Last Version

0.2
Release Date

Release Date

Type

Type

jar
Description

Description

embed-apache-tomcat-test
Embed Apache Tomcat
Project URL

Project URL

http://pidster.org
Source Code Management

Source Code Management

https://github.com/pidster-dot-org/embed-apache-tomcat

Download embed-apache-tomcat-test

How to add to project

<!-- https://jarcasting.com/artifacts/org.pidster/embed-apache-tomcat-test/ -->
<dependency>
    <groupId>org.pidster</groupId>
    <artifactId>embed-apache-tomcat-test</artifactId>
    <version>0.2</version>
</dependency>
// https://jarcasting.com/artifacts/org.pidster/embed-apache-tomcat-test/
implementation 'org.pidster:embed-apache-tomcat-test:0.2'
// https://jarcasting.com/artifacts/org.pidster/embed-apache-tomcat-test/
implementation ("org.pidster:embed-apache-tomcat-test:0.2")
'org.pidster:embed-apache-tomcat-test:jar:0.2'
<dependency org="org.pidster" name="embed-apache-tomcat-test" rev="0.2">
  <artifact name="embed-apache-tomcat-test" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.pidster', module='embed-apache-tomcat-test', version='0.2')
)
libraryDependencies += "org.pidster" % "embed-apache-tomcat-test" % "0.2"
[org.pidster/embed-apache-tomcat-test "0.2"]

Dependencies

compile (2)

Group / Artifact Type Version
org.pidster : embed-apache-tomcat-core jar 0.2
junit : junit jar 4.11

Project Modules

There are no modules declared in this project.

Embed Apache Tomcat

Just like the name says, this project provides a fluent API for embedding Apache Tomcat.

Embed a Tomcat instance for testing using the following code, where MyContainerInitializer.class is a ServletContainerInitializer implementation used to programmatically bootstrap a Servlet 3.0 application.

Properties properties = new Properties();
properties.put("catalina.base", "build/resources/test");

Tomcat tomcat = new TomcatFactory(properties).create()
  .newMinimalServer(url.getPort())
      .createApplication("test-make-dirs")
      .makeDirs()
      .addServletContainerInitializer(MyContainerInitializer.class)
  .build();

Alternatively, add Servlet components individually:

Tomcat tomcat = new TomcatFactory(properties).create()
  .newMinimalServer(url.getPort())
      .createApplication("test")
      .addServletContextListener(DummyListener.class)
      .addServletFilter(DummyFilter.class, "/*")
      .addServlet(DummyServlet.class, "/dummy")
  .build();

These minimal Tomcat instances are examples of minimal configurations, but the container can be configured entirely programmatically, specifying Valves, Connectors, LifecycleListeners etc as required.

Testing usage

You can embed Tomcat in a unit test (e.g. JUnit) as follows:

public class SomeTest {

    private TomcatRuntime runtime;

    @Before
    public void setupTomcat() throws Exception {

        Tomcat tomcat = new TomcatFactory().create()
            .newMinimalServer(8082)
                .createApplication("test")
                .addServlet(DummyServlet.class, "/dummy")
            .build();

        this.runtime = tomcat.start(5000L);  // wait for start
    }

    @Test
    public void testServlet() throws Exception {
    	// do some HTTP tests
    }

    @After
    public void teardown() {
        runtime.stop(5000L); // wait for stop
    }
}

Build configuration

Using Gradle and the current snapshot, the dependency is:

repositories {
    mavenCentral()
}

dependencies {
	    compile "org.pidster:embed-apache-tomcat-core:0.2"
}
org.pidster

Pidster.org

Versions

Version
0.2
0.1-ALPHA