Skip to content

Commit 67f580f

Browse files
committed
Revert "Removed unnecessary util"
This reverts commit 9ece9af.
1 parent 9ece9af commit 67f580f

File tree

3 files changed

+106
-4
lines changed

3 files changed

+106
-4
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.utplsql.api;
2+
3+
import javax.annotation.Nullable;
4+
5+
/**
6+
* This class provides an easy way to get environmental variables.
7+
* This is mainly to improve testability but also to standardize the way how utPLSQL API and CLI read from
8+
* environment.
9+
* <p>
10+
* Variables are obtained from the following scopes in that order (chain breaks as soon as a value is obtained):
11+
* <ul>
12+
* <li>Properties (System.getProperty())</li>
13+
* <li>Environment (System.getEnv())</li>
14+
* <li>Default value</li>
15+
* </ul>
16+
* <p>
17+
* An empty string is treated the same as null.
18+
*
19+
* @author pesse
20+
*/
21+
public class EnvironmentVariableUtil {
22+
23+
private EnvironmentVariableUtil() {
24+
}
25+
26+
/**
27+
* Returns the value for a given key from environment (see class description)
28+
*
29+
* @param key Key of environment or property value
30+
* @return Environment value or null
31+
*/
32+
public static String getEnvValue(String key) {
33+
return getEnvValue(key, null);
34+
}
35+
36+
/**
37+
* Returns the value for a given key from environment or a default value (see class description)
38+
*
39+
* @param key Key of environment or property value
40+
* @param defaultValue Default value if nothing found
41+
* @return Environment value or defaultValue
42+
*/
43+
public static String getEnvValue(String key, @Nullable String defaultValue) {
44+
45+
String val = System.getProperty(key);
46+
if (val == null || val.isEmpty()) val = System.getenv(key);
47+
if (val == null || val.isEmpty()) val = defaultValue;
48+
49+
return val;
50+
}
51+
52+
53+
}

src/test/java/org/utplsql/api/AbstractDatabaseTest.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,18 @@
1111

1212
public abstract class AbstractDatabaseTest {
1313

14-
private static final String sUrl = "localhost:1521:XE";
15-
private static final String sUser = "APP";
16-
private static final String sPass = "pass";
14+
private static String sUrl;
15+
private static String sUser;
16+
private static String sPass;
17+
18+
static {
19+
sUrl = EnvironmentVariableUtil.getEnvValue("DB_URL", "localhost:1521:XE");
20+
sUser = EnvironmentVariableUtil.getEnvValue("DB_USER", "app");
21+
sPass = EnvironmentVariableUtil.getEnvValue("DB_PASS", "pass");
22+
}
1723

1824
private Connection conn;
19-
private final List<Connection> connectionList = new ArrayList<>();
25+
private List<Connection> connectionList = new ArrayList<>();
2026

2127
public static String getUser() {
2228
return sUser;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.utplsql.api;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.Map;
6+
import java.util.Optional;
7+
import java.util.Set;
8+
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
10+
import static org.junit.jupiter.api.Assertions.fail;
11+
12+
13+
class EnvironmentVariableUtilTest {
14+
15+
@Test
16+
void testGetVariableFromEnvironment() {
17+
// Let's find an environment variable which is not in Properties list and not empty
18+
Set<Object> props = System.getProperties().keySet();
19+
Optional<Map.Entry<String, String>> envVariable = System.getenv().entrySet().stream()
20+
.filter((e) -> !props.contains(e.getKey()) && e.getValue() != null && !e.getValue().isEmpty())
21+
.findFirst();
22+
23+
if (!envVariable.isPresent()) {
24+
fail("Can't test for there is no environment variable not overridden by property");
25+
}
26+
27+
assertEquals(envVariable.get().getValue(), EnvironmentVariableUtil.getEnvValue(envVariable.get().getKey()));
28+
}
29+
30+
@Test
31+
void testGetVariableFromProperty() {
32+
System.setProperty("PATH", "MyPath");
33+
34+
assertEquals("MyPath", EnvironmentVariableUtil.getEnvValue("PATH"));
35+
}
36+
37+
@Test
38+
void testGetVariableFromDefault() {
39+
40+
assertEquals("defaultValue", EnvironmentVariableUtil.getEnvValue("RANDOM" + System.currentTimeMillis(), "defaultValue"));
41+
}
42+
43+
}

0 commit comments

Comments
 (0)