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 47cb3fee3a6..6e3928794bd 100644 --- a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -1806,11 +1806,11 @@ Spring decides not to handle it. Most of the time this will not happen (unless y the default MVC configuration) because Spring will always be able to handle requests through the `DispatcherServlet`. - -By default, resources are mapped on `/**` but you can tune that via +By default, resources are mapped on `+/**+` but you can tune that via `spring.mvc.static-path-pattern`. For instance, relocating all resources to `/resources/**` can be achieved as follows: +[source,properties,indent=0,subs="verbatim,quotes,attributes"] ---- spring.mvc.static-path-pattern=/resources/** ---- diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootContextLoader.java b/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootContextLoader.java index d4b86156449..efa90c14b16 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootContextLoader.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootContextLoader.java @@ -21,10 +21,12 @@ import java.util.Arrays; import java.util.Collections; import java.util.LinkedHashSet; import java.util.List; +import java.util.Map; import java.util.Set; import org.springframework.beans.BeanUtils; import org.springframework.boot.SpringApplication; +import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.boot.test.mock.web.SpringBootMockServletContext; import org.springframework.boot.test.util.EnvironmentTestUtils; import org.springframework.boot.web.support.ServletContextApplicationContextInitializer; @@ -34,6 +36,9 @@ import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.SpringVersion; import org.springframework.core.annotation.AnnotatedElementUtils; import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.MapPropertySource; +import org.springframework.core.env.MutablePropertySources; +import org.springframework.core.env.PropertySourcesPropertyResolver; import org.springframework.core.env.StandardEnvironment; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.test.context.ContextConfigurationAttributes; @@ -65,6 +70,7 @@ import org.springframework.web.context.support.GenericWebApplicationContext; * @author Dave Syer * @author Phillip Webb * @author Andy Wilkinson + * @author Stephane Nicoll * @see SpringBootTest */ public class SpringBootContextLoader extends AbstractContextLoader { @@ -143,7 +149,7 @@ public class SpringBootContextLoader extends AbstractContextLoader { // JMX bean names will clash if the same bean is used in multiple contexts disableJmx(properties); properties.addAll(Arrays.asList(config.getPropertySourceProperties())); - if (!isEmbeddedWebEnvironment(config)) { + if (!isEmbeddedWebEnvironment(config) && !hasCustomServerPort(properties)) { properties.add("server.port=-1"); } return properties.toArray(new String[properties.size()]); @@ -153,6 +159,16 @@ public class SpringBootContextLoader extends AbstractContextLoader { properties.add("spring.jmx.enabled=false"); } + private boolean hasCustomServerPort(List properties) { + Map props = TestPropertySourceUtils.convertInlinedPropertiesToMap( + properties.toArray(new String[properties.size()])); + MutablePropertySources sources = new MutablePropertySources(); + sources.addFirst(new MapPropertySource("inline", props)); + RelaxedPropertyResolver resolver = new RelaxedPropertyResolver( + new PropertySourcesPropertyResolver(sources), "server."); + return resolver.containsProperty("port"); + } + private List> getInitializers( MergedContextConfiguration config, SpringApplication application) { List> initializers = new ArrayList>(); diff --git a/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringBootTestCustomPortTests.java b/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringBootTestCustomPortTests.java new file mode 100644 index 00000000000..4d50efa1e67 --- /dev/null +++ b/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringBootTestCustomPortTests.java @@ -0,0 +1,53 @@ +/* + * 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.test.context; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.Environment; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Test for {@link SpringBootTest} with a custom inline server.port in a non-embedded + * web environment. + * + * @author Stephane Nicoll + */ +@RunWith(SpringRunner.class) +@SpringBootTest(properties = "server.port=12345") +public class SpringBootTestCustomPortTests { + + @Autowired + private Environment environment; + + @Test + public void validatePortIsNotOverwritten() { + String port = this.environment.getProperty("server.port"); + assertThat(port).isEqualTo("12345"); + } + + @Configuration + protected static class Config { + + } + +} diff --git a/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringBootTestWebEnvironmentRandomPortCustomPortTests.java b/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringBootTestWebEnvironmentRandomPortCustomPortTests.java new file mode 100644 index 00000000000..f8e439ba058 --- /dev/null +++ b/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringBootTestWebEnvironmentRandomPortCustomPortTests.java @@ -0,0 +1,58 @@ +/* + * 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.test.context; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.AbstractSpringBootTestEmbeddedWebEnvironmentTests.AbstractConfig; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.Environment; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Test for {@link SpringBootTest} with a custom inline server.port in an embedded web + * environment. + * + * @author Stephane Nicoll + */ +@RunWith(SpringRunner.class) +@DirtiesContext +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = { "server.port=12345" }) +public class SpringBootTestWebEnvironmentRandomPortCustomPortTests { + + @Autowired + private Environment environment; + + @Test + public void validatePortIsNotOverwritten() { + String port = this.environment.getProperty("server.port"); + assertThat(port).isEqualTo("0"); + } + + @Configuration + @EnableWebMvc + protected static class Config extends AbstractConfig { + + } +}