public class ClosureBackedEmbeddedApplication extends LaunchConfigEmbeddedApplication
EmbeddedApplication implementation that allows the application to be defined in code at runtime.
This implementation is usually sufficient for testing Ratpack modules or extensions.
import ratpack.test.embed.PathBaseDirBuilder
import ratpack.groovy.test.TestHttpClients
import ratpack.session.SessionModule
import java.nio.file.Files
import static ratpack.groovy.test.embed.EmbeddedApplications.embeddedApp
def tmp = Files.createTempDirectory("ratpack-test")
def baseDir = new PathBaseDirBuilder(tmp)
// Create a file within the application base dir
baseDir.file "public/foo.txt", "bar"
def app = embeddedApp(baseDir) {
// Configure the launch config
launchConfig {
other "some.other.property": "value"
reloadable true
}
// Configure the module registry
modules {
register new SessionModule()
}
// Use the GroovyChain DSL for defining the application handlers
handlers {
get {
render "root"
}
assets "public"
}
}
// Test the application with the test http client
def client = TestHttpClients.testHttpClient(app)
assert client.getText() == "root"
assert client.getText("foo.txt") == "bar"
// Cleanup
app.close()
PathBaseDirBuilder,
EmbeddedApplications| Constructor and Description |
|---|
ClosureBackedEmbeddedApplication()
Constructor.
|
ClosureBackedEmbeddedApplication(BaseDirBuilder baseDirBuilder)
Constructor.
|
ClosureBackedEmbeddedApplication(Factory<Path> baseDirFactory)
Constructor.
|
| Modifier and Type | Method and Description |
|---|---|
protected GuiceBackedHandlerFactory |
createHandlerFactory(LaunchConfig launchConfig)
Returns the factory to use to create the actual handler.
|
protected Transformer<? super Injector,? extends Handler> |
createHandlerTransformer(LaunchConfig launchConfig)
Provides the object that, given the
Injector created by the module definition, creates the application handler. |
protected Transformer<? super Module,? extends Injector> |
createInjectorFactory(LaunchConfig launchConfig)
Creates a module to injector transformer based on the given launch config.
|
protected LaunchConfig |
createLaunchConfig()
Constructs the launch config using the other
create* methods. |
protected Action<? super ModuleRegistry> |
createModulesAction()
Provides the module registry configuration action.
|
List<Module> |
getModules()
A mutable list of modules to use in this application.
|
void |
handlers(Closure<?> closure)
Specifies the handlers of the application.
|
void |
launchConfig(Closure<?> closure)
Modifies the launch config of the application.
|
void |
modules(Closure<?> closure)
Specifies the modules of the application.
|
createServerclose, getAddress, getServerpublic ClosureBackedEmbeddedApplication()
public ClosureBackedEmbeddedApplication(Factory<Path> baseDirFactory)
Useful for deferring the base dir determination
baseDirFactory - The factory for the base dirpublic ClosureBackedEmbeddedApplication(BaseDirBuilder baseDirBuilder)
baseDirBuilder - the builder whose BaseDirBuilder.build() method will be called to provide the base dir for this apppublic List<Module> getModules()
protected LaunchConfig createLaunchConfig()
create* methods.
A launch config will be created using LaunchConfigBuilder.baseDir(java.io.File), with the path returned by the factory given at construction.
The launch config will also default to using a port value of 0 (i.e. an ephemeral port).
Register modules and objects using the modules(groovy.lang.Closure) method.
Define the handlers using the handlers(groovy.lang.Closure) method.
Prefer overriding specific create* methods instead of this one.
createLaunchConfig in class LaunchConfigEmbeddedApplicationpublic void handlers(@DelegatesTo(value=GroovyChain.class,strategy=1) Closure<?> closure)
The given closure will not be executed until this application is started.
Subsequent calls to this method will replace the previous definition. Calling this method after the application has started has no effect.
closure - The definition of the application handlerspublic void modules(@DelegatesTo(value=GroovyModuleRegistry.class,strategy=1) Closure<?> closure)
The given closure will not be executed until this application is started.
Subsequent calls to this method will replace the previous definition. Calling this method after the application has started has no effect.
closure - The definition of the application handlerspublic void launchConfig(@DelegatesTo(value=LaunchConfigBuilder.class,strategy=1) Closure<?> closure)
The given closure will not be executed until this application is started.
Subsequent calls to this method will replace the previous definition. Calling this method after the application has started has no effect.
closure - The definition of the application handlersprotected Transformer<? super Module,? extends Injector> createInjectorFactory(LaunchConfig launchConfig)
Delegates to Guice.newInjectorFactory(ratpack.launch.LaunchConfig).
launchConfig - The launch configGuice.newInjectorFactory(ratpack.launch.LaunchConfig) with the given launch configprotected GuiceBackedHandlerFactory createHandlerFactory(LaunchConfig launchConfig)
This implementation does not add any implicit behaviour. Subclasses may wish to provide different implementations that do perform implicit behaviour (e.g. implied modules)
launchConfig - The launch configprotected Action<? super ModuleRegistry> createModulesAction()
This implementation creates an action that registers getModules() in order, then executes the closure given with modules(groovy.lang.Closure).
protected Transformer<? super Injector,? extends Handler> createHandlerTransformer(LaunchConfig launchConfig)
Injector created by the module definition, creates the application handler.
This implementation uses the closure given to handlers(groovy.lang.Closure) to build a handler using the Groovy.chain(ratpack.launch.LaunchConfig, ratpack.registry.Registry, groovy.lang.Closure) method.
An registry based on the injector backs the chain.
launchConfig - the launch config