Skip to content

Commit 9bde1e8

Browse files
committed
Move some tests to AbstractEndpointHandlerMapping
See gh-7108
1 parent 5fc58b4 commit 9bde1e8

File tree

4 files changed

+112
-34
lines changed

4 files changed

+112
-34
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/AbstractEndpointHandlerMapping.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
* @author Dave Syer
5353
* @author Madhura Bhave
5454
*/
55-
public class AbstractEndpointHandlerMapping<E extends MvcEndpoint>
55+
public abstract class AbstractEndpointHandlerMapping<E extends MvcEndpoint>
5656
extends RequestMappingHandlerMapping {
5757

5858
private final Set<E> endpoints;

spring-boot-actuator/src/test/java/org/springframework/boot/actuate/cloudfoundry/CloudFoundryEndpointHandlerMappingTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.junit.Test;
2323

2424
import org.springframework.boot.actuate.endpoint.AbstractEndpoint;
25+
import org.springframework.boot.actuate.endpoint.mvc.AbstractEndpointHandlerMappingTests;
2526
import org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter;
2627
import org.springframework.boot.actuate.endpoint.mvc.HalJsonMvcEndpoint;
2728
import org.springframework.boot.actuate.endpoint.mvc.ManagementServletContext;
@@ -39,7 +40,7 @@
3940
*
4041
* @author Madhura Bhave
4142
*/
42-
public class CloudFoundryEndpointHandlerMappingTests {
43+
public class CloudFoundryEndpointHandlerMappingTests extends AbstractEndpointHandlerMappingTests {
4344

4445
@Test
4546
public void getHandlerExecutionChainShouldHaveSecurityInterceptor() throws Exception {
@@ -117,7 +118,6 @@ public String getContextPath() {
117118
}
118119

119120
});
120-
121121
}
122122
}
123123

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Copyright 2012-2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.endpoint.mvc;
18+
19+
import java.util.Arrays;
20+
import java.util.Collection;
21+
22+
import org.junit.Test;
23+
24+
import org.springframework.boot.actuate.endpoint.AbstractEndpoint;
25+
import org.springframework.context.support.StaticApplicationContext;
26+
import org.springframework.mock.web.MockHttpServletRequest;
27+
import org.springframework.web.bind.annotation.PostMapping;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
31+
/**
32+
* Tests for {@link AbstractEndpointHandlerMapping}.
33+
*
34+
* @author Madhura Bhave
35+
*/
36+
public abstract class AbstractEndpointHandlerMappingTests {
37+
38+
private final StaticApplicationContext context = new StaticApplicationContext();
39+
40+
@Test
41+
public void pathNotMappedWhenGetPathReturnsNull() throws Exception {
42+
TestMvcEndpoint endpoint = new TestMvcEndpoint(new TestEndpoint("a"));
43+
TestActionEndpoint other = new TestActionEndpoint(new TestEndpoint("b"));
44+
AbstractEndpointHandlerMapping mapping = new TestEndpointHandlerMapping(
45+
Arrays.asList(endpoint, other));
46+
mapping.setApplicationContext(this.context);
47+
mapping.afterPropertiesSet();
48+
assertThat(mapping.getHandlerMethods()).hasSize(1);
49+
assertThat(mapping.getHandler(request("GET", "/a"))).isNull();
50+
assertThat(mapping.getHandler(request("POST", "/b"))).isNotNull();
51+
}
52+
53+
private MockHttpServletRequest request(String method, String requestURI) {
54+
return new MockHttpServletRequest(method, requestURI);
55+
}
56+
57+
private static class TestEndpoint extends AbstractEndpoint<Object> {
58+
59+
TestEndpoint(String id) {
60+
super(id);
61+
}
62+
63+
@Override
64+
public Object invoke() {
65+
return null;
66+
}
67+
68+
}
69+
70+
private static class TestMvcEndpoint extends EndpointMvcAdapter {
71+
72+
TestMvcEndpoint(TestEndpoint delegate) {
73+
super(delegate);
74+
}
75+
76+
}
77+
78+
private static class TestActionEndpoint extends EndpointMvcAdapter {
79+
80+
TestActionEndpoint(TestEndpoint delegate) {
81+
super(delegate);
82+
}
83+
84+
@Override
85+
@PostMapping
86+
public Object invoke() {
87+
return null;
88+
}
89+
90+
}
91+
92+
private static class TestEndpointHandlerMapping extends AbstractEndpointHandlerMapping {
93+
94+
TestEndpointHandlerMapping(Collection<? extends MvcEndpoint> endpoints) {
95+
super(endpoints);
96+
}
97+
98+
@Override
99+
protected String getPath(MvcEndpoint endpoint) {
100+
if (endpoint instanceof TestActionEndpoint) {
101+
return super.getPath(endpoint);
102+
}
103+
return null;
104+
}
105+
106+
}
107+
108+
}

spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/EndpointHandlerMappingTests.java

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.lang.reflect.Method;
2020
import java.util.Arrays;
21-
import java.util.Collection;
2221

2322
import org.junit.Before;
2423
import org.junit.Test;
@@ -39,7 +38,7 @@
3938
* @author Phillip Webb
4039
* @author Dave Syer
4140
*/
42-
public class EndpointHandlerMappingTests {
41+
public class EndpointHandlerMappingTests extends AbstractEndpointHandlerMappingTests {
4342

4443
private final StaticApplicationContext context = new StaticApplicationContext();
4544

@@ -137,19 +136,6 @@ public void duplicatePath() throws Exception {
137136
assertThat(mapping.getHandler(request("POST", "/a"))).isNull();
138137
}
139138

140-
@Test
141-
public void pathNotMappedWhenGetPathReturnsNull() throws Exception {
142-
TestMvcEndpoint endpoint = new TestMvcEndpoint(new TestEndpoint("a"));
143-
TestActionEndpoint other = new TestActionEndpoint(new TestEndpoint("b"));
144-
EndpointHandlerMapping mapping = new TestEndpointHandlerMapping(
145-
Arrays.asList(endpoint, other));
146-
mapping.setApplicationContext(this.context);
147-
mapping.afterPropertiesSet();
148-
assertThat(mapping.getHandlerMethods()).hasSize(1);
149-
assertThat(mapping.getHandler(request("GET", "/a"))).isNull();
150-
assertThat(mapping.getHandler(request("POST", "/b"))).isNotNull();
151-
}
152-
153139
private MockHttpServletRequest request(String method, String requestURI) {
154140
return new MockHttpServletRequest(method, requestURI);
155141
}
@@ -189,20 +175,4 @@ public Object invoke() {
189175

190176
}
191177

192-
static class TestEndpointHandlerMapping extends EndpointHandlerMapping {
193-
194-
TestEndpointHandlerMapping(Collection<? extends MvcEndpoint> endpoints) {
195-
super(endpoints);
196-
}
197-
198-
@Override
199-
protected String getPath(MvcEndpoint endpoint) {
200-
if (endpoint instanceof TestActionEndpoint) {
201-
return super.getPath(endpoint);
202-
}
203-
return null;
204-
}
205-
206-
}
207-
208178
}

0 commit comments

Comments
 (0)