@FrameworkEndpoint replaces EndpointHandlerAdapter

This commit is contained in:
Dave Syer
2013-12-08 15:46:12 +00:00
committed by Phillip Webb
parent 5a978e2f31
commit bbac4ea9fb
27 changed files with 425 additions and 529 deletions

View File

@@ -25,7 +25,7 @@ import org.junit.After;
import org.junit.Test;
import org.springframework.boot.TestUtils;
import org.springframework.boot.actuate.endpoint.AbstractEndpoint;
import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.boot.actuate.endpoint.mvc.FrameworkEndpoint;
import org.springframework.boot.actuate.properties.ManagementServerProperties;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration;
@@ -200,13 +200,8 @@ public class EndpointWebMvcAutoConfigurationTests {
}
@Bean
public Endpoint<String> testEndpoint() {
return new AbstractEndpoint<String>("/endpoint", false, true) {
@Override
public String doInvoke() {
return "endpointoutput";
}
};
public TestEndpoint testEndpoint() {
return new TestEndpoint();
}
}
@@ -245,4 +240,20 @@ public class EndpointWebMvcAutoConfigurationTests {
}
@FrameworkEndpoint
public static class TestEndpoint extends AbstractEndpoint<String> {
public TestEndpoint() {
super("/endpoint", false, true);
}
@Override
@RequestMapping
@ResponseBody
public String invoke() {
return "endpointoutput";
}
}
}

View File

