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