-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTestResourcesPropertySource.java
52 lines (44 loc) · 2.19 KB
/
TestResourcesPropertySource.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
package io.cloudflight.testresources.springboot.client;
import io.micronaut.testresources.client.TestResourcesClient;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.EnumerablePropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertySource;
import java.util.HashMap;
import java.util.Map;
import static java.util.Collections.emptyMap;
public class TestResourcesPropertySource extends PropertySource<TestResourcesClient> {
private static final String NAME = "testresources";
private static final String TEST_RESOURCES_PROPERTY_PREFIX = "test-resources.";
private final ConfigurableEnvironment environment;
private static Map<String, Object> testResourcesConfiguration;
public TestResourcesPropertySource(TestResourcesClient client, ConfigurableEnvironment environment) {
super(NAME, client);
this.environment = environment;
}
@Override
public Object getProperty(String name) {
if (source.getResolvableProperties().contains(name)) {
return source.resolve(name, emptyMap(), getTestResourcesConfiguration(environment)).orElse(null);
} else {
return null;
}
}
private synchronized static Map<String, Object> getTestResourcesConfiguration(Environment env) {
if (testResourcesConfiguration == null) {
testResourcesConfiguration = new HashMap<>();
if (env instanceof ConfigurableEnvironment) {
for (PropertySource<?> propertySource : ((ConfigurableEnvironment) env).getPropertySources()) {
if (propertySource instanceof EnumerablePropertySource) {
for (String key : ((EnumerablePropertySource<?>) propertySource).getPropertyNames()) {
if (key.startsWith(TEST_RESOURCES_PROPERTY_PREFIX)) {
testResourcesConfiguration.put(key.substring(TEST_RESOURCES_PROPERTY_PREFIX.length()), propertySource.getProperty(key));
}
}
}
}
}
}
return testResourcesConfiguration;
}
}