Skip to content

Commit

Permalink
Merge pull request #81 from vtintillier/gh80
Browse files Browse the repository at this point in the history
Fix set and remove method signatures
  • Loading branch information
ashleyfrieze committed Jan 13, 2024
2 parents 1cb1edd + 1cc987d commit 987b72c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* that will be applied to the system to be set before the stubbing is triggered.
*/
public class SystemPropertiesImpl<T extends SystemPropertiesImpl<T>> extends SingularTestResource
implements NameValuePairSetter<SystemPropertiesImpl<T>> {
implements NameValuePairSetter<T> {
private Properties originalProperties;
private Properties properties;

Expand Down Expand Up @@ -70,12 +70,13 @@ public SystemPropertiesImpl(String name, String value, String... nameValues) {
* @since 1.0.0
*/
@Override
public SystemPropertiesImpl<T> 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;
}

/**
Expand All @@ -86,12 +87,13 @@ public SystemPropertiesImpl<T> set(String name, String value) {
* @since 2.1.5
*/
@Override
public SystemPropertiesImpl<T> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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<Object, Object> properties) {
return (SystemPropertiesRule)super.set(properties);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 987b72c

Please sign in to comment.