forked from jooby-project/jooby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnvTest.java
More file actions
341 lines (277 loc) · 9.09 KB
/
EnvTest.java
File metadata and controls
341 lines (277 loc) · 9.09 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package org.jooby;
import com.google.common.collect.ImmutableMap;
import com.google.inject.Key;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import com.typesafe.config.ConfigValueFactory;
import org.jooby.funzy.Throwing;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import org.junit.Test;
import javax.annotation.Nullable;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.function.Function;
public class EnvTest {
@Test
public void resolveOneAtMiddle() {
Config config = ConfigFactory.empty()
.withValue("contextPath", ConfigValueFactory.fromAnyRef("/myapp"));
Env env = Env.DEFAULT.build(config);
assertEquals("function ($) {$.ajax(\"/myapp/api\")}",
env.resolve("function ($) {$.ajax(\"${contextPath}/api\")}"));
}
@Test
public void resolveSingle() {
Config config = ConfigFactory.empty()
.withValue("var", ConfigValueFactory.fromAnyRef("foo.bar"));
Env env = Env.DEFAULT.build(config);
assertEquals("foo.bar", env.resolve("${var}"));
}
@Test
public void altplaceholder() {
Env env = Env.DEFAULT.build(ConfigFactory.empty()
.withValue("var", ConfigValueFactory.fromAnyRef("foo.bar")));
assertEquals("foo.bar", env.resolver().delimiters("{{", "}}").resolve("{{var}}"));
assertEquals("foo.bar", env.resolver().delimiters("<%", "%>").resolve("<%var%>"));
}
@Test
public void resolveHead() {
Config config = ConfigFactory.empty()
.withValue("var", ConfigValueFactory.fromAnyRef("foo.bar"));
Env env = Env.DEFAULT.build(config);
assertEquals("foo.bar-", env.resolve("${var}-"));
}
@Test
public void resolveTail() {
Config config = ConfigFactory.empty()
.withValue("var", ConfigValueFactory.fromAnyRef("foo.bar"));
Env env = Env.DEFAULT.build(config);
assertEquals("-foo.bar", env.resolve("-${var}"));
}
@Test
public void resolveMore() {
Config config = ConfigFactory.empty()
.withValue("var", ConfigValueFactory.fromAnyRef("foo.bar"));
Env env = Env.DEFAULT.build(config);
assertEquals("foo.bar - foo.bar", env.resolve("${var} - ${var}"));
}
@Test
public void resolveMap() {
Config config = ConfigFactory.empty();
Env env = Env.DEFAULT.build(config);
assertEquals("foo.bar - foo.bar", env.resolver().source(ImmutableMap.of("var", "foo.bar"))
.resolve("${var} - ${var}"));
}
@Test
public void resolveMapIgnore() {
Config config = ConfigFactory.empty();
Env env = Env.DEFAULT.build(config);
assertEquals("${varx} - ${varx}",
env.resolver().ignoreMissing().source(ImmutableMap.of("var", "foo.bar"))
.resolve("${varx} - ${varx}"));
}
@Test
public void resolveIgnoreMissing() {
Config config = ConfigFactory.empty();
Env env = Env.DEFAULT.build(config);
assertEquals("${var} - ${var}", env.resolver().ignoreMissing().resolve("${var} - ${var}"));
assertEquals(" - ${foo.var} -", env.resolver().ignoreMissing().resolve(" - ${foo.var} -"));
}
@Test
public void novars() {
Config config = ConfigFactory.empty()
.withValue("var", ConfigValueFactory.fromAnyRef("foo.bar"));
Env env = Env.DEFAULT.build(config);
assertEquals("var", env.resolve("var"));
}
@Test
public void globalObject() {
Config config = ConfigFactory.empty()
.withValue("var", ConfigValueFactory.fromAnyRef("foo.bar"));
Env env = Env.DEFAULT.build(config);
Object value = new Object();
env.set(Object.class, value);
assertEquals(value, env.get(Object.class).get());
}
@Test
public void serviceKey() {
Config config = ConfigFactory.empty()
.withValue("var", ConfigValueFactory.fromAnyRef("foo.bar"));
Env env = Env.DEFAULT.build(config);
assertNotNull(env.serviceKey());
assertNotNull(new Env() {
@Override public <T> Env set(Key<T> key, T value) {
throw new UnsupportedOperationException();
}
@Override public <T> Optional<T> get(Key<T> key) {
throw new UnsupportedOperationException();
}
@Nullable @Override public <T> T unset(Key<T> key) {
throw new UnsupportedOperationException();
}
@Override
public LifeCycle onStart(final Throwing.Consumer<Registry> task) {
return null;
}
@Override
public LifeCycle onStarted(final Throwing.Consumer<Registry> task) {
return null;
}
@Override
public LifeCycle onStop(final Throwing.Consumer<Registry> task) {
return null;
}
@Override
public Map<String, Function<String, String>> xss() {
return null;
}
@Override
public Env xss(final String name, final Function<String, String> escaper) {
return null;
}
@Override
public String name() {
return null;
}
@Override
public Router router() throws UnsupportedOperationException {
return null;
}
@Override
public Config config() {
return null;
}
@Override
public Locale locale() {
return null;
}
@Override
public List<Throwing.Consumer<Registry>> startTasks() {
return null;
}
@Override
public List<Throwing.Consumer<Registry>> startedTasks() {
return null;
}
@Override
public List<Throwing.Consumer<Registry>> stopTasks() {
return null;
}
}.serviceKey());
}
@Test(expected = NullPointerException.class)
public void nullText() {
Env env = Env.DEFAULT.build(ConfigFactory.empty());
env.resolve(null);
}
@Test
public void unclosedDelimiterWithSpace() {
Env env = Env.DEFAULT.build(ConfigFactory.empty());
try {
env.resolve(env.resolve("function ($) {$.ajax(\"${contextPath /api\")"));
fail();
} catch (IllegalArgumentException ex) {
assertEquals("found '${' expecting '}' at 1:23", ex.getMessage());
}
}
@Test
public void unclosedDelimiter() {
Env env = Env.DEFAULT.build(ConfigFactory.empty());
try {
env.resolve(env.resolve("function ($) {$.ajax(\"${contextPath/api\")"));
fail();
} catch (IllegalArgumentException ex) {
assertEquals("found '${' expecting '}' at 1:23", ex.getMessage());
}
}
@Test
public void noSuchKey() {
Env env = Env.DEFAULT.build(ConfigFactory.empty());
try {
env.resolve(env.resolve("${key}"));
fail();
} catch (NoSuchElementException ex) {
assertEquals("Missing ${key} at 1:1", ex.getMessage());
}
try {
env.resolve(env.resolve(" ${key}"));
fail();
} catch (NoSuchElementException ex) {
assertEquals("Missing ${key} at 1:5", ex.getMessage());
}
try {
env.resolve(env.resolve(" \n ${key}"));
fail();
} catch (NoSuchElementException ex) {
assertEquals("Missing ${key} at 2:3", ex.getMessage());
}
try {
env.resolve(env.resolve(" \n ${key}"));
fail();
} catch (NoSuchElementException ex) {
assertEquals("Missing ${key} at 2:3", ex.getMessage());
}
try {
env.resolve(env.resolve(" \n \n ${key}"));
fail();
} catch (NoSuchElementException ex) {
assertEquals("Missing ${key} at 3:2", ex.getMessage());
}
}
@Test
public void resolveEmpty() {
Env env = Env.DEFAULT.build(ConfigFactory.empty());
assertEquals("", env.resolve(""));
}
@Test
public void ifMode() throws Throwable {
assertEquals("$dev",
Env.DEFAULT.build(ConfigFactory.empty()).ifMode("dev", () -> "$dev").get());
assertEquals(Optional.empty(),
Env.DEFAULT.build(ConfigFactory.empty()).ifMode("prod", () -> "$dev"));
assertEquals(
"$prod",
Env.DEFAULT
.build(
ConfigFactory.empty().withValue("application.env",
ConfigValueFactory.fromAnyRef("prod")))
.ifMode("prod", () -> "$prod").get());
assertEquals(Optional.empty(),
Env.DEFAULT
.build(
ConfigFactory.empty().withValue("application.env",
ConfigValueFactory.fromAnyRef("prod")))
.ifMode("dev", () -> "$prod"));
}
@Test(expected = UnsupportedOperationException.class)
public void noRouter() {
Env.DEFAULT.build(ConfigFactory.empty()).router();
}
@Test
public void name() throws Exception {
assertEquals("dev", Env.DEFAULT.build(ConfigFactory.empty()).toString());
assertEquals("prod", Env.DEFAULT.build(ConfigFactory.empty().withValue("application.env",
ConfigValueFactory.fromAnyRef("prod"))).toString());
}
@Test
public void onStart() throws Exception {
Env env = Env.DEFAULT.build(ConfigFactory.empty());
Throwing.Runnable task = () -> {
};
env.onStart(task);
assertEquals(1, env.startTasks().size());
}
@Test
public void onStop() throws Exception {
Env env = Env.DEFAULT.build(ConfigFactory.empty());
Throwing.Runnable task = () -> {
};
env.onStop(task);
assertEquals(1, env.stopTasks().size());
}
}