1 package com.atlassian.sal.core.features;
2
3 import org.junit.rules.TestWatcher;
4 import org.junit.runner.Description;
5
6 import java.util.HashSet;
7 import java.util.Set;
8
9 /**
10 * All system properties set through this rule are guaranteed to be cleaned up after the test method has finished.
11 */
12 public class ClearSystemPropertyRule extends TestWatcher {
13 private final Set<String> properties = new HashSet<String>();
14
15 /**
16 * Set a system property key to the given value. The system property will be cleared when the test finishes.
17 *
18 * @param key the system property to be set
19 * @param value the value to be associated with the system property
20 */
21 public void setProperty(final String key, final String value) {
22 properties.add(key);
23 System.setProperty(key, value);
24 }
25
26 /**
27 * Set a system property key to the given value. Will always put the {@link DefaultDarkFeatureManager#ATLASSIAN_DARKFEATURE_PREFIX}
28 * in for you.
29 *
30 * @param key the system property to be set. Will be effectively: {@link DefaultDarkFeatureManager#ATLASSIAN_DARKFEATURE_PREFIX} + key
31 * @param value the value to be set
32 */
33 public void setPropertyWithDarkFeaturePrefix(final String key, final String value) {
34 setProperty(DefaultDarkFeatureManager.ATLASSIAN_DARKFEATURE_PREFIX + key, value);
35 }
36
37 @Override
38 protected void finished(final Description description) {
39 for (final String property : properties) {
40 System.clearProperty(property);
41 }
42 }
43 }