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 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