-
-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathBasicTests.java
More file actions
193 lines (158 loc) · 5.45 KB
/
BasicTests.java
File metadata and controls
193 lines (158 loc) · 5.45 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package tests;
import io.github.cdimascio.dotenv.Dotenv;
import io.github.cdimascio.dotenv.DotenvException;
import org.junit.jupiter.api.Test;
import java.util.HashMap;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.*;
class BasicTests {
private final Map<String, String> envVars = new HashMap<>() {{
put("MY_TEST_EV1", "my test ev 1");
put("MY_TEST_EV2", "my test ev 2");
put("WITHOUT_VALUE", "");
put("MULTI_LINE", "hello\nworld\n# not a comment\nmulti");
put("TWO_LINE", "hello\nworld");
put("TRAILING_COMMENT", "value");
put("QUOTED_VALUE", "iH4>hb_d0#_GN8d]6");
put("MY_TEST_EV4", "my test ev 4");
put("MULTI_LINE_WITH_SHARP", "hello\n#world");
put("UTF8_STRING", "äöüßé😀");
}};
@Test
void dotenvMalformed() {
assertThrows(DotenvException.class, () -> Dotenv.configure().directory("./src/test/resources").load());
}
@Test
void dotenvIgnoreMalformed() {
final var dotenv = Dotenv.configure()
.directory("./src/test/resources")
.ignoreIfMalformed()
.load();
envVars.forEach((key, expected) -> assertEquals(expected, dotenv.get(key)));
assertHostEnvVar(dotenv);
}
@Test
void dotenvDuplicateVariable() {
final var dotenv = Dotenv.configure()
.directory("./duplicateVariable")
.load();
assertEquals("Overridden Again Variable", dotenv.get("MY_TEST_EV1"));
assertEquals("Variable 2", dotenv.get("MY_TEST_EV2"));
}
@Test
void dotenvFilename() {
final var dotenv = Dotenv.configure()
.directory("./src/test/resources")
.filename("env")
.ignoreIfMalformed()
.load();
envVars.forEach((key, expected) -> assertEquals(expected, dotenv.get(key)));
assertHostEnvVar(dotenv);
}
@Test
void resourceRelative() {
final var dotenv = Dotenv.configure()
.directory("./")
.ignoreIfMalformed()
.load();
assertEquals("my test ev 1", dotenv.get("MY_TEST_EV1"));
assertHostEnvVar(dotenv);
}
@Test
void resourceAbsoluteDir() {
assertDirectory("/envdir","Simple Subdirectory");
}
@Test
void resourceRelativeDir() {
assertDirectory("./envdir", "Simple Subdirectory");
}
@Test
void resourceUnanchoredDir() {
assertDirectory("envdir", "Simple Subdirectory");
}
@Test
void resourceAbsoluteTrailingDotDir() {
assertDirectory("/trailingdot./envdir", "Trailing Dot Directory With Subdirectory");
}
@Test
void resourceRelativeTrailingDotDir() {
assertDirectory("./trailingdot./envdir", "Trailing Dot Directory With Subdirectory");
}
@Test
void resourceUnanchoredTrailingDotDir() {
assertDirectory("trailingdot./envdir", "Trailing Dot Directory With Subdirectory");
}
@Test
void resourceCurrent() {
final var dotenv = Dotenv.configure()
.ignoreIfMalformed()
.load();
assertEquals("my test ev 1", dotenv.get("MY_TEST_EV1"));
assertHostEnvVar(dotenv);
}
@Test
void systemProperties() {
final var dotenv = Dotenv.configure()
.ignoreIfMalformed()
.systemProperties()
.load();
assertHostEnvVar(dotenv);
assertEquals("my test ev 1", dotenv.get("MY_TEST_EV1"));
assertEquals("my test ev 1", System.getProperty("MY_TEST_EV1"));
dotenv.entries().forEach(entry -> System.clearProperty(entry.getKey()));
}
@Test
void noSystemProperties() {
final var dotenv = Dotenv.configure()
.ignoreIfMalformed()
.load();
assertHostEnvVar(dotenv);
assertEquals("my test ev 1", dotenv.get("MY_TEST_EV1"));
assertNull(System.getProperty("MY_TEST_EV1"));
}
@Test
void iterateOverDotenv() {
final var dotenv = Dotenv.configure()
.ignoreIfMalformed()
.load();
for (final var e : dotenv.entries()) {
assertEquals(dotenv.get(e.getKey()), e.getValue());
}
}
@Test
void iterateOverEnvVar() {
final var dotenv = Dotenv.configure()
.ignoreIfMalformed()
.load();
envVars.forEach((key, expected) -> assertEquals(expected, dotenv.get(key)));
}
@Test
void dotenvMissing() {
assertThrows(DotenvException.class, () -> Dotenv.configure().directory("/missing/.env").load());
}
@Test
void dotenvIgnoreMissing() {
final var dotenv = Dotenv.configure()
.directory("/missing/.env")
.ignoreIfMissing()
.load();
assertHostEnvVar(dotenv);
assertNull(dotenv.get("MY_TEST_EV1"));
}
private void assertDirectory(final String directory, final String expected) {
final var dotenv = Dotenv.configure()
.directory(directory)
.load();
assertEquals(expected, dotenv.get("MY_TEST_EV1"));
}
private void assertHostEnvVar(final Dotenv env) {
final var isWindows = System.getProperty("os.name").toLowerCase().contains("win");
if (isWindows)
assertNotNull(env.get("PATH"));
else {
final var expectedHome = System.getProperty("user.home");
final var actualHome = env.get("HOME");
assertEquals(expectedHome, actualHome);
}
}
}