Class Impositions
- java.lang.Object
-
- ratpack.impose.Impositions
-
public final class Impositions extends java.lang.Object
A mechanism for imposing things on an application from the outside and typically during testing.The impositions mechanism exists primarily to facilitate convenient overriding of configuration and behaviour at test time.
MainClassApplicationUnderTest
builds upon this mechanism. It uses theimpose(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 overcurrent()
as this method is thread sensitive.Actual impositions are implemented as specific classes, known to the consumer.
ForceServerListenPortImposition
andUserRegistryImposition
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()) ); } }
- Since:
- 1.2
- See Also:
ServerConfigImposition
,ForceServerListenPortImposition
,ForceDevelopmentImposition
,UserRegistryImposition
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static Impositions
current()
The currently imposed impositions.<T extends Imposition>
java.util.Optional<T>get(java.lang.Class<T> type)
Return an imposition of the given type, if one is currently imposed.<T> T
impose(Factory<? extends T> during)
Delegates toimpose(Impositions, Factory)
, withthis
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.
-
-
-
Method Detail
-
impose
public static <T> T impose(Impositions impositions, Factory<? extends T> during) throws java.lang.Exception
Sets impositions that will be available during execution of the given function, from this thread.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.- Type Parameters:
T
- the type of result of the given function- Parameters:
impositions
- the impositions to impose during the given functionduring
- the function to execute while the impositions are imposed- Returns:
- the result of the given function
- Throws:
java.lang.Exception
- any thrown byduring
-
impose
public <T> T impose(Factory<? extends T> during) throws java.lang.Exception
Delegates toimpose(Impositions, Factory)
, withthis
as the impositions.- Type Parameters:
T
- the type of result of the given function- Parameters:
during
- the function to execute while the impositions are imposed- Returns:
- the result of the given function
- Throws:
java.lang.Exception
- any thrown byduring
-
current
public static Impositions current()
The currently imposed impositions.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.
- Returns:
- the currently imposed impositions
-
none
public static Impositions none()
An empty set of impositions.Possibly useful during testing.
- Returns:
- an empty set of impositions
-
of
public static Impositions of(Action<? super ImpositionsSpec> consumer) throws java.lang.Exception
Creates an impositions instance of the given imposition objects.Possibly useful during testing.
- Returns:
- an impositions instance of the given imposition objects
- Throws:
java.lang.Exception
-
get
public <T extends Imposition> java.util.Optional<T> get(java.lang.Class<T> type)
Return an imposition of the given type, if one is currently imposed.- Type Parameters:
T
- the type of imposition- Parameters:
type
- the type of imposition- Returns:
- the impositions of the given type
-
-