Skip to content

Commit

Permalink
Merge pull request #68 from webcompere/interoperability-with-other-li…
Browse files Browse the repository at this point in the history
…braries

Provide backwards compatibility for tools that should be able to mock the environment
  • Loading branch information
ashleyfrieze authored Oct 1, 2023
2 parents 2a472d1 + fbcb134 commit 3cdac62
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class EnvironmentVariableMocker {
private static final Map<String, String> ORIGINAL_ENV;

static {
ORIGINAL_ENV = System.getenv();
ORIGINAL_ENV = new HashMap<>(System.getenv());
try {
Instrumentation instrumentation = ByteBuddyAgent.install();
installInterceptorIntoBootLoader(instrumentation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ void can_load_properties_from_resources() throws Exception {
new EnvironmentVariables()
.set(fromResource("test.properties"))
.execute(() -> {
assertThat(System.getenv()).containsEntry("value1", "foo").containsEntry("value2", "bar");
assertThat(System.getenv("value1")).isEqualTo("foo");
assertThat(System.getenv("value2")).isEqualTo("bar");
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import java.util.*;

import static java.util.stream.Collectors.toMap;
import static java.util.stream.Collectors.toSet;

/**
* Plugs into the boot loader to provide an alternative implementation to ProcessEnvironment
Expand All @@ -13,6 +13,9 @@
public class ProcessEnvironmentInterceptor {
private static Map<String, String> CURRENT_ENVIRONMENT_VARIABLES = new HashMap<>();

@SuppressFBWarnings("URF_UNREAD_FIELD")
private static Map<String, String> theEnvironment;

/**
* For use by the EnvironmentMocker - this overwrites the effective environment variables that the system
* appears to have.
Expand All @@ -21,15 +24,19 @@ public class ProcessEnvironmentInterceptor {
@SuppressFBWarnings("EI_EXPOSE_STATIC_REP2")
public static void setEnv(Map<String, String> env) {
CURRENT_ENVIRONMENT_VARIABLES = env;

// this copy exposes process environment to tools looking to mock it
theEnvironment = Collections.unmodifiableMap(CURRENT_ENVIRONMENT_VARIABLES);
}

/**
* The equivalent of <code>getenv</code> in the original ProcessEnvironment, assuming that
* mocking is "turned on"
* @return the current effective environment
*/
@SuppressFBWarnings("MS_EXPOSE_REP")
public static Map<String, String> getenv() {
return filterNulls(CURRENT_ENVIRONMENT_VARIABLES);
return Collections.unmodifiableMap(filterNulls(CURRENT_ENVIRONMENT_VARIABLES));
}

/**
Expand All @@ -47,7 +54,7 @@ public static String getenv(String name) {
* @return the environment map
*/
public static Map<String, String> environment() {
return getenv();
return new HashMap<>(getenv());
}

/**
Expand Down Expand Up @@ -129,10 +136,14 @@ public static byte[] toEnvironmentBlock(Map<String, String> m, int[] envc) {
}

private static Map<String, String> filterNulls(Map<String, String> currentMockedEnvironment) {
return currentMockedEnvironment.entrySet()
var nullsToRemove = currentMockedEnvironment.entrySet()
.stream()
.filter(entry -> entry.getValue() != null)
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
.filter(entry -> entry.getValue() == null)
.map(Map.Entry::getKey)
.collect(toSet());

nullsToRemove.forEach(currentMockedEnvironment::remove);
return currentMockedEnvironment;
}

@SuppressFBWarnings("SE_COMPARATOR_SHOULD_BE_SERIALIZABLE")
Expand Down
7 changes: 7 additions & 0 deletions system-stubs-jupiter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit-pioneer</groupId>
<artifactId>junit-pioneer</artifactId>
<version>2.1.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package uk.org.webcompere.systemstubs.jupiter.compatibility;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledForJreRange;
import org.junit.jupiter.api.condition.JRE;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junitpioneer.jupiter.SetEnvironmentVariable;
import uk.org.webcompere.systemstubs.environment.EnvironmentVariables;
import uk.org.webcompere.systemstubs.jupiter.SystemStub;
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;

import static org.assertj.core.api.Assertions.assertThat;

@EnabledForJreRange(max = JRE.JAVA_16)
class DoesNotPreventJUnitPioneerFromWorkingTest {

@Nested
@ExtendWith(SystemStubsExtension.class)
static class StubsWorks {
@SystemStub
private EnvironmentVariables variables;

@Test
void canStubVariable() {
variables.set("MACHINE", "hot");

assertThat(System.getenv("MACHINE")).isEqualTo("hot");
}
}

@Nested
@SetEnvironmentVariable(key="MACHINE", value="COLD")
static class PioneerWorks {
@Test
void canPioneerVariable() {
assertThat(System.getenv("MACHINE")).isEqualTo("cold");
}
}

}

0 comments on commit 3cdac62

Please sign in to comment.