public static final class SystemLambda.WithEnvironmentVariables extends Object
and(String, String). The EnvironmentVariables
object is then used to execute an arbitrary piece of code with these
environment variables being present.| Modifier and Type | Method and Description |
|---|---|
SystemLambda.WithEnvironmentVariables |
and(String name,
String value)
Creates a new
WithEnvironmentVariables object that
additionally stores the value for an additional environment variable. |
<T> T |
execute(Callable<T> callable)
Executes a
Callable with environment variable values
according to what was set before. |
void |
execute(Statement statement)
Executes a statement with environment variable values according to
what was set before.
|
public SystemLambda.WithEnvironmentVariables and(String name, String value)
WithEnvironmentVariables object that
additionally stores the value for an additional environment variable.
You cannot specify the value of an environment variable twice. An
IllegalArgumentException when you try.
name - the name of the environment variable.value - the value of the environment variable.WithEnvironmentVariables object.IllegalArgumentException - when a value for the environment
variable name is already specified.SystemLambda.withEnvironmentVariable(String, String),
execute(Statement)public <T> T execute(Callable<T> callable) throws Exception
Callable with environment variable values
according to what was set before. It exposes the return value of the
Callable. All changes to environment variables are reverted
after the Callable has been executed.
@Test
void execute_code_with_environment_variables(
) throws Exception {
List<String> values = withEnvironmentVariable("first", "first value")
.and("second", "second value")
.and("third", null)
.execute(() -> asList(
System.getenv("first"),
System.getenv("second"),
System.getenv("third")
));
assertEquals(
asList("first value", "second value", null),
values
);
}
Warning: This method uses reflection for modifying internals of the
environment variables map. It fails if your SecurityManager forbids
such modifications.
T - the type of callable's resultcallable - an arbitrary piece of code.callable.Exception - any exception thrown by the callable.SystemLambda.withEnvironmentVariable(String, String),
and(String, String),
execute(Statement)public void execute(Statement statement) throws Exception
@Test
void execute_code_with_environment_variables(
) throws Exception {
withEnvironmentVariable("first", "first value")
.and("second", "second value")
.and("third", null)
.execute(() -> {
assertEquals(
"first value",
System.getenv("first")
);
assertEquals(
"second value",
System.getenv("second")
);
assertNull(
System.getenv("third")
);
});
}
Warning: This method uses reflection for modifying internals of the
environment variables map. It fails if your SecurityManager forbids
such modifications.
statement - an arbitrary piece of code.Exception - any exception thrown by the statement.SystemLambda.withEnvironmentVariable(String, String),
and(String, String),
execute(Callable)Copyright © 2020. All rights reserved.