BAEL-7231: Moved the code to a separate module (#16272)
This commit is contained in:
+44
@@ -0,0 +1,44 @@
|
||||
package com.baeldung.setenvironment;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import org.junit.jupiter.api.Tag;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
|
||||
|
||||
class SettingChildProcessEnvironmentVariableUnitTest {
|
||||
|
||||
public static final String ENVIRONMENT_VARIABLE_NAME = "test";
|
||||
public static final String ENVIRONMENT_VARIABLE_VALUE = "Hello World";
|
||||
public static final String CHILD_PROCESS_CONDITION = "CHILD_PROCESS_TEST";
|
||||
public static final String CHILD_PROCESS_VALUE = "true";
|
||||
public static final String CHILD_PROCESS_TAG = "child_process";
|
||||
public static final String TAG = String.format("-Dgroups=%s", CHILD_PROCESS_TAG);
|
||||
private final String testClass = String.format("-Dtest=%s", getClass().getName());
|
||||
private final String[] arguments = {"mvn", "test", TAG, testClass};
|
||||
|
||||
@Test
|
||||
void givenChildProcessTestRunner_whenRunTheTest_thenAllSucceed()
|
||||
throws IOException, InterruptedException {
|
||||
ProcessBuilder processBuilder = new ProcessBuilder();
|
||||
processBuilder.inheritIO();
|
||||
|
||||
Map<String, String> environment = processBuilder.environment();
|
||||
environment.put(CHILD_PROCESS_CONDITION, CHILD_PROCESS_VALUE);
|
||||
environment.put(ENVIRONMENT_VARIABLE_NAME, ENVIRONMENT_VARIABLE_VALUE);
|
||||
Process process = processBuilder.command(arguments).start();
|
||||
|
||||
int errorCode = process.waitFor();
|
||||
assertThat(errorCode).isZero();
|
||||
}
|
||||
|
||||
@Test
|
||||
@EnabledIfEnvironmentVariable(named = CHILD_PROCESS_CONDITION, matches = CHILD_PROCESS_VALUE)
|
||||
@Tag(CHILD_PROCESS_TAG)
|
||||
void givenChildProcess_whenGetEnvironmentVariable_thenReturnsCorrectValue() {
|
||||
String actual = System.getenv(ENVIRONMENT_VARIABLE_NAME);
|
||||
assertThat(actual).isEqualTo(ENVIRONMENT_VARIABLE_VALUE);
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.setenvironment;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
|
||||
|
||||
class SettingDockerEnvironmentVariableUnitTest {
|
||||
|
||||
public static final String ENV_VARIABLE_NAME = "CUSTOM_DOCKER_ENV_VARIABLE";
|
||||
public static final String ENV_VARIABLE_VALUE = "TRUE";
|
||||
|
||||
@Test
|
||||
@EnabledIfEnvironmentVariable(named = ENV_VARIABLE_NAME, matches = ENV_VARIABLE_VALUE)
|
||||
void givenDockerEnvironment_whenGetEnvironmentVariable_thenReturnsCorrectValue() {
|
||||
String actual = System.getenv(ENV_VARIABLE_NAME);
|
||||
assertEquals(ENV_VARIABLE_VALUE, actual);
|
||||
}
|
||||
}
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
package com.baeldung.setenvironment;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.condition.EnabledForJreRange;
|
||||
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
|
||||
import org.junit.jupiter.api.condition.JRE;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junitpioneer.jupiter.SetEnvironmentVariable;
|
||||
|
||||
class SettingSameProcessEnvironmentVariableUnitTest {
|
||||
|
||||
private static final String PROCESS_ENVIRONMENT = "java.lang.ProcessEnvironment";
|
||||
private static final String ENVIRONMENT = "theUnmodifiableEnvironment";
|
||||
private static final String SOURCE_MAP = "m";
|
||||
private static final Object STATIC_METHOD = null;
|
||||
private static final Class<?> UMODIFIABLE_MAP_CLASS
|
||||
= Collections.unmodifiableMap(Collections.emptyMap()).getClass();
|
||||
private static final Class<?> MAP_CLASS = Map.class;
|
||||
public static final String ENV_VARIABLE_NAME = "test";
|
||||
public static final String ENV_VARIABLE_VALUE = "Hello World";
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({ENV_VARIABLE_VALUE + "," + ENV_VARIABLE_NAME})
|
||||
@EnabledForJreRange(max = JRE.JAVA_16)
|
||||
void givenReflexiveAccess_whenGetSourceMap_thenSuccessfullyModifyVariables(String environmentVariable, String value)
|
||||
throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
|
||||
Map<String, String> modifiableEnvironment = getModifiableEnvironment();
|
||||
assertThat(modifiableEnvironment).isNotNull();
|
||||
|
||||
modifiableEnvironment.put(environmentVariable, value);
|
||||
String actual = modifiableEnvironment.get(environmentVariable);
|
||||
assertThat(actual).isEqualTo(value);
|
||||
}
|
||||
|
||||
@Test
|
||||
@EnabledIfEnvironmentVariable(named = "PATH", matches = ".*",
|
||||
disabledReason = "The test relies on the presence of PATH variable")
|
||||
void givenOS_whenGetPath_thenVariableIsPresent() {
|
||||
String classPath = System.getenv("PATH");
|
||||
assertThat(classPath).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenOS_whenGetEnv_thenVariablesArePresent() {
|
||||
Map<String, String> environment = System.getenv();
|
||||
assertThat(environment).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
@SetEnvironmentVariable(key = ENV_VARIABLE_NAME, value = ENV_VARIABLE_VALUE)
|
||||
@EnabledForJreRange(max = JRE.JAVA_16)
|
||||
void givenVariableSet_whenGetEnvironmentVariable_thenReturnsCorrectValue() {
|
||||
String actual = System.getenv(ENV_VARIABLE_NAME);
|
||||
assertThat(actual).isEqualTo(ENV_VARIABLE_VALUE);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static Map<String, String> getModifiableEnvironment()
|
||||
throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
|
||||
Class<?> environmentClass = Class.forName(PROCESS_ENVIRONMENT);
|
||||
Field environmentField = environmentClass.getDeclaredField(ENVIRONMENT);
|
||||
assertThat(environmentField).isNotNull();
|
||||
environmentField.setAccessible(true);
|
||||
|
||||
Object unmodifiableEnvironmentMap = environmentField.get(STATIC_METHOD);
|
||||
assertThat(unmodifiableEnvironmentMap).isNotNull();
|
||||
assertThat(unmodifiableEnvironmentMap).isInstanceOf(UMODIFIABLE_MAP_CLASS);
|
||||
|
||||
Field underlyingMapField = unmodifiableEnvironmentMap.getClass().getDeclaredField(SOURCE_MAP);
|
||||
underlyingMapField.setAccessible(true);
|
||||
Object underlyingMap = underlyingMapField.get(unmodifiableEnvironmentMap);
|
||||
assertThat(underlyingMap).isNotNull();
|
||||
assertThat(underlyingMap).isInstanceOf(MAP_CLASS);
|
||||
|
||||
return (Map<String, String>) underlyingMap;
|
||||
}
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.setenvironment;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.testcontainers.containers.GenericContainer;
|
||||
import org.testcontainers.images.builder.ImageFromDockerfile;
|
||||
|
||||
class SettingTestcontainerVariableUnitTest {
|
||||
|
||||
public static final String CONTAINER_REPORT_FILE = "/app/target/surefire-reports/TEST-com.baeldung.setenvironment.SettingDockerEnvironmentVariableUnitTest.xml";
|
||||
public static final String HOST_REPORT_FILE = "./container-test-report.xml";
|
||||
public static final String DOCKERFILE = "./Dockerfile";
|
||||
|
||||
@Test
|
||||
@Disabled("Requires working Docker environment ")
|
||||
void givenTestcontainerEnvironment_whenGetEnvironmentVariable_thenReturnsCorrectValue() {
|
||||
Path dockerfilePath = Paths.get(DOCKERFILE);
|
||||
GenericContainer container = new GenericContainer(
|
||||
new ImageFromDockerfile().withDockerfile(dockerfilePath));
|
||||
assertThat(container).isNotNull();
|
||||
container.start();
|
||||
while (container.isRunning()) {
|
||||
// Busy spin
|
||||
}
|
||||
container.copyFileFromContainer(CONTAINER_REPORT_FILE, HOST_REPORT_FILE);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user