From f6a32a1d5a2c7ca6b5869dae1f44cd5f073d4795 Mon Sep 17 00:00:00 2001 From: Anand Shah Date: Tue, 1 Mar 2016 16:02:41 +0000 Subject: [PATCH] Add `@LocalServerPort` Closes gh-5262 --- .../EndpointMvcIntegrationTests.java | 4 +- ...ointServerContextPathIntegrationTests.java | 4 +- ...igurationCustomFilterContextPathTests.java | 3 +- ...utoConfigurationCustomFilterPathTests.java | 3 +- ...rationCustomObjectMapperProviderTests.java | 4 +- ...gurationCustomServletContextPathTests.java | 3 +- ...toConfigurationCustomServletPathTests.java | 3 +- ...toConfigurationDefaultFilterPathTests.java | 3 +- ...oConfigurationDefaultServletPathTests.java | 3 +- ...onfigurationObjectMapperProviderTests.java | 4 +- ...utoConfigurationServletContainerTests.java | 3 +- ...figurationWithoutApplicationPathTests.java | 3 +- ...serInfoTokenServicesRefreshTokenTests.java | 4 +- .../RemappedErrorViewIntegrationTests.java | 4 +- ...SpringApplicationIntegrationTestTests.java | 3 +- ...ingApplicationWebIntegrationTestTests.java | 3 +- .../boot/bind/LocalServerPort.java | 34 ++++++++++ .../boot/bind/LocalServerPortTests.java | 67 +++++++++++++++++++ .../src/test/resources/some.properties | 1 + 19 files changed, 134 insertions(+), 22 deletions(-) create mode 100644 spring-boot/src/main/java/org/springframework/boot/bind/LocalServerPort.java create mode 100644 spring-boot/src/test/java/org/springframework/boot/bind/LocalServerPortTests.java diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/EndpointMvcIntegrationTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/EndpointMvcIntegrationTests.java index cbd38d637f9..9dcef908601 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/EndpointMvcIntegrationTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/EndpointMvcIntegrationTests.java @@ -34,7 +34,6 @@ 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.actuate.autoconfigure.EndpointMvcIntegrationTests.Application; import org.springframework.boot.actuate.endpoint.Endpoint; import org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping; @@ -48,6 +47,7 @@ import org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration; import org.springframework.boot.autoconfigure.web.HttpMessageConverters; import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration; import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; +import org.springframework.boot.bind.LocalServerPort; import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.TestRestTemplate; @@ -78,7 +78,7 @@ import static org.assertj.core.api.Assertions.assertThat; @DirtiesContext public class EndpointMvcIntegrationTests { - @Value("${local.server.port}") + @LocalServerPort private int port; @Autowired diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/HalBrowserMvcEndpointServerContextPathIntegrationTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/HalBrowserMvcEndpointServerContextPathIntegrationTests.java index 78968489eda..b386751cfd8 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/HalBrowserMvcEndpointServerContextPathIntegrationTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/HalBrowserMvcEndpointServerContextPathIntegrationTests.java @@ -21,9 +21,9 @@ import java.util.Arrays; import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.actuate.autoconfigure.MinimalActuatorHypermediaApplication; import org.springframework.boot.actuate.endpoint.mvc.HalBrowserMvcEndpointServerContextPathIntegrationTests.SpringBootHypermediaApplication; +import org.springframework.boot.bind.LocalServerPort; import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.TestRestTemplate; @@ -57,7 +57,7 @@ import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo; @DirtiesContext public class HalBrowserMvcEndpointServerContextPathIntegrationTests { - @Value("${local.server.port}") + @LocalServerPort private int port; @Test diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomFilterContextPathTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomFilterContextPathTests.java index 7ee8a124b4f..5d70900db58 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomFilterContextPathTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomFilterContextPathTests.java @@ -36,6 +36,7 @@ import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfigurati import org.springframework.boot.autoconfigure.jersey.JerseyAutoConfigurationCustomFilterContextPathTests.Application; import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration; import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration; +import org.springframework.boot.bind.LocalServerPort; import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.TestRestTemplate; @@ -62,7 +63,7 @@ import static org.assertj.core.api.Assertions.assertThat; @WebAppConfiguration public class JerseyAutoConfigurationCustomFilterContextPathTests { - @Value("${local.server.port}") + @LocalServerPort private int port; private RestTemplate restTemplate = new TestRestTemplate(); diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomFilterPathTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomFilterPathTests.java index 15105c63929..869b014512b 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomFilterPathTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomFilterPathTests.java @@ -36,6 +36,7 @@ import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfigurati import org.springframework.boot.autoconfigure.jersey.JerseyAutoConfigurationCustomFilterPathTests.Application; import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration; import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration; +import org.springframework.boot.bind.LocalServerPort; import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.TestRestTemplate; @@ -61,7 +62,7 @@ import static org.assertj.core.api.Assertions.assertThat; @WebAppConfiguration public class JerseyAutoConfigurationCustomFilterPathTests { - @Value("${local.server.port}") + @LocalServerPort private int port; private RestTemplate restTemplate = new TestRestTemplate(); diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomObjectMapperProviderTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomObjectMapperProviderTests.java index e0bc6685136..d2fdfad5b9f 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomObjectMapperProviderTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomObjectMapperProviderTests.java @@ -30,13 +30,13 @@ import org.glassfish.jersey.server.ResourceConfig; 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.jackson.JacksonAutoConfiguration; import org.springframework.boot.autoconfigure.jersey.JerseyAutoConfigurationObjectMapperProviderTests.Application; import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration; import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration; +import org.springframework.boot.bind.LocalServerPort; import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.TestRestTemplate; @@ -62,7 +62,7 @@ import static org.assertj.core.api.Assertions.assertThat; @WebAppConfiguration public class JerseyAutoConfigurationCustomObjectMapperProviderTests { - @Value("${local.server.port}") + @LocalServerPort private int port; private RestTemplate restTemplate = new TestRestTemplate(); diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomServletContextPathTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomServletContextPathTests.java index 9d544a68f41..1f75a0a961c 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomServletContextPathTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomServletContextPathTests.java @@ -36,6 +36,7 @@ import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfigurati import org.springframework.boot.autoconfigure.jersey.JerseyAutoConfigurationCustomServletContextPathTests.Application; import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration; import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration; +import org.springframework.boot.bind.LocalServerPort; import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.TestRestTemplate; @@ -61,7 +62,7 @@ import static org.assertj.core.api.Assertions.assertThat; @WebAppConfiguration public class JerseyAutoConfigurationCustomServletContextPathTests { - @Value("${local.server.port}") + @LocalServerPort private int port; private RestTemplate restTemplate = new TestRestTemplate(); diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomServletPathTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomServletPathTests.java index ecc987b7839..30780bb6f53 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomServletPathTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomServletPathTests.java @@ -36,6 +36,7 @@ import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfigurati import org.springframework.boot.autoconfigure.jersey.JerseyAutoConfigurationCustomServletPathTests.Application; import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration; import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration; +import org.springframework.boot.bind.LocalServerPort; import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.TestRestTemplate; @@ -61,7 +62,7 @@ import static org.assertj.core.api.Assertions.assertThat; @WebAppConfiguration public class JerseyAutoConfigurationCustomServletPathTests { - @Value("${local.server.port}") + @LocalServerPort private int port; private RestTemplate restTemplate = new TestRestTemplate(); diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationDefaultFilterPathTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationDefaultFilterPathTests.java index a8af5e0dccd..c8439706c3e 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationDefaultFilterPathTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationDefaultFilterPathTests.java @@ -35,6 +35,7 @@ import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfigurati import org.springframework.boot.autoconfigure.jersey.JerseyAutoConfigurationDefaultFilterPathTests.Application; import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration; import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration; +import org.springframework.boot.bind.LocalServerPort; import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.TestRestTemplate; @@ -60,7 +61,7 @@ import static org.assertj.core.api.Assertions.assertThat; @WebAppConfiguration public class JerseyAutoConfigurationDefaultFilterPathTests { - @Value("${local.server.port}") + @LocalServerPort private int port; private RestTemplate restTemplate = new TestRestTemplate(); diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationDefaultServletPathTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationDefaultServletPathTests.java index a74ef3d5690..8a14ae5b631 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationDefaultServletPathTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationDefaultServletPathTests.java @@ -35,6 +35,7 @@ import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfigurati import org.springframework.boot.autoconfigure.jersey.JerseyAutoConfigurationDefaultServletPathTests.Application; import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration; import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration; +import org.springframework.boot.bind.LocalServerPort; import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.TestRestTemplate; @@ -60,7 +61,7 @@ import static org.assertj.core.api.Assertions.assertThat; @WebAppConfiguration public class JerseyAutoConfigurationDefaultServletPathTests { - @Value("${local.server.port}") + @LocalServerPort private int port; private RestTemplate restTemplate = new TestRestTemplate(); diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationObjectMapperProviderTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationObjectMapperProviderTests.java index 8f105a9509e..fb8df62eb38 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationObjectMapperProviderTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationObjectMapperProviderTests.java @@ -30,13 +30,13 @@ import org.glassfish.jersey.server.ResourceConfig; 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.jackson.JacksonAutoConfiguration; import org.springframework.boot.autoconfigure.jersey.JerseyAutoConfigurationObjectMapperProviderTests.Application; import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration; import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration; +import org.springframework.boot.bind.LocalServerPort; import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.TestRestTemplate; @@ -62,7 +62,7 @@ import static org.assertj.core.api.Assertions.assertThat; @WebAppConfiguration public class JerseyAutoConfigurationObjectMapperProviderTests { - @Value("${local.server.port}") + @LocalServerPort private int port; private RestTemplate restTemplate = new TestRestTemplate(); diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationServletContainerTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationServletContainerTests.java index 88895d788da..77eda0b361b 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationServletContainerTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationServletContainerTests.java @@ -33,6 +33,7 @@ import org.springframework.boot.autoconfigure.jersey.JerseyAutoConfigurationServ import org.springframework.boot.autoconfigure.test.ImportAutoConfiguration; import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration; import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration; +import org.springframework.boot.bind.LocalServerPort; import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.OutputCapture; @@ -62,7 +63,7 @@ public class JerseyAutoConfigurationServletContainerTests { @ClassRule public static OutputCapture output = new OutputCapture(); - @Value("${local.server.port}") + @LocalServerPort private int port; @Test diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationWithoutApplicationPathTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationWithoutApplicationPathTests.java index 1718e6a1009..095d878f5c1 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationWithoutApplicationPathTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationWithoutApplicationPathTests.java @@ -35,6 +35,7 @@ import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfigurati import org.springframework.boot.autoconfigure.jersey.JerseyAutoConfigurationCustomServletPathTests.Application; import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration; import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration; +import org.springframework.boot.bind.LocalServerPort; import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.TestRestTemplate; @@ -60,7 +61,7 @@ import static org.assertj.core.api.Assertions.assertThat; @WebAppConfiguration public class JerseyAutoConfigurationWithoutApplicationPathTests { - @Value("${local.server.port}") + @LocalServerPort private int port; private RestTemplate restTemplate = new TestRestTemplate(); diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/oauth2/resource/UserInfoTokenServicesRefreshTokenTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/oauth2/resource/UserInfoTokenServicesRefreshTokenTests.java index c0b3b9d38ba..da41b87e52a 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/oauth2/resource/UserInfoTokenServicesRefreshTokenTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/oauth2/resource/UserInfoTokenServicesRefreshTokenTests.java @@ -24,7 +24,6 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoTokenServicesRefreshTokenTests.Application; import org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration; @@ -32,6 +31,7 @@ import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoCo import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration; import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration; import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; +import org.springframework.boot.bind.LocalServerPort; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.WebIntegrationTest; import org.springframework.context.annotation.Configuration; @@ -71,7 +71,7 @@ public class UserInfoTokenServicesRefreshTokenTests { @Rule public ExpectedException expected = ExpectedException.none(); - @Value("${local.server.port}") + @LocalServerPort private int port; private UserInfoTokenServices services; diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/RemappedErrorViewIntegrationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/RemappedErrorViewIntegrationTests.java index a1a81aaee9b..787d00c538f 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/RemappedErrorViewIntegrationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/RemappedErrorViewIntegrationTests.java @@ -19,9 +19,9 @@ package org.springframework.boot.autoconfigure.web; import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.web.RemappedErrorViewIntegrationTests.TestConfiguration; +import org.springframework.boot.bind.LocalServerPort; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer; import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer; @@ -50,7 +50,7 @@ import static org.assertj.core.api.Assertions.assertThat; @DirtiesContext public class RemappedErrorViewIntegrationTests { - @Value("${local.server.port}") + @LocalServerPort private int port; private RestTemplate template = new TestRestTemplate(); diff --git a/spring-boot-test/src/test/java/org/springframework/boot/test/SpringApplicationIntegrationTestTests.java b/spring-boot-test/src/test/java/org/springframework/boot/test/SpringApplicationIntegrationTestTests.java index b175b1464ef..05569f45898 100644 --- a/spring-boot-test/src/test/java/org/springframework/boot/test/SpringApplicationIntegrationTestTests.java +++ b/spring-boot-test/src/test/java/org/springframework/boot/test/SpringApplicationIntegrationTestTests.java @@ -23,6 +23,7 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.bind.LocalServerPort; import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory; import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; import org.springframework.boot.test.SpringApplicationIntegrationTestTests.Config; @@ -54,7 +55,7 @@ import static org.assertj.core.api.Assertions.assertThat; @IntegrationTest({ "server.port=0", "value=123" }) public class SpringApplicationIntegrationTestTests { - @Value("${local.server.port}") + @LocalServerPort private int port = 0; @Value("${value}") diff --git a/spring-boot-test/src/test/java/org/springframework/boot/test/SpringApplicationWebIntegrationTestTests.java b/spring-boot-test/src/test/java/org/springframework/boot/test/SpringApplicationWebIntegrationTestTests.java index 72d5e8cb6db..b25b3247695 100644 --- a/spring-boot-test/src/test/java/org/springframework/boot/test/SpringApplicationWebIntegrationTestTests.java +++ b/spring-boot-test/src/test/java/org/springframework/boot/test/SpringApplicationWebIntegrationTestTests.java @@ -23,6 +23,7 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.bind.LocalServerPort; import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory; import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; import org.springframework.boot.test.SpringApplicationWebIntegrationTestTests.Config; @@ -52,7 +53,7 @@ import static org.assertj.core.api.Assertions.assertThat; @WebIntegrationTest({ "server.port=0", "value=123" }) public class SpringApplicationWebIntegrationTestTests { - @Value("${local.server.port}") + @LocalServerPort private int port = 0; @Value("${value}") diff --git a/spring-boot/src/main/java/org/springframework/boot/bind/LocalServerPort.java b/spring-boot/src/main/java/org/springframework/boot/bind/LocalServerPort.java new file mode 100644 index 00000000000..c6beaa53d1e --- /dev/null +++ b/spring-boot/src/main/java/org/springframework/boot/bind/LocalServerPort.java @@ -0,0 +1,34 @@ +/* + * Copyright 2012-2016 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.bind; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +import org.springframework.beans.factory.annotation.Value; + +/** + * Meta annotation for replacement of property local.server.port. Can be used instead of + * @Value("${local.server.port}") + * + * @author Anand Shah + */ +@Value("${local.server.port}") +@Retention(RetentionPolicy.RUNTIME) +public @interface LocalServerPort { + +} diff --git a/spring-boot/src/test/java/org/springframework/boot/bind/LocalServerPortTests.java b/spring-boot/src/test/java/org/springframework/boot/bind/LocalServerPortTests.java new file mode 100644 index 00000000000..7921aa29e10 --- /dev/null +++ b/spring-boot/src/test/java/org/springframework/boot/bind/LocalServerPortTests.java @@ -0,0 +1,67 @@ +/* + * Copyright 2012-2016 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.bind; + +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.bind.PropertySourcesBindingTests.TestConfig; +import org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link LocalServerPort} annotation based injection of local.server.port + * property. + * + * @author Anand Shah + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = TestConfig.class) +public class LocalServerPortTests { + + private ConfigurableApplicationContext context; + + @Value("${local.server.port:}") + private String localServerPortFromValue; + + @LocalServerPort + private String localServerPortFromAnnotation; + + @Test + public void testLocalServerPortAnnotation() { + SpringApplication application = new SpringApplication(LocalServerPortTests.class); + application.setWebEnvironment(true); + + this.context = application.run(); + + assertThat(this.localServerPortFromAnnotation).isNotNull().isNotEmpty() + .isEqualTo(this.localServerPortFromValue).isEqualTo( + this.context.getEnvironment().getProperty("local.server.port")); + } + + @Bean + public JettyEmbeddedServletContainerFactory container() { + return new JettyEmbeddedServletContainerFactory(8081); + } +} diff --git a/spring-boot/src/test/resources/some.properties b/spring-boot/src/test/resources/some.properties index a1e02b44809..e0efc305ac0 100644 --- a/spring-boot/src/test/resources/some.properties +++ b/spring-boot/src/test/resources/some.properties @@ -2,3 +2,4 @@ foo=spam bar=some spam=bucket the-name=name +local.server.port=8081