Bael 7231 (#15707)
* feat: Simple test with Junit Pioneer * feat: Simple test for PATH env variable * feat: Test for getting all the environment variables * feat: Guardrails for an environment test * feat: Fixed the test * feat: Test for reflexive access to the environment * feat: Renamed a test * feat: Renamed a utility method * feat: Removed unused import * feat: Renamed a test class * feat: Child process runner * feat: Method and tag rename * feat: Docker example * feat: Testcontainer example * feat: Cleanup and renames * feat: Cleanup and renames * feat: Prevent Docker test from running * feat: Change assertion to JUnit to prevent adding assertj into the container * feat: Renamed constants * feat: Fixed the naming problem * feat: Changed conditional execution * feat: Changed conditional execution * feat: Changed conditional execution
This commit is contained in:
parent
2b82e913a6
commit
b6c126b183
|
@ -0,0 +1,7 @@
|
||||||
|
FROM maven:3.9-amazoncorretto-17
|
||||||
|
WORKDIR /app
|
||||||
|
COPY /src/test/java/com/baeldung/setenvironment/SettingDockerEnvironmentVariableUnitTest.java \
|
||||||
|
./src/test/java/com/baeldung/setenvironment/
|
||||||
|
COPY /docker-pom.xml ./
|
||||||
|
ENV CUSTOM_DOCKER_ENV_VARIABLE=TRUE
|
||||||
|
ENTRYPOINT mvn -f docker-pom.xml test
|
|
@ -0,0 +1,42 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>com.baeldung.core-java-modules</groupId>
|
||||||
|
<artifactId>core-java-lang-6</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.testcontainers</groupId>
|
||||||
|
<artifactId>testcontainers</artifactId>
|
||||||
|
<version>${testcontainers.junit.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.testcontainers</groupId>
|
||||||
|
<artifactId>junit-jupiter</artifactId>
|
||||||
|
<version>${testcontainers.junit.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-api</artifactId>
|
||||||
|
<version>5.10.1</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-engine</artifactId>
|
||||||
|
<version>5.10.1</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<testcontainers.junit.version>1.19.3</testcontainers.junit.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
|
@ -33,6 +33,25 @@
|
||||||
<artifactId>jmh-generator-annprocess</artifactId>
|
<artifactId>jmh-generator-annprocess</artifactId>
|
||||||
<version>${jmh.version}</version>
|
<version>${jmh.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit-pioneer</groupId>
|
||||||
|
<artifactId>junit-pioneer</artifactId>
|
||||||
|
<version>${junit.pioneer.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.testcontainers</groupId>
|
||||||
|
<artifactId>testcontainers</artifactId>
|
||||||
|
<version>${testcontaienr.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.testcontainers</groupId>
|
||||||
|
<artifactId>junit-jupiter</artifactId>
|
||||||
|
<version>${testcontaienr.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
@ -53,6 +72,8 @@
|
||||||
<version>${jmh.version}</version>
|
<version>${jmh.version}</version>
|
||||||
</path>
|
</path>
|
||||||
</annotationProcessorPaths>
|
</annotationProcessorPaths>
|
||||||
|
<source>14</source>
|
||||||
|
<target>14</target>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
|
@ -61,6 +82,8 @@
|
||||||
<properties>
|
<properties>
|
||||||
<mapstruct.version>1.6.0.Beta1</mapstruct.version>
|
<mapstruct.version>1.6.0.Beta1</mapstruct.version>
|
||||||
<jmh.version>1.37</jmh.version>
|
<jmh.version>1.37</jmh.version>
|
||||||
|
<junit.pioneer.version>2.2.0</junit.pioneer.version>
|
||||||
|
<testcontaienr.version>1.19.3</testcontaienr.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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 ENB_VARIABLE_VALUE = "Hello World";
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@CsvSource({ENB_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_whenGetPath_thenVariablesArePresent() {
|
||||||
|
Map<String, String> environment = System.getenv();
|
||||||
|
assertThat(environment).isNotNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@SetEnvironmentVariable(key = ENV_VARIABLE_NAME, value = ENB_VARIABLE_VALUE)
|
||||||
|
@EnabledForJreRange(max = JRE.JAVA_16)
|
||||||
|
void givenVariableSet_whenGetEnvironmentVariable_thenReturnsCorrectValue() {
|
||||||
|
String actual = System.getenv(ENV_VARIABLE_NAME);
|
||||||
|
assertThat(actual).isEqualTo(ENB_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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue