Skip to content

Commit 1a76ea9

Browse files
committed
RestoreSystemProperties sets correct properties.
RestoreSystemProperties now replaces the system properties with a copy of the original properties. This ensures that the properties behave like the original properties. That was not the case before. E.g. the `contains` method returned `false` for a property that has been originally set but was not modified in the test. Fixes #43.
1 parent e778307 commit 1a76ea9

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ System Rules is available from
1414
<dependency>
1515
<groupId>com.github.stefanbirkner</groupId>
1616
<artifactId>system-rules</artifactId>
17-
<version>1.16.0</version>
17+
<version>1.16.1</version>
1818
</dependency>
1919

2020
Please don't forget to add the scope `test` if you're using System

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</parent>
1010

1111
<artifactId>system-rules</artifactId>
12-
<version>1.16.0</version>
12+
<version>1.16.1</version>
1313
<packaging>jar</packaging>
1414

1515
<name>System Rules</name>

src/main/java/org/junit/contrib/java/lang/system/RestoreSystemProperties.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,13 @@ public void add(String property) {
6767
@Override
6868
protected void before() throws Throwable {
6969
originalProperties = getProperties();
70-
setProperties(new Properties(originalProperties));
70+
setProperties(copyOf(originalProperties));
71+
}
72+
73+
private Properties copyOf(Properties source) {
74+
Properties copy = new Properties();
75+
copy.putAll(source);
76+
return copy;
7177
}
7278

7379
@Override

src/test/java/org/junit/contrib/java/lang/system/RestoreSystemPropertiesTest.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package org.junit.contrib.java.lang.system;
22

33
import static java.lang.System.clearProperty;
4+
import static java.lang.System.getProperties;
45
import static java.lang.System.getProperty;
56
import static org.assertj.core.api.Assertions.assertThat;
67
import static org.junit.contrib.java.lang.system.Executor.executeTestWithRule;
78

89
import org.junit.After;
910
import org.junit.Test;
1011
import org.junit.rules.TestRule;
12+
import org.junit.runners.model.Statement;
13+
14+
import java.util.Properties;
1115

1216
public class RestoreSystemPropertiesTest {
1317
//ensure that every test uses the same property, because this one is restored after the test
@@ -43,11 +47,22 @@ public void property_that_does_not_exist_before_the_test_does_not_exist_after_th
4347
}
4448

4549
@Test
46-
public void property_value_is_unchanged_at_start_of_test() {
50+
public void at_start_of_a_test_properties_are_equal_to_the_original_properties() {
4751
System.setProperty(PROPERTY_KEY, "dummy value");
48-
TestThatCapturesProperties test = new TestThatCapturesProperties();
49-
executeTestWithRule(test, rule);
50-
assertThat(test.propertiesAtStart)
51-
.containsEntry(PROPERTY_KEY, "dummy value");
52+
Properties originalProperties = getProperties();
53+
executeTestWithRule(
54+
assertPropertiesAreEqualTo(originalProperties),
55+
rule
56+
);
57+
}
58+
59+
private Statement assertPropertiesAreEqualTo(
60+
final Properties expectedProperties) {
61+
return new Statement() {
62+
@Override
63+
public void evaluate() throws Throwable {
64+
assertThat(getProperties()).isEqualTo(expectedProperties);
65+
}
66+
};
5267
}
5368
}

0 commit comments

Comments
 (0)