org.junit.contrib.java.lang.system.RestoreSystemProperties Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of system-rules Show documentation
Show all versions of system-rules Show documentation
A collection of JUnit rules for testing code which uses java.lang.System.
package org.junit.contrib.java.lang.system;
import static java.lang.System.clearProperty;
import static java.lang.System.getProperty;
import static java.lang.System.setProperty;
import org.junit.rules.ExternalResource;
/**
* The {@code RestoreSystemProperties} rule undoes changes of system properties.
*
* Let's assume the system property {@code MyProperty} has the value
* {@code MyValue}. Now run the test
*
*
* public void MyTest {
* @Rule
* public final RestoreSystemProperties restoreSystemProperties
* = new RestoreSystemProperties("MyProperty");
*
* @Test
* public void overrideProperty() {
* System.setProperty("MyProperty", "other value");
* ...
* }
* }
*
*
* After running the test, the system property {@code MyProperty} has still the
* value {@code MyValue}.
*/
public class RestoreSystemProperties extends ExternalResource {
private final String[] properties;
private String[] originalValues;
public RestoreSystemProperties(String... properties) {
this.properties = properties;
}
@Override
protected void before() throws Throwable {
originalValues = new String[properties.length];
for (int i = 0; i < properties.length; i++)
originalValues[i] = getProperty(properties[i]);
}
@Override
protected void after() {
for (int i = 0; i < properties.length; i++)
if (originalValues[i] == null)
clearProperty(properties[i]);
else
setProperty(properties[i], originalValues[i]);
}
}