Polish
This commit is contained in:
parent
b5d49b3099
commit
c3b344fdc2
|
|
@ -112,18 +112,19 @@ public class SpringApplicationAdminJmxAutoConfigurationTests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
@Test
|
@Test
|
||||||
public void registerWithSimpleWebApp() throws Exception {
|
public void registerWithSimpleWebApp() throws Exception {
|
||||||
this.context = new SpringApplicationBuilder()
|
this.context = new SpringApplicationBuilder().sources(
|
||||||
.sources(
|
EmbeddedServletContainerAutoConfiguration.class,
|
||||||
EmbeddedServletContainerAutoConfiguration.class,
|
ServerPropertiesAutoConfiguration.class,
|
||||||
ServerPropertiesAutoConfiguration.class, DispatcherServletAutoConfiguration.class,
|
DispatcherServletAutoConfiguration.class, JmxAutoConfiguration.class,
|
||||||
JmxAutoConfiguration.class, SpringApplicationAdminJmxAutoConfiguration.class)
|
SpringApplicationAdminJmxAutoConfiguration.class).run(
|
||||||
.run("--" + ENABLE_ADMIN_PROP, "--server.port=0");
|
"--" + ENABLE_ADMIN_PROP, "--server.port=0");
|
||||||
assertTrue(this.context instanceof EmbeddedWebApplicationContext);
|
assertTrue(this.context instanceof EmbeddedWebApplicationContext);
|
||||||
assertEquals(true, this.mBeanServer.getAttribute(createDefaultObjectName(), "EmbeddedWebApplication"));
|
assertEquals(true, this.mBeanServer.getAttribute(createDefaultObjectName(),
|
||||||
int expected = ((EmbeddedWebApplicationContext) this.context).getEmbeddedServletContainer().getPort();
|
"EmbeddedWebApplication"));
|
||||||
|
int expected = ((EmbeddedWebApplicationContext) this.context)
|
||||||
|
.getEmbeddedServletContainer().getPort();
|
||||||
String actual = getProperty(createDefaultObjectName(), "local.server.port");
|
String actual = getProperty(createDefaultObjectName(), "local.server.port");
|
||||||
assertEquals(String.valueOf(expected), actual);
|
assertEquals(String.valueOf(expected), actual);
|
||||||
}
|
}
|
||||||
|
|
@ -143,7 +144,7 @@ public class SpringApplicationAdminJmxAutoConfigurationTests {
|
||||||
|
|
||||||
private String getProperty(ObjectName objectName, String key) throws Exception {
|
private String getProperty(ObjectName objectName, String key) throws Exception {
|
||||||
return (String) this.mBeanServer.invoke(objectName, "getProperty",
|
return (String) this.mBeanServer.invoke(objectName, "getProperty",
|
||||||
new Object[]{key}, new String[]{String.class.getName()});
|
new Object[] { key }, new String[] { String.class.getName() });
|
||||||
}
|
}
|
||||||
|
|
||||||
private void load(String... environment) {
|
private void load(String... environment) {
|
||||||
|
|
|
||||||
|
|
@ -136,7 +136,7 @@ public class WebMvcAutoConfigurationTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void resourceHandlerMappingOverrideAll() throws Exception {
|
public void resourceHandlerMappingOverrideAll() throws Exception {
|
||||||
load(AllResources.class);
|
load(AllResources.class);
|
||||||
Map<String, List<Resource>> mappingLocations = getResourceMappingLocations();
|
Map<String, List<Resource>> mappingLocations = getResourceMappingLocations();
|
||||||
assertThat(mappingLocations.get("/**").size(), equalTo(1));
|
assertThat(mappingLocations.get("/**").size(), equalTo(1));
|
||||||
|
|
@ -200,7 +200,8 @@ public class WebMvcAutoConfigurationTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void overrideMessageCodesFormat() throws Exception {
|
public void overrideMessageCodesFormat() throws Exception {
|
||||||
load(AllResources.class, "spring.mvc.messageCodesResolverFormat:POSTFIX_ERROR_CODE");
|
load(AllResources.class,
|
||||||
|
"spring.mvc.messageCodesResolverFormat:POSTFIX_ERROR_CODE");
|
||||||
assertNotNull(this.context.getBean(WebMvcAutoConfigurationAdapter.class)
|
assertNotNull(this.context.getBean(WebMvcAutoConfigurationAdapter.class)
|
||||||
.getMessageCodesResolver());
|
.getMessageCodesResolver());
|
||||||
}
|
}
|
||||||
|
|
@ -318,7 +319,6 @@ public class WebMvcAutoConfigurationTests {
|
||||||
assertEquals(123456L, actual);
|
assertEquals(123456L, actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
private void load(Class<?> config, String... environment) {
|
private void load(Class<?> config, String... environment) {
|
||||||
this.context = new AnnotationConfigEmbeddedWebApplicationContext();
|
this.context = new AnnotationConfigEmbeddedWebApplicationContext();
|
||||||
|
|
@ -338,7 +338,6 @@ public class WebMvcAutoConfigurationTests {
|
||||||
load(null, environment);
|
load(null, environment);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
protected static class ViewConfig {
|
protected static class ViewConfig {
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -34,8 +34,8 @@ public interface SpringApplicationAdminMXBean {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Specify if the application runs in an embedded web container. Can return
|
* Specify if the application runs in an embedded web container. Can return
|
||||||
* {@code null} if that information is not yet available. It is preferable to
|
* {@code null} if that information is not yet available. It is preferable to wait for
|
||||||
* wait for the application to be {@link #isReady() ready}.
|
* the application to be {@link #isReady() ready}.
|
||||||
* @return {@code true} if the application runs in an embedded web container
|
* @return {@code true} if the application runs in an embedded web container
|
||||||
* @see #isReady()
|
* @see #isReady()
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -46,7 +46,8 @@ import org.springframework.util.Assert;
|
||||||
* @since 1.3.0
|
* @since 1.3.0
|
||||||
*/
|
*/
|
||||||
public class SpringApplicationAdminMXBeanRegistrar implements ApplicationContextAware,
|
public class SpringApplicationAdminMXBeanRegistrar implements ApplicationContextAware,
|
||||||
EnvironmentAware, InitializingBean, DisposableBean, ApplicationListener<ApplicationReadyEvent> {
|
EnvironmentAware, InitializingBean, DisposableBean,
|
||||||
|
ApplicationListener<ApplicationReadyEvent> {
|
||||||
|
|
||||||
private static final Log logger = LogFactory.getLog(SpringApplicationAdmin.class);
|
private static final Log logger = LogFactory.getLog(SpringApplicationAdmin.class);
|
||||||
|
|
||||||
|
|
@ -105,13 +106,13 @@ public class SpringApplicationAdminMXBeanRegistrar implements ApplicationContext
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isEmbeddedWebApplication() {
|
public boolean isEmbeddedWebApplication() {
|
||||||
return (applicationContext != null
|
return (SpringApplicationAdminMXBeanRegistrar.this.applicationContext != null && SpringApplicationAdminMXBeanRegistrar.this.applicationContext instanceof EmbeddedWebApplicationContext);
|
||||||
&& applicationContext instanceof EmbeddedWebApplicationContext);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getProperty(String key) {
|
public String getProperty(String key) {
|
||||||
return environment.getProperty(key);
|
return SpringApplicationAdminMXBeanRegistrar.this.environment
|
||||||
|
.getProperty(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -48,6 +48,7 @@ import org.springframework.util.StringUtils;
|
||||||
*
|
*
|
||||||
* @author Dave Syer
|
* @author Dave Syer
|
||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
|
* @since 1.3.0
|
||||||
*/
|
*/
|
||||||
public class ServerPortInfoApplicationContextInitializer implements
|
public class ServerPortInfoApplicationContextInitializer implements
|
||||||
ApplicationContextInitializer<ConfigurableApplicationContext> {
|
ApplicationContextInitializer<ConfigurableApplicationContext> {
|
||||||
|
|
@ -81,7 +82,8 @@ public class ServerPortInfoApplicationContextInitializer implements
|
||||||
|
|
||||||
private void setPortProperty(ApplicationContext context, String propertyName, int port) {
|
private void setPortProperty(ApplicationContext context, String propertyName, int port) {
|
||||||
if (context instanceof ConfigurableApplicationContext) {
|
if (context instanceof ConfigurableApplicationContext) {
|
||||||
ConfigurableEnvironment environment = ((ConfigurableApplicationContext) context).getEnvironment();
|
ConfigurableEnvironment environment = ((ConfigurableApplicationContext) context)
|
||||||
|
.getEnvironment();
|
||||||
MutablePropertySources sources = environment.getPropertySources();
|
MutablePropertySources sources = environment.getPropertySources();
|
||||||
Map<String, Object> map;
|
Map<String, Object> map;
|
||||||
if (!sources.contains("server.ports")) {
|
if (!sources.contains("server.ports")) {
|
||||||
|
|
@ -91,8 +93,8 @@ public class ServerPortInfoApplicationContextInitializer implements
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
Map<String, Object> value = (Map<String, Object>) sources.get("server.ports")
|
Map<String, Object> value = (Map<String, Object>) sources.get(
|
||||||
.getSource();
|
"server.ports").getSource();
|
||||||
map = value;
|
map = value;
|
||||||
}
|
}
|
||||||
map.put(propertyName, port);
|
map.put(propertyName, port);
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,47 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2012-2015 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;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.boot.context.embedded.EmbeddedServletContainer;
|
||||||
|
import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext;
|
||||||
|
import org.springframework.context.ApplicationContextInitializer;
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link ApplicationContextInitializer} that sets {@link Environment} properties for the
|
||||||
|
* ports that {@link EmbeddedServletContainer} servers are actually listening on. The
|
||||||
|
* property {@literal "local.server.port"} can be injected directly into tests using
|
||||||
|
* {@link Value @Value} or obtained via the {@link Environment}.
|
||||||
|
* <p>
|
||||||
|
* If the {@link EmbeddedWebApplicationContext} has a
|
||||||
|
* {@link EmbeddedWebApplicationContext#setNamespace(String) namespace} set, it will be
|
||||||
|
* used to construct the property name. For example, the "management" actuator context
|
||||||
|
* will have the property name {@literal "local.management.port"}.
|
||||||
|
* <p>
|
||||||
|
* Properties are automatically propagated up to any parent context.
|
||||||
|
*
|
||||||
|
* @author Dave Syer
|
||||||
|
* @author Phillip Webb
|
||||||
|
* @deprecated since 1.3 in favor of
|
||||||
|
* org.springframework.boot.context.web.ServerPortInfoApplicationContextInitializer
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
|
public class ServerPortInfoApplicationContextInitializer extends
|
||||||
|
org.springframework.boot.context.web.ServerPortInfoApplicationContextInitializer {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -124,7 +124,7 @@ public class SpringApplicationAdminMXBeanRegistrarTests {
|
||||||
private String getProperty(ObjectName objectName, String key) {
|
private String getProperty(ObjectName objectName, String key) {
|
||||||
try {
|
try {
|
||||||
return (String) this.mBeanServer.invoke(objectName, "getProperty",
|
return (String) this.mBeanServer.invoke(objectName, "getProperty",
|
||||||
new Object[] {key}, new String[] {String.class.getName()});
|
new Object[] { key }, new String[] { String.class.getName() });
|
||||||
}
|
}
|
||||||
catch (Exception ex) {
|
catch (Exception ex) {
|
||||||
throw new IllegalStateException(ex.getMessage(), ex);
|
throw new IllegalStateException(ex.getMessage(), ex);
|
||||||
|
|
@ -48,6 +48,7 @@ import org.springframework.context.support.AbstractApplicationContext;
|
||||||
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
|
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
|
||||||
import org.springframework.core.Ordered;
|
import org.springframework.core.Ordered;
|
||||||
import org.springframework.core.annotation.Order;
|
import org.springframework.core.annotation.Order;
|
||||||
|
import org.springframework.core.env.ConfigurableEnvironment;
|
||||||
import org.springframework.web.context.ServletContextAware;
|
import org.springframework.web.context.ServletContextAware;
|
||||||
import org.springframework.web.context.WebApplicationContext;
|
import org.springframework.web.context.WebApplicationContext;
|
||||||
import org.springframework.web.context.request.SessionScope;
|
import org.springframework.web.context.request.SessionScope;
|
||||||
|
|
@ -151,8 +152,9 @@ public class EmbeddedWebApplicationContextTests {
|
||||||
addEmbeddedServletContainerFactoryBean();
|
addEmbeddedServletContainerFactoryBean();
|
||||||
new ServerPortInfoApplicationContextInitializer().initialize(this.context);
|
new ServerPortInfoApplicationContextInitializer().initialize(this.context);
|
||||||
this.context.refresh();
|
this.context.refresh();
|
||||||
assertTrue(this.context.getEnvironment().containsProperty("local.server.port"));
|
ConfigurableEnvironment environment = this.context.getEnvironment();
|
||||||
assertEquals("8080", this.context.getEnvironment().getProperty("local.server.port"));
|
assertTrue(environment.containsProperty("local.server.port"));
|
||||||
|
assertEquals("8080", environment.getProperty("local.server.port"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue