Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d7a61a5

Browse files
author
Samuel Nitsche
committed
Added new EnvironmentVariableUtil
this is to simplify testing with environment setting preconditions
1 parent acffab3 commit d7a61a5

2 files changed

Lines changed: 86 additions & 0 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.utplsql.api;
2+
3+
/**
4+
* This class provides an easy way to get environmental variables.
5+
* This is mainly to improve testability but also to standardize the way how utPLSQL API and CLI read from
6+
* environment.
7+
* <p>
8+
* Variables are obtained from the following scopes in that order (chain breaks as soon as a value is obtained):
9+
* <ul>
10+
* <li>Properties (System.getProperty())</li>
11+
* <li>Environment (System.getEnv())</li>
12+
* <li>Default value</li>
13+
* </ul>
14+
* <p>
15+
* An empty string is treated the same as null.
16+
*
17+
* @author pesse
18+
*/
19+
public class EnvironmentVariableUtil {
20+
21+
/**
22+
* Returns the value for a given key from environment (see class description)
23+
*
24+
* @param key Key of environment or property value
25+
* @return Environment value or null
26+
*/
27+
public static String getEnvValue(String key) {
28+
return getEnvValue(key, null);
29+
}
30+
31+
/**
32+
* Returns the value for a given key from environment or a default value (see class description)
33+
*
34+
* @param key Key of environment or property value
35+
* @param defaultValue Default value if nothing found
36+
* @return Environment value or defaultValue
37+
*/
38+
public static String getEnvValue(String key, String defaultValue) {
39+
40+
String val = System.getProperty(key);
41+
if (val == null || val.isEmpty()) val = System.getenv(key);
42+
if (val == null || val.isEmpty()) val = defaultValue;
43+
44+
return val;
45+
}
46+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.utplsql.api;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
import java.util.Map;
7+
import java.util.Optional;
8+
import java.util.Set;
9+
10+
11+
public class EnvironmentVariableUtilTest {
12+
13+
@Test
14+
public void testGetVariableFromEnvironment() {
15+
// Let's find an environment variable which is not in Properties list and not empty
16+
Set<Object> props = System.getProperties().keySet();
17+
Optional<Map.Entry<String, String>> envVariable = System.getenv().entrySet().stream()
18+
.filter((e) -> !props.contains(e.getKey()) && e.getValue() != null && !e.getValue().isEmpty())
19+
.findFirst();
20+
21+
if ( !envVariable.isPresent() )
22+
Assert.fail("Can't test for there is no environment variable not overridden by property");
23+
24+
Assert.assertEquals(envVariable.get().getValue(), EnvironmentVariableUtil.getEnvValue(envVariable.get().getKey()));
25+
}
26+
27+
@Test
28+
public void testGetVariableFromProperty() {
29+
System.setProperty("PATH", "MyPath");
30+
31+
Assert.assertEquals("MyPath", EnvironmentVariableUtil.getEnvValue("PATH"));
32+
}
33+
34+
@Test
35+
public void testGetVariableFromDefault() {
36+
37+
Assert.assertEquals("defaultValue", EnvironmentVariableUtil.getEnvValue("RANDOM"+String.valueOf(System.currentTimeMillis()), "defaultValue"));
38+
}
39+
40+
}

0 commit comments

Comments
 (0)