@@ -25,7 +25,6 @@ import org.springframework.boot.TestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.http.MediaType;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
@@ -49,16 +48,13 @@ public abstract class AbstractEndpointTests<T extends Endpoint<?>> {
private final String property;
private MediaType[] produces;
public AbstractEndpointTests(Class<?> configClass, Class<?> type, String path,
boolean sensitive, String property, MediaType... produces) {
boolean sensitive, String property) {
this.configClass = configClass;
this.type = type;
this.path = path;
this.sensitive = sensitive;
this.property = property;
this.produces = produces;
}
@Before
@@ -75,11 +71,6 @@ public abstract class AbstractEndpointTests<T extends Endpoint<?>> {
}
}
@Test
public void producesMediaType() {
assertThat(getEndpointBean().produces(), equalTo(this.produces));
}
@Test
public void getPath() throws Exception {
assertThat(getEndpointBean().getPath(), equalTo(this.path));

View File

@@ -17,11 +17,9 @@
package org.springframework.boot.actuate.endpoint;
import org.junit.Test;
import org.springframework.boot.actuate.endpoint.BeansEndpoint;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertThat;
@@ -34,8 +32,7 @@ import static org.junit.Assert.assertThat;
public class BeansEndpointTests extends AbstractEndpointTests<BeansEndpoint> {
public BeansEndpointTests() {
super(Config.class, BeansEndpoint.class, "/beans", true, "endpoints.beans",
MediaType.APPLICATION_JSON);
super(Config.class, BeansEndpoint.class, "/beans", true, "endpoints.beans");
}
@Test

View File

@@ -1,74 +0,0 @@
/*
* 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.boot.actuate.endpoint.mvc;
import java.util.Collections;
import java.util.Map;
import org.junit.Test;
import org.springframework.boot.actuate.endpoint.AbstractEndpoint;
import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link EndpointHandlerAdapter}.
*
* @author Phillip Webb
*/
public class EndpointHandlerAdapterTests {
private EndpointHandlerAdapter adapter = new EndpointHandlerAdapter();
private MockHttpServletRequest request = new MockHttpServletRequest();
private MockHttpServletResponse response = new MockHttpServletResponse();
@Test
public void onlySupportsEndpoints() throws Exception {
assertTrue(this.adapter.supports(mock(Endpoint.class)));
assertFalse(this.adapter.supports(mock(Object.class)));
}
@Test
public void rendersJson() throws Exception {
this.adapter.handle(this.request, this.response,
new AbstractEndpoint<Map<String, String>>("/foo") {
@Override
protected Map<String, String> doInvoke() {
return Collections.singletonMap("hello", "world");
}
});
assertEquals("{\"hello\":\"world\"}", this.response.getContentAsString());
}
@Test
public void rendersString() throws Exception {
this.request.addHeader("Accept", "text/plain");
this.adapter.handle(this.request, this.response, new AbstractEndpoint<String>(
"/foo") {
@Override
protected String doInvoke() {
return "hello world";
}
});
assertEquals("hello world", this.response.getContentAsString());
}
}

View File

@@ -16,12 +16,18 @@
package org.springframework.boot.actuate.endpoint.mvc;
import java.util.Arrays;
import java.lang.reflect.Method;
import org.junit.Before;
import org.junit.Test;
import org.springframework.boot.actuate.endpoint.AbstractEndpoint;
import org.springframework.http.HttpMethod;
import org.springframework.context.support.StaticApplicationContext;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.method.HandlerMethod;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;
@@ -33,21 +39,38 @@ import static org.junit.Assert.assertThat;
* Tests for {@link EndpointHandlerMapping}.
*
* @author Phillip Webb
* @author Dave Syer
*/
public class EndpointHandlerMappingTests {
private StaticApplicationContext context = new StaticApplicationContext();
private EndpointHandlerMapping mapping = new EndpointHandlerMapping();
private Method method;
@Before
public void init() throws Exception {
this.context.getDefaultListableBeanFactory().registerSingleton("mapping",
this.mapping);
this.mapping.setApplicationContext(this.context);
this.method = ReflectionUtils.findMethod(TestEndpoint.class, "invoke");
}
@Test
public void withoutPrefix() throws Exception {
TestEndpoint endpointA = new TestEndpoint("/a");
TestEndpoint endpointB = new TestEndpoint("/b");
EndpointHandlerMapping mapping = new EndpointHandlerMapping(Arrays.asList(
endpointA, endpointB));
mapping.afterPropertiesSet();
assertThat(mapping.getHandler(new MockHttpServletRequest("GET", "/a"))
.getHandler(), equalTo((Object) endpointA));
assertThat(mapping.getHandler(new MockHttpServletRequest("GET", "/b"))
.getHandler(), equalTo((Object) endpointB));
assertThat(mapping.getHandler(new MockHttpServletRequest("GET", "/c")),
this.context.getDefaultListableBeanFactory().registerSingleton(
endpointA.getPath(), endpointA);
this.context.getDefaultListableBeanFactory().registerSingleton(
endpointB.getPath(), endpointB);
this.mapping.afterPropertiesSet();
assertThat(this.mapping.getHandler(new MockHttpServletRequest("GET", "/a"))
.getHandler(),
equalTo((Object) new HandlerMethod(endpointA, this.method)));
assertThat(this.mapping.getHandler(new MockHttpServletRequest("GET", "/b"))
.getHandler(),
equalTo((Object) new HandlerMethod(endpointB, this.method)));
assertThat(this.mapping.getHandler(new MockHttpServletRequest("GET", "/c")),
nullValue());
}
@@ -55,49 +78,63 @@ public class EndpointHandlerMappingTests {
public void withPrefix() throws Exception {
TestEndpoint endpointA = new TestEndpoint("/a");
TestEndpoint endpointB = new TestEndpoint("/b");
EndpointHandlerMapping mapping = new EndpointHandlerMapping(Arrays.asList(
endpointA, endpointB));
mapping.setPrefix("/a");
mapping.afterPropertiesSet();
assertThat(mapping.getHandler(new MockHttpServletRequest("GET", "/a/a"))
.getHandler(), equalTo((Object) endpointA));
assertThat(mapping.getHandler(new MockHttpServletRequest("GET", "/a/b"))
.getHandler(), equalTo((Object) endpointB));
assertThat(mapping.getHandler(new MockHttpServletRequest("GET", "/a")),
this.context.getDefaultListableBeanFactory().registerSingleton(
endpointA.getPath(), endpointA);
this.context.getDefaultListableBeanFactory().registerSingleton(
endpointB.getPath(), endpointB);
this.mapping.setPrefix("/a");
this.mapping.afterPropertiesSet();
assertThat(this.mapping.getHandler(new MockHttpServletRequest("GET", "/a/a"))
.getHandler(),
equalTo((Object) new HandlerMethod(endpointA, this.method)));
assertThat(this.mapping.getHandler(new MockHttpServletRequest("GET", "/a/b"))
.getHandler(),
equalTo((Object) new HandlerMethod(endpointB, this.method)));
assertThat(this.mapping.getHandler(new MockHttpServletRequest("GET", "/a")),
nullValue());
}
@Test
@Test(expected = HttpRequestMethodNotSupportedException.class)
public void onlyGetHttpMethodForNonActionEndpoints() throws Exception {
TestEndpoint endpoint = new TestEndpoint("/a");
EndpointHandlerMapping mapping = new EndpointHandlerMapping(
Arrays.asList(endpoint));
mapping.afterPropertiesSet();
assertNotNull(mapping.getHandler(new MockHttpServletRequest("GET", "/a")));
assertNull(mapping.getHandler(new MockHttpServletRequest("POST", "/a")));
this.context.getDefaultListableBeanFactory().registerSingleton(
endpoint.getPath(), endpoint);
this.mapping.afterPropertiesSet();
assertNotNull(this.mapping.getHandler(new MockHttpServletRequest("GET", "/a")));
assertNull(this.mapping.getHandler(new MockHttpServletRequest("POST", "/a")));
}
@Test
public void postHttpMethodForActionEndpoints() throws Exception {
TestEndpoint endpoint = new TestActionEndpoint("/a");
this.context.getDefaultListableBeanFactory().registerSingleton(
endpoint.getPath(), endpoint);
this.mapping.afterPropertiesSet();
assertNotNull(this.mapping.getHandler(new MockHttpServletRequest("POST", "/a")));
}
@Test(expected = HttpRequestMethodNotSupportedException.class)
public void onlyPostHttpMethodForActionEndpoints() throws Exception {
TestEndpoint endpoint = new TestActionEndpoint("/a");
EndpointHandlerMapping mapping = new EndpointHandlerMapping(
Arrays.asList(endpoint));
mapping.afterPropertiesSet();
assertNull(mapping.getHandler(new MockHttpServletRequest("GET", "/a")));
assertNotNull(mapping.getHandler(new MockHttpServletRequest("POST", "/a")));
this.context.getDefaultListableBeanFactory().registerSingleton(
endpoint.getPath(), endpoint);
this.mapping.afterPropertiesSet();
assertNotNull(this.mapping.getHandler(new MockHttpServletRequest("POST", "/a")));
assertNull(this.mapping.getHandler(new MockHttpServletRequest("GET", "/a")));
}
@Test
public void disabled() throws Exception {
TestEndpoint endpointA = new TestEndpoint("/a");
EndpointHandlerMapping mapping = new EndpointHandlerMapping(
Arrays.asList(endpointA));
mapping.setDisabled(true);
mapping.afterPropertiesSet();
assertThat(mapping.getHandler(new MockHttpServletRequest("GET", "/a")),
TestEndpoint endpoint = new TestEndpoint("/a");
this.context.getDefaultListableBeanFactory().registerSingleton(
endpoint.getPath(), endpoint);
this.mapping.setDisabled(true);
this.mapping.afterPropertiesSet();
assertThat(this.mapping.getHandler(new MockHttpServletRequest("GET", "/a")),
nullValue());
}
@FrameworkEndpoint
private static class TestEndpoint extends AbstractEndpoint<Object> {
public TestEndpoint(String path) {
@@ -105,12 +142,14 @@ public class EndpointHandlerMappingTests {
}
@Override
public Object doInvoke() {
@RequestMapping(method = RequestMethod.GET)
public Object invoke() {
return null;
}
}
@FrameworkEndpoint
private static class TestActionEndpoint extends TestEndpoint {
public TestActionEndpoint(String path) {
@@ -118,8 +157,9 @@ public class EndpointHandlerMappingTests {
}
@Override
public HttpMethod[] methods() {
return POST_HTTP_METHOD;
@RequestMapping(method = RequestMethod.POST)
public Object invoke() {
return null;
}
}

View File

@@ -26,8 +26,10 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.autoconfigure.ManagementSecurityAutoConfiguration;
import org.springframework.boot.actuate.web.BasicErrorControllerIntegrationTests.TestConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -81,7 +83,8 @@ public class BasicErrorControllerIntegrationTests {
}
@Configuration
@EnableAutoConfiguration
@EnableAutoConfiguration(exclude = { SecurityAutoConfiguration.class,
ManagementSecurityAutoConfiguration.class })
public static class TestConfiguration {
// For manual testing

View File

@@ -19,7 +19,9 @@ package org.springframework.boot.actuate.web;
import org.junit.After;
import org.junit.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.autoconfigure.ManagementSecurityAutoConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
@@ -77,13 +79,15 @@ public class BasicErrorControllerSpecialIntegrationTests {
}
@Configuration
@EnableAutoConfiguration
@EnableAutoConfiguration(exclude = { SecurityAutoConfiguration.class,
ManagementSecurityAutoConfiguration.class })
protected static class ParentConfiguration {
}
@Configuration
@EnableAutoConfiguration
@EnableAutoConfiguration(exclude = { SecurityAutoConfiguration.class,
ManagementSecurityAutoConfiguration.class })
@EnableWebMvc
protected static class WebMvcIncludedConfiguration {
// For manual testing
@@ -94,7 +98,19 @@ public class BasicErrorControllerSpecialIntegrationTests {
}
@Configuration
@EnableAutoConfiguration
@EnableAutoConfiguration(exclude = { SecurityAutoConfiguration.class,
ManagementSecurityAutoConfiguration.class })
protected static class VanillaConfiguration {
// For manual testing
public static void main(String[] args) {
SpringApplication.run(VanillaConfiguration.class, args);
}
}
@Configuration
@EnableAutoConfiguration(exclude = { SecurityAutoConfiguration.class,
ManagementSecurityAutoConfiguration.class })
protected static class ChildConfiguration {
// For manual testing