From 5c049a9e11608be583ddc7090b79167140adeaad Mon Sep 17 00:00:00 2001 From: Vivien Tintillier Date: Thu, 11 Jan 2024 22:02:11 +0000 Subject: [PATCH 1/2] add test --- .../properties/SystemPropertiesTest.java | 3 +-- .../jupiter/SystemStubsExtensionTest.java | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/system-stubs-core/src/test/java/uk/org/webcompere/systemstubs/properties/SystemPropertiesTest.java b/system-stubs-core/src/test/java/uk/org/webcompere/systemstubs/properties/SystemPropertiesTest.java index 58a83ae..33feaad 100644 --- a/system-stubs-core/src/test/java/uk/org/webcompere/systemstubs/properties/SystemPropertiesTest.java +++ b/system-stubs-core/src/test/java/uk/org/webcompere/systemstubs/properties/SystemPropertiesTest.java @@ -147,8 +147,7 @@ void settingAfterPreDeleteAlsoWorks() throws Exception { properties.set("bar", "h"); assertThat(System.getProperty("bar")).isEqualTo("h"); - SystemProperties nested = new SystemProperties(); - nested.remove("bar"); + SystemProperties nested = new SystemProperties().remove("bar"); nested.execute(() -> { nested.set("bar", "bong"); assertThat(System.getProperty("bar")).isEqualTo("bong"); diff --git a/system-stubs-jupiter/src/test/java/uk/org/webcompere/systemstubs/jupiter/SystemStubsExtensionTest.java b/system-stubs-jupiter/src/test/java/uk/org/webcompere/systemstubs/jupiter/SystemStubsExtensionTest.java index 46f6fa9..f3719fc 100644 --- a/system-stubs-jupiter/src/test/java/uk/org/webcompere/systemstubs/jupiter/SystemStubsExtensionTest.java +++ b/system-stubs-jupiter/src/test/java/uk/org/webcompere/systemstubs/jupiter/SystemStubsExtensionTest.java @@ -43,6 +43,29 @@ void method3_hasCleanPropertiesAndEnvironment() { } } + @ExtendWith(SystemStubsExtension.class) + @Nested + class SystemPropertiesAsParentClass { + + @SystemStub + private SystemProperties systemProperties = new WithDefaultsSystemProperties() + .set("key1", "value3"); + + @Test + void defaultValuesAreSet() { + assertThat(System.getProperty("key1")).isEqualTo("value3"); + assertThat(System.getProperty("key2")).isEqualTo("value2"); + } + } + + private static class WithDefaultsSystemProperties extends SystemProperties { + + WithDefaultsSystemProperties() { + super("key1", "value1"); + this.set("key2", "value2"); + } + } + @ExtendWith(SystemStubsExtension.class) @Nested class FieldWithoutAnnotation { From 1cc987dbc6efad72148ba7494c3569bdeee21e29 Mon Sep 17 00:00:00 2001 From: Vivien Tintillier Date: Thu, 11 Jan 2024 22:03:24 +0000 Subject: [PATCH 2/2] Fix signatures --- .../systemstubs/properties/SystemPropertiesImpl.java | 12 +++++++----- .../systemstubs/rules/SystemPropertiesRule.java | 9 --------- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/system-stubs-core/src/main/java/uk/org/webcompere/systemstubs/properties/SystemPropertiesImpl.java b/system-stubs-core/src/main/java/uk/org/webcompere/systemstubs/properties/SystemPropertiesImpl.java index 3e5bf5d..5426ba8 100644 --- a/system-stubs-core/src/main/java/uk/org/webcompere/systemstubs/properties/SystemPropertiesImpl.java +++ b/system-stubs-core/src/main/java/uk/org/webcompere/systemstubs/properties/SystemPropertiesImpl.java @@ -16,7 +16,7 @@ * that will be applied to the system to be set before the stubbing is triggered. */ public class SystemPropertiesImpl> extends SingularTestResource - implements NameValuePairSetter> { + implements NameValuePairSetter { private Properties originalProperties; private Properties properties; @@ -70,12 +70,13 @@ public SystemPropertiesImpl(String name, String value, String... nameValues) { * @since 1.0.0 */ @Override - public SystemPropertiesImpl set(String name, String value) { + @SuppressWarnings("unchecked") + public T set(String name, String value) { properties.setProperty(name, value); if (isActive()) { System.setProperty(name, value); } - return this; + return (T) this; } /** @@ -86,12 +87,13 @@ public SystemPropertiesImpl set(String name, String value) { * @since 2.1.5 */ @Override - public SystemPropertiesImpl remove(String name) { + @SuppressWarnings("unchecked") + public T remove(String name) { propertiesToRemove.add(name); if (isActive()) { System.getProperties().remove(name); } - return this; + return (T) this; } @Override diff --git a/system-stubs-junit4/src/main/java/uk/org/webcompere/systemstubs/rules/SystemPropertiesRule.java b/system-stubs-junit4/src/main/java/uk/org/webcompere/systemstubs/rules/SystemPropertiesRule.java index 3fd8295..0e39b38 100644 --- a/system-stubs-junit4/src/main/java/uk/org/webcompere/systemstubs/rules/SystemPropertiesRule.java +++ b/system-stubs-junit4/src/main/java/uk/org/webcompere/systemstubs/rules/SystemPropertiesRule.java @@ -3,7 +3,6 @@ import uk.org.webcompere.systemstubs.properties.SystemPropertiesImpl; import uk.org.webcompere.systemstubs.rules.internal.SystemStubTestRule; -import java.util.Map; import java.util.Properties; /** @@ -30,12 +29,4 @@ public SystemPropertiesRule(Properties properties) { public SystemPropertiesRule(String name, String value, String... nameValues) { super(name, value, nameValues); } - - /** - * {@inheritDoc} - */ - @Override - public SystemPropertiesRule set(Map properties) { - return (SystemPropertiesRule)super.set(properties); - } }