Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix set and remove method signatures #81

Merged
merged 2 commits into from
Jan 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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