Permalink
Browse files

First Rule: SystemProperties

  • Loading branch information...
0 parents commit c159350e755c41eeed1ffe2a6475c6ae516d9270 @marcphilipp committed Dec 2, 2010
1 .gitignore
@@ -0,0 +1 @@
+.metadata
3 .gitmodules
@@ -0,0 +1,3 @@
+[submodule "junit"]
+ path = junit
+ url = git://github.com/KentBeck/junit.git
1 junit
@@ -0,0 +1 @@
+Subproject commit 81b50e9f3bdb66543d07e7349026bd3b447f433e
8 junit-rules/.classpath
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" path="src/main/java"/>
+ <classpathentry kind="src" path="src/test/java"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
+ <classpathentry combineaccessrules="false" kind="src" path="/junit"/>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
2 junit-rules/.gitignore
@@ -0,0 +1,2 @@
+bin
+
17 junit-rules/.project
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>junit-rules</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
12 junit-rules/.settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,12 @@
+#Thu Dec 02 20:55:52 CET 2010
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=1.5
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.source=1.5
52 junit-rules/src/main/java/org/junit/rules/ext/SystemProperties.java
@@ -0,0 +1,52 @@
+package org.junit.rules.ext;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.junit.rules.ExternalResource;
+
+public class SystemProperties extends ExternalResource {
+
+ private final Map<String, String> values = new HashMap<String, String>();
+
+ public SystemProperties set(String key, String value) {
+ backup(key);
+ put(key, value);
+ return this;
+ }
+
+ public SystemProperties unset(String key) {
+ set(key, null);
+ return this;
+ }
+
+ @Override
+ protected void after() {
+ restoreAll();
+ }
+
+ public void restoreAll() {
+ for (String key : values.keySet()) {
+ restore(key);
+ }
+ }
+
+ public void restore(String key) {
+ put(key, values.get(key));
+ }
+
+ private void backup(String key) {
+ if (values.containsKey(key)) {
+ return;
+ }
+ values.put(key, System.getProperty(key));
+ }
+
+ private void put(String key, String value) {
+ if (value == null) {
+ System.getProperties().remove(key);
+ } else {
+ System.setProperty(key, value);
+ }
+ }
+}
61 junit-rules/src/test/java/org/junit/rules/ext/SystemPropertiesTest.java
@@ -0,0 +1,61 @@
+package org.junit.rules.ext;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import java.util.Properties;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class SystemPropertiesTest {
+
+ private static final String UNKNOWN_KEY = "unknownKey";
+ private static final String KNOWN_KEY = "foo";
+ private static final String ORIGINAL_VALUE_OF_KNOWN_KEY = "nonsense";
+
+ public SystemProperties system = new SystemProperties();
+
+ @Before
+ public void putKnownKey() {
+ System.setProperty(KNOWN_KEY, ORIGINAL_VALUE_OF_KNOWN_KEY);
+ }
+
+ @After
+ public void removeKnownKey() {
+ System.getProperties().remove(KNOWN_KEY);
+ }
+
+ @Test
+ public void restoresAllKeysInTheEnd() {
+ Properties original = new Properties(System.getProperties());
+ system.set(KNOWN_KEY, "bar").set(UNKNOWN_KEY, "baz").after();
+ Properties modified = new Properties(System.getProperties());
+ assertEquals(original, modified);
+ }
+
+ @Test
+ public void setsSystemProperty() {
+ system.set(KNOWN_KEY, "bar");
+ assertEquals("bar", System.getProperty(KNOWN_KEY));
+ }
+
+ @Test
+ public void lastSetWins() {
+ system.set(KNOWN_KEY, "bar").set(KNOWN_KEY, "baz");
+ assertEquals("baz", System.getProperty(KNOWN_KEY));
+ }
+
+ @Test
+ public void restorePutsOriginalValueForKnownKey() {
+ system.set(KNOWN_KEY, "bar").set(KNOWN_KEY, "baz").restore(KNOWN_KEY);
+ assertEquals(ORIGINAL_VALUE_OF_KNOWN_KEY, System.getProperty(KNOWN_KEY));
+ }
+
+ @Test
+ public void restoreRemovesKeyForUnknownKey() {
+ system.set(UNKNOWN_KEY, "bar").set(UNKNOWN_KEY, "baz").restore(UNKNOWN_KEY);
+ assertNull(System.getProperty(UNKNOWN_KEY));
+ }
+}

0 comments on commit c159350

Please sign in to comment.