Commit 8b88c6e8 authored by Stephane Nicoll's avatar Stephane Nicoll

Polish

parent e244d75b
...@@ -73,17 +73,18 @@ public class EnvironmentEndpoint { ...@@ -73,17 +73,18 @@ public class EnvironmentEndpoint {
@ReadOperation @ReadOperation
public EnvironmentDescriptor environment(String pattern) { public EnvironmentDescriptor environment(String pattern) {
if (StringUtils.hasText(pattern)) { if (StringUtils.hasText(pattern)) {
return environment(Pattern.compile(pattern).asPredicate()); return getEnvironmentDescriptor(Pattern.compile(pattern).asPredicate());
} }
return environment((name) -> true); return getEnvironmentDescriptor((name) -> true);
} }
@ReadOperation @ReadOperation
public Object getEnvironmentEntry(@Selector String toMatch) { public EnvironmentDescriptor environmentEntry(@Selector String toMatch) {
return environment((name) -> toMatch.equals(name)); return getEnvironmentDescriptor(toMatch::equals);
} }
private EnvironmentDescriptor environment(Predicate<String> propertyNamePredicate) { private EnvironmentDescriptor getEnvironmentDescriptor(
Predicate<String> propertyNamePredicate) {
PropertyResolver resolver = getResolver(); PropertyResolver resolver = getResolver();
List<PropertySourceDescriptor> propertySources = new ArrayList<>(); List<PropertySourceDescriptor> propertySources = new ArrayList<>();
getPropertySourcesAsMap().forEach((sourceName, source) -> { getPropertySourcesAsMap().forEach((sourceName, source) -> {
......
...@@ -69,9 +69,9 @@ public class EnvironmentEndpointTests { ...@@ -69,9 +69,9 @@ public class EnvironmentEndpointTests {
StandardEnvironment environment = new StandardEnvironment(); StandardEnvironment environment = new StandardEnvironment();
CompositePropertySource source = new CompositePropertySource("composite"); CompositePropertySource source = new CompositePropertySource("composite");
source.addPropertySource(new MapPropertySource("one", source.addPropertySource(new MapPropertySource("one",
Collections.singletonMap("foo", (Object) "bar"))); Collections.singletonMap("foo", "bar")));
source.addPropertySource(new MapPropertySource("two", source.addPropertySource(new MapPropertySource("two",
Collections.singletonMap("foo", (Object) "spam"))); Collections.singletonMap("foo", "spam")));
environment.getPropertySources().addFirst(source); environment.getPropertySources().addFirst(source);
EnvironmentDescriptor env = new EnvironmentEndpoint(environment) EnvironmentDescriptor env = new EnvironmentEndpoint(environment)
.environment(null); .environment(null);
...@@ -230,6 +230,25 @@ public class EnvironmentEndpointTests { ...@@ -230,6 +230,25 @@ public class EnvironmentEndpointTests {
assertThat(foo.get("bar")).isEqualTo("baz"); assertThat(foo.get("bar")).isEqualTo("baz");
} }
@Test
public void propertyEntry() {
StandardEnvironment environment = new StandardEnvironment();
TestPropertyValues.of("my.foo=bar", "my.foo2=bar2")
.applyTo(environment);
EnvironmentDescriptor env = new EnvironmentEndpoint(environment)
.environmentEntry("my.foo");
assertThat(env).isNotNull();
assertThat(getSource("test", env).getProperties().get("my.foo").getValue())
.isEqualTo("bar");
}
@Test
public void propertyEntryNoMatchReturnNull() {
EnvironmentDescriptor env = new EnvironmentEndpoint(new StandardEnvironment())
.environmentEntry("this.property.does-not-exist");
assertThat(env).isNull();
}
private void clearSystemProperties(String... properties) { private void clearSystemProperties(String... properties) {
for (String property : properties) { for (String property : properties) {
System.clearProperty(property); System.clearProperty(property);
...@@ -254,5 +273,4 @@ public class EnvironmentEndpointTests { ...@@ -254,5 +273,4 @@ public class EnvironmentEndpointTests {
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment