public final class Impositions extends Object
The impositions mechanism exists primarily to facilitate convenient overriding of configuration and behaviour at test time.
MainClassApplicationUnderTest
builds upon this mechanism.
It uses the impose(Impositions, Factory)
to register impositions, while starting up the application under test within the given function.
Ratpack “components” are explicitly designed to be aware of impositions.
Such components obtain the impositions either via the current()
method, or via the server registry (e.g. Guice injection).
User code does not typically need to be aware of impositions, as impositions are general used to influence upstream configuration.
If you do need access to the impositions, prefer obtaining it from the server registry over current()
as this method is thread sensitive.
Actual impositions are implemented as specific classes, known to the consumer.
ForceServerListenPortImposition
and UserRegistryImposition
are both examples of such.
The consumers of these impositions simply obtain them from the impositions registry.
import ratpack.server.ServerConfig;
import ratpack.impose.Impositions;
import ratpack.impose.ServerConfigImposition;
import ratpack.test.embed.EmbeddedApp;
import static groovy.util.GroovyTestCase.assertEquals;
import static java.util.Collections.singletonMap;
public class Example {
public static void main(String[] args) throws Exception {
Impositions.of(i ->
i.add(ServerConfigImposition.of(s -> s .props(singletonMap("foo", "imposed!"))))
).impose(() ->
EmbeddedApp.of(s -> s
.serverConfig(c -> c
.props(singletonMap("foo", "original"))
)
.handlers(c -> c
.get(ctx -> ctx.render(ctx.get(ServerConfig.class).get("/foo", String.class)))
)
)
).test(testHttpClient ->
assertEquals("imposed!", testHttpClient.getText())
);
}
}
ServerConfigImposition
,
ForceServerListenPortImposition
,
ForceDevelopmentImposition
,
UserRegistryImposition
Modifier and Type | Method and Description |
---|---|
static Impositions |
current()
The currently imposed impositions.
|
<T extends Imposition> |
get(Class<T> type)
Return an imposition of the given type, if one is currently imposed.
|
<T> T |
impose(Factory<? extends T> during)
Delegates to
impose(Impositions, Factory) , with this as the impositions. |
static <T> T |
impose(Impositions impositions,
Factory<? extends T> during)
Sets impositions that will be available during execution of the given function, from this thread.
|
static Impositions |
none()
An empty set of impositions.
|
static Impositions |
of(Action<? super ImpositionsSpec> consumer)
Creates an impositions instance of the given imposition objects.
|
public static <T> T impose(Impositions impositions, Factory<? extends T> during) throws Exception
The given impositions will effectively be the value returned by current()
during the given function,
which is executed immediately.
The given impositions will only be returned from current()
if called from the same thread that is calling this method.
T
- the type of result of the given functionimpositions
- the impositions to impose during the given functionduring
- the function to execute while the impositions are imposedException
- any thrown by during
public <T> T impose(Factory<? extends T> during) throws Exception
impose(Impositions, Factory)
, with this
as the impositions.T
- the type of result of the given functionduring
- the function to execute while the impositions are imposedException
- any thrown by during
public static Impositions current()
When called during a call to impose(Impositions, Factory)
from the same thread,
returns an the impositions given to that method.
If no impositions have been imposed at call time, the returned impositions object is effectively empty.
public static Impositions none()
Possibly useful during testing.
public static Impositions of(Action<? super ImpositionsSpec> consumer) throws Exception
Possibly useful during testing.
Exception
public <T extends Imposition> Optional<T> get(Class<T> type)
T
- the type of impositiontype
- the type of imposition