forked from cdimascio/dotenv-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDotenvTests.java
117 lines (94 loc) · 3.15 KB
/
DotenvTests.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package tests;
import io.github.cdimascio.dotenv.DotenvException;
import io.github.cdimascio.dotenv.Dotenv;
import io.github.cdimascio.dotenv.DotenvEntry;
import org.junit.Before;
import org.junit.Test;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
public class DotenvTests {
private Map<String, String> envVars;
@Before
public void setUp() {
envVars = new HashMap<>();
envVars.put("MY_TEST_EV1", "my test ev 1");
envVars.put("MY_TEST_EV2", "my test ev 2");
envVars.put("WITHOUT_VALUE", "");
}
@Test(expected = DotenvException.class)
public void throwIfMalconfigured() {
Dotenv.configure().load();
}
@Test(expected = DotenvException.class)
public void load() {
Dotenv dotenv = Dotenv.load();
for (String envName : envVars.keySet()) {
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()));
for (DotenvEntry e : dotenv.entries()) {
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());
for (Map.Entry<String, String> e: envVars.entrySet()) {
assertEquals(dotenv.get(e.getKey()), e.getValue());
}
}
@Test(expected = UnsupportedOperationException.class)
public void failToRemoveFromDotenv() {
Dotenv dotenv = Dotenv.configure()
.ignoreIfMalformed()
.load();
Iterator<DotenvEntry> iter = dotenv.entries().iterator();
while (iter.hasNext()) {
iter.next();
iter.remove();
}
}
@Test(expected = UnsupportedOperationException.class)
public void failToAddToDotenv() {
Dotenv dotenv = Dotenv.configure()
.ignoreIfMalformed()
.load();
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"));
}
}