View Javadoc

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  {
14      private final Set<String> properties = new HashSet<String>();
15  
16      /**
17       * Set a system property key to the given value. The system property will be cleared when the test finishes.
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      {
23          properties.add(key);
24          System.setProperty(key, value);
25      }
26  
27      /**
28       * Set a system property key to the given value. Will always put the {@link DefaultDarkFeatureManager#ATLASSIAN_DARKFEATURE_PREFIX}
29       * in for you.
30       *
31       * @param key the system property to be set. Will be effectively: {@link DefaultDarkFeatureManager#ATLASSIAN_DARKFEATURE_PREFIX} + key
32       * @param value the value to be set
33       */
34      public void setPropertyWithDarkFeaturePrefix(final String key, final String value)
35      {
36          setProperty(DefaultDarkFeatureManager.ATLASSIAN_DARKFEATURE_PREFIX + key, value);
37      }
38  
39      @Override
40      protected void finished(final Description description)
41      {
42          for (final String property : properties)
43          {
44              System.clearProperty(property);
45          }
46      }
47  }