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

org.junit.contrib.java.lang.system.ProvideSystemProperty 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.clearProperty;
import static java.lang.System.setProperty;

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.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}.
 * 

* 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 myPropertyHasMyValue
 *       = new ProvideSystemProperty("MyProperty", "MyValue");
 * 
 *     @Rule
 *     public final ProvideSystemProperty otherPropertyIsMissing
 *       = new ProvideSystemProperty("OtherProperty", null);
 * 
 *     @Test
 *     public void overrideProperty() {
 *       assertEquals("MyValue", System.getProperty("MyProperty"));
 *       assertNull(System.getProperty("OtherProperty"));
 *     }
 *   }
 * 
* * The test succeeds and after the test, the system property {@code MyProperty} * is not set and the system property {@code OtherProperty} has the value * {@code OtherValue}. *

* You can also use a single instance of the rule to achieve the same effect: * *

 *   public void MyTest {
 *     @Rule
 *     public final ProvideSystemProperty properties
 *       = new ProvideSystemProperty("MyProperty", "MyValue")
 *                              .and("OtherProperty", null);
 * 
 *     @Test
 *     public void overrideProperty() {
 *       assertEquals("MyValue", System.getProperty("MyProperty"));
 *       assertNull(System.getProperty("OtherProperty"));
 *     }
 *   }
 * 
* * 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");
 * 
*/ public class ProvideSystemProperty extends ExternalResource { private final Map properties = new LinkedHashMap(); private RestoreSystemProperties restoreSystemProperty; public static ProvideSystemProperty fromFile(String name) throws IOException { FileInputStream fis = new FileInputStream(name); return fromInputStream(fis); } public static ProvideSystemProperty fromResource(String name) throws IOException { InputStream is = ProvideSystemProperty.class.getResourceAsStream(name); return fromInputStream(is); } 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; } private ProvideSystemProperty() { } 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 { restoreSystemProperty = new RestoreSystemProperties( collectPropertyNames()); restoreSystemProperty.before(); updateProperties(); } private String[] collectPropertyNames() { return properties.keySet().toArray(new String[properties.size()]); } private void updateProperties() { for (Entry property : properties.entrySet()) { String name = property.getKey(); String value = property.getValue(); updateProperty(name, value); } } private void updateProperty(String name, String value) { if (value == null) clearProperty(name); else setProperty(name, value); } @Override protected void after() { restoreSystemProperty.after(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy