org.junit.contrib.java.lang.system.ProvideSystemProperty 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.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