Skip to content

Commit 4ec95b8

Browse files
committed
Ensure that JolokiaMvcEndpoint destroys underlying controller
Closes gh-7606
1 parent b80ad2a commit 4ec95b8

File tree

3 files changed

+142
-77
lines changed

3 files changed

+142
-77
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.jolokia.http.AgentServlet;
2727

2828
import org.springframework.beans.BeansException;
29+
import org.springframework.beans.factory.DisposableBean;
2930
import org.springframework.beans.factory.InitializingBean;
3031
import org.springframework.boot.context.properties.ConfigurationProperties;
3132
import org.springframework.context.ApplicationContext;
@@ -44,8 +45,9 @@
4445
*/
4546
@ConfigurationProperties(prefix = "endpoints.jolokia", ignoreUnknownFields = false)
4647
@HypermediaDisabled
47-
public class JolokiaMvcEndpoint extends AbstractMvcEndpoint
48-
implements InitializingBean, ApplicationContextAware, ServletContextAware {
48+
public class JolokiaMvcEndpoint extends AbstractMvcEndpoint implements InitializingBean,
49+
ApplicationContextAware, ServletContextAware, DisposableBean {
50+
4951
private final ServletWrappingController controller = new ServletWrappingController();
5052

5153
public JolokiaMvcEndpoint() {
@@ -74,6 +76,11 @@ public final void setApplicationContext(ApplicationContext context)
7476
this.controller.setApplicationContext(context);
7577
}
7678

79+
@Override
80+
public void destroy() {
81+
this.controller.destroy();
82+
}
83+
7784
@RequestMapping("/**")
7885
public ModelAndView handle(HttpServletRequest request, HttpServletResponse response)
7986
throws Exception {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* Copyright 2013-2016 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.Set;
20+
21+
import org.junit.Before;
22+
import org.junit.Test;
23+
import org.junit.runner.RunWith;
24+
25+
import org.springframework.beans.factory.annotation.Autowired;
26+
import org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfiguration;
27+
import org.springframework.boot.actuate.autoconfigure.JolokiaAutoConfiguration;
28+
import org.springframework.boot.actuate.autoconfigure.ManagementServerPropertiesAutoConfiguration;
29+
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
30+
import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration;
31+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
32+
import org.springframework.boot.test.context.SpringBootTest;
33+
import org.springframework.boot.test.util.EnvironmentTestUtils;
34+
import org.springframework.context.ConfigurableApplicationContext;
35+
import org.springframework.context.annotation.Configuration;
36+
import org.springframework.context.annotation.Import;
37+
import org.springframework.test.context.junit4.SpringRunner;
38+
import org.springframework.test.web.servlet.MockMvc;
39+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
40+
import org.springframework.web.context.WebApplicationContext;
41+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
42+
43+
import static org.assertj.core.api.Assertions.assertThat;
44+
import static org.hamcrest.Matchers.containsString;
45+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
46+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
47+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
48+
49+
/**
50+
* Integration tests for {@link JolokiaMvcEndpoint}
51+
*
52+
* @author Christian Dupuis
53+
* @author Dave Syer
54+
*/
55+
@RunWith(SpringRunner.class)
56+
@SpringBootTest
57+
public class JolokiaMvcEndpointIntegrationTests {
58+
59+
@Autowired
60+
private MvcEndpoints endpoints;
61+
62+
@Autowired
63+
private WebApplicationContext context;
64+
65+
private MockMvc mvc;
66+
67+
@Before
68+
public void setUp() {
69+
this.mvc = MockMvcBuilders.webAppContextSetup(this.context).build();
70+
EnvironmentTestUtils.addEnvironment((ConfigurableApplicationContext) this.context,
71+
"foo:bar");
72+
}
73+
74+
@Test
75+
public void endpointRegistered() throws Exception {
76+
Set<? extends MvcEndpoint> values = this.endpoints.getEndpoints();
77+
assertThat(values).hasAtLeastOneElementOfType(JolokiaMvcEndpoint.class);
78+
}
79+
80+
@Test
81+
public void search() throws Exception {
82+
this.mvc.perform(get("/jolokia/search/java.lang:*")).andExpect(status().isOk())
83+
.andExpect(content().string(containsString("GarbageCollector")));
84+
}
85+
86+
@Test
87+
public void read() throws Exception {
88+
this.mvc.perform(get("/jolokia/read/java.lang:type=Memory"))
89+
.andExpect(status().isOk())
90+
.andExpect(content().string(containsString("NonHeapMemoryUsage")));
91+
}
92+
93+
@Test
94+
public void list() throws Exception {
95+
this.mvc.perform(get("/jolokia/list/java.lang/type=Memory/attr"))
96+
.andExpect(status().isOk())
97+
.andExpect(content().string(containsString("NonHeapMemoryUsage")));
98+
}
99+
100+
@Configuration
101+
@EnableConfigurationProperties
102+
@EnableWebMvc
103+
@Import({ JacksonAutoConfiguration.class,
104+
HttpMessageConvertersAutoConfiguration.class,
105+
EndpointWebMvcAutoConfiguration.class, JolokiaAutoConfiguration.class,
106+
ManagementServerPropertiesAutoConfiguration.class })
107+
public static class Config {
108+
109+
}
110+
111+
}
Lines changed: 22 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2016 the original author or authors.
2+
* Copyright 2012-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,98 +16,45 @@
1616

1717
package org.springframework.boot.actuate.endpoint.mvc;
1818

19-
import java.util.Set;
20-
2119
import org.junit.Before;
2220
import org.junit.Test;
23-
import org.junit.runner.RunWith;
2421

25-
import org.springframework.beans.factory.annotation.Autowired;
26-
import org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfiguration;
27-
import org.springframework.boot.actuate.autoconfigure.JolokiaAutoConfiguration;
28-
import org.springframework.boot.actuate.autoconfigure.ManagementServerPropertiesAutoConfiguration;
29-
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
30-
import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration;
31-
import org.springframework.boot.context.properties.EnableConfigurationProperties;
32-
import org.springframework.boot.test.context.SpringBootTest;
33-
import org.springframework.boot.test.util.EnvironmentTestUtils;
34-
import org.springframework.context.ConfigurableApplicationContext;
35-
import org.springframework.context.annotation.Configuration;
36-
import org.springframework.context.annotation.Import;
37-
import org.springframework.test.annotation.DirtiesContext;
38-
import org.springframework.test.context.junit4.SpringRunner;
39-
import org.springframework.test.web.servlet.MockMvc;
40-
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
22+
import org.springframework.beans.factory.DisposableBean;
23+
import org.springframework.mock.web.MockServletContext;
24+
import org.springframework.test.util.ReflectionTestUtils;
4125
import org.springframework.web.context.WebApplicationContext;
42-
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
26+
import org.springframework.web.servlet.mvc.ServletWrappingController;
4327

4428
import static org.assertj.core.api.Assertions.assertThat;
45-
import static org.hamcrest.Matchers.containsString;
46-
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
47-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
48-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
29+
import static org.mockito.Mockito.mock;
30+
import static org.mockito.Mockito.spy;
31+
import static org.mockito.Mockito.verify;
4932

5033
/**
51-
* Tests for {@link JolokiaMvcEndpoint}
34+
* Tests for {@link JolokiaMvcEndpoint}.
5235
*
53-
* @author Christian Dupuis
54-
* @author Dave Syer
36+
* @author Andy Wilkinson
5537
*/
56-
@RunWith(SpringRunner.class)
57-
@DirtiesContext
58-
@SpringBootTest
5938
public class JolokiaMvcEndpointTests {
6039

61-
@Autowired
62-
private MvcEndpoints endpoints;
63-
64-
@Autowired
65-
private WebApplicationContext context;
40+
private final JolokiaMvcEndpoint endpoint = new JolokiaMvcEndpoint();
6641

67-
private MockMvc mvc;
42+
private final ServletWrappingController controller = (ServletWrappingController) spy(
43+
ReflectionTestUtils.getField(this.endpoint, "controller"));
6844

6945
@Before
70-
public void setUp() {
71-
this.mvc = MockMvcBuilders.webAppContextSetup(this.context).build();
72-
EnvironmentTestUtils.addEnvironment((ConfigurableApplicationContext) this.context,
73-
"foo:bar");
74-
}
75-
76-
@Test
77-
public void endpointRegistered() throws Exception {
78-
Set<? extends MvcEndpoint> values = this.endpoints.getEndpoints();
79-
assertThat(values).hasAtLeastOneElementOfType(JolokiaMvcEndpoint.class);
80-
}
81-
82-
@Test
83-
public void search() throws Exception {
84-
this.mvc.perform(get("/jolokia/search/java.lang:*")).andExpect(status().isOk())
85-
.andExpect(content().string(containsString("GarbageCollector")));
86-
}
87-
88-
@Test
89-
public void read() throws Exception {
90-
this.mvc.perform(get("/jolokia/read/java.lang:type=Memory"))
91-
.andExpect(status().isOk())
92-
.andExpect(content().string(containsString("NonHeapMemoryUsage")));
46+
public void before() {
47+
ReflectionTestUtils.setField(this.endpoint, "controller", this.controller);
9348
}
9449

9550
@Test
96-
public void list() throws Exception {
97-
this.mvc.perform(get("/jolokia/list/java.lang/type=Memory/attr"))
98-
.andExpect(status().isOk())
99-
.andExpect(content().string(containsString("NonHeapMemoryUsage")));
100-
}
101-
102-
@Configuration
103-
@EnableConfigurationProperties
104-
@EnableWebMvc
105-
@Import({ JacksonAutoConfiguration.class,
106-
HttpMessageConvertersAutoConfiguration.class,
107-
EndpointWebMvcAutoConfiguration.class, JolokiaAutoConfiguration.class,
108-
ManagementServerPropertiesAutoConfiguration.class })
109-
public static class Config {
110-
51+
public void controllerIsDestroyed() throws Exception {
52+
this.endpoint.setApplicationContext(mock(WebApplicationContext.class));
53+
this.endpoint.setServletContext(new MockServletContext());
54+
this.endpoint.afterPropertiesSet();
55+
this.endpoint.destroy();
56+
assertThat(this.endpoint).isInstanceOf(DisposableBean.class);
57+
verify(this.controller).destroy();
11158
}
11259

11360
}

0 commit comments

Comments
 (0)