• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  *  http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 package software.amazon.awssdk.testutils;
17 
18 import java.util.function.Consumer;
19 import org.junit.rules.ExternalResource;
20 import software.amazon.awssdk.utils.SystemSetting;
21 import software.amazon.awssdk.utils.internal.SystemSettingUtilsTestBackdoor;
22 
23 /**
24  * A utility that can temporarily forcibly set environment variables and then allows resetting them to the original values.
25  *
26  * This only works for environment variables read by the SDK.
27  */
28 public class EnvironmentVariableHelper extends ExternalResource {
remove(SystemSetting setting)29     public void remove(SystemSetting setting) {
30         remove(setting.environmentVariable());
31     }
32 
remove(String key)33     public void remove(String key) {
34         SystemSettingUtilsTestBackdoor.addEnvironmentVariableOverride(key, null);
35     }
36 
set(SystemSetting setting, String value)37     public void set(SystemSetting setting, String value) {
38         set(setting.environmentVariable(), value);
39     }
40 
set(String key, String value)41     public void set(String key, String value) {
42         SystemSettingUtilsTestBackdoor.addEnvironmentVariableOverride(key, value);
43     }
44 
reset()45     public void reset() {
46         SystemSettingUtilsTestBackdoor.clearEnvironmentVariableOverrides();
47     }
48 
49     @Override
after()50     protected void after() {
51         reset();
52     }
53 
54     /**
55      * Static run method that allows for "single-use" environment variable modification.
56      *
57      * Example use:
58      * <pre>
59      * {@code
60      * EnvironmentVariableHelper.run(helper -> {
61      *    helper.set("variable", "value");
62      *    //run some test that uses "variable"
63      * });
64      * }
65      * </pre>
66      *
67      * Will call {@link #reset} at the end of the block (even if the block exits exceptionally).
68      *
69      * @param helperConsumer a code block to run that gets an {@link EnvironmentVariableHelper} as an argument
70      */
run(Consumer<EnvironmentVariableHelper> helperConsumer)71     public static void run(Consumer<EnvironmentVariableHelper> helperConsumer) {
72         EnvironmentVariableHelper helper = new EnvironmentVariableHelper();
73         try {
74             helperConsumer.accept(helper);
75         } finally {
76             helper.reset();
77         }
78     }
79 }
80