-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathJavaTests.java
101 lines (82 loc) · 2.8 KB
/
JavaTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package tests;
import io.github.cdimascio.dotenv.Dotenv;
import io.github.cdimascio.dotenv.DotenvEntry;
import io.github.cdimascio.dotenv.DotenvException;
import org.junit.jupiter.api.Test;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.*;
public class JavaTests {
private final Map<String, String> envVars = Map.of(
"MY_TEST_EV1", "my test ev 1",
"MY_TEST_EV2", "my test ev 2",
"WITHOUT_VALUE", ""
);
@Test
public void throwIfMalconfigured() {
assertThrows(DotenvException.class, () -> Dotenv.configure().load());
}
@Test
public void load() {
assertThrows(DotenvException.class, () -> {
Dotenv dotenv = Dotenv.load();
envVars.keySet().forEach(envName -> assertEquals(envVars.get(envName), dotenv.get(envName)));
});
}
@Test
public void iteratorOverDotenv() {
Dotenv dotenv = Dotenv.configure()
.ignoreIfMalformed()
.load();
dotenv.entries().forEach(e -> assertEquals(dotenv.get(e.getKey()), e.getValue()));
}
@Test
public void iteratorOverDotenvWithFilter() {
Dotenv dotenv = Dotenv.configure()
.ignoreIfMalformed()
.load();
Set<DotenvEntry> entriesInFile = dotenv.entries(Dotenv.Filter.DECLARED_IN_ENV_FILE);
Set<DotenvEntry> entriesAll = dotenv.entries();
assertTrue(entriesInFile.size() < entriesAll.size());
envVars.forEach((key, value) -> assertEquals(dotenv.get(key), value));
}
@Test
public void failToRemoveFromDotenv() {
Dotenv dotenv = Dotenv.configure()
.ignoreIfMalformed()
.load();
assertThrows(UnsupportedOperationException.class, () -> {
Iterator<DotenvEntry> iter = dotenv.entries().iterator();
while (iter.hasNext()) {
iter.next();
iter.remove();
}
});
}
@Test
public void failToAddToDotenv() {
Dotenv dotenv = Dotenv.configure()
.ignoreIfMalformed()
.load();
assertThrows(UnsupportedOperationException.class, () -> dotenv.entries().add(new DotenvEntry("new", "value")));
}
@Test
public void configureWithIgnoreMalformed() {
Dotenv dotenv = Dotenv.configure()
.ignoreIfMalformed()
.load();
for (String envName : envVars.keySet()) {
assertEquals(envVars.get(envName), dotenv.get(envName));
}
}
@Test
public void configureWithIgnoreMissingAndMalformed() {
Dotenv dotenv = Dotenv.configure()
.directory("/missing/dir")
.ignoreIfMalformed()
.ignoreIfMissing()
.load();
assertNotNull(dotenv.get("PATH"));
}
}