forked from dsaff/junit.contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request dsaff#2 from marcphilipp/ClearSystemProperty
Introduced ClearSystemProperty rule.
- Loading branch information
Showing
2 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
system-rules/src/main/java/org/junit/contrib/java/lang/system/ClearSystemProperty.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
* @Rule | ||
* public final ClearSystemProperty myPropertyIsCleared | ||
* = new ClearSystemProperty("MyProperty"); | ||
* | ||
* @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(); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
system-rules/src/test/java/org/junit/contrib/java/lang/system/ClearSystemPropertyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} | ||
} | ||
} |