From c3b344fdc2d2074f3dc1f451e0f56844fa9462f1 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 17 Jun 2015 12:42:06 -0700 Subject: [PATCH] Polish --- ...icationAdminJmxAutoConfigurationTests.java | 21 +++++---- .../web/WebMvcAutoConfigurationTests.java | 7 ++- .../admin/SpringApplicationAdminMXBean.java | 4 +- ...SpringApplicationAdminMXBeanRegistrar.java | 9 ++-- ...PortInfoApplicationContextInitializer.java | 8 ++-- ...PortInfoApplicationContextInitializer.java | 47 +++++++++++++++++++ ...gApplicationAdminMXBeanRegistrarTests.java | 2 +- .../EmbeddedWebApplicationContextTests.java | 6 ++- 8 files changed, 78 insertions(+), 26 deletions(-) create mode 100644 spring-boot/src/main/java/org/springframework/boot/test/ServerPortInfoApplicationContextInitializer.java rename spring-boot/src/{main => test}/java/org/springframework/boot/admin/SpringApplicationAdminMXBeanRegistrarTests.java (98%) diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/admin/SpringApplicationAdminJmxAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/admin/SpringApplicationAdminJmxAutoConfigurationTests.java index abeb308b24..27e842b1ea 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/admin/SpringApplicationAdminJmxAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/admin/SpringApplicationAdminJmxAutoConfigurationTests.java @@ -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) { diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfigurationTests.java index ed1f184b25..9a743a9339 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfigurationTests.java @@ -136,7 +136,7 @@ public class WebMvcAutoConfigurationTests { } @Test - public void resourceHandlerMappingOverrideAll() throws Exception { + public void resourceHandlerMappingOverrideAll() throws Exception { load(AllResources.class); Map> 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 { diff --git a/spring-boot/src/main/java/org/springframework/boot/admin/SpringApplicationAdminMXBean.java b/spring-boot/src/main/java/org/springframework/boot/admin/SpringApplicationAdminMXBean.java index 6b10bf6085..7aa1f67ad7 100644 --- a/spring-boot/src/main/java/org/springframework/boot/admin/SpringApplicationAdminMXBean.java +++ b/spring-boot/src/main/java/org/springframework/boot/admin/SpringApplicationAdminMXBean.java @@ -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() */ diff --git a/spring-boot/src/main/java/org/springframework/boot/admin/SpringApplicationAdminMXBeanRegistrar.java b/spring-boot/src/main/java/org/springframework/boot/admin/SpringApplicationAdminMXBeanRegistrar.java index 544343e98c..58f14f28b9 100644 --- a/spring-boot/src/main/java/org/springframework/boot/admin/SpringApplicationAdminMXBeanRegistrar.java +++ b/spring-boot/src/main/java/org/springframework/boot/admin/SpringApplicationAdminMXBeanRegistrar.java @@ -46,7 +46,8 @@ import org.springframework.util.Assert; * @since 1.3.0 */ public class SpringApplicationAdminMXBeanRegistrar implements ApplicationContextAware, - EnvironmentAware, InitializingBean, DisposableBean, ApplicationListener { + EnvironmentAware, InitializingBean, DisposableBean, + ApplicationListener { 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 diff --git a/spring-boot/src/main/java/org/springframework/boot/context/web/ServerPortInfoApplicationContextInitializer.java b/spring-boot/src/main/java/org/springframework/boot/context/web/ServerPortInfoApplicationContextInitializer.java index 180c6c9b84..770b3b9ad4 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/web/ServerPortInfoApplicationContextInitializer.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/web/ServerPortInfoApplicationContextInitializer.java @@ -48,6 +48,7 @@ import org.springframework.util.StringUtils; * * @author Dave Syer * @author Phillip Webb + * @since 1.3.0 */ public class ServerPortInfoApplicationContextInitializer implements ApplicationContextInitializer { @@ -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 map; if (!sources.contains("server.ports")) { @@ -91,8 +93,8 @@ public class ServerPortInfoApplicationContextInitializer implements } else { @SuppressWarnings("unchecked") - Map value = (Map) sources.get("server.ports") - .getSource(); + Map value = (Map) sources.get( + "server.ports").getSource(); map = value; } map.put(propertyName, port); diff --git a/spring-boot/src/main/java/org/springframework/boot/test/ServerPortInfoApplicationContextInitializer.java b/spring-boot/src/main/java/org/springframework/boot/test/ServerPortInfoApplicationContextInitializer.java new file mode 100644 index 0000000000..988ce075d8 --- /dev/null +++ b/spring-boot/src/main/java/org/springframework/boot/test/ServerPortInfoApplicationContextInitializer.java @@ -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}. + *

+ * 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"}. + *

+ * 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 { + +} diff --git a/spring-boot/src/main/java/org/springframework/boot/admin/SpringApplicationAdminMXBeanRegistrarTests.java b/spring-boot/src/test/java/org/springframework/boot/admin/SpringApplicationAdminMXBeanRegistrarTests.java similarity index 98% rename from spring-boot/src/main/java/org/springframework/boot/admin/SpringApplicationAdminMXBeanRegistrarTests.java rename to spring-boot/src/test/java/org/springframework/boot/admin/SpringApplicationAdminMXBeanRegistrarTests.java index 3d2c1ae0dc..1ed5aacec7 100644 --- a/spring-boot/src/main/java/org/springframework/boot/admin/SpringApplicationAdminMXBeanRegistrarTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/admin/SpringApplicationAdminMXBeanRegistrarTests.java @@ -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); diff --git a/spring-boot/src/test/java/org/springframework/boot/context/embedded/EmbeddedWebApplicationContextTests.java b/spring-boot/src/test/java/org/springframework/boot/context/embedded/EmbeddedWebApplicationContextTests.java index b48f567ceb..22f196be98 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/embedded/EmbeddedWebApplicationContextTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/embedded/EmbeddedWebApplicationContextTests.java @@ -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