All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.junit.contrib.java.lang.system.RestoreSystemProperties Maven / Gradle / Ivy

There is a newer version: 1.19.0
Show newest version
package org.junit.contrib.java.lang.system;

import static java.lang.System.getProperties;
import static java.lang.System.setProperties;

import java.util.Properties;

import org.junit.rules.ExternalResource;

/**
 * The {@code RestoreSystemProperties} rule undoes changes of system
 * properties when the test finishes (whether it passes or fails).
 * 

Let's assume the system property {@code YourProperty} has the * value {@code YourValue}. Now run the test *

 *   public void YourTest {
 *     @Rule
 *     public final TestRule restoreSystemProperties = new RestoreSystemProperties();
 *
 *     @Test
 *     public void overrideProperty() {
 *       System.setProperty("YourProperty", "other value");
 *       assertEquals("other value", System.getProperty("YourProperty"));
 *     }
 *   }
 * 
* After running the test, the system property {@code YourProperty} has * the value {@code YourValue} again. */ public class RestoreSystemProperties extends ExternalResource { private Properties originalProperties; /** * Creates a {@code RestoreSystemProperties} rule that restores all * system properties. * * @deprecated Please use {@link #RestoreSystemProperties()}. The * rule restores all properties. That's why you don't have to * specify the properties anymore. */ @Deprecated public RestoreSystemProperties(String... properties) { } /** * Creates a {@code RestoreSystemProperties} rule that restores all * system properties. * * @since 1.8.0 */ public RestoreSystemProperties() { } /** * Does nothing. * * @since 1.6.0 * @deprecated Simply remove all calls to this method. * {@code RestoreSystemProperties} restores all properties * automatically. That's why you don't have to add the properties * anymore. */ @Deprecated public void add(String property) { } @Override protected void before() throws Throwable { originalProperties = getProperties(); setProperties(new Properties(originalProperties)); } @Override protected void after() { setProperties(originalProperties); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy