-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #75 from webcompere/delete-settings
Support removal of environment variables and system properties
- Loading branch information
Showing
12 changed files
with
401 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 4 additions & 69 deletions
73
...m-stubs-core/src/main/java/uk/org/webcompere/systemstubs/properties/SystemProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,23 @@ | ||
package uk.org.webcompere.systemstubs.properties; | ||
|
||
import uk.org.webcompere.systemstubs.resource.NameValuePairSetter; | ||
import uk.org.webcompere.systemstubs.resource.SingularTestResource; | ||
|
||
import java.util.Properties; | ||
|
||
import static java.lang.System.getProperties; | ||
import static java.lang.System.setProperties; | ||
|
||
/** | ||
* Maintain system properties after a test from the ones before the test. Stores the | ||
* existing properties when started, and restores them when complete. Allows for a list of properties | ||
* that will be applied to the system to be set before the stubbing is triggered. | ||
*/ | ||
public class SystemProperties extends SingularTestResource implements NameValuePairSetter<SystemProperties> { | ||
private Properties originalProperties; | ||
private Properties properties; | ||
public class SystemProperties extends SystemPropertiesImpl<SystemProperties> { | ||
|
||
/** | ||
* Default constructor with no properties. Use {@link #set} to set properties | ||
* either while active or before activation. | ||
* @since 1.0.0 | ||
*/ | ||
public SystemProperties() { | ||
this.properties = new Properties(); | ||
super(); | ||
} | ||
|
||
/** | ||
* Construct with a specific set of properties. | ||
* @param properties properties to use | ||
* @since 1.0.0 | ||
*/ | ||
public SystemProperties(Properties properties) { | ||
this.properties = PropertiesUtils.copyOf(properties); | ||
super(properties); | ||
} | ||
|
||
/** | ||
* Construct with a set of properties to apply when the object is active | ||
* @param name name of the first property | ||
* @param value value of the first property | ||
* @param nameValues pairs of names and values for further properties | ||
* @since 1.0.0 | ||
*/ | ||
public SystemProperties(String name, String value, String... nameValues) { | ||
this(); | ||
if (nameValues.length % 2 != 0) { | ||
throw new IllegalArgumentException("Must have pairs of values"); | ||
} | ||
properties.setProperty(name, value); | ||
for (int i = 0; i < nameValues.length; i += 2) { | ||
properties.setProperty(nameValues[i], nameValues[i + 1]); | ||
} | ||
} | ||
|
||
/** | ||
* Set a system property. If active, this will set it with {@link System#setProperty(String, String)}. | ||
* If not active, then this will store the property to apply when this object is part of an execution. | ||
* It is also possible to use {@link System#setProperty(String, String)} while this object is active, | ||
* but when the execution finishes, this object will be unaware of the property set, so will not set | ||
* it next time. | ||
* @param name name of the property | ||
* @param value value to set | ||
* @return this object for fluent use | ||
* @since 1.0.0 | ||
*/ | ||
@Override | ||
public SystemProperties set(String name, String value) { | ||
properties.setProperty(name, value); | ||
if (isActive()) { | ||
System.setProperty(name, value); | ||
} | ||
return this; | ||
} | ||
|
||
@Override | ||
protected void doSetup() throws Exception { | ||
originalProperties = getProperties(); | ||
Properties copyProperties = PropertiesUtils.copyOf(originalProperties); | ||
copyProperties.putAll(properties); | ||
setProperties(copyProperties); | ||
} | ||
|
||
@Override | ||
protected void doTeardown() throws Exception { | ||
setProperties(originalProperties); | ||
super(name, value, nameValues); | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
...ubs-core/src/main/java/uk/org/webcompere/systemstubs/properties/SystemPropertiesImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package uk.org.webcompere.systemstubs.properties; | ||
|
||
import uk.org.webcompere.systemstubs.resource.NameValuePairSetter; | ||
import uk.org.webcompere.systemstubs.resource.SingularTestResource; | ||
|
||
import java.util.HashSet; | ||
import java.util.Properties; | ||
import java.util.Set; | ||
|
||
import static java.lang.System.getProperties; | ||
import static java.lang.System.setProperties; | ||
|
||
/** | ||
* Maintain system properties after a test from the ones before the test. Stores the | ||
* existing properties when started, and restores them when complete. Allows for a list of properties | ||
* 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>> { | ||
private Properties originalProperties; | ||
private Properties properties; | ||
|
||
private Set<String> propertiesToRemove = new HashSet<>(); | ||
|
||
/** | ||
* Default constructor with no properties. Use {@link #set} to set properties | ||
* either while active or before activation. | ||
* @since 1.0.0 | ||
*/ | ||
public SystemPropertiesImpl() { | ||
this.properties = new Properties(); | ||
} | ||
|
||
/** | ||
* Construct with a specific set of properties. | ||
* @param properties properties to use | ||
* @since 1.0.0 | ||
*/ | ||
public SystemPropertiesImpl(Properties properties) { | ||
this.properties = PropertiesUtils.copyOf(properties); | ||
} | ||
|
||
/** | ||
* Construct with a set of properties to apply when the object is active | ||
* @param name name of the first property | ||
* @param value value of the first property | ||
* @param nameValues pairs of names and values for further properties | ||
* @since 1.0.0 | ||
*/ | ||
public SystemPropertiesImpl(String name, String value, String... nameValues) { | ||
this(); | ||
if (nameValues.length % 2 != 0) { | ||
throw new IllegalArgumentException("Must have pairs of values"); | ||
} | ||
properties.setProperty(name, value); | ||
for (int i = 0; i < nameValues.length; i += 2) { | ||
properties.setProperty(nameValues[i], nameValues[i + 1]); | ||
} | ||
} | ||
|
||
/** | ||
* Set a system property. If active, this will set it with {@link System#setProperty(String, String)}. | ||
* If not active, then this will store the property to apply when this object is part of an execution. | ||
* It is also possible to use {@link System#setProperty(String, String)} while this object is active, | ||
* but when the execution finishes, this object will be unaware of the property set, so will not set | ||
* it next time. | ||
* @param name name of the property | ||
* @param value value to set | ||
* @return this object for fluent use | ||
* @since 1.0.0 | ||
*/ | ||
@Override | ||
public SystemPropertiesImpl<T> set(String name, String value) { | ||
properties.setProperty(name, value); | ||
if (isActive()) { | ||
System.setProperty(name, value); | ||
} | ||
return this; | ||
} | ||
|
||
/** | ||
* Remove a property - this removes it from system properties if active, and remembers to remove it | ||
* while the object is active | ||
* @param name the name of the property to remove | ||
* @return <code>this</code> for fluent use | ||
* @since 2.1.5 | ||
*/ | ||
@Override | ||
public SystemPropertiesImpl<T> remove(String name) { | ||
propertiesToRemove.add(name); | ||
if (isActive()) { | ||
System.getProperties().remove(name); | ||
} | ||
return this; | ||
} | ||
|
||
@Override | ||
protected void doSetup() throws Exception { | ||
originalProperties = getProperties(); | ||
Properties copyProperties = PropertiesUtils.copyOf(originalProperties); | ||
propertiesToRemove.forEach(copyProperties::remove); | ||
copyProperties.putAll(properties); | ||
setProperties(copyProperties); | ||
} | ||
|
||
@Override | ||
protected void doTeardown() throws Exception { | ||
setProperties(originalProperties); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.