[bs-22] Add loads of actuator tests

Discovered a few issues along the way and refactored
accordingly.

[#48127729] [bs-22] Add missing unit tests
pull/1/merge
Dave Syer 12 years ago
parent 19d8315639
commit b7c0e3279e

@ -32,7 +32,7 @@ import org.springframework.context.annotation.Import;
*/
@Configuration
@Import({ ActuatorWebConfiguration.class, ManagementConfiguration.class,
MetricRepositoryConfiguration.class, ServerConfiguration.class,
MetricRepositoryConfiguration.class, ErrorConfiguration.class,
SecurityConfiguration.class, TraceFilterConfiguration.class,
MetricFilterConfiguration.class, AuditConfiguration.class })
public class ActuatorAutoConfiguration {

@ -36,7 +36,7 @@ import org.springframework.context.annotation.Import;
@Configuration
@ConditionalOnClass({ Servlet.class })
@Import(InfoConfiguration.class)
public class ServerConfiguration implements EmbeddedServletContainerCustomizer {
public class ErrorConfiguration implements EmbeddedServletContainerCustomizer {
@Value("${endpoints.error.path:/error}")
private String errorPath = "/error";

@ -16,16 +16,14 @@
package org.springframework.bootstrap.actuate.autoconfigure;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;
import javax.annotation.Resource;
import javax.servlet.Servlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.bootstrap.actuate.endpoint.info.InfoEndpoint;
import org.springframework.bootstrap.bind.PropertiesConfigurationFactory;
import org.springframework.bootstrap.context.annotation.ConditionalOnClass;
@ -35,8 +33,9 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.DispatcherServlet;
/**
@ -49,55 +48,52 @@ import org.springframework.web.servlet.DispatcherServlet;
@ConditionalOnMissingBean({ InfoEndpoint.class })
public class InfoConfiguration {
@Resource(name = "infoMap")
private Map<String, Object> infoMap;
@Autowired
@Qualifier("gitInfo")
private GitInfo gitInfo;
private InfoPropertiesConfiguration properties;
@Bean
public Map<String, Object> applicationInfo() {
protected Map<String, Object> applicationInfo() throws Exception {
LinkedHashMap<String, Object> info = new LinkedHashMap<String, Object>();
info.putAll(this.infoMap);
if (this.gitInfo.getBranch() != null) {
info.put("git", this.gitInfo);
info.putAll(this.properties.infoMap());
GitInfo gitInfo = this.properties.gitInfo();
if (gitInfo.getBranch() != null) {
info.put("git", gitInfo);
}
return info;
}
@Bean
public InfoEndpoint infoEndpoint() {
public InfoEndpoint infoEndpoint() throws Exception {
return new InfoEndpoint(applicationInfo());
}
@Configuration
public static class InfoPropertiesConfiguration {
@Component
protected static class InfoPropertiesConfiguration {
@Autowired
private ConfigurableEnvironment environment = new StandardEnvironment();
@Bean
public PropertiesConfigurationFactory<GitInfo> gitInfo() throws IOException {
@Value("${spring.git.properties:classpath:git.properties}")
private Resource gitProperties;
public GitInfo gitInfo() throws Exception {
PropertiesConfigurationFactory<GitInfo> factory = new PropertiesConfigurationFactory<GitInfo>(
new GitInfo());
factory.setTargetName("git");
Properties properties = new Properties();
if (new ClassPathResource("git.properties").exists()) {
properties = PropertiesLoaderUtils.loadProperties(new ClassPathResource(
"git.properties"));
if (this.gitProperties.exists()) {
properties = PropertiesLoaderUtils.loadProperties(this.gitProperties);
}
factory.setProperties(properties);
return factory;
return factory.getObject();
}
@Bean
public PropertiesConfigurationFactory<Map<String, Object>> infoMap() {
public Map<String, Object> infoMap() throws Exception {
PropertiesConfigurationFactory<Map<String, Object>> factory = new PropertiesConfigurationFactory<Map<String, Object>>(
new LinkedHashMap<String, Object>());
factory.setTargetName("info");
factory.setPropertySources(this.environment.getPropertySources());
return factory;
return factory.getObject();
}
}

@ -16,9 +16,9 @@
package org.springframework.bootstrap.actuate.autoconfigure;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.bootstrap.actuate.properties.ManagementServerProperties;
import org.springframework.bootstrap.autoconfigure.web.EmbeddedContainerCustomizerConfiguration;
import org.springframework.bootstrap.context.annotation.ConditionalOnExpression;
import org.springframework.bootstrap.context.embedded.AnnotationConfigEmbeddedWebApplicationContext;
import org.springframework.bootstrap.properties.ServerProperties;
@ -26,8 +26,10 @@ import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
/**
@ -36,8 +38,7 @@ import org.springframework.context.event.ContextRefreshedEvent;
*/
@Configuration
@ConditionalOnExpression("${management.port:${server.port:8080}}>0")
public class ManagementConfiguration implements ApplicationContextAware, DisposableBean,
ApplicationListener<ContextRefreshedEvent> {
public class ManagementConfiguration implements ApplicationContextAware {
private ApplicationContext parent;
private ConfigurableApplicationContext context;
@ -61,27 +62,42 @@ public class ManagementConfiguration implements ApplicationContextAware, Disposa
this.parent = applicationContext;
}
@Override
public void destroy() throws Exception {
if (this.context != null) {
this.context.close();
}
@Bean
public ApplicationListener<ContextClosedEvent> managementContextClosedListener() {
return new ApplicationListener<ContextClosedEvent>() {
@Override
public void onApplicationEvent(ContextClosedEvent event) {
if (event.getSource() != ManagementConfiguration.this.parent) {
return;
}
if (ManagementConfiguration.this.context != null) {
ManagementConfiguration.this.context.close();
}
}
};
}
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
if (event.getSource() != this.parent) {
return;
}
if (this.configuration.getPort() != this.management.getPort()) {
AnnotationConfigEmbeddedWebApplicationContext context = new AnnotationConfigEmbeddedWebApplicationContext();
context.setParent(this.parent);
context.register(ManagementServerConfiguration.class,
MetricsConfiguration.class, HealthConfiguration.class,
ShutdownConfiguration.class, TraceConfiguration.class);
context.refresh();
this.context = context;
}
@Bean
public ApplicationListener<ContextRefreshedEvent> managementContextRefeshedListener() {
return new ApplicationListener<ContextRefreshedEvent>() {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
if (event.getSource() != ManagementConfiguration.this.parent) {
return;
}
if (ManagementConfiguration.this.configuration.getPort() != ManagementConfiguration.this.management
.getPort()) {
AnnotationConfigEmbeddedWebApplicationContext context = new AnnotationConfigEmbeddedWebApplicationContext();
context.setParent(ManagementConfiguration.this.parent);
context.register(EmbeddedContainerCustomizerConfiguration.class,
ManagementServerConfiguration.class,
MetricsConfiguration.class, HealthConfiguration.class,
ShutdownConfiguration.class, TraceConfiguration.class);
context.refresh();
ManagementConfiguration.this.context = context;
}
}
};
}
}

@ -16,16 +16,14 @@
package org.springframework.bootstrap.actuate.autoconfigure;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.HierarchicalBeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.bootstrap.actuate.endpoint.error.ErrorEndpoint;
import org.springframework.bootstrap.actuate.properties.ManagementServerProperties;
import org.springframework.bootstrap.context.annotation.ConditionalOnBean;
import org.springframework.bootstrap.context.embedded.AbstractEmbeddedServletContainerFactory;
import org.springframework.bootstrap.context.embedded.ConfigurableEmbeddedServletContainerFactory;
import org.springframework.bootstrap.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.bootstrap.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.bootstrap.context.embedded.ErrorPage;
import org.springframework.bootstrap.context.embedded.jetty.JettyEmbeddedServletContainerFactory;
@ -34,6 +32,7 @@ import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@ -44,15 +43,7 @@ import org.springframework.web.servlet.config.annotation.EnableWebMvc;
*/
@Configuration
@EnableWebMvc
public class ManagementServerConfiguration implements BeanPostProcessor {
@Autowired
private ManagementServerProperties configuration = new ManagementServerProperties();
private boolean initialized = false;
@Value("${endpoints.error.path:/error}")
private String errorPath = "/error";
public class ManagementServerConfiguration {
@Bean
public DispatcherServlet dispatcherServlet() {
@ -86,35 +77,26 @@ public class ManagementServerConfiguration implements BeanPostProcessor {
return new JettyEmbeddedServletContainerFactory();
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
@Component
protected static class ServerCustomizationConfiguration implements
EmbeddedServletContainerCustomizer {
if (bean instanceof EmbeddedServletContainerFactory) {
@Value("${endpoints.error.path:/error}")
private String errorPath = "/error";
if (bean instanceof AbstractEmbeddedServletContainerFactory
&& !this.initialized) {
ConfigurableEmbeddedServletContainerFactory factory = (ConfigurableEmbeddedServletContainerFactory) bean;
factory.setPort(this.configuration.getPort());
factory.setAddress(this.configuration.getAddress());
factory.setContextPath(this.configuration.getContextPath());
factory.addErrorPages(new ErrorPage(this.errorPath));
this.initialized = true;
}
@Autowired
private ApplicationContext beanFactory;
@Override
public void customize(ConfigurableEmbeddedServletContainerFactory factory) {
ManagementServerProperties configuration = this.beanFactory
.getBean(ManagementServerProperties.class);
factory.setPort(configuration.getPort());
factory.setAddress(configuration.getAddress());
factory.setContextPath(configuration.getContextPath());
factory.addErrorPages(new ErrorPage(this.errorPath));
}
return bean;
}
}

@ -21,7 +21,7 @@ import javax.servlet.Servlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.bootstrap.actuate.endpoint.metrics.PublicMetrics;
import org.springframework.bootstrap.actuate.endpoint.metrics.VanillaPublicMetrics;
import org.springframework.bootstrap.actuate.endpoint.metrics.VarzEndpoint;
import org.springframework.bootstrap.actuate.endpoint.metrics.MetricsEndpoint;
import org.springframework.bootstrap.actuate.metrics.MetricRepository;
import org.springframework.bootstrap.context.annotation.ConditionalOnClass;
import org.springframework.bootstrap.context.annotation.ConditionalOnMissingBean;
@ -37,7 +37,7 @@ import org.springframework.web.servlet.DispatcherServlet;
*/
@Configuration
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class })
@ConditionalOnMissingBean({ VarzEndpoint.class })
@ConditionalOnMissingBean({ MetricsEndpoint.class })
public class MetricsConfiguration {
@Autowired
@ -47,11 +47,11 @@ public class MetricsConfiguration {
private PublicMetrics metrics;
@Bean
public VarzEndpoint varzEndpoint() {
public MetricsEndpoint metricsEndpoint() {
if (this.metrics == null) {
this.metrics = new VanillaPublicMetrics(this.repository);
}
return new VarzEndpoint(this.metrics);
return new MetricsEndpoint(this.metrics);
}
}

@ -28,14 +28,14 @@ import org.springframework.web.bind.annotation.ResponseBody;
* @author Dave Syer
*/
@Controller
public class VarzEndpoint {
public class MetricsEndpoint {
private PublicMetrics metrics;
/**
* @param metrics
*/
public VarzEndpoint(PublicMetrics metrics) {
public MetricsEndpoint(PublicMetrics metrics) {
this.metrics = metrics;
}

@ -0,0 +1,43 @@
/*
* Copyright 2012-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.bootstrap.actuate;
import java.util.HashMap;
import java.util.Map;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.MapPropertySource;
/**
* @author Dave Syer
*
*/
public class TestUtils {
public static void addEnviroment(ConfigurableApplicationContext context,
String... pairs) {
Map<String, Object> map = new HashMap<String, Object>();
for (String pair : pairs) {
int index = pair.indexOf(":");
String key = pair.substring(0, index > 0 ? index : pair.length());
String value = index > 0 ? pair.substring(index + 1) : "";
map.put(key, value);
}
context.getEnvironment().getPropertySources()
.addFirst(new MapPropertySource("test", map));
}
}

@ -0,0 +1,42 @@
/*
* Copyright 2012-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.bootstrap.actuate.autoconfigure;
import org.junit.Test;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import static org.junit.Assert.assertNotNull;
/**
* @author Dave Syer
*/
public class ActuatorWebConfigurationTests {
private AnnotationConfigWebApplicationContext context;
@Test
public void testWebConfiguration() throws Exception {
this.context = new AnnotationConfigWebApplicationContext();
this.context.setServletContext(new MockServletContext());
this.context.register(ActuatorWebConfiguration.class);
this.context.refresh();
assertNotNull(this.context.getBean(WebMvcConfigurationSupport.class));
}
}

@ -0,0 +1,51 @@
/*
* Copyright 2012-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.bootstrap.actuate.autoconfigure;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.bootstrap.actuate.autoconfigure.ActuatorAutoConfiguration.ServerPropertiesConfiguration;
import org.springframework.bootstrap.actuate.endpoint.error.ErrorEndpoint;
import org.springframework.bootstrap.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.bootstrap.context.embedded.ConfigurableEmbeddedServletContainerFactory;
import org.springframework.bootstrap.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.bootstrap.context.embedded.ErrorPage;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import static org.junit.Assert.assertNotNull;
/**
* @author Dave Syer
*/
public class ErrorConfigurationTests {
private AnnotationConfigApplicationContext context;
@Test
public void testErrorEndpointConfiguration() throws Exception {
this.context = new AnnotationConfigApplicationContext();
this.context.register(ErrorConfiguration.class,
ServerPropertiesConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertNotNull(this.context.getBean(ErrorEndpoint.class));
ConfigurableEmbeddedServletContainerFactory factory = Mockito
.mock(ConfigurableEmbeddedServletContainerFactory.class);
this.context.getBean(EmbeddedServletContainerCustomizer.class).customize(factory);
Mockito.verify(factory).addErrorPages(Mockito.any(ErrorPage.class));
}
}

@ -0,0 +1,39 @@
/*
* Copyright 2012-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.bootstrap.actuate.autoconfigure;
import org.junit.Test;
import org.springframework.bootstrap.actuate.endpoint.health.HealthEndpoint;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import static org.junit.Assert.assertNotNull;
/**
* @author Dave Syer
*/
public class HealthConfigurationTests {
private AnnotationConfigApplicationContext context;
@Test
public void testTraceConfiguration() throws Exception {
this.context = new AnnotationConfigApplicationContext();
this.context.register(HealthConfiguration.class);
this.context.refresh();
assertNotNull(this.context.getBean(HealthEndpoint.class));
}
}

@ -0,0 +1,59 @@
/*
* Copyright 2012-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.bootstrap.actuate.autoconfigure;
import org.junit.Test;
import org.springframework.bootstrap.actuate.TestUtils;
import org.springframework.bootstrap.actuate.endpoint.info.InfoEndpoint;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
/**
* @author Dave Syer
*/
public class InfoConfigurationTests {
private AnnotationConfigApplicationContext context;
@Test
public void testInfoEndpointConfiguration() throws Exception {
this.context = new AnnotationConfigApplicationContext();
TestUtils.addEnviroment(this.context, "info.foo:bar");
this.context.register(InfoConfiguration.class);
this.context.refresh();
InfoEndpoint endpoint = this.context.getBean(InfoEndpoint.class);
assertNotNull(endpoint);
assertNotNull(endpoint.info().get("git"));
assertEquals("bar", endpoint.info().get("foo"));
}
@Test
public void testNoGitProperties() throws Exception {
this.context = new AnnotationConfigApplicationContext();
TestUtils.addEnviroment(this.context,
"spring.git.properties:classpath:nonexistent");
this.context.register(InfoConfiguration.class);
this.context.refresh();
InfoEndpoint endpoint = this.context.getBean(InfoEndpoint.class);
assertNotNull(endpoint);
assertNull(endpoint.info().get("git"));
}
}

@ -0,0 +1,119 @@
/*
* Copyright 2012-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.bootstrap.actuate.autoconfigure;
import javax.servlet.Servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;
import org.junit.After;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.bootstrap.actuate.TestUtils;
import org.springframework.bootstrap.actuate.endpoint.health.HealthEndpoint;
import org.springframework.bootstrap.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.bootstrap.autoconfigure.web.ServerPropertiesConfiguration;
import org.springframework.bootstrap.context.embedded.EmbeddedServletContainer;
import org.springframework.bootstrap.context.embedded.EmbeddedServletContainerException;
import org.springframework.bootstrap.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.bootstrap.context.embedded.ServletContextInitializer;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mock.web.MockServletContext;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
/**
* @author Dave Syer
*/
public class ManagementConfigurationTests {
private AnnotationConfigApplicationContext context;
@After
public void close() {
if (this.context != null) {
this.context.close();
}
}
@Test
public void testManagementConfiguration() throws Exception {
this.context = new AnnotationConfigApplicationContext();
this.context
.register(MetricRepositoryConfiguration.class,
TraceFilterConfiguration.class,
ServerPropertiesConfiguration.class,
ActuatorAutoConfiguration.ServerPropertiesConfiguration.class,
ManagementConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertNotNull(this.context.getBean(HealthEndpoint.class));
}
@Test
public void testChildContextCreated() throws Exception {
this.context = new AnnotationConfigApplicationContext();
TestUtils.addEnviroment(this.context, "server.port:7000", "management.port:7001");
this.context
.register(ParentContext.class, MetricRepositoryConfiguration.class,
TraceFilterConfiguration.class,
ServerPropertiesConfiguration.class,
ActuatorAutoConfiguration.ServerPropertiesConfiguration.class,
ManagementConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertEquals(0, this.context.getBeanNamesForType(HealthEndpoint.class).length);
}
@Configuration
protected static class ParentContext {
@Bean
public EmbeddedServletContainerFactory factory() {
return new EmbeddedServletContainerFactory() {
@Override
public EmbeddedServletContainer getEmbdeddedServletContainer(
ServletContextInitializer... initializers) {
ServletContext servletContext = new MockServletContext() {
@Override
public Dynamic addServlet(String servletName, Servlet servlet) {
return Mockito.mock(Dynamic.class);
}
};
for (ServletContextInitializer initializer : initializers) {
try {
initializer.onStartup(servletContext);
} catch (ServletException ex) {
throw new IllegalStateException(ex);
}
}
return new EmbeddedServletContainer() {
@Override
public void stop() throws EmbeddedServletContainerException {
}
};
}
};
}
}
}

@ -0,0 +1,57 @@
/*
* Copyright 2012-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.bootstrap.actuate.autoconfigure;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.bootstrap.actuate.autoconfigure.ActuatorAutoConfiguration.ServerPropertiesConfiguration;
import org.springframework.bootstrap.actuate.endpoint.error.ErrorEndpoint;
import org.springframework.bootstrap.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.bootstrap.context.embedded.ConfigurableEmbeddedServletContainerFactory;
import org.springframework.bootstrap.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.bootstrap.context.embedded.ErrorPage;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import static org.junit.Assert.assertNotNull;
/**
* @author Dave Syer
*/
public class ManagementServerConfigurationTests {
private AnnotationConfigWebApplicationContext context;
@Test
public void testWebConfiguration() throws Exception {
this.context = new AnnotationConfigWebApplicationContext();
this.context.setServletContext(new MockServletContext());
this.context.register(ManagementServerConfiguration.class,
ServerPropertiesConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertNotNull(this.context.getBean(WebMvcConfigurationSupport.class));
assertNotNull(this.context.getBean(ErrorEndpoint.class));
ConfigurableEmbeddedServletContainerFactory factory = Mockito
.mock(ConfigurableEmbeddedServletContainerFactory.class);
this.context.getBean(EmbeddedServletContainerCustomizer.class).customize(factory);
Mockito.verify(factory).addErrorPages(Mockito.any(ErrorPage.class));
Mockito.verify(factory).setPort(8080);
}
}

@ -0,0 +1,51 @@
/*
* Copyright 2012-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.bootstrap.actuate.autoconfigure;
import javax.servlet.Filter;
import org.junit.Test;
import org.springframework.mock.web.MockFilterChain;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import static org.junit.Assert.assertNotNull;
/**
* @author Dave Syer
*/
public class MetricFilterConfigurationTests {
private AnnotationConfigWebApplicationContext context;
@Test
public void testMetricFilterConfiguration() throws Exception {
this.context = new AnnotationConfigWebApplicationContext();
this.context.setServletContext(new MockServletContext());
// Order is important
this.context.register(MetricRepositoryConfiguration.class,
MetricFilterConfiguration.class);
this.context.refresh();
Filter filter = this.context.getBean(Filter.class);
assertNotNull(filter);
filter.doFilter(new MockHttpServletRequest(), new MockHttpServletResponse(),
new MockFilterChain());
}
}

@ -0,0 +1,40 @@
/*
* Copyright 2012-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.bootstrap.actuate.autoconfigure;
import org.junit.Test;
import org.springframework.bootstrap.actuate.metrics.MetricRepository;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import static org.junit.Assert.assertNotNull;
/**
* @author Dave Syer
*/
public class MetricRepositoryConfigurationTests {
private AnnotationConfigApplicationContext context;
@Test
public void testTraceConfiguration() throws Exception {
this.context = new AnnotationConfigApplicationContext();
this.context.register(MetricRepositoryConfiguration.class);
this.context.refresh();
assertNotNull(this.context.getBean(MetricRepository.class));
}
}

@ -0,0 +1,41 @@
/*
* Copyright 2012-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.bootstrap.actuate.autoconfigure;
import org.junit.Test;
import org.springframework.bootstrap.actuate.endpoint.metrics.MetricsEndpoint;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import static org.junit.Assert.assertNotNull;
/**
* @author Dave Syer
*/
public class MetricsConfigurationTests {
private AnnotationConfigApplicationContext context;
@Test
public void testTraceConfiguration() throws Exception {
this.context = new AnnotationConfigApplicationContext();
this.context.register(MetricRepositoryConfiguration.class,
MetricsConfiguration.class);
this.context.refresh();
assertNotNull(this.context.getBean(MetricsEndpoint.class));
}
}

@ -0,0 +1,44 @@
/*
* Copyright 2012-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.bootstrap.actuate.autoconfigure;
import org.junit.Test;
import org.springframework.bootstrap.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.mock.web.MockServletContext;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import static org.junit.Assert.assertNotNull;
/**
* @author Dave Syer
*/
public class SecurityConfigurationTests {
private AnnotationConfigWebApplicationContext context;
@Test
public void testWebConfiguration() throws Exception {
this.context = new AnnotationConfigWebApplicationContext();
this.context.setServletContext(new MockServletContext());
this.context.register(SecurityConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertNotNull(this.context.getBean(AuthenticationManager.class));
}
}

@ -0,0 +1,43 @@
/*
* Copyright 2012-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.bootstrap.actuate.autoconfigure;
import org.junit.Test;
import org.springframework.bootstrap.actuate.autoconfigure.ActuatorAutoConfiguration.ServerPropertiesConfiguration;
import org.springframework.bootstrap.actuate.endpoint.shutdown.ShutdownEndpoint;
import org.springframework.bootstrap.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import static org.junit.Assert.assertNotNull;
/**
* @author Dave Syer
*/
public class ShutdownConfigurationTests {
private AnnotationConfigApplicationContext context;
@Test
public void testEndpointConfiguration() throws Exception {
this.context = new AnnotationConfigApplicationContext();
this.context.register(ShutdownConfiguration.class,
ServerPropertiesConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertNotNull(this.context.getBean(ShutdownEndpoint.class));
}
}

@ -0,0 +1,41 @@
/*
* Copyright 2012-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.bootstrap.actuate.autoconfigure;
import org.junit.Test;
import org.springframework.bootstrap.actuate.endpoint.trace.TraceEndpoints;
import org.springframework.bootstrap.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import static org.junit.Assert.assertNotNull;
/**
* @author Dave Syer
*/
public class TraceConfigurationTests {
private AnnotationConfigApplicationContext context;
@Test
public void testEndpointConfiguration() throws Exception {
this.context = new AnnotationConfigApplicationContext();
this.context.register(TraceFilterConfiguration.class, TraceConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertNotNull(this.context.getBean(TraceEndpoints.class));
}
}

@ -0,0 +1,13 @@
#Generated by Git-Commit-Id-Plugin
#Thu May 23 09:26:42 BST 2013
git.commit.id.abbrev=e02a4f3
git.commit.user.email=dsyer@vmware.com
git.commit.message.full=Update Spring
git.commit.id=e02a4f3b6f452cdbf6dd311f1362679eb4c31ced
git.commit.message.short=Update Spring
git.commit.user.name=Dave Syer
git.build.user.name=Dave Syer
git.build.user.email=dsyer@vmware.com
git.branch=develop
git.commit.time=2013-04-24T08\:42\:13+0100
git.build.time=2013-05-23T09\:26\:42+0100

@ -35,7 +35,7 @@ import static org.junit.Assert.assertTrue;
* @author Dave Syer
*
*/
public class VarzAddressServiceBootstrapApplicationTests {
public class ManagementAddressServiceBootstrapApplicationTests {
private static ConfigurableApplicationContext context;

@ -26,7 +26,7 @@ import static org.junit.Assert.assertEquals;
* @author Dave Syer
*
*/
public class VarzContextServiceBootstrapApplicationTests {
public class ManagementServiceBootstrapApplicationTests {
private static ConfigurableApplicationContext context;

@ -35,7 +35,7 @@ import static org.junit.Assert.assertEquals;
* @author Dave Syer
*
*/
public class NoVarzContextServiceBootstrapApplicationTests {
public class NoManagementServiceBootstrapApplicationTests {
private static ConfigurableApplicationContext context;

@ -17,28 +17,26 @@
package org.springframework.bootstrap.autoconfigure.web;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.servlet.Servlet;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.bootstrap.context.annotation.ConditionalOnClass;
import org.springframework.bootstrap.context.annotation.EnableAutoConfiguration;
import org.springframework.bootstrap.context.embedded.ConfigurableEmbeddedServletContainerFactory;
import org.springframework.bootstrap.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.bootstrap.context.embedded.jetty.JettyEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
/**
* {@link EnableAutoConfiguration Auto-configuration} for
* {@link JettyEmbeddedServletContainerFactory}.
* {@link EmbeddedServletContainerCustomizer}.
*
* @author Dave Syer
*/
@ -46,12 +44,14 @@ import org.springframework.core.annotation.AnnotationAwareOrderComparator;
@ConditionalOnClass({ Servlet.class, EmbeddedServletContainerCustomizer.class })
public class EmbeddedContainerCustomizerConfiguration {
@Autowired(required = false)
private Set<EmbeddedServletContainerCustomizer> customizers = new HashSet<EmbeddedServletContainerCustomizer>();
@Bean
public BeanPostProcessor embeddedContainerCustomizerBeanPostProcessor() {
return new EmbeddedContainerCustomizerBeanPostProcessor(this.customizers);
public BeanPostProcessor embeddedContainerCustomizerBeanPostProcessor(
ListableBeanFactory beanFactory) {
// Look these up, not autowired because we don't want the ones from the parent
// context
Collection<EmbeddedServletContainerCustomizer> customizers = beanFactory
.getBeansOfType(EmbeddedServletContainerCustomizer.class).values();
return new EmbeddedContainerCustomizerBeanPostProcessor(customizers);
}
private static final class EmbeddedContainerCustomizerBeanPostProcessor implements
@ -60,7 +60,7 @@ public class EmbeddedContainerCustomizerConfiguration {
private List<EmbeddedServletContainerCustomizer> customizers;
public EmbeddedContainerCustomizerBeanPostProcessor(
Set<EmbeddedServletContainerCustomizer> customizers) {
Collection<EmbeddedServletContainerCustomizer> customizers) {
final List<EmbeddedServletContainerCustomizer> list = new ArrayList<EmbeddedServletContainerCustomizer>(
customizers);
Collections.sort(list, new AnnotationAwareOrderComparator());

Loading…
Cancel
Save