diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/ManagementServerPropertiesAutoConfiguration.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/ManagementServerPropertiesAutoConfiguration.java index 18a3571e858..71a7c75c14b 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/ManagementServerPropertiesAutoConfiguration.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/ManagementServerPropertiesAutoConfiguration.java @@ -21,6 +21,7 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.security.SecurityProperties; +import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; @@ -51,4 +52,11 @@ public class ManagementServerPropertiesAutoConfiguration { return new SecurityProperties(); } + // In case server auto configuration hasn't been included + @Bean + @ConditionalOnMissingBean + public ServerProperties serverProperties() { + return new ServerProperties(); + } + } diff --git a/spring-boot-docs/src/main/asciidoc/howto.adoc b/spring-boot-docs/src/main/asciidoc/howto.adoc index 0fb48a6016c..a8ffc4ade56 100644 --- a/spring-boot-docs/src/main/asciidoc/howto.adoc +++ b/spring-boot-docs/src/main/asciidoc/howto.adoc @@ -364,30 +364,23 @@ that and be sure that it has initialized is to add a `@Bean` of type `ApplicationListener` and pull the container out of the event when it is published. -A really useful thing to do in is to autowire the `EmbeddedWebApplicationContext` into a -test case and use it to discover the port that the app is running on. In that way you can -use a test profile that chooses a random port (`server.port=0`) and make your test suite -independent of its environment. Example: +A really useful thing to do in is to use `@IntegrationTest` to set `server.port=0` +and then inject the actual ("local") port as a `@Value`. Example: [source,java,indent=0,subs="verbatim,quotes,attributes"] ---- @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = SampleDataJpaApplication.class) @WebApplication - @IntegrationTest - @ActiveProfiles("test") + @IntegrationTest("server.port:0") public class CityRepositoryIntegrationTests { @Autowired EmbeddedWebApplicationContext server; - + + @Value("${local.server.port}") int port; - @Before - public void init() { - port = server.getEmbeddedServletContainer().getPort(); - } - // ... } diff --git a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index 8a3dc693352..71d782276b3 100644 --- a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -199,6 +199,7 @@ sensible overriding of values, properties are considered in the the following or . Command line arguments. . Java System properties (`System.getProperties()`). . OS environment variables. +. A `RandomValueProperySource` that only has properties in `random.*`. . `@PropertySource` annotations on your `@Configuration` classes. . Application properties outside of your packaged jar (`application.properties` including YAML and profile variants). @@ -230,8 +231,25 @@ default `name`. When running in production, an `application.properties` can be p outside of your jar that overrides `name`; and for one-off testing, you can launch with a specific command line switch (e.g. `java -jar app.jar --name="Spring"`). +The `RandomValueProperySource` is useful for injecting random values +(e.g. into secrets or test cases). It can produce integers, longs or +strings, e.g. +[source,properties,indent=0] +---- +my.secret=${random.value} +my.number=${random.int} +my.bignumber=${random.long} +my.number.less.than.ten=${random.int(10)} +my.number.in.range=${random.int[1024,65536]} +---- + +The `random.int*` syntax is `OPEN value (,max) CLOSE` where the +`OPEN,CLOSE` are any character and `value,max` are integers. If +`max` is provided then `value` is the minimum value and `max` is the +maximum (exclusive). + [[boot-features-external-config-command-line-args]] === Accessing command line properties By default `SpringApplication` will convert any command line option arguments (starting @@ -1538,6 +1556,10 @@ interaction. For Example: } ---- +To change the port you can add environment properties to +`@IntegrationTest` as colon- or equals-separated name-value pairs, +e.g. `@IntegrationTest("server.port:9000")`. + [[boot-features-test-utilities]] === Test utilities A few test utility classes are packaged as part of `spring-boot` that are generally diff --git a/spring-boot-samples/spring-boot-sample-actuator-log4j/src/test/java/sample/actuator/log4j/SampleActuatorApplicationTests.java b/spring-boot-samples/spring-boot-sample-actuator-log4j/src/test/java/sample/actuator/log4j/SampleActuatorApplicationTests.java index a7ec5dcc2fe..13bc45706d8 100644 --- a/spring-boot-samples/spring-boot-sample-actuator-log4j/src/test/java/sample/actuator/log4j/SampleActuatorApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-actuator-log4j/src/test/java/sample/actuator/log4j/SampleActuatorApplicationTests.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; @@ -39,15 +40,18 @@ import static org.junit.Assert.assertEquals; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = SampleActuatorApplication.class) @WebAppConfiguration -@IntegrationTest +@IntegrationTest("server.port=0") @DirtiesContext public class SampleActuatorApplicationTests { + @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-ui/src/test/java/sample/actuator/ui/SampleActuatorUiApplicationPortTests.java b/spring-boot-samples/spring-boot-sample-actuator-ui/src/test/java/sample/actuator/ui/SampleActuatorUiApplicationPortTests.java index b26cc3ad477..fbd9b4d0e60 100644 --- a/spring-boot-samples/spring-boot-sample-actuator-ui/src/test/java/sample/actuator/ui/SampleActuatorUiApplicationPortTests.java +++ b/spring-boot-samples/spring-boot-sample-actuator-ui/src/test/java/sample/actuator/ui/SampleActuatorUiApplicationPortTests.java @@ -16,6 +16,8 @@ package sample.actuator.ui; +import static org.junit.Assert.assertEquals; + import java.util.Map; import org.junit.Test; @@ -24,17 +26,14 @@ 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; 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; - /** * Integration tests for separate management and main service ports. * @@ -43,18 +42,17 @@ import static org.junit.Assert.assertEquals; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = SampleActuatorUiApplication.class) @WebAppConfiguration -@IntegrationTest +@IntegrationTest({"server.port=0", "management.port:0"}) @DirtiesContext -@ActiveProfiles("management-port") public class SampleActuatorUiApplicationPortTests { @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-ui/src/test/java/sample/actuator/ui/SampleActuatorUiApplicationTests.java b/spring-boot-samples/spring-boot-sample-actuator-ui/src/test/java/sample/actuator/ui/SampleActuatorUiApplicationTests.java index f0472edb185..6bb0111a2c8 100644 --- a/spring-boot-samples/spring-boot-sample-actuator-ui/src/test/java/sample/actuator/ui/SampleActuatorUiApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-actuator-ui/src/test/java/sample/actuator/ui/SampleActuatorUiApplicationTests.java @@ -21,6 +21,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; @@ -44,17 +45,20 @@ import static org.junit.Assert.assertTrue; */ @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = SampleActuatorUiApplication.class) +@IntegrationTest("server.port=0") @WebAppConfiguration -@IntegrationTest @DirtiesContext public class SampleActuatorUiApplicationTests { + @Value("${local.server.port}") + private int port; + @Test public void testHome() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); ResponseEntity entity = new TestRestTemplate().exchange( - "http://localhost:8080", HttpMethod.GET, new HttpEntity(headers), + "http://localhost:" + port, HttpMethod.GET, new HttpEntity(headers), String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); assertTrue("Wrong body (title doesn't match):\n" + entity.getBody(), entity @@ -64,7 +68,7 @@ public class SampleActuatorUiApplicationTests { @Test public void testCss() throws Exception { ResponseEntity entity = new TestRestTemplate().getForEntity( - "http://localhost:8080/css/bootstrap.min.css", String.class); + "http://localhost:" + port + "/css/bootstrap.min.css", String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); assertTrue("Wrong body:\n" + entity.getBody(), entity.getBody().contains("body")); } @@ -73,7 +77,7 @@ public class SampleActuatorUiApplicationTests { public void testMetrics() 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()); } @@ -82,7 +86,7 @@ public class SampleActuatorUiApplicationTests { HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); ResponseEntity entity = new TestRestTemplate().exchange( - "http://localhost:8080/error", HttpMethod.GET, new HttpEntity( + "http://localhost:" + port + "/error", HttpMethod.GET, new HttpEntity( headers), String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); assertTrue("Wrong body:\n" + entity.getBody(), entity.getBody() diff --git a/spring-boot-samples/spring-boot-sample-actuator-ui/src/test/resources/application-management-port.properties b/spring-boot-samples/spring-boot-sample-actuator-ui/src/test/resources/application-management-port.properties deleted file mode 100644 index b04a05c04d6..00000000000 --- a/spring-boot-samples/spring-boot-sample-actuator-ui/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/java/sample/actuator/UnsecureSampleActuatorApplicationTests.java b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/UnsecureSampleActuatorApplicationTests.java index f607e271fbd..557c4eb671a 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 @@ -48,6 +48,7 @@ public class UnsecureSampleActuatorApplicationTests { @Value("${local.server.port}") private int port; + @Test public void testHome() throws Exception { @SuppressWarnings("rawtypes") diff --git a/spring-boot-samples/spring-boot-sample-jetty/src/test/java/sample/jetty/SampleJettyApplicationTests.java b/spring-boot-samples/spring-boot-sample-jetty/src/test/java/sample/jetty/SampleJettyApplicationTests.java index 9d95cce365a..0b25050724e 100644 --- a/spring-boot-samples/spring-boot-sample-jetty/src/test/java/sample/jetty/SampleJettyApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-jetty/src/test/java/sample/jetty/SampleJettyApplicationTests.java @@ -18,6 +18,7 @@ package sample.jetty; 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; @@ -37,14 +38,17 @@ import static org.junit.Assert.assertEquals; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = SampleJettyApplication.class) @WebAppConfiguration -@IntegrationTest +@IntegrationTest("server.port:0") @DirtiesContext public class SampleJettyApplicationTests { + @Value("${local.server.port}") + private int port; + @Test public void testHome() throws Exception { ResponseEntity entity = new TestRestTemplate().getForEntity( - "http://localhost:8080", String.class); + "http://localhost:" + port, String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals("Hello World", entity.getBody()); } diff --git a/spring-boot-samples/spring-boot-sample-servlet/src/test/java/sample/servlet/SampleServletApplicationTests.java b/spring-boot-samples/spring-boot-sample-servlet/src/test/java/sample/servlet/SampleServletApplicationTests.java index 6733934ddc0..fc34523f57e 100644 --- a/spring-boot-samples/spring-boot-sample-servlet/src/test/java/sample/servlet/SampleServletApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-servlet/src/test/java/sample/servlet/SampleServletApplicationTests.java @@ -16,21 +16,22 @@ package sample.servlet; +import static org.junit.Assert.assertEquals; + 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; 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.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; -import static org.junit.Assert.assertEquals; - /** * Basic integration tests for demo application. * @@ -39,24 +40,27 @@ import static org.junit.Assert.assertEquals; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = SampleServletApplication.class) @WebAppConfiguration -@IntegrationTest +@IntegrationTest("server.port:0") @DirtiesContext public class SampleServletApplicationTests { + @Value("${local.server.port}") + private int port; + @Autowired private SecurityProperties security; @Test public void testHomeIsSecure() throws Exception { ResponseEntity entity = new TestRestTemplate().getForEntity( - "http://localhost:8080", String.class); + "http://localhost:" + port, String.class); assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode()); } @Test public void testHome() throws Exception { ResponseEntity entity = new TestRestTemplate("user", getPassword()) - .getForEntity("http://localhost:8080", String.class); + .getForEntity("http://localhost:" + port, String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals("Hello World", entity.getBody()); } diff --git a/spring-boot-samples/spring-boot-sample-tomcat/src/test/java/sample/tomcat/NonAutoConfigurationSampleTomcatApplicationTests.java b/spring-boot-samples/spring-boot-sample-tomcat/src/test/java/sample/tomcat/NonAutoConfigurationSampleTomcatApplicationTests.java index b3c5ab7a484..6f70c2dd666 100644 --- a/spring-boot-samples/spring-boot-sample-tomcat/src/test/java/sample/tomcat/NonAutoConfigurationSampleTomcatApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-tomcat/src/test/java/sample/tomcat/NonAutoConfigurationSampleTomcatApplicationTests.java @@ -18,6 +18,7 @@ package sample.tomcat; import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration; @@ -40,7 +41,6 @@ import org.springframework.test.context.web.WebAppConfiguration; import sample.tomcat.NonAutoConfigurationSampleTomcatApplicationTests.NonAutoConfigurationSampleTomcatApplication; import sample.tomcat.service.HelloWorldService; import sample.tomcat.web.SampleController; - import static org.junit.Assert.assertEquals; /** @@ -51,10 +51,13 @@ import static org.junit.Assert.assertEquals; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = NonAutoConfigurationSampleTomcatApplication.class) @WebAppConfiguration -@IntegrationTest +@IntegrationTest("server.port=0") @DirtiesContext public class NonAutoConfigurationSampleTomcatApplicationTests { + @Value("${local.server.port}") + private int port; + @Configuration @Import({ EmbeddedServletContainerAutoConfiguration.class, DispatcherServletAutoConfiguration.class, @@ -73,7 +76,7 @@ public class NonAutoConfigurationSampleTomcatApplicationTests { @Test public void testHome() throws Exception { ResponseEntity entity = new TestRestTemplate().getForEntity( - "http://localhost:8080", String.class); + "http://localhost:" + port, String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals("Hello World", entity.getBody()); } diff --git a/spring-boot-samples/spring-boot-sample-tomcat/src/test/java/sample/tomcat/SampleTomcatApplicationTests.java b/spring-boot-samples/spring-boot-sample-tomcat/src/test/java/sample/tomcat/SampleTomcatApplicationTests.java index 2ed1307a13f..1d33bfb07c1 100644 --- a/spring-boot-samples/spring-boot-sample-tomcat/src/test/java/sample/tomcat/SampleTomcatApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-tomcat/src/test/java/sample/tomcat/SampleTomcatApplicationTests.java @@ -18,6 +18,7 @@ package sample.tomcat; 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; @@ -37,14 +38,17 @@ import static org.junit.Assert.assertEquals; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = SampleTomcatApplication.class) @WebAppConfiguration -@IntegrationTest +@IntegrationTest("server.port:0") @DirtiesContext public class SampleTomcatApplicationTests { + @Value("${local.server.port}") + private int port; + @Test public void testHome() throws Exception { ResponseEntity entity = new TestRestTemplate().getForEntity( - "http://localhost:8080", String.class); + "http://localhost:" + port, String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals("Hello World", entity.getBody()); } diff --git a/spring-boot-samples/spring-boot-sample-traditional/src/test/java/sample/traditional/SampleTraditionalApplicationTests.java b/spring-boot-samples/spring-boot-sample-traditional/src/test/java/sample/traditional/SampleTraditionalApplicationTests.java index f5f3cabc92c..6803669cb66 100644 --- a/spring-boot-samples/spring-boot-sample-traditional/src/test/java/sample/traditional/SampleTraditionalApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-traditional/src/test/java/sample/traditional/SampleTraditionalApplicationTests.java @@ -18,6 +18,7 @@ package sample.traditional; 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; @@ -38,14 +39,17 @@ import static org.junit.Assert.assertTrue; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = SampleTraditionalApplication.class) @WebAppConfiguration -@IntegrationTest +@IntegrationTest("server.port:0") @DirtiesContext public class SampleTraditionalApplicationTests { + @Value("${local.server.port}") + private int port; + @Test public void testHomeJsp() throws Exception { ResponseEntity entity = new TestRestTemplate().getForEntity( - "http://localhost:8080", String.class); + "http://localhost:" + port, String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); String body = entity.getBody(); assertTrue("Wrong body:\n" + body, body.contains("")); @@ -55,7 +59,7 @@ public class SampleTraditionalApplicationTests { @Test public void testStaticPage() throws Exception { ResponseEntity entity = new TestRestTemplate().getForEntity( - "http://localhost:8080/index.html", String.class); + "http://localhost:" + port + "/index.html", String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); String body = entity.getBody(); assertTrue("Wrong body:\n" + body, body.contains("")); diff --git a/spring-boot-samples/spring-boot-sample-web-jsp/src/test/java/sample/jsp/SampleWebJspApplicationTests.java b/spring-boot-samples/spring-boot-sample-web-jsp/src/test/java/sample/jsp/SampleWebJspApplicationTests.java index ca9a593eb17..8d4875d2337 100644 --- a/spring-boot-samples/spring-boot-sample-web-jsp/src/test/java/sample/jsp/SampleWebJspApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-web-jsp/src/test/java/sample/jsp/SampleWebJspApplicationTests.java @@ -18,6 +18,7 @@ package sample.jsp; 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; @@ -38,14 +39,17 @@ import static org.junit.Assert.assertTrue; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = SampleWebJspApplication.class) @WebAppConfiguration -@IntegrationTest +@IntegrationTest("server.port:0") @DirtiesContext public class SampleWebJspApplicationTests { + @Value("${local.server.port}") + private int port; + @Test public void testJspWithEl() throws Exception { ResponseEntity entity = new TestRestTemplate().getForEntity( - "http://localhost:8080", String.class); + "http://localhost:" + port, String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); assertTrue("Wrong body:\n" + entity.getBody(), entity.getBody().contains("/resources/text.txt")); diff --git a/spring-boot-samples/spring-boot-sample-web-method-security/src/test/java/sample/ui/method/SampleMethodSecurityApplicationTests.java b/spring-boot-samples/spring-boot-sample-web-method-security/src/test/java/sample/ui/method/SampleMethodSecurityApplicationTests.java index 0a3559e5908..58935422a07 100644 --- a/spring-boot-samples/spring-boot-sample-web-method-security/src/test/java/sample/ui/method/SampleMethodSecurityApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-web-method-security/src/test/java/sample/ui/method/SampleMethodSecurityApplicationTests.java @@ -22,6 +22,7 @@ import java.util.regex.Pattern; 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; @@ -48,16 +49,19 @@ import static org.junit.Assert.assertTrue; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = SampleMethodSecurityApplication.class) @WebAppConfiguration -@IntegrationTest +@IntegrationTest("server.port:0") @DirtiesContext public class SampleMethodSecurityApplicationTests { + @Value("${local.server.port}") + private int port; + @Test public void testHome() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); ResponseEntity entity = new TestRestTemplate().exchange( - "http://localhost:8080", HttpMethod.GET, new HttpEntity(headers), + "http://localhost:" + port, HttpMethod.GET, new HttpEntity(headers), String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); assertTrue("Wrong body (title doesn't match):\n" + entity.getBody(), entity @@ -73,11 +77,11 @@ public class SampleMethodSecurityApplicationTests { form.set("password", "admin"); getCsrf(form, headers); ResponseEntity entity = new TestRestTemplate().exchange( - "http://localhost:8080/login", HttpMethod.POST, + "http://localhost:" + port + "/login", HttpMethod.POST, new HttpEntity>(form, headers), String.class); assertEquals(HttpStatus.FOUND, entity.getStatusCode()); - assertEquals("http://localhost:8080/", entity.getHeaders().getLocation() + assertEquals("http://localhost:" + port + "/", entity.getHeaders().getLocation() .toString()); } @@ -90,7 +94,7 @@ public class SampleMethodSecurityApplicationTests { form.set("password", "user"); getCsrf(form, headers); ResponseEntity entity = new TestRestTemplate().exchange( - "http://localhost:8080/login", HttpMethod.POST, + "http://localhost:" + port + "/login", HttpMethod.POST, new HttpEntity>(form, headers), String.class); assertEquals(HttpStatus.FOUND, entity.getStatusCode()); @@ -106,7 +110,7 @@ public class SampleMethodSecurityApplicationTests { private void getCsrf(MultiValueMap form, HttpHeaders headers) { ResponseEntity page = new TestRestTemplate().getForEntity( - "http://localhost:8080/login", String.class); + "http://localhost:" + port + "/login", String.class); String cookie = page.getHeaders().getFirst("Set-Cookie"); headers.set("Cookie", cookie); String body = page.getBody(); diff --git a/spring-boot-samples/spring-boot-sample-web-secure/src/test/java/sample/ui/secure/SampleSecureApplicationTests.java b/spring-boot-samples/spring-boot-sample-web-secure/src/test/java/sample/ui/secure/SampleSecureApplicationTests.java index 2909fe9d95e..742f76e6e76 100644 --- a/spring-boot-samples/spring-boot-sample-web-secure/src/test/java/sample/ui/secure/SampleSecureApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-web-secure/src/test/java/sample/ui/secure/SampleSecureApplicationTests.java @@ -20,6 +20,7 @@ import java.util.Arrays; 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; @@ -44,16 +45,19 @@ import static org.junit.Assert.assertTrue; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = SampleWebSecureApplication.class) @WebAppConfiguration -@IntegrationTest +@IntegrationTest("server.port:0") @DirtiesContext public class SampleSecureApplicationTests { + @Value("${local.server.port}") + private int port; + @Test public void testHome() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); ResponseEntity entity = new TestRestTemplate().exchange( - "http://localhost:8080", HttpMethod.GET, new HttpEntity(headers), + "http://localhost:" + port, HttpMethod.GET, new HttpEntity(headers), String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); assertTrue("Wrong body (title doesn't match):\n" + entity.getBody(), entity @@ -63,7 +67,7 @@ public class SampleSecureApplicationTests { @Test public void testCss() throws Exception { ResponseEntity entity = new TestRestTemplate().getForEntity( - "http://localhost:8080/css/bootstrap.min.css", String.class); + "http://localhost:" + port + "/css/bootstrap.min.css", String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); assertTrue("Wrong body:\n" + entity.getBody(), entity.getBody().contains("body")); } diff --git a/spring-boot-samples/spring-boot-sample-web-ui/src/test/java/sample/ui/SampleWebUiApplicationTests.java b/spring-boot-samples/spring-boot-sample-web-ui/src/test/java/sample/ui/SampleWebUiApplicationTests.java index c93b73e2b7f..ca5478eb77a 100644 --- a/spring-boot-samples/spring-boot-sample-web-ui/src/test/java/sample/ui/SampleWebUiApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-web-ui/src/test/java/sample/ui/SampleWebUiApplicationTests.java @@ -20,6 +20,7 @@ import java.net.URI; 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,14 +44,17 @@ import static org.junit.Assert.assertTrue; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = SampleWebUiApplication.class) @WebAppConfiguration -@IntegrationTest +@IntegrationTest("server.port:0") @DirtiesContext public class SampleWebUiApplicationTests { + @Value("${local.server.port}") + private int port; + @Test public void testHome() throws Exception { ResponseEntity entity = new TestRestTemplate().getForEntity( - "http://localhost:8080", String.class); + "http://localhost:" + port, String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); assertTrue("Wrong body (title doesn't match):\n" + entity.getBody(), entity .getBody().contains("Messages")); @@ -63,16 +67,16 @@ public class SampleWebUiApplicationTests { MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>(); map.set("text", "FOO text"); map.set("summary", "FOO"); - URI location = new TestRestTemplate().postForLocation("http://localhost:8080", + URI location = new TestRestTemplate().postForLocation("http://localhost:" + port, map); assertTrue("Wrong location:\n" + location, - location.toString().contains("localhost:8080")); + location.toString().contains("localhost:" + port)); } @Test public void testCss() throws Exception { ResponseEntity<String> entity = new TestRestTemplate().getForEntity( - "http://localhost:8080/css/bootstrap.min.css", String.class); + "http://localhost:" + port + "/css/bootstrap.min.css", String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); assertTrue("Wrong body:\n" + entity.getBody(), entity.getBody().contains("body")); } diff --git a/spring-boot-samples/spring-boot-sample-websocket/src/test/java/samples/websocket/echo/CustomContainerWebSocketsApplicationTests.java b/spring-boot-samples/spring-boot-sample-websocket/src/test/java/samples/websocket/echo/CustomContainerWebSocketsApplicationTests.java index 8c44421f6e6..2c563f878da 100644 --- a/spring-boot-samples/spring-boot-sample-websocket/src/test/java/samples/websocket/echo/CustomContainerWebSocketsApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-websocket/src/test/java/samples/websocket/echo/CustomContainerWebSocketsApplicationTests.java @@ -16,6 +16,8 @@ package samples.websocket.echo; +import static org.junit.Assert.assertEquals; + import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; @@ -35,6 +37,7 @@ import org.springframework.context.annotation.Configuration; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.util.SocketUtils; import org.springframework.web.socket.client.WebSocketConnectionManager; import org.springframework.web.socket.client.standard.StandardWebSocketClient; @@ -44,8 +47,6 @@ import samples.websocket.client.SimpleGreetingService; import samples.websocket.config.SampleWebSocketsApplication; import samples.websocket.echo.CustomContainerWebSocketsApplicationTests.CustomContainerConfiguration; -import static org.junit.Assert.assertEquals; - @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = { SampleWebSocketsApplication.class, CustomContainerConfiguration.class }) @@ -56,14 +57,16 @@ public class CustomContainerWebSocketsApplicationTests { private static Log logger = LogFactory .getLog(CustomContainerWebSocketsApplicationTests.class); + + private static int PORT = SocketUtils.findAvailableTcpPort(); - private static final String WS_URI = "ws://localhost:9010/ws/echo/websocket"; + private static final String WS_URI = "ws://localhost:" + PORT + "/ws/echo/websocket"; @Configuration protected static class CustomContainerConfiguration { @Bean public EmbeddedServletContainerFactory embeddedServletContainerFactory() { - return new TomcatEmbeddedServletContainerFactory("/ws", 9010); + return new TomcatEmbeddedServletContainerFactory("/ws", PORT); } } diff --git a/spring-boot-samples/spring-boot-sample-websocket/src/test/java/samples/websocket/echo/SampleWebSocketsApplicationTests.java b/spring-boot-samples/spring-boot-sample-websocket/src/test/java/samples/websocket/echo/SampleWebSocketsApplicationTests.java index 8e43904a7ac..faa94e33d64 100644 --- a/spring-boot-samples/spring-boot-sample-websocket/src/test/java/samples/websocket/echo/SampleWebSocketsApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-websocket/src/test/java/samples/websocket/echo/SampleWebSocketsApplicationTests.java @@ -16,13 +16,17 @@ package samples.websocket.echo; +import static org.junit.Assert.assertEquals; + import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.test.IntegrationTest; @@ -41,18 +45,24 @@ import samples.websocket.client.SimpleClientWebSocketHandler; import samples.websocket.client.SimpleGreetingService; import samples.websocket.config.SampleWebSocketsApplication; -import static org.junit.Assert.assertEquals; - @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = SampleWebSocketsApplication.class) @WebAppConfiguration -@IntegrationTest +@IntegrationTest("server.port:0") @DirtiesContext public class SampleWebSocketsApplicationTests { private static Log logger = LogFactory.getLog(SampleWebSocketsApplicationTests.class); - private static final String WS_URI = "ws://localhost:8080/echo/websocket"; + private static String WS_URI; + + @Value("${local.server.port}") + private int port; + + @Before + public void init() { + WS_URI = "ws://localhost:" + port + "/echo/websocket"; + } @Test public void runAndWait() throws Exception { diff --git a/spring-boot/src/main/java/org/springframework/boot/StartupInfoLogger.java b/spring-boot/src/main/java/org/springframework/boot/StartupInfoLogger.java index 3a1f0ed1460..b5396e68cbb 100644 --- a/spring-boot/src/main/java/org/springframework/boot/StartupInfoLogger.java +++ b/spring-boot/src/main/java/org/springframework/boot/StartupInfoLogger.java @@ -137,6 +137,12 @@ class StartupInfoLogger { return System.getProperty("user.name"); } }); + String in = getValue("in ", new Callable<Object>() { + @Override + public Object call() throws Exception { + return System.getProperty("user.dir"); + } + }); File codeSourceLocation = getCodeSourceLocation(); String path = (codeSourceLocation == null ? "" : codeSourceLocation .getAbsolutePath()); @@ -146,7 +152,10 @@ class StartupInfoLogger { if (StringUtils.hasLength(startedBy) && StringUtils.hasLength(path)) { startedBy = " " + startedBy; } - return " (" + path + startedBy + ")"; + if (StringUtils.hasLength(in) && StringUtils.hasLength(startedBy)) { + in = " " + in; + } + return " (" + path + startedBy + in + ")"; } private File getCodeSourceLocation() { diff --git a/spring-boot/src/main/java/org/springframework/boot/context/config/RandomValuePropertySource.java b/spring-boot/src/main/java/org/springframework/boot/context/config/RandomValuePropertySource.java index 1bc92de4b25..bbec8ad0d2c 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/config/RandomValuePropertySource.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/config/RandomValuePropertySource.java @@ -22,6 +22,7 @@ import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.PropertySource; import org.springframework.core.env.StandardEnvironment; import org.springframework.util.DigestUtils; +import org.springframework.util.StringUtils; /** * {@link PropertySource} that returns a random value for any property that starts with @@ -44,14 +45,28 @@ public class RandomValuePropertySource extends PropertySource<Random> { if (name.endsWith("int")) { return getSource().nextInt(); } - if (name.endsWith("long")) { + if (name.startsWith("random.long")) { return getSource().nextLong(); } + if (name.startsWith("random.int") && name.length() > "random.int".length() + 1) { + String range = name.substring("random.int".length() + 1); + range = range.substring(0, range.length() - 1); + return getNextInRange(range); + } byte[] bytes = new byte[32]; getSource().nextBytes(bytes); return DigestUtils.md5DigestAsHex(bytes); } + private int getNextInRange(String range) { + String[] tokens = StringUtils.commaDelimitedListToStringArray(range); + Integer start = Integer.valueOf(tokens[0]); + if (tokens.length == 1) { + return getSource().nextInt(start); + } + return start + getSource().nextInt(Integer.valueOf(tokens[1]) - start); + } + public static void addToEnvironment(ConfigurableEnvironment environment) { environment.getPropertySources().addAfter( StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, 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 5ad1c3d5fa9..2be5d122051 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,7 +143,6 @@ 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())); diff --git a/spring-boot/src/test/java/org/springframework/boot/context/config/RandomValuePropertySourceTests.java b/spring-boot/src/test/java/org/springframework/boot/context/config/RandomValuePropertySourceTests.java new file mode 100644 index 00000000000..538dc2e6efb --- /dev/null +++ b/spring-boot/src/test/java/org/springframework/boot/context/config/RandomValuePropertySourceTests.java @@ -0,0 +1,68 @@ +/* + * Copyright 2012-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.context.config; + +import org.junit.Test; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +/** + * @author Dave Syer + */ +public class RandomValuePropertySourceTests { + + private RandomValuePropertySource source = new RandomValuePropertySource("random"); + + @Test + public void notRandom() { + assertNull(this.source.getProperty("foo")); + } + + @Test + public void string() { + assertNotNull(this.source.getProperty("random.string")); + } + + @Test + public void intValue() { + Integer value = (Integer) this.source.getProperty("random.int"); + assertNotNull(value); + } + + @Test + public void intRange() { + Integer value = (Integer) this.source.getProperty("random.int[4,10]"); + assertNotNull(value); + assertTrue(value >= 4); + } + + @Test + public void intMax() { + Integer value = (Integer) this.source.getProperty("random.int(10)"); + assertNotNull(value); + assertTrue(value < 10); + } + + @Test + public void longValue() { + Long value = (Long) this.source.getProperty("random.long"); + assertNotNull(value); + } + +}