Skip to content

Commit

Permalink
Merge pull request dsaff#2 from marcphilipp/ClearSystemProperty
Browse files Browse the repository at this point in the history
Introduced ClearSystemProperty rule.
  • Loading branch information
stefanbirkner committed Oct 6, 2011
2 parents 5a2ced3 + 732c4b3 commit 678289c
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.junit.contrib.java.lang.system;

import static java.lang.System.clearProperty;

import org.junit.rules.ExternalResource;

/**
* The {@code ClearSystemProperty} rule clears a system property to a test.
* After the test the original value is restored.
*
* Let's assume the system property {@code MyProperty} has the value
* {@code MyValue}. Now run the test
*
* <pre>
* public void MyTest {
* &#064;Rule
* public final ClearSystemProperty myPropertyIsCleared
* = new ClearSystemProperty("MyProperty");
*
* &#064;Test
* public void overrideProperty() {
* assertNull(System.getProperty("MyProperty"));
* }
* }
* </pre>
*
* The test succeeds and after the test, the system property {@code MyProperty}
* has the value {@code MyValue}.
*/
public class ClearSystemProperty extends ExternalResource {

private final RestoreSystemProperties restoreSystemProperty;
private final String name;

public ClearSystemProperty(String name) {
this.name = name;
this.restoreSystemProperty = new RestoreSystemProperties(name);
}

@Override
protected void before() throws Throwable {
backupOriginalValue();
clearProperty(name);
}

@Override
protected void after() {
restoreOriginalValue();
}

private void backupOriginalValue() throws Throwable {
restoreSystemProperty.before();
}

private void restoreOriginalValue() {
restoreSystemProperty.after();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.junit.contrib.java.lang.system;

import static java.lang.System.clearProperty;
import static java.lang.System.getProperty;
import static java.lang.System.setProperty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runners.model.Statement;

public class ClearSystemPropertyTest {

private static final String ARBITRARY_NAME = "arbitrary property";
private static final String ARBITRARY_VALUE = "arbitrary value";

@Rule
public final RestoreSystemProperties restore = new RestoreSystemProperties(
ARBITRARY_NAME);

@Test
public void restoresOriginalValue() throws Throwable {
setProperty(ARBITRARY_NAME, ARBITRARY_VALUE);

ClearSystemProperty rule = new ClearSystemProperty(ARBITRARY_NAME);
rule.apply(new ClearedValue(), null).evaluate();

assertThat(getProperty(ARBITRARY_NAME), is(equalTo(ARBITRARY_VALUE)));
}

@Test
public void originallyUnsetPropertyRemainsUnset() throws Throwable {
clearProperty(ARBITRARY_NAME);

ClearSystemProperty rule = new ClearSystemProperty(ARBITRARY_NAME);
rule.apply(new ClearedValue(), null).evaluate();

assertThat(getProperty(ARBITRARY_NAME), is(nullValue(String.class)));
}

private class ClearedValue extends Statement {

@Override
public void evaluate() throws Throwable {
assertThat(getProperty(ARBITRARY_NAME), is(nullValue(String.class)));
}
}
}

0 comments on commit 678289c

Please sign in to comment.