diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfiguration.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfiguration.java index d01a544136c..345f8d3fbc1 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfiguration.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfiguration.java @@ -53,7 +53,9 @@ import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoCo import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext; +import org.springframework.boot.context.embedded.EmbeddedServletContainer; import org.springframework.boot.context.embedded.EmbeddedServletContainerException; +import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationListener; @@ -195,6 +197,8 @@ public class EndpointWebMvcAutoConfiguration implements ApplicationContextAware, } try { childContext.refresh(); + registerContainer(this.applicationContext, + childContext.getEmbeddedServletContainer()); } catch (RuntimeException e) { // No support currently for deploying a war with management.port=, @@ -207,6 +211,16 @@ public class EndpointWebMvcAutoConfiguration implements ApplicationContextAware, throw e; } } + }; + + private void registerContainer(ApplicationContext applicationContext, + EmbeddedServletContainer embeddedServletContainer) { + if (applicationContext instanceof EmbeddedWebApplicationContext) { + ((EmbeddedWebApplicationContext) applicationContext) + .getEmbeddedServletContainers().put("management", + embeddedServletContainer); + // Maybe unregister it when it shuts down? + } } protected static enum ManagementServerPort { diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/ManagementServerProperties.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/ManagementServerProperties.java index fe9374bc369..c76e385166f 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/ManagementServerProperties.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/ManagementServerProperties.java @@ -126,4 +126,5 @@ public class ManagementServerProperties implements SecurityPrequisite { } return null; } + } diff --git a/spring-boot-samples/spring-boot-sample-actuator/src/main/resources/application.properties b/spring-boot-samples/spring-boot-sample-actuator/src/main/resources/application.properties index 0871f68b6cb..943f5bb6863 100644 --- a/spring-boot-samples/spring-boot-sample-actuator/src/main/resources/application.properties +++ b/spring-boot-samples/spring-boot-sample-actuator/src/main/resources/application.properties @@ -1,8 +1,6 @@ logging.file: /tmp/logs/app.log -management.port: 8080 management.address: 127.0.0.1 endpoints.shutdown.enabled: true -server.port: 8080 server.tomcat.basedir: target/tomcat server.tomcat.access_log_pattern: %h %t "%r" %s %b security.require_ssl: false diff --git a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/EndpointsPropertiesSampleActuatorApplicationTests.java b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/EndpointsPropertiesSampleActuatorApplicationTests.java index cf2ae71f55c..f3aae405aca 100644 --- a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/EndpointsPropertiesSampleActuatorApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/EndpointsPropertiesSampleActuatorApplicationTests.java @@ -20,6 +20,7 @@ import java.util.Map; import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.TestRestTemplate; @@ -40,16 +41,19 @@ import static org.junit.Assert.assertEquals; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = SampleActuatorApplication.class) @WebAppConfiguration -@IntegrationTest +@IntegrationTest("server.port=0") @DirtiesContext @ActiveProfiles("endpoints") public class EndpointsPropertiesSampleActuatorApplicationTests { + @Value("${local.server.port}") + private int port; + @Test public void testCustomErrorPath() throws Exception { @SuppressWarnings("rawtypes") ResponseEntity entity = new TestRestTemplate("user", "password") - .getForEntity("http://localhost:8080/oops", Map.class); + .getForEntity("http://localhost:" + port + "/oops", Map.class); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, entity.getStatusCode()); @SuppressWarnings("unchecked") Map body = entity.getBody(); @@ -60,7 +64,7 @@ public class EndpointsPropertiesSampleActuatorApplicationTests { @Test public void testCustomContextPath() throws Exception { ResponseEntity entity = new TestRestTemplate().getForEntity( - "http://localhost:8080/admin/health", String.class); + "http://localhost:" + port + "/admin/health", String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); String body = entity.getBody(); assertEquals("ok", body); diff --git a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ManagementAddressActuatorApplicationTests.java b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ManagementAddressActuatorApplicationTests.java index bc734fd3cd9..6b3383bbc69 100644 --- a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ManagementAddressActuatorApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ManagementAddressActuatorApplicationTests.java @@ -16,6 +16,8 @@ package sample.actuator; +import static org.junit.Assert.assertEquals; + import java.util.Map; import org.junit.Test; @@ -29,12 +31,9 @@ import org.springframework.boot.test.TestRestTemplate; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.test.annotation.DirtiesContext; -import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; -import static org.junit.Assert.assertEquals; - /** * Integration tests for separate management and main service ports. * @@ -43,18 +42,17 @@ import static org.junit.Assert.assertEquals; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = SampleActuatorApplication.class) @WebAppConfiguration -@IntegrationTest +@IntegrationTest({"server.port=0", "management.port=0", "management.address=127.0.0.1", "management.contextPath:/admin"}) @DirtiesContext -@ActiveProfiles("management-address") public class ManagementAddressActuatorApplicationTests { @Autowired private SecurityProperties security; - @Value("${server.port}") + @Value("${local.server.port}") private int port = 9010; - @Value("${management.port}") + @Value("${local.management.port}") private int managementPort = 9011; @Test diff --git a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ManagementPortSampleActuatorApplicationTests.java b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ManagementPortSampleActuatorApplicationTests.java index 715f8f77e6c..d3d8b86b72e 100644 --- a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ManagementPortSampleActuatorApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ManagementPortSampleActuatorApplicationTests.java @@ -16,6 +16,8 @@ package sample.actuator; +import static org.junit.Assert.assertEquals; + import java.util.Map; import org.junit.Test; @@ -29,12 +31,9 @@ import org.springframework.boot.test.TestRestTemplate; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.test.annotation.DirtiesContext; -import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; -import static org.junit.Assert.assertEquals; - /** * Integration tests for separate management and main service ports. * @@ -43,18 +42,17 @@ import static org.junit.Assert.assertEquals; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = SampleActuatorApplication.class) @WebAppConfiguration -@IntegrationTest +@IntegrationTest({"server.port=0", "management.port=0"}) @DirtiesContext -@ActiveProfiles("management-port") public class ManagementPortSampleActuatorApplicationTests { @Autowired private SecurityProperties security; - @Value("${server.port}") + @Value("${local.server.port}") private int port = 9010; - @Value("${management.port}") + @Value("${local.management.port}") private int managementPort = 9011; @Test diff --git a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/NoManagementSampleActuatorApplicationTests.java b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/NoManagementSampleActuatorApplicationTests.java index 7ad5d0e470f..16d46c9240f 100644 --- a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/NoManagementSampleActuatorApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/NoManagementSampleActuatorApplicationTests.java @@ -16,11 +16,14 @@ package sample.actuator; +import static org.junit.Assert.assertEquals; + import java.util.Map; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.security.SecurityProperties; import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.SpringApplicationConfiguration; @@ -28,12 +31,9 @@ import org.springframework.boot.test.TestRestTemplate; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.test.annotation.DirtiesContext; -import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; -import static org.junit.Assert.assertEquals; - /** * Integration tests for switching off management endpoints. * @@ -42,19 +42,21 @@ import static org.junit.Assert.assertEquals; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = SampleActuatorApplication.class) @WebAppConfiguration -@IntegrationTest +@IntegrationTest({"server.port=0", "management.port=-1"}) @DirtiesContext -@ActiveProfiles("nomanagement") public class NoManagementSampleActuatorApplicationTests { @Autowired private SecurityProperties security; + + @Value("${local.server.port}") + private int port = 0; @Test public void testHome() throws Exception { @SuppressWarnings("rawtypes") ResponseEntity entity = new TestRestTemplate("user", getPassword()) - .getForEntity("http://localhost:8080", Map.class); + .getForEntity("http://localhost:" + port, Map.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); @SuppressWarnings("unchecked") Map body = entity.getBody(); @@ -66,7 +68,7 @@ public class NoManagementSampleActuatorApplicationTests { testHome(); // makes sure some requests have been made @SuppressWarnings("rawtypes") ResponseEntity entity = new TestRestTemplate("user", getPassword()) - .getForEntity("http://localhost:8080/metrics", Map.class); + .getForEntity("http://localhost:" + port + "/metrics", Map.class); assertEquals(HttpStatus.NOT_FOUND, entity.getStatusCode()); } diff --git a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/SampleActuatorApplicationTests.java b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/SampleActuatorApplicationTests.java index 7721c5690eb..fe320bd4709 100644 --- a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/SampleActuatorApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/SampleActuatorApplicationTests.java @@ -23,6 +23,7 @@ import java.util.Map; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.security.SecurityProperties; import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.SpringApplicationConfiguration; @@ -50,18 +51,21 @@ import static org.junit.Assert.assertTrue; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = SampleActuatorApplication.class) @WebAppConfiguration -@IntegrationTest +@IntegrationTest("server.port=0") @DirtiesContext public class SampleActuatorApplicationTests { @Autowired private SecurityProperties security; + @Value("${local.server.port}") + private int port; + @Test public void testHomeIsSecure() throws Exception { @SuppressWarnings("rawtypes") ResponseEntity entity = new TestRestTemplate().getForEntity( - "http://localhost:8080", Map.class); + "http://localhost:" + port, Map.class); assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode()); @SuppressWarnings("unchecked") Map body = entity.getBody(); @@ -74,16 +78,16 @@ public class SampleActuatorApplicationTests { public void testMetricsIsSecure() throws Exception { @SuppressWarnings("rawtypes") ResponseEntity entity = new TestRestTemplate().getForEntity( - "http://localhost:8080/metrics", Map.class); + "http://localhost:" + port + "/metrics", Map.class); + assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode()); + entity = new TestRestTemplate().getForEntity("http://localhost:" + port + "/metrics/", + Map.class); + assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode()); + entity = new TestRestTemplate().getForEntity("http://localhost:" + port + "/metrics/foo", + Map.class); assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode()); entity = new TestRestTemplate().getForEntity( - "http://localhost:8080/metrics/", Map.class); - assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode()); - entity = new TestRestTemplate().getForEntity( - "http://localhost:8080/metrics/foo", Map.class); - assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode()); - entity = new TestRestTemplate().getForEntity( - "http://localhost:8080/metrics.json", Map.class); + "http://localhost:" + port + "/metrics.json", Map.class); assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode()); } @@ -91,7 +95,7 @@ public class SampleActuatorApplicationTests { public void testHome() throws Exception { @SuppressWarnings("rawtypes") ResponseEntity entity = new TestRestTemplate("user", getPassword()) - .getForEntity("http://localhost:8080", Map.class); + .getForEntity("http://localhost:" + port, Map.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); @SuppressWarnings("unchecked") Map body = entity.getBody(); @@ -103,7 +107,7 @@ public class SampleActuatorApplicationTests { testHome(); // makes sure some requests have been made @SuppressWarnings("rawtypes") ResponseEntity entity = new TestRestTemplate("user", getPassword()) - .getForEntity("http://localhost:8080/metrics", Map.class); + .getForEntity("http://localhost:" + port + "/metrics", Map.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); @SuppressWarnings("unchecked") Map body = entity.getBody(); @@ -114,7 +118,7 @@ public class SampleActuatorApplicationTests { public void testEnv() throws Exception { @SuppressWarnings("rawtypes") ResponseEntity entity = new TestRestTemplate("user", getPassword()) - .getForEntity("http://localhost:8080/env", Map.class); + .getForEntity("http://localhost:" + port + "/env", Map.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); @SuppressWarnings("unchecked") Map body = entity.getBody(); @@ -124,7 +128,7 @@ public class SampleActuatorApplicationTests { @Test public void testHealth() throws Exception { ResponseEntity entity = new TestRestTemplate().getForEntity( - "http://localhost:8080/health", String.class); + "http://localhost:" + port + "/health", String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals("ok", entity.getBody()); } @@ -132,7 +136,7 @@ public class SampleActuatorApplicationTests { @Test public void testErrorPage() throws Exception { ResponseEntity entity = new TestRestTemplate("user", getPassword()) - .getForEntity("http://localhost:8080/foo", String.class); + .getForEntity("http://localhost:" + port + "/foo", String.class); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, entity.getStatusCode()); String body = entity.getBody(); assertNotNull(body); @@ -145,7 +149,7 @@ public class SampleActuatorApplicationTests { headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); HttpEntity request = new HttpEntity(headers); ResponseEntity entity = new TestRestTemplate("user", getPassword()) - .exchange("http://localhost:8080/foo", HttpMethod.GET, request, + .exchange("http://localhost:" + port + "/foo", HttpMethod.GET, request, String.class); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, entity.getStatusCode()); String body = entity.getBody(); @@ -156,10 +160,10 @@ public class SampleActuatorApplicationTests { @Test public void testTrace() throws Exception { - new TestRestTemplate().getForEntity("http://localhost:8080/health", String.class); + new TestRestTemplate().getForEntity("http://localhost:" + port + "/health", String.class); @SuppressWarnings("rawtypes") ResponseEntity entity = new TestRestTemplate("user", getPassword()) - .getForEntity("http://localhost:8080/trace", List.class); + .getForEntity("http://localhost:" + port + "/trace", List.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); @SuppressWarnings("unchecked") List> list = entity.getBody(); @@ -174,7 +178,7 @@ public class SampleActuatorApplicationTests { public void testErrorPageDirectAccess() throws Exception { @SuppressWarnings("rawtypes") ResponseEntity entity = new TestRestTemplate().getForEntity( - "http://localhost:8080/error", Map.class); + "http://localhost:" + port + "/error", Map.class); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, entity.getStatusCode()); @SuppressWarnings("unchecked") Map body = entity.getBody(); @@ -186,7 +190,7 @@ public class SampleActuatorApplicationTests { public void testBeans() throws Exception { @SuppressWarnings("rawtypes") ResponseEntity entity = new TestRestTemplate("user", getPassword()) - .getForEntity("http://localhost:8080/beans", List.class); + .getForEntity("http://localhost:" + port + "/beans", List.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(1, entity.getBody().size()); @SuppressWarnings("unchecked") diff --git a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ShutdownSampleActuatorApplicationTests.java b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ShutdownSampleActuatorApplicationTests.java index 2a3532c17d7..b2caa70dc85 100644 --- a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ShutdownSampleActuatorApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ShutdownSampleActuatorApplicationTests.java @@ -21,6 +21,7 @@ import java.util.Map; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.security.SecurityProperties; import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.TestRestTemplate; @@ -42,18 +43,21 @@ import static org.junit.Assert.assertTrue; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = SampleActuatorApplication.class) @WebAppConfiguration -@IntegrationTest +@IntegrationTest("server.port=0") @DirtiesContext public class ShutdownSampleActuatorApplicationTests { @Autowired private SecurityProperties security; + @Value("${local.server.port}") + private int port; + @Test public void testHome() throws Exception { @SuppressWarnings("rawtypes") ResponseEntity entity = new TestRestTemplate("user", getPassword()) - .getForEntity("http://localhost:8080", Map.class); + .getForEntity("http://localhost:" + port, Map.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); @SuppressWarnings("unchecked") Map body = entity.getBody(); @@ -64,7 +68,7 @@ public class ShutdownSampleActuatorApplicationTests { public void testShutdown() throws Exception { @SuppressWarnings("rawtypes") ResponseEntity entity = new TestRestTemplate("user", getPassword()) - .postForEntity("http://localhost:8080/shutdown", null, Map.class); + .postForEntity("http://localhost:" + port + "/shutdown", null, Map.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); @SuppressWarnings("unchecked") Map body = entity.getBody(); diff --git a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/UnsecureManagementSampleActuatorApplicationTests.java b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/UnsecureManagementSampleActuatorApplicationTests.java index ed9fb6561b4..ffd6ef0e788 100644 --- a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/UnsecureManagementSampleActuatorApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/UnsecureManagementSampleActuatorApplicationTests.java @@ -20,6 +20,7 @@ import java.util.Map; import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.TestRestTemplate; import org.springframework.boot.test.SpringApplicationConfiguration; @@ -43,16 +44,19 @@ import static org.junit.Assert.assertTrue; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = SampleActuatorApplication.class) @WebAppConfiguration -@IntegrationTest +@IntegrationTest({"server.port:0", "management.security.enabled:false"}) @DirtiesContext @ActiveProfiles("unsecure-management") public class UnsecureManagementSampleActuatorApplicationTests { + @Value("${local.server.port}") + private int port; + @Test public void testHomeIsSecure() throws Exception { @SuppressWarnings("rawtypes") ResponseEntity entity = new TestRestTemplate().getForEntity( - "http://localhost:8080", Map.class); + "http://localhost:" + port, Map.class); assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode()); @SuppressWarnings("unchecked") Map body = entity.getBody(); @@ -71,7 +75,7 @@ public class UnsecureManagementSampleActuatorApplicationTests { } @SuppressWarnings("rawtypes") ResponseEntity entity = new TestRestTemplate().getForEntity( - "http://localhost:8080/metrics", Map.class); + "http://localhost:" + port + "/metrics", Map.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); @SuppressWarnings("unchecked") Map body = entity.getBody(); diff --git a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/UnsecureSampleActuatorApplicationTests.java b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/UnsecureSampleActuatorApplicationTests.java index 6a401a42bb6..f607e271fbd 100644 --- a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/UnsecureSampleActuatorApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/UnsecureSampleActuatorApplicationTests.java @@ -16,23 +16,23 @@ package sample.actuator; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; + import java.util.Map; import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.IntegrationTest; -import org.springframework.boot.test.TestRestTemplate; import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.boot.test.TestRestTemplate; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.test.annotation.DirtiesContext; -import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; - /** * Integration tests for unsecured service endpoints (even with Spring Security on * classpath). @@ -42,16 +42,17 @@ import static org.junit.Assert.assertFalse; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = SampleActuatorApplication.class) @WebAppConfiguration -@IntegrationTest +@IntegrationTest({"server.port:0", "security.basic.enabled:false"}) @DirtiesContext -@ActiveProfiles("unsecure") public class UnsecureSampleActuatorApplicationTests { + @Value("${local.server.port}") + private int port; @Test public void testHome() throws Exception { @SuppressWarnings("rawtypes") ResponseEntity entity = new TestRestTemplate().getForEntity( - "http://localhost:8080", Map.class); + "http://localhost:" + port, Map.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); @SuppressWarnings("unchecked") Map body = entity.getBody(); diff --git a/spring-boot-samples/spring-boot-sample-actuator/src/test/resources/application-management-address.properties b/spring-boot-samples/spring-boot-sample-actuator/src/test/resources/application-management-address.properties deleted file mode 100644 index 074f16ed18b..00000000000 --- a/spring-boot-samples/spring-boot-sample-actuator/src/test/resources/application-management-address.properties +++ /dev/null @@ -1,4 +0,0 @@ -server.port: 9010 -management.port: 9011 -management.address: 127.0.0.1 -management.contextPath: /admin \ No newline at end of file diff --git a/spring-boot-samples/spring-boot-sample-actuator/src/test/resources/application-management-port.properties b/spring-boot-samples/spring-boot-sample-actuator/src/test/resources/application-management-port.properties deleted file mode 100644 index b04a05c04d6..00000000000 --- a/spring-boot-samples/spring-boot-sample-actuator/src/test/resources/application-management-port.properties +++ /dev/null @@ -1,2 +0,0 @@ -server.port: 9010 -management.port: 9011 \ No newline at end of file diff --git a/spring-boot-samples/spring-boot-sample-actuator/src/test/resources/application-nomanagement.properties b/spring-boot-samples/spring-boot-sample-actuator/src/test/resources/application-nomanagement.properties deleted file mode 100644 index 835785d0c50..00000000000 --- a/spring-boot-samples/spring-boot-sample-actuator/src/test/resources/application-nomanagement.properties +++ /dev/null @@ -1 +0,0 @@ -management.port: -1 \ No newline at end of file diff --git a/spring-boot-samples/spring-boot-sample-actuator/src/test/resources/application-unsecure-management.properties b/spring-boot-samples/spring-boot-sample-actuator/src/test/resources/application-unsecure-management.properties deleted file mode 100644 index 2c4b3e4403f..00000000000 --- a/spring-boot-samples/spring-boot-sample-actuator/src/test/resources/application-unsecure-management.properties +++ /dev/null @@ -1 +0,0 @@ -management.security.enabled: false \ No newline at end of file diff --git a/spring-boot-samples/spring-boot-sample-actuator/src/test/resources/application-unsecure.properties b/spring-boot-samples/spring-boot-sample-actuator/src/test/resources/application-unsecure.properties deleted file mode 100644 index b536e4db28b..00000000000 --- a/spring-boot-samples/spring-boot-sample-actuator/src/test/resources/application-unsecure.properties +++ /dev/null @@ -1 +0,0 @@ -security.basic.enabled: false \ No newline at end of file diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedWebApplicationContext.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedWebApplicationContext.java index dfde2065df4..a7198153c87 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedWebApplicationContext.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedWebApplicationContext.java @@ -21,6 +21,7 @@ import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.EventListener; +import java.util.HashMap; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; @@ -88,6 +89,11 @@ import org.springframework.web.context.support.WebApplicationContextUtils; */ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext { + /** + * + */ + private static final String SERVER = "server"; + /** * Constant value for the DispatcherServlet bean name. A Servlet bean with this name * is deemed to be the "main" servlet and is automatically given a mapping of "/" by @@ -102,6 +108,8 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext private String namespace; + private Map containers = new HashMap(); + /** * Register ServletContextAwareProcessor. * @see ServletContextAwareProcessor @@ -158,6 +166,7 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext EmbeddedServletContainerFactory containerFactory = getEmbeddedServletContainerFactory(); this.embeddedServletContainer = containerFactory .getEmbeddedServletContainer(getSelfInitializer()); + this.containers.put(SERVER, this.embeddedServletContainer); } else if (getServletContext() != null) { try { @@ -382,6 +391,7 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext try { this.embeddedServletContainer.stop(); this.embeddedServletContainer = null; + this.containers.remove(SERVER); } catch (Exception ex) { throw new IllegalStateException(ex); @@ -425,4 +435,15 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext return this.embeddedServletContainer; } + /** + * A registry of embedded containers by name. The + * {@link #getEmbeddedServletContainer() canonical container} is called "server". + * Anyone else who creates one can register it with whatever name they please. + * + * @return the containers + */ + public Map getEmbeddedServletContainers() { + return this.containers; + } + } diff --git a/spring-boot/src/main/java/org/springframework/boot/test/EmbeddedServletContainerListener.java b/spring-boot/src/main/java/org/springframework/boot/test/EmbeddedServletContainerListener.java index f2cce73ce36..d7e378fcd51 100644 --- a/spring-boot/src/main/java/org/springframework/boot/test/EmbeddedServletContainerListener.java +++ b/spring-boot/src/main/java/org/springframework/boot/test/EmbeddedServletContainerListener.java @@ -16,7 +16,10 @@ package org.springframework.boot.test; +import java.util.Map; + import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.context.embedded.EmbeddedServletContainer; import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext; import org.springframework.context.ApplicationContext; import org.springframework.test.context.TestContext; @@ -37,7 +40,12 @@ public class EmbeddedServletContainerListener extends AbstractTestExecutionListe return; } EmbeddedWebApplicationContext embedded = (EmbeddedWebApplicationContext) context; - final int port = embedded.getEmbeddedServletContainer().getPort(); - EnvironmentTestUtils.addEnvironment(embedded, "local.server.port:" + port); + Map containers = embedded + .getEmbeddedServletContainers(); + for (String name : containers.keySet()) { + int port = containers.get(name).getPort(); + EnvironmentTestUtils.addEnvironment(embedded, "local." + name + ".port:" + + port); + } } } diff --git a/spring-boot/src/main/java/org/springframework/boot/test/SpringApplicationContextLoader.java b/spring-boot/src/main/java/org/springframework/boot/test/SpringApplicationContextLoader.java index 2be5d122051..5ad1c3d5fa9 100644 --- a/spring-boot/src/main/java/org/springframework/boot/test/SpringApplicationContextLoader.java +++ b/spring-boot/src/main/java/org/springframework/boot/test/SpringApplicationContextLoader.java @@ -143,6 +143,7 @@ public class SpringApplicationContextLoader extends AbstractContextLoader { if (annotation == null) { // Not running an embedded server, just setting up web context args.put("server.port", "-1"); + args.put("management.port", "-1"); } else { args.putAll(extractProperties(annotation.value()));