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

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

package org.junit.contrib.java.lang.system;

import static java.lang.System.clearProperty;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

import org.junit.contrib.java.lang.system.internal.RestoreSpecificSystemProperties;
import org.junit.rules.ExternalResource;

/**
 * The {@code ProvideSystemProperty} rule provides an arbitrary value for a
 * system property to a test. After the test the original value is restored. You
 * can ensure that a property is not set by providing {@code null} (or using
 * {@link org.junit.contrib.java.lang.system.ClearSystemProperties}).
 * 

* Let's assume the system property {@code MyProperty} is not set and the system * property {@code OtherProperty} has the value {@code OtherValue}. Now run the * test * *

 *   public void MyTest {
 *     @Rule
 *     public final ProvideSystemProperty provideSystemProperty
 *       = new ProvideSystemProperty("MyProperty", "MyValue")
 *         .and("OtherProperty", null);
 *
 *     @Test
 *     public void overridesProperty() {
 *       assertEquals("MyValue", System.getProperty("MyProperty"));
 *     }
 *
 *     @Test
 *     public void deletesProperty() {
 *       assertNull(System.getProperty("OtherProperty"));
 *     }
 *   }
 * 
* * Both tests succeed and after the tests, the system property * {@code MyProperty} is not set and the system property {@code OtherProperty} * has the value {@code OtherValue}. *

* You can use a properties file to supply properties for the * ProvideSystemProperty rule. The file can be from the file system or the class * path. In the first case use * *

 * @Rule
 * public final ProvideSystemProperty properties = ProvideSystemProperty
 * 		.fromFile("/home/myself/example.properties");
 * 
* * and in the second case use * *
 * @Rule
 * public final ProvideSystemProperty properties = ProvideSystemProperty
 * 		.fromResource("example.properties");
 * 
*

Set property for a single test

*

If you want to set a property for a single test then you can use * {@link org.junit.contrib.java.lang.system.RestoreSystemProperties} * along with {@link System#setProperty(String, String)}. *

 * @Rule
 * public final TestRule restoreSystemProperties
 *   = new RestoreSystemProperties();
 *
 * @Test
 * public void test() {
 *   System.setProperty("YourProperty", "YourValue");
 *   ...
 * }
*/ public class ProvideSystemProperty extends ExternalResource { private final Map properties = new LinkedHashMap(); private final RestoreSpecificSystemProperties restoreSystemProperty = new RestoreSpecificSystemProperties(); public static ProvideSystemProperty fromFile(String name) { try { FileInputStream fis = new FileInputStream(name); return fromInputStream(fis); } catch (IOException e) { throw new IllegalArgumentException( "Cannot create ProvideSystemProperty rule because file \"" + name + "\" cannot be read.", e); } } public static ProvideSystemProperty fromResource(String name) { InputStream is = ProvideSystemProperty.class.getResourceAsStream(name); try { return fromInputStream(is); } catch (IOException e) { throw new IllegalArgumentException( "Cannot create ProvideSystemProperty rule because resource \"" + name + "\" cannot be read.", e); } } private static ProvideSystemProperty fromInputStream(InputStream is) throws IOException { Properties p = new Properties(); p.load(is); ProvideSystemProperty rule = new ProvideSystemProperty(); for (Map.Entry property : p.entrySet()) rule.addProperty((String) property.getKey(), (String) property.getValue()); return rule; } /** * @deprecated see {@link #setProperty(String, String)}. */ @Deprecated public ProvideSystemProperty() { } /** * Sets the property with the name to the specified value. After the test * the rule restores the value of the property at the point of setting it. * *

This method is deprecated. If you're still using it, please replace your current code *

	 * @Rule
	 * public final ProvideSystemProperty provideSystemProperty = new ProvideSystemProperty();
	 *
	 * @Test
	 * public void test() {
	 *   provideSystemProperty.setProperty("YourProperty", "YourValue");
	 *   ...
	 * }
* with this code: *
	 * @Rule
	 * public final TestRule restoreSystemProperties = new RestoreSystemProperties();
	 *
	 * @Test
	 * public void test() {
	 *   System.setProperty("YourProperty", "YourValue");
	 *   ...
	 * }
* * @param name the name of the property. * @param value the new value of the property. * @since 1.6.0 * @deprecated Please use {@link org.junit.contrib.java.lang.system.RestoreSystemProperties} * along with {@link System#setProperty(String, String)}. */ @Deprecated public void setProperty(String name, String value) { restoreSystemProperty.add(name); if (value == null) clearProperty(name); else System.setProperty(name, value); } public ProvideSystemProperty(String name, String value) { addProperty(name, value); } public ProvideSystemProperty and(String name, String value) { addProperty(name, value); return this; } private void addProperty(String name, String value) { properties.put(name, value); } @Override protected void before() throws Throwable { setProperties(); } private void setProperties() { for (Entry property : properties.entrySet()) { String name = property.getKey(); String value = property.getValue(); setProperty(name, value); } } @Override protected void after() { restoreSystemProperty.restore(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy