From 8b88c6e8844ce3811dd9ac8f7318efe0da867e43 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Wed, 6 Sep 2017 12:36:41 +0200 Subject: [PATCH] Polish --- .../actuate/endpoint/EnvironmentEndpoint.java | 11 +++++---- .../endpoint/EnvironmentEndpointTests.java | 24 ++++++++++++++++--- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/EnvironmentEndpoint.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/EnvironmentEndpoint.java index 02fa5035d2..2862b5d793 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/EnvironmentEndpoint.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/EnvironmentEndpoint.java @@ -73,17 +73,18 @@ public class EnvironmentEndpoint { @ReadOperation public EnvironmentDescriptor environment(String 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 - public Object getEnvironmentEntry(@Selector String toMatch) { - return environment((name) -> toMatch.equals(name)); + public EnvironmentDescriptor environmentEntry(@Selector String toMatch) { + return getEnvironmentDescriptor(toMatch::equals); } - private EnvironmentDescriptor environment(Predicate propertyNamePredicate) { + private EnvironmentDescriptor getEnvironmentDescriptor( + Predicate propertyNamePredicate) { PropertyResolver resolver = getResolver(); List propertySources = new ArrayList<>(); getPropertySourcesAsMap().forEach((sourceName, source) -> { diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/EnvironmentEndpointTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/EnvironmentEndpointTests.java index cc58c26a26..22b6caa0b2 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/EnvironmentEndpointTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/EnvironmentEndpointTests.java @@ -69,9 +69,9 @@ public class EnvironmentEndpointTests { StandardEnvironment environment = new StandardEnvironment(); CompositePropertySource source = new CompositePropertySource("composite"); source.addPropertySource(new MapPropertySource("one", - Collections.singletonMap("foo", (Object) "bar"))); + Collections.singletonMap("foo", "bar"))); source.addPropertySource(new MapPropertySource("two", - Collections.singletonMap("foo", (Object) "spam"))); + Collections.singletonMap("foo", "spam"))); environment.getPropertySources().addFirst(source); EnvironmentDescriptor env = new EnvironmentEndpoint(environment) .environment(null); @@ -230,6 +230,25 @@ public class EnvironmentEndpointTests { 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) { for (String property : properties) { System.clearProperty(property); @@ -254,5 +273,4 @@ public class EnvironmentEndpointTests { } - }