Browse Source

Convert Actuator sample to dynamic ports

pull/682/merge
Dave Syer 12 years ago
parent
commit
f134e96053
  1. 14
      spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfiguration.java
  2. 1
      spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/ManagementServerProperties.java
  3. 2
      spring-boot-samples/spring-boot-sample-actuator/src/main/resources/application.properties
  4. 10
      spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/EndpointsPropertiesSampleActuatorApplicationTests.java
  5. 12
      spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ManagementAddressActuatorApplicationTests.java
  6. 12
      spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ManagementPortSampleActuatorApplicationTests.java
  7. 16
      spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/NoManagementSampleActuatorApplicationTests.java
  8. 40
      spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/SampleActuatorApplicationTests.java
  9. 10
      spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ShutdownSampleActuatorApplicationTests.java
  10. 10
      spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/UnsecureManagementSampleActuatorApplicationTests.java
  11. 17
      spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/UnsecureSampleActuatorApplicationTests.java
  12. 4
      spring-boot-samples/spring-boot-sample-actuator/src/test/resources/application-management-address.properties
  13. 2
      spring-boot-samples/spring-boot-sample-actuator/src/test/resources/application-management-port.properties
  14. 1
      spring-boot-samples/spring-boot-sample-actuator/src/test/resources/application-nomanagement.properties
  15. 1
      spring-boot-samples/spring-boot-sample-actuator/src/test/resources/application-unsecure-management.properties
  16. 1
      spring-boot-samples/spring-boot-sample-actuator/src/test/resources/application-unsecure.properties
  17. 21
      spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedWebApplicationContext.java
  18. 12
      spring-boot/src/main/java/org/springframework/boot/test/EmbeddedServletContainerListener.java
  19. 1
      spring-boot/src/main/java/org/springframework/boot/test/SpringApplicationContextLoader.java

14
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.ServerProperties;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext; 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.EmbeddedServletContainerException;
import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
@ -195,6 +197,8 @@ public class EndpointWebMvcAutoConfiguration implements ApplicationContextAware,
} }
try { try {
childContext.refresh(); childContext.refresh();
registerContainer(this.applicationContext,
childContext.getEmbeddedServletContainer());
} }
catch (RuntimeException e) { catch (RuntimeException e) {
// No support currently for deploying a war with management.port=<different>, // No support currently for deploying a war with management.port=<different>,
@ -207,6 +211,16 @@ public class EndpointWebMvcAutoConfiguration implements ApplicationContextAware,
throw e; 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 { protected static enum ManagementServerPort {

1
spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/ManagementServerProperties.java

@ -126,4 +126,5 @@ public class ManagementServerProperties implements SecurityPrequisite {
} }
return null; return null;
} }
} }

2
spring-boot-samples/spring-boot-sample-actuator/src/main/resources/application.properties

@ -1,8 +1,6 @@
logging.file: /tmp/logs/app.log logging.file: /tmp/logs/app.log
management.port: 8080
management.address: 127.0.0.1 management.address: 127.0.0.1
endpoints.shutdown.enabled: true endpoints.shutdown.enabled: true
server.port: 8080
server.tomcat.basedir: target/tomcat server.tomcat.basedir: target/tomcat
server.tomcat.access_log_pattern: %h %t "%r" %s %b server.tomcat.access_log_pattern: %h %t "%r" %s %b
security.require_ssl: false security.require_ssl: false

10
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.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.TestRestTemplate; import org.springframework.boot.test.TestRestTemplate;
@ -40,16 +41,19 @@ import static org.junit.Assert.assertEquals;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SampleActuatorApplication.class) @SpringApplicationConfiguration(classes = SampleActuatorApplication.class)
@WebAppConfiguration @WebAppConfiguration
@IntegrationTest @IntegrationTest("server.port=0")
@DirtiesContext @DirtiesContext
@ActiveProfiles("endpoints") @ActiveProfiles("endpoints")
public class EndpointsPropertiesSampleActuatorApplicationTests { public class EndpointsPropertiesSampleActuatorApplicationTests {
@Value("${local.server.port}")
private int port;
@Test @Test
public void testCustomErrorPath() throws Exception { public void testCustomErrorPath() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate("user", "password") ResponseEntity<Map> 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()); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, entity.getStatusCode());
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody(); Map<String, Object> body = entity.getBody();
@ -60,7 +64,7 @@ public class EndpointsPropertiesSampleActuatorApplicationTests {
@Test @Test
public void testCustomContextPath() throws Exception { public void testCustomContextPath() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate().getForEntity( ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
"http://localhost:8080/admin/health", String.class); "http://localhost:" + port + "/admin/health", String.class);
assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(HttpStatus.OK, entity.getStatusCode());
String body = entity.getBody(); String body = entity.getBody();
assertEquals("ok", body); assertEquals("ok", body);

12
spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ManagementAddressActuatorApplicationTests.java

@ -16,6 +16,8 @@
package sample.actuator; package sample.actuator;
import static org.junit.Assert.assertEquals;
import java.util.Map; import java.util.Map;
import org.junit.Test; import org.junit.Test;
@ -29,12 +31,9 @@ import org.springframework.boot.test.TestRestTemplate;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.context.web.WebAppConfiguration;
import static org.junit.Assert.assertEquals;
/** /**
* Integration tests for separate management and main service ports. * Integration tests for separate management and main service ports.
* *
@ -43,18 +42,17 @@ import static org.junit.Assert.assertEquals;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SampleActuatorApplication.class) @SpringApplicationConfiguration(classes = SampleActuatorApplication.class)
@WebAppConfiguration @WebAppConfiguration
@IntegrationTest @IntegrationTest({"server.port=0", "management.port=0", "management.address=127.0.0.1", "management.contextPath:/admin"})
@DirtiesContext @DirtiesContext
@ActiveProfiles("management-address")
public class ManagementAddressActuatorApplicationTests { public class ManagementAddressActuatorApplicationTests {
@Autowired @Autowired
private SecurityProperties security; private SecurityProperties security;
@Value("${server.port}") @Value("${local.server.port}")
private int port = 9010; private int port = 9010;
@Value("${management.port}") @Value("${local.management.port}")
private int managementPort = 9011; private int managementPort = 9011;
@Test @Test

12
spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ManagementPortSampleActuatorApplicationTests.java

@ -16,6 +16,8 @@
package sample.actuator; package sample.actuator;
import static org.junit.Assert.assertEquals;
import java.util.Map; import java.util.Map;
import org.junit.Test; import org.junit.Test;
@ -29,12 +31,9 @@ import org.springframework.boot.test.TestRestTemplate;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.context.web.WebAppConfiguration;
import static org.junit.Assert.assertEquals;
/** /**
* Integration tests for separate management and main service ports. * Integration tests for separate management and main service ports.
* *
@ -43,18 +42,17 @@ import static org.junit.Assert.assertEquals;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SampleActuatorApplication.class) @SpringApplicationConfiguration(classes = SampleActuatorApplication.class)
@WebAppConfiguration @WebAppConfiguration
@IntegrationTest @IntegrationTest({"server.port=0", "management.port=0"})
@DirtiesContext @DirtiesContext
@ActiveProfiles("management-port")
public class ManagementPortSampleActuatorApplicationTests { public class ManagementPortSampleActuatorApplicationTests {
@Autowired @Autowired
private SecurityProperties security; private SecurityProperties security;
@Value("${server.port}") @Value("${local.server.port}")
private int port = 9010; private int port = 9010;
@Value("${management.port}") @Value("${local.management.port}")
private int managementPort = 9011; private int managementPort = 9011;
@Test @Test

16
spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/NoManagementSampleActuatorApplicationTests.java

@ -16,11 +16,14 @@
package sample.actuator; package sample.actuator;
import static org.junit.Assert.assertEquals;
import java.util.Map; import java.util.Map;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; 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.autoconfigure.security.SecurityProperties;
import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.SpringApplicationConfiguration; 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.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.context.web.WebAppConfiguration;
import static org.junit.Assert.assertEquals;
/** /**
* Integration tests for switching off management endpoints. * Integration tests for switching off management endpoints.
* *
@ -42,19 +42,21 @@ import static org.junit.Assert.assertEquals;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SampleActuatorApplication.class) @SpringApplicationConfiguration(classes = SampleActuatorApplication.class)
@WebAppConfiguration @WebAppConfiguration
@IntegrationTest @IntegrationTest({"server.port=0", "management.port=-1"})
@DirtiesContext @DirtiesContext
@ActiveProfiles("nomanagement")
public class NoManagementSampleActuatorApplicationTests { public class NoManagementSampleActuatorApplicationTests {
@Autowired @Autowired
private SecurityProperties security; private SecurityProperties security;
@Value("${local.server.port}")
private int port = 0;
@Test @Test
public void testHome() throws Exception { public void testHome() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword()) ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword())
.getForEntity("http://localhost:8080", Map.class); .getForEntity("http://localhost:" + port, Map.class);
assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(HttpStatus.OK, entity.getStatusCode());
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody(); Map<String, Object> body = entity.getBody();
@ -66,7 +68,7 @@ public class NoManagementSampleActuatorApplicationTests {
testHome(); // makes sure some requests have been made testHome(); // makes sure some requests have been made
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword()) ResponseEntity<Map> 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()); assertEquals(HttpStatus.NOT_FOUND, entity.getStatusCode());
} }

40
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.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; 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.autoconfigure.security.SecurityProperties;
import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.SpringApplicationConfiguration;
@ -50,18 +51,21 @@ import static org.junit.Assert.assertTrue;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SampleActuatorApplication.class) @SpringApplicationConfiguration(classes = SampleActuatorApplication.class)
@WebAppConfiguration @WebAppConfiguration
@IntegrationTest @IntegrationTest("server.port=0")
@DirtiesContext @DirtiesContext
public class SampleActuatorApplicationTests { public class SampleActuatorApplicationTests {
@Autowired @Autowired
private SecurityProperties security; private SecurityProperties security;
@Value("${local.server.port}")
private int port;
@Test @Test
public void testHomeIsSecure() throws Exception { public void testHomeIsSecure() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate().getForEntity( ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(
"http://localhost:8080", Map.class); "http://localhost:" + port, Map.class);
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode()); assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody(); Map<String, Object> body = entity.getBody();
@ -74,16 +78,16 @@ public class SampleActuatorApplicationTests {
public void testMetricsIsSecure() throws Exception { public void testMetricsIsSecure() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate().getForEntity( ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(
"http://localhost:8080/metrics", Map.class); "http://localhost:" + port + "/metrics", Map.class);
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode()); assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
entity = new TestRestTemplate().getForEntity( entity = new TestRestTemplate().getForEntity("http://localhost:" + port + "/metrics/",
"http://localhost:8080/metrics/", Map.class); Map.class);
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode()); assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
entity = new TestRestTemplate().getForEntity( entity = new TestRestTemplate().getForEntity("http://localhost:" + port + "/metrics/foo",
"http://localhost:8080/metrics/foo", Map.class); Map.class);
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode()); assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
entity = new TestRestTemplate().getForEntity( entity = new TestRestTemplate().getForEntity(
"http://localhost:8080/metrics.json", Map.class); "http://localhost:" + port + "/metrics.json", Map.class);
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode()); assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
} }
@ -91,7 +95,7 @@ public class SampleActuatorApplicationTests {
public void testHome() throws Exception { public void testHome() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword()) ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword())
.getForEntity("http://localhost:8080", Map.class); .getForEntity("http://localhost:" + port, Map.class);
assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(HttpStatus.OK, entity.getStatusCode());
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody(); Map<String, Object> body = entity.getBody();
@ -103,7 +107,7 @@ public class SampleActuatorApplicationTests {
testHome(); // makes sure some requests have been made testHome(); // makes sure some requests have been made
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword()) ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword())
.getForEntity("http://localhost:8080/metrics", Map.class); .getForEntity("http://localhost:" + port + "/metrics", Map.class);
assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(HttpStatus.OK, entity.getStatusCode());
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody(); Map<String, Object> body = entity.getBody();
@ -114,7 +118,7 @@ public class SampleActuatorApplicationTests {
public void testEnv() throws Exception { public void testEnv() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword()) ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword())
.getForEntity("http://localhost:8080/env", Map.class); .getForEntity("http://localhost:" + port + "/env", Map.class);
assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(HttpStatus.OK, entity.getStatusCode());
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody(); Map<String, Object> body = entity.getBody();
@ -124,7 +128,7 @@ public class SampleActuatorApplicationTests {
@Test @Test
public void testHealth() throws Exception { public void testHealth() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate().getForEntity( ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
"http://localhost:8080/health", String.class); "http://localhost:" + port + "/health", String.class);
assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(HttpStatus.OK, entity.getStatusCode());
assertEquals("ok", entity.getBody()); assertEquals("ok", entity.getBody());
} }
@ -132,7 +136,7 @@ public class SampleActuatorApplicationTests {
@Test @Test
public void testErrorPage() throws Exception { public void testErrorPage() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate("user", getPassword()) ResponseEntity<String> 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()); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, entity.getStatusCode());
String body = entity.getBody(); String body = entity.getBody();
assertNotNull(body); assertNotNull(body);
@ -145,7 +149,7 @@ public class SampleActuatorApplicationTests {
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
HttpEntity<?> request = new HttpEntity<Void>(headers); HttpEntity<?> request = new HttpEntity<Void>(headers);
ResponseEntity<String> entity = new TestRestTemplate("user", getPassword()) ResponseEntity<String> entity = new TestRestTemplate("user", getPassword())
.exchange("http://localhost:8080/foo", HttpMethod.GET, request, .exchange("http://localhost:" + port + "/foo", HttpMethod.GET, request,
String.class); String.class);
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, entity.getStatusCode()); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, entity.getStatusCode());
String body = entity.getBody(); String body = entity.getBody();
@ -156,10 +160,10 @@ public class SampleActuatorApplicationTests {
@Test @Test
public void testTrace() throws Exception { 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") @SuppressWarnings("rawtypes")
ResponseEntity<List> entity = new TestRestTemplate("user", getPassword()) ResponseEntity<List> entity = new TestRestTemplate("user", getPassword())
.getForEntity("http://localhost:8080/trace", List.class); .getForEntity("http://localhost:" + port + "/trace", List.class);
assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(HttpStatus.OK, entity.getStatusCode());
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
List<Map<String, Object>> list = entity.getBody(); List<Map<String, Object>> list = entity.getBody();
@ -174,7 +178,7 @@ public class SampleActuatorApplicationTests {
public void testErrorPageDirectAccess() throws Exception { public void testErrorPageDirectAccess() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate().getForEntity( ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(
"http://localhost:8080/error", Map.class); "http://localhost:" + port + "/error", Map.class);
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, entity.getStatusCode()); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, entity.getStatusCode());
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody(); Map<String, Object> body = entity.getBody();
@ -186,7 +190,7 @@ public class SampleActuatorApplicationTests {
public void testBeans() throws Exception { public void testBeans() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<List> entity = new TestRestTemplate("user", getPassword()) ResponseEntity<List> 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(HttpStatus.OK, entity.getStatusCode());
assertEquals(1, entity.getBody().size()); assertEquals(1, entity.getBody().size());
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")

10
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.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; 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.autoconfigure.security.SecurityProperties;
import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.TestRestTemplate; import org.springframework.boot.test.TestRestTemplate;
@ -42,18 +43,21 @@ import static org.junit.Assert.assertTrue;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SampleActuatorApplication.class) @SpringApplicationConfiguration(classes = SampleActuatorApplication.class)
@WebAppConfiguration @WebAppConfiguration
@IntegrationTest @IntegrationTest("server.port=0")
@DirtiesContext @DirtiesContext
public class ShutdownSampleActuatorApplicationTests { public class ShutdownSampleActuatorApplicationTests {
@Autowired @Autowired
private SecurityProperties security; private SecurityProperties security;
@Value("${local.server.port}")
private int port;
@Test @Test
public void testHome() throws Exception { public void testHome() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword()) ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword())
.getForEntity("http://localhost:8080", Map.class); .getForEntity("http://localhost:" + port, Map.class);
assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(HttpStatus.OK, entity.getStatusCode());
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody(); Map<String, Object> body = entity.getBody();
@ -64,7 +68,7 @@ public class ShutdownSampleActuatorApplicationTests {
public void testShutdown() throws Exception { public void testShutdown() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword()) ResponseEntity<Map> 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()); assertEquals(HttpStatus.OK, entity.getStatusCode());
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody(); Map<String, Object> body = entity.getBody();

10
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.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.TestRestTemplate; import org.springframework.boot.test.TestRestTemplate;
import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.SpringApplicationConfiguration;
@ -43,16 +44,19 @@ import static org.junit.Assert.assertTrue;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SampleActuatorApplication.class) @SpringApplicationConfiguration(classes = SampleActuatorApplication.class)
@WebAppConfiguration @WebAppConfiguration
@IntegrationTest @IntegrationTest({"server.port:0", "management.security.enabled:false"})
@DirtiesContext @DirtiesContext
@ActiveProfiles("unsecure-management") @ActiveProfiles("unsecure-management")
public class UnsecureManagementSampleActuatorApplicationTests { public class UnsecureManagementSampleActuatorApplicationTests {
@Value("${local.server.port}")
private int port;
@Test @Test
public void testHomeIsSecure() throws Exception { public void testHomeIsSecure() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate().getForEntity( ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(
"http://localhost:8080", Map.class); "http://localhost:" + port, Map.class);
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode()); assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody(); Map<String, Object> body = entity.getBody();
@ -71,7 +75,7 @@ public class UnsecureManagementSampleActuatorApplicationTests {
} }
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate().getForEntity( ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(
"http://localhost:8080/metrics", Map.class); "http://localhost:" + port + "/metrics", Map.class);
assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(HttpStatus.OK, entity.getStatusCode());
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody(); Map<String, Object> body = entity.getBody();

17
spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/UnsecureSampleActuatorApplicationTests.java

@ -16,23 +16,23 @@
package sample.actuator; package sample.actuator;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import java.util.Map; import java.util.Map;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.TestRestTemplate;
import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.TestRestTemplate;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration; 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 * Integration tests for unsecured service endpoints (even with Spring Security on
* classpath). * classpath).
@ -42,16 +42,17 @@ import static org.junit.Assert.assertFalse;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SampleActuatorApplication.class) @SpringApplicationConfiguration(classes = SampleActuatorApplication.class)
@WebAppConfiguration @WebAppConfiguration
@IntegrationTest @IntegrationTest({"server.port:0", "security.basic.enabled:false"})
@DirtiesContext @DirtiesContext
@ActiveProfiles("unsecure")
public class UnsecureSampleActuatorApplicationTests { public class UnsecureSampleActuatorApplicationTests {
@Value("${local.server.port}")
private int port;
@Test @Test
public void testHome() throws Exception { public void testHome() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate().getForEntity( ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(
"http://localhost:8080", Map.class); "http://localhost:" + port, Map.class);
assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(HttpStatus.OK, entity.getStatusCode());
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody(); Map<String, Object> body = entity.getBody();

4
spring-boot-samples/spring-boot-sample-actuator/src/test/resources/application-management-address.properties

@ -1,4 +0,0 @@
server.port: 9010
management.port: 9011
management.address: 127.0.0.1
management.contextPath: /admin

2
spring-boot-samples/spring-boot-sample-actuator/src/test/resources/application-management-port.properties

@ -1,2 +0,0 @@
server.port: 9010
management.port: 9011

1
spring-boot-samples/spring-boot-sample-actuator/src/test/resources/application-nomanagement.properties

@ -1 +0,0 @@
management.port: -1

1
spring-boot-samples/spring-boot-sample-actuator/src/test/resources/application-unsecure-management.properties

@ -1 +0,0 @@
management.security.enabled: false

1
spring-boot-samples/spring-boot-sample-actuator/src/test/resources/application-unsecure.properties

@ -1 +0,0 @@
security.basic.enabled: false

21
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.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.EventListener; import java.util.EventListener;
import java.util.HashMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
@ -88,6 +89,11 @@ import org.springframework.web.context.support.WebApplicationContextUtils;
*/ */
public class EmbeddedWebApplicationContext extends GenericWebApplicationContext { public class EmbeddedWebApplicationContext extends GenericWebApplicationContext {
/**
*
*/
private static final String SERVER = "server";
/** /**
* Constant value for the DispatcherServlet bean name. A Servlet bean with this name * 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 * 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 String namespace;
private Map<String, EmbeddedServletContainer> containers = new HashMap<String, EmbeddedServletContainer>();
/** /**
* Register ServletContextAwareProcessor. * Register ServletContextAwareProcessor.
* @see ServletContextAwareProcessor * @see ServletContextAwareProcessor
@ -158,6 +166,7 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext
EmbeddedServletContainerFactory containerFactory = getEmbeddedServletContainerFactory(); EmbeddedServletContainerFactory containerFactory = getEmbeddedServletContainerFactory();
this.embeddedServletContainer = containerFactory this.embeddedServletContainer = containerFactory
.getEmbeddedServletContainer(getSelfInitializer()); .getEmbeddedServletContainer(getSelfInitializer());
this.containers.put(SERVER, this.embeddedServletContainer);
} }
else if (getServletContext() != null) { else if (getServletContext() != null) {
try { try {
@ -382,6 +391,7 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext
try { try {
this.embeddedServletContainer.stop(); this.embeddedServletContainer.stop();
this.embeddedServletContainer = null; this.embeddedServletContainer = null;
this.containers.remove(SERVER);
} }
catch (Exception ex) { catch (Exception ex) {
throw new IllegalStateException(ex); throw new IllegalStateException(ex);
@ -425,4 +435,15 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext
return this.embeddedServletContainer; 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<String, EmbeddedServletContainer> getEmbeddedServletContainers() {
return this.containers;
}
} }

12
spring-boot/src/main/java/org/springframework/boot/test/EmbeddedServletContainerListener.java

@ -16,7 +16,10 @@
package org.springframework.boot.test; package org.springframework.boot.test;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.embedded.EmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext; import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.test.context.TestContext; import org.springframework.test.context.TestContext;
@ -37,7 +40,12 @@ public class EmbeddedServletContainerListener extends AbstractTestExecutionListe
return; return;
} }
EmbeddedWebApplicationContext embedded = (EmbeddedWebApplicationContext) context; EmbeddedWebApplicationContext embedded = (EmbeddedWebApplicationContext) context;
final int port = embedded.getEmbeddedServletContainer().getPort(); Map<String, EmbeddedServletContainer> containers = embedded
EnvironmentTestUtils.addEnvironment(embedded, "local.server.port:" + port); .getEmbeddedServletContainers();
for (String name : containers.keySet()) {
int port = containers.get(name).getPort();
EnvironmentTestUtils.addEnvironment(embedded, "local." + name + ".port:"
+ port);
}
} }
} }

1
spring-boot/src/main/java/org/springframework/boot/test/SpringApplicationContextLoader.java

@ -143,6 +143,7 @@ public class SpringApplicationContextLoader extends AbstractContextLoader {
if (annotation == null) { if (annotation == null) {
// Not running an embedded server, just setting up web context // Not running an embedded server, just setting up web context
args.put("server.port", "-1"); args.put("server.port", "-1");
args.put("management.port", "-1");
} }
else { else {
args.putAll(extractProperties(annotation.value())); args.putAll(extractProperties(annotation.value()));

Loading…
Cancel
Save