Merge branch '1.5.x'
This commit is contained in:
commit
576d5dd58f
|
@ -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
|
the default MVC configuration) because Spring will always be able to handle requests
|
||||||
through the `DispatcherServlet`.
|
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/**`
|
`spring.mvc.static-path-pattern`. For instance, relocating all resources to `/resources/**`
|
||||||
can be achieved as follows:
|
can be achieved as follows:
|
||||||
|
|
||||||
|
[source,properties,indent=0,subs="verbatim,quotes,attributes"]
|
||||||
----
|
----
|
||||||
spring.mvc.static-path-pattern=/resources/**
|
spring.mvc.static-path-pattern=/resources/**
|
||||||
----
|
----
|
||||||
|
|
|
@ -21,10 +21,12 @@ import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.bind.RelaxedPropertyResolver;
|
||||||
import org.springframework.boot.test.mock.web.SpringBootMockServletContext;
|
import org.springframework.boot.test.mock.web.SpringBootMockServletContext;
|
||||||
import org.springframework.boot.test.util.EnvironmentTestUtils;
|
import org.springframework.boot.test.util.EnvironmentTestUtils;
|
||||||
import org.springframework.boot.web.support.ServletContextApplicationContextInitializer;
|
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.SpringVersion;
|
||||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||||
import org.springframework.core.env.ConfigurableEnvironment;
|
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.env.StandardEnvironment;
|
||||||
import org.springframework.core.io.DefaultResourceLoader;
|
import org.springframework.core.io.DefaultResourceLoader;
|
||||||
import org.springframework.test.context.ContextConfigurationAttributes;
|
import org.springframework.test.context.ContextConfigurationAttributes;
|
||||||
|
@ -65,6 +70,7 @@ import org.springframework.web.context.support.GenericWebApplicationContext;
|
||||||
* @author Dave Syer
|
* @author Dave Syer
|
||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
* @author Andy Wilkinson
|
* @author Andy Wilkinson
|
||||||
|
* @author Stephane Nicoll
|
||||||
* @see SpringBootTest
|
* @see SpringBootTest
|
||||||
*/
|
*/
|
||||||
public class SpringBootContextLoader extends AbstractContextLoader {
|
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
|
// JMX bean names will clash if the same bean is used in multiple contexts
|
||||||
disableJmx(properties);
|
disableJmx(properties);
|
||||||
properties.addAll(Arrays.asList(config.getPropertySourceProperties()));
|
properties.addAll(Arrays.asList(config.getPropertySourceProperties()));
|
||||||
if (!isEmbeddedWebEnvironment(config)) {
|
if (!isEmbeddedWebEnvironment(config) && !hasCustomServerPort(properties)) {
|
||||||
properties.add("server.port=-1");
|
properties.add("server.port=-1");
|
||||||
}
|
}
|
||||||
return properties.toArray(new String[properties.size()]);
|
return properties.toArray(new String[properties.size()]);
|
||||||
|
@ -153,6 +159,16 @@ public class SpringBootContextLoader extends AbstractContextLoader {
|
||||||
properties.add("spring.jmx.enabled=false");
|
properties.add("spring.jmx.enabled=false");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean hasCustomServerPort(List<String> properties) {
|
||||||
|
Map<String, Object> 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<ApplicationContextInitializer<?>> getInitializers(
|
private List<ApplicationContextInitializer<?>> getInitializers(
|
||||||
MergedContextConfiguration config, SpringApplication application) {
|
MergedContextConfiguration config, SpringApplication application) {
|
||||||
List<ApplicationContextInitializer<?>> initializers = new ArrayList<ApplicationContextInitializer<?>>();
|
List<ApplicationContextInitializer<?>> initializers = new ArrayList<ApplicationContextInitializer<?>>();
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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 {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue