Polish
This commit is contained in:
parent
b5d49b3099
commit
c3b344fdc2
|
|
@ -112,18 +112,19 @@ public class SpringApplicationAdminJmxAutoConfigurationTests {
|
|||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void registerWithSimpleWebApp() throws Exception {
|
||||
this.context = new SpringApplicationBuilder()
|
||||
.sources(
|
||||
EmbeddedServletContainerAutoConfiguration.class,
|
||||
ServerPropertiesAutoConfiguration.class, DispatcherServletAutoConfiguration.class,
|
||||
JmxAutoConfiguration.class, SpringApplicationAdminJmxAutoConfiguration.class)
|
||||
.run("--" + ENABLE_ADMIN_PROP, "--server.port=0");
|
||||
this.context = new SpringApplicationBuilder().sources(
|
||||
EmbeddedServletContainerAutoConfiguration.class,
|
||||
ServerPropertiesAutoConfiguration.class,
|
||||
DispatcherServletAutoConfiguration.class, JmxAutoConfiguration.class,
|
||||
SpringApplicationAdminJmxAutoConfiguration.class).run(
|
||||
"--" + ENABLE_ADMIN_PROP, "--server.port=0");
|
||||
assertTrue(this.context instanceof EmbeddedWebApplicationContext);
|
||||
assertEquals(true, this.mBeanServer.getAttribute(createDefaultObjectName(), "EmbeddedWebApplication"));
|
||||
int expected = ((EmbeddedWebApplicationContext) this.context).getEmbeddedServletContainer().getPort();
|
||||
assertEquals(true, this.mBeanServer.getAttribute(createDefaultObjectName(),
|
||||
"EmbeddedWebApplication"));
|
||||
int expected = ((EmbeddedWebApplicationContext) this.context)
|
||||
.getEmbeddedServletContainer().getPort();
|
||||
String actual = getProperty(createDefaultObjectName(), "local.server.port");
|
||||
assertEquals(String.valueOf(expected), actual);
|
||||
}
|
||||
|
|
@ -143,7 +144,7 @@ public class SpringApplicationAdminJmxAutoConfigurationTests {
|
|||
|
||||
private String getProperty(ObjectName objectName, String key) throws Exception {
|
||||
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) {
|
||||
|
|
|
|||
|
|
@ -136,7 +136,7 @@ public class WebMvcAutoConfigurationTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void resourceHandlerMappingOverrideAll() throws Exception {
|
||||
public void resourceHandlerMappingOverrideAll() throws Exception {
|
||||
load(AllResources.class);
|
||||
Map<String, List<Resource>> mappingLocations = getResourceMappingLocations();
|
||||
assertThat(mappingLocations.get("/**").size(), equalTo(1));
|
||||
|
|
@ -200,7 +200,8 @@ public class WebMvcAutoConfigurationTests {
|
|||
|
||||
@Test
|
||||
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)
|
||||
.getMessageCodesResolver());
|
||||
}
|
||||
|
|
@ -318,7 +319,6 @@ public class WebMvcAutoConfigurationTests {
|
|||
assertEquals(123456L, actual);
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void load(Class<?> config, String... environment) {
|
||||
this.context = new AnnotationConfigEmbeddedWebApplicationContext();
|
||||
|
|
@ -338,7 +338,6 @@ public class WebMvcAutoConfigurationTests {
|
|||
load(null, environment);
|
||||
}
|
||||
|
||||
|
||||
@Configuration
|
||||
protected static class ViewConfig {
|
||||
|
||||
|
|
|
|||
|
|
@ -34,8 +34,8 @@ public interface SpringApplicationAdminMXBean {
|
|||
|
||||
/**
|
||||
* 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
|
||||
* wait for the application to be {@link #isReady() ready}.
|
||||
* {@code null} if that information is not yet available. It is preferable to wait for
|
||||
* the application to be {@link #isReady() ready}.
|
||||
* @return {@code true} if the application runs in an embedded web container
|
||||
* @see #isReady()
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -46,7 +46,8 @@ import org.springframework.util.Assert;
|
|||
* @since 1.3.0
|
||||
*/
|
||||
public class SpringApplicationAdminMXBeanRegistrar implements ApplicationContextAware,
|
||||
EnvironmentAware, InitializingBean, DisposableBean, ApplicationListener<ApplicationReadyEvent> {
|
||||
EnvironmentAware, InitializingBean, DisposableBean,
|
||||
ApplicationListener<ApplicationReadyEvent> {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(SpringApplicationAdmin.class);
|
||||
|
||||
|
|
@ -105,13 +106,13 @@ public class SpringApplicationAdminMXBeanRegistrar implements ApplicationContext
|
|||
|
||||
@Override
|
||||
public boolean isEmbeddedWebApplication() {
|
||||
return (applicationContext != null
|
||||
&& applicationContext instanceof EmbeddedWebApplicationContext);
|
||||
return (SpringApplicationAdminMXBeanRegistrar.this.applicationContext != null && SpringApplicationAdminMXBeanRegistrar.this.applicationContext instanceof EmbeddedWebApplicationContext);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getProperty(String key) {
|
||||
return environment.getProperty(key);
|
||||
return SpringApplicationAdminMXBeanRegistrar.this.environment
|
||||
.getProperty(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -48,6 +48,7 @@ import org.springframework.util.StringUtils;
|
|||
*
|
||||
* @author Dave Syer
|
||||
* @author Phillip Webb
|
||||
* @since 1.3.0
|
||||
*/
|
||||
public class ServerPortInfoApplicationContextInitializer implements
|
||||
ApplicationContextInitializer<ConfigurableApplicationContext> {
|
||||
|
|
@ -81,7 +82,8 @@ public class ServerPortInfoApplicationContextInitializer implements
|
|||
|
||||
private void setPortProperty(ApplicationContext context, String propertyName, int port) {
|
||||
if (context instanceof ConfigurableApplicationContext) {
|
||||
ConfigurableEnvironment environment = ((ConfigurableApplicationContext) context).getEnvironment();
|
||||
ConfigurableEnvironment environment = ((ConfigurableApplicationContext) context)
|
||||
.getEnvironment();
|
||||
MutablePropertySources sources = environment.getPropertySources();
|
||||
Map<String, Object> map;
|
||||
if (!sources.contains("server.ports")) {
|
||||
|
|
@ -91,8 +93,8 @@ public class ServerPortInfoApplicationContextInitializer implements
|
|||
}
|
||||
else {
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> value = (Map<String, Object>) sources.get("server.ports")
|
||||
.getSource();
|
||||
Map<String, Object> value = (Map<String, Object>) sources.get(
|
||||
"server.ports").getSource();
|
||||
map = value;
|
||||
}
|
||||
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) {
|
||||
try {
|
||||
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) {
|
||||
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.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.web.context.ServletContextAware;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.context.request.SessionScope;
|
||||
|
|
@ -151,8 +152,9 @@ public class EmbeddedWebApplicationContextTests {
|
|||
addEmbeddedServletContainerFactoryBean();
|
||||
new ServerPortInfoApplicationContextInitializer().initialize(this.context);
|
||||
this.context.refresh();
|
||||
assertTrue(this.context.getEnvironment().containsProperty("local.server.port"));
|
||||
assertEquals("8080", this.context.getEnvironment().getProperty("local.server.port"));
|
||||
ConfigurableEnvironment environment = this.context.getEnvironment();
|
||||
assertTrue(environment.containsProperty("local.server.port"));
|
||||
assertEquals("8080", environment.getProperty("local.server.port"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
|||
Loading…
Reference in New Issue