Polish
This commit is contained in:
@@ -35,7 +35,7 @@ import org.springframework.context.annotation.Conditional;
|
||||
* @since 1.4.0
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.TYPE, ElementType.METHOD})
|
||||
@Target({ ElementType.TYPE, ElementType.METHOD })
|
||||
@Documented
|
||||
@Conditional(OnEnabledInfoContributorCondition.class)
|
||||
public @interface ConditionalOnEnabledInfoContributor {
|
||||
|
||||
@@ -78,7 +78,7 @@ import org.springframework.web.servlet.handler.AbstractHandlerMethodMapping;
|
||||
*
|
||||
*/
|
||||
@Configuration
|
||||
@AutoConfigureAfter({FlywayAutoConfiguration.class, LiquibaseAutoConfiguration.class})
|
||||
@AutoConfigureAfter({ FlywayAutoConfiguration.class, LiquibaseAutoConfiguration.class })
|
||||
@EnableConfigurationProperties(EndpointProperties.class)
|
||||
public class EndpointAutoConfiguration {
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2015 the original author or authors.
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -34,7 +34,8 @@ import org.springframework.core.annotation.Order;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
|
||||
/**
|
||||
* {@link EnableAutoConfiguration Auto-configuration} for standard {@link InfoContributor}s.
|
||||
* {@link EnableAutoConfiguration Auto-configuration} for standard
|
||||
* {@link InfoContributor}s.
|
||||
*
|
||||
* @author Meang Akira Tanaka
|
||||
* @author Stephane Nicoll
|
||||
@@ -53,7 +54,8 @@ public class InfoContributorAutoConfiguration {
|
||||
@Bean
|
||||
@ConditionalOnEnabledInfoContributor("env")
|
||||
@Order(DEFAULT_ORDER)
|
||||
public EnvironmentInfoContributor envInfoContributor(ConfigurableEnvironment environment) {
|
||||
public EnvironmentInfoContributor envInfoContributor(
|
||||
ConfigurableEnvironment environment) {
|
||||
return new EnvironmentInfoContributor(environment);
|
||||
}
|
||||
|
||||
|
||||
@@ -24,8 +24,8 @@ import org.springframework.core.annotation.AnnotationAttributes;
|
||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||
|
||||
/**
|
||||
* Base endpoint element condition. An element can be disabled globally via the
|
||||
* `defaults` name or individually via the name of the element.
|
||||
* Base endpoint element condition. An element can be disabled globally via the `defaults`
|
||||
* name or individually via the name of the element.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
@@ -78,7 +78,8 @@ abstract class OnEnabledEndpointElementCondition extends SpringBootCondition {
|
||||
RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(
|
||||
context.getEnvironment(), this.prefix + "defaults.");
|
||||
boolean match = Boolean.valueOf(resolver.getProperty("enabled", "true"));
|
||||
return new ConditionOutcome(match, getDefaultEndpointElementOutcomeMessage(match));
|
||||
return new ConditionOutcome(match,
|
||||
getDefaultEndpointElementOutcomeMessage(match));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -39,4 +39,5 @@ class OnEnabledInfoContributorCondition extends OnEnabledEndpointElementConditio
|
||||
return "All default info contributors are " + (match ? "enabled" : "disabled")
|
||||
+ " by default";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -47,7 +47,6 @@ public class InfoEndpoint extends AbstractEndpoint<Info> {
|
||||
this.infoContributors = infoContributors;
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
public Info invoke() {
|
||||
Info.Builder builder = new Info.Builder();
|
||||
|
||||
@@ -42,11 +42,11 @@ public abstract class AbstractEnvironmentInfoContributor implements InfoContribu
|
||||
return this.environment;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Extract the keys from the environment using the specified {@code prefix}. The
|
||||
* prefix won't be included.
|
||||
* <p>Any key that starts with the {@code prefix} will be included
|
||||
* <p>
|
||||
* Any key that starts with the {@code prefix} will be included
|
||||
* @param prefix the prefix to use
|
||||
* @return the keys from the environment matching the prefix
|
||||
*/
|
||||
@@ -58,13 +58,14 @@ public abstract class AbstractEnvironmentInfoContributor implements InfoContribu
|
||||
|
||||
/**
|
||||
* Bind the specified {@code target} from the environment using the {@code prefix}.
|
||||
* <p>Any key that starts with the {@code prefix} will be bound to the {@code target}.
|
||||
* <p>
|
||||
* Any key that starts with the {@code prefix} will be bound to the {@code target}.
|
||||
* @param prefix the prefix to use
|
||||
* @param target the object to bind to
|
||||
*/
|
||||
protected void bindEnvironmentTo(String prefix, Object target) {
|
||||
PropertiesConfigurationFactory<Object> factory =
|
||||
new PropertiesConfigurationFactory<Object>(target);
|
||||
PropertiesConfigurationFactory<Object> factory = new PropertiesConfigurationFactory<Object>(
|
||||
target);
|
||||
factory.setTargetName(prefix);
|
||||
factory.setPropertySources(this.environment.getPropertySources());
|
||||
try {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2015 the original author or authors.
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -21,8 +21,7 @@ import java.util.Map;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
|
||||
/**
|
||||
* A {@link InfoContributor} that provides all environment entries prefixed with
|
||||
* info.
|
||||
* A {@link InfoContributor} that provides all environment entries prefixed with info.
|
||||
*
|
||||
* @author Meang Akira Tanaka
|
||||
* @author Stephane Nicoll
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2015 the original author or authors.
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -60,7 +60,8 @@ public final class Info {
|
||||
public <T> T get(String id, Class<T> type) {
|
||||
Object value = get(id);
|
||||
if (value != null && type != null && !type.isInstance(value)) {
|
||||
throw new IllegalStateException("Info entry is not of required type [" + type.getName() + "]: " + value);
|
||||
throw new IllegalStateException("Info entry is not of required type ["
|
||||
+ type.getName() + "]: " + value);
|
||||
}
|
||||
return (T) value;
|
||||
}
|
||||
@@ -129,4 +130,5 @@ public final class Info {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -121,8 +121,8 @@ public class CacheStatisticsAutoConfigurationTests {
|
||||
@Test
|
||||
public void baseCaffeineCacheStatistics() {
|
||||
load(CaffeineCacheConfig.class);
|
||||
CacheStatisticsProvider provider = this.context
|
||||
.getBean("caffeineCacheStatisticsProvider", CacheStatisticsProvider.class);
|
||||
CacheStatisticsProvider provider = this.context.getBean(
|
||||
"caffeineCacheStatisticsProvider", CacheStatisticsProvider.class);
|
||||
doTestCoreStatistics(provider, true);
|
||||
}
|
||||
|
||||
|
||||
@@ -168,7 +168,8 @@ public class EndpointAutoConfigurationTests {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
EnvironmentTestUtils.addEnvironment(this.context,
|
||||
"spring.info.git.location:classpath:nonexistent");
|
||||
this.context.register(InfoContributorAutoConfiguration.class, EndpointAutoConfiguration.class);
|
||||
this.context.register(InfoContributorAutoConfiguration.class,
|
||||
EndpointAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
InfoEndpoint endpoint = this.context.getBean(InfoEndpoint.class);
|
||||
assertThat(endpoint).isNotNull();
|
||||
@@ -179,7 +180,8 @@ public class EndpointAutoConfigurationTests {
|
||||
public void testInfoEndpointOrdering() throws Exception {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
EnvironmentTestUtils.addEnvironment(this.context, "info.name:foo");
|
||||
this.context.register(CustomInfoContributorsConfig.class, ProjectInfoAutoConfiguration.class,
|
||||
this.context.register(CustomInfoContributorsConfig.class,
|
||||
ProjectInfoAutoConfiguration.class,
|
||||
InfoContributorAutoConfiguration.class, EndpointAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
|
||||
@@ -265,8 +267,8 @@ public class EndpointAutoConfigurationTests {
|
||||
GitFullInfoContributor(Resource location) throws BindException, IOException {
|
||||
this.content = new LinkedHashMap<String, Object>();
|
||||
if (location.exists()) {
|
||||
PropertiesConfigurationFactory<Map<String, Object>> factory
|
||||
= new PropertiesConfigurationFactory<Map<String, Object>>(this.content);
|
||||
PropertiesConfigurationFactory<Map<String, Object>> factory = new PropertiesConfigurationFactory<Map<String, Object>>(
|
||||
this.content);
|
||||
factory.setTargetName("git");
|
||||
Properties gitInfoProperties = PropertiesLoaderUtils
|
||||
.loadProperties(location);
|
||||
@@ -281,7 +283,9 @@ public class EndpointAutoConfigurationTests {
|
||||
builder.withDetail("git", this.content);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -107,6 +107,7 @@ public class InfoContributorAutoConfigurationTests {
|
||||
gitInfo.getCommit().setId("abcdefg");
|
||||
return gitInfo;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@@ -120,6 +121,7 @@ public class InfoContributorAutoConfigurationTests {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -57,10 +57,11 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = {TestConfiguration.class})
|
||||
@SpringApplicationConfiguration(classes = { TestConfiguration.class })
|
||||
@WebAppConfiguration
|
||||
@TestPropertySource(properties = {"info.app.name=MyService"})
|
||||
@TestPropertySource(properties = { "info.app.name=MyService" })
|
||||
public class InfoMvcEndpointTests {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext context;
|
||||
|
||||
@@ -68,7 +69,6 @@ public class InfoMvcEndpointTests {
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
this.context.getBean(InfoEndpoint.class).setEnabled(true);
|
||||
this.mvc = MockMvcBuilders.webAppContextSetup(this.context).build();
|
||||
}
|
||||
@@ -76,18 +76,16 @@ public class InfoMvcEndpointTests {
|
||||
@Test
|
||||
public void home() throws Exception {
|
||||
this.mvc.perform(get("/info")).andExpect(status().isOk())
|
||||
.andExpect(content().string(
|
||||
containsString("\"beanName1\":{\"key11\":\"value11\",\"key12\":\"value12\"}")
|
||||
))
|
||||
.andExpect(content().string(
|
||||
containsString("\"beanName2\":{\"key21\":\"value21\",\"key22\":\"value22\"}")));
|
||||
.andExpect(content().string(containsString(
|
||||
"\"beanName1\":{\"key11\":\"value11\",\"key12\":\"value12\"}")))
|
||||
.andExpect(content().string(containsString(
|
||||
"\"beanName2\":{\"key21\":\"value21\",\"key22\":\"value22\"}")));
|
||||
}
|
||||
|
||||
@Import({JacksonAutoConfiguration.class,
|
||||
@Import({ JacksonAutoConfiguration.class,
|
||||
HttpMessageConvertersAutoConfiguration.class,
|
||||
EndpointWebMvcAutoConfiguration.class,
|
||||
WebMvcAutoConfiguration.class,
|
||||
ManagementServerPropertiesAutoConfiguration.class})
|
||||
EndpointWebMvcAutoConfiguration.class, WebMvcAutoConfiguration.class,
|
||||
ManagementServerPropertiesAutoConfiguration.class })
|
||||
@Configuration
|
||||
public static class TestConfiguration {
|
||||
|
||||
@@ -122,6 +120,7 @@ public class InfoMvcEndpointTests {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -50,9 +50,10 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
||||
* @author Meang Akira Tanaka
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = {TestConfiguration.class})
|
||||
@SpringApplicationConfiguration(classes = { TestConfiguration.class })
|
||||
@WebAppConfiguration
|
||||
public class InfoMvcEndpointWithoutAnyInfoProvidersTests {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext context;
|
||||
|
||||
@@ -70,11 +71,10 @@ public class InfoMvcEndpointWithoutAnyInfoProvidersTests {
|
||||
this.mvc.perform(get("/info")).andExpect(status().isOk());
|
||||
}
|
||||
|
||||
@Import({JacksonAutoConfiguration.class,
|
||||
@Import({ JacksonAutoConfiguration.class,
|
||||
HttpMessageConvertersAutoConfiguration.class,
|
||||
EndpointWebMvcAutoConfiguration.class,
|
||||
WebMvcAutoConfiguration.class,
|
||||
ManagementServerPropertiesAutoConfiguration.class})
|
||||
EndpointWebMvcAutoConfiguration.class, WebMvcAutoConfiguration.class,
|
||||
ManagementServerPropertiesAutoConfiguration.class })
|
||||
@Configuration
|
||||
public static class TestConfiguration {
|
||||
|
||||
|
||||
@@ -33,9 +33,8 @@ public class EnvironmentInfoContributorTests {
|
||||
|
||||
@Test
|
||||
public void extractOnlyInfoProperty() {
|
||||
EnvironmentTestUtils.addEnvironment(this.environment,
|
||||
"info.app=my app", "info.version=1.0.0", "foo=bar");
|
||||
|
||||
EnvironmentTestUtils.addEnvironment(this.environment, "info.app=my app",
|
||||
"info.version=1.0.0", "foo=bar");
|
||||
Info actual = contributeFrom(this.environment);
|
||||
assertThat(actual.get("app", String.class)).isEqualTo("my app");
|
||||
assertThat(actual.get("version", String.class)).isEqualTo("1.0.0");
|
||||
@@ -45,13 +44,13 @@ public class EnvironmentInfoContributorTests {
|
||||
@Test
|
||||
public void extractNoEntry() {
|
||||
EnvironmentTestUtils.addEnvironment(this.environment, "foo=bar");
|
||||
|
||||
Info actual = contributeFrom(this.environment);
|
||||
assertThat(actual.getDetails().size()).isEqualTo(0);
|
||||
}
|
||||
|
||||
private static Info contributeFrom(ConfigurableEnvironment environment) {
|
||||
EnvironmentInfoContributor contributor = new EnvironmentInfoContributor(environment);
|
||||
EnvironmentInfoContributor contributor = new EnvironmentInfoContributor(
|
||||
environment);
|
||||
Info.Builder builder = new Info.Builder();
|
||||
contributor.contribute(builder);
|
||||
return builder.build();
|
||||
|
||||
@@ -45,7 +45,6 @@ public class SimpleInfoContributorTests {
|
||||
assertThat(info.get("test")).isSameAs(o);
|
||||
}
|
||||
|
||||
|
||||
private static Info contributeFrom(String prefix, Object detail) {
|
||||
SimpleInfoContributor contributor = new SimpleInfoContributor(prefix, detail);
|
||||
Info.Builder builder = new Info.Builder();
|
||||
|
||||
Reference in New Issue
Block a user