org.junitpioneer.jupiter.EnvironmentVariableUtils Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2016-2023 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v2.0 which
* accompanies this distribution and is available at
*
* http://www.eclipse.org/legal/epl-v20.html
*/
package org.junitpioneer.jupiter;
import java.lang.reflect.Field;
import java.lang.reflect.InaccessibleObjectException;
import java.util.Map;
import java.util.function.Consumer;
import org.junit.platform.commons.PreconditionViolationException;
/**
* This class modifies the internals of the environment variables map with reflection.
* Warning: If your {@link SecurityManager} does not allow modifications, it fails.
*/
class EnvironmentVariableUtils {
private EnvironmentVariableUtils() {
// private constructor to prevent instantiation of utility class
}
/**
* Set a value of an environment variable.
*
* @param name of the environment variable
* @param value of the environment variable
*/
public static void set(String name, String value) {
modifyEnvironmentVariables(map -> map.put(name, value));
}
/**
* Clear an environment variable.
*
* @param name of the environment variable
*/
public static void clear(String name) {
modifyEnvironmentVariables(map -> map.remove(name));
}
private static void modifyEnvironmentVariables(Consumer