From 576297c88eae87dbd87c11aed8a1bd138af69e8e Mon Sep 17 00:00:00 2001 From: Walliee Date: Thu, 9 May 2019 01:30:02 -0400 Subject: [PATCH] Fix binder bootstrap when spring.main.web-application-type is set resolves #1708 Fix BindServiceTests.testDefaultPropertyBehavior(..) Fix binder bootstrap when spring.main.web-application-type is set resolves #1708 Fix BindServiceTests.testDefaultPropertyBehavior(..) --- .../stream/binder/DefaultBinderFactory.java | 11 ++++++++++ .../BinderFactoryAutoConfigurationTests.java | 22 +++++++++++++++++++ .../stream/binding/BindingServiceTests.java | 3 +++ 3 files changed, 36 insertions(+) diff --git a/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binder/DefaultBinderFactory.java b/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binder/DefaultBinderFactory.java index aeb18fc0e..dd7160ca0 100644 --- a/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binder/DefaultBinderFactory.java +++ b/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binder/DefaultBinderFactory.java @@ -38,6 +38,7 @@ import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.StandardEnvironment; import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; @@ -52,6 +53,7 @@ import org.springframework.util.StringUtils; * @author Oleg Zhurakousky * @author Soby Chacko * @author Artem Bilan + * @author Anshul Mehra */ public class DefaultBinderFactory implements BinderFactory, DisposableBean, ApplicationContextAware { @@ -285,6 +287,15 @@ public class DefaultBinderFactory binderEnvironment.merge(environment); // See ConfigurationPropertySources.ATTACHED_PROPERTY_SOURCE_NAME binderEnvironment.getPropertySources().remove("configurationProperties"); + /* + * Ensure that the web mode is set to NONE despite what the + * parent application context says. + * https://github.com/spring-cloud/spring-cloud-stream/issues/1708 + */ + binderEnvironment.getPropertySources() + .addFirst(new MapPropertySource("defaultBinderFactoryProperties", + Collections.singletonMap("spring.main.web-application-type", "NONE"))); + springApplicationBuilder.environment(binderEnvironment); } diff --git a/spring-cloud-stream/src/test/java/org/springframework/cloud/stream/binder/BinderFactoryAutoConfigurationTests.java b/spring-cloud-stream/src/test/java/org/springframework/cloud/stream/binder/BinderFactoryAutoConfigurationTests.java index c398e6766..3e9f85e64 100644 --- a/spring-cloud-stream/src/test/java/org/springframework/cloud/stream/binder/BinderFactoryAutoConfigurationTests.java +++ b/spring-cloud-stream/src/test/java/org/springframework/cloud/stream/binder/BinderFactoryAutoConfigurationTests.java @@ -24,6 +24,7 @@ import org.junit.Test; import org.springframework.boot.WebApplicationType; import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; +import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.stream.annotation.EnableBinding; import org.springframework.cloud.stream.binder.stub1.StubBinder1; @@ -51,6 +52,7 @@ import static org.junit.Assert.fail; * @author Ilayaperumal Gopinathan * @author Soby Chacko * @author Artem Bilan + * @author Anshul Mehra */ public class BinderFactoryAutoConfigurationTests { @@ -129,6 +131,26 @@ public class BinderFactoryAutoConfigurationTests { assertThat(binder1).hasFieldOrPropertyWithValue("name", "foo"); } + /* + * See https://github.com/spring-cloud/spring-cloud-stream/issues/1708 + */ + @SuppressWarnings("rawtypes") + @Test + public void loadBinderTypeRegistryWithSharedEnvironmentAndServletWebApplicationType() + throws Exception { + String[] properties = new String[] {"binder1.name=foo", "spring.main.web-application-type=SERVLET"}; + ClassLoader classLoader = createClassLoader(new String[] { "binder1" }, + properties); + ConfigurableApplicationContext context = new SpringApplicationBuilder(SimpleApplication.class, ServletWebServerFactoryAutoConfiguration.class) + .resourceLoader(new DefaultResourceLoader(classLoader)) + .properties(properties).web(WebApplicationType.SERVLET).run(); + + BinderFactory binderFactory = context.getBean(BinderFactory.class); + + Binder binder1 = binderFactory.getBinder("binder1", MessageChannel.class); + assertThat(binder1).hasFieldOrPropertyWithValue("name", "foo"); + } + @SuppressWarnings("rawtypes") @Test public void loadBinderTypeRegistryWithOneCustomBinderAndSharedEnvironment() diff --git a/spring-cloud-stream/src/test/java/org/springframework/cloud/stream/binding/BindingServiceTests.java b/spring-cloud-stream/src/test/java/org/springframework/cloud/stream/binding/BindingServiceTests.java index 7714831a3..b7a6375c7 100644 --- a/spring-cloud-stream/src/test/java/org/springframework/cloud/stream/binding/BindingServiceTests.java +++ b/spring-cloud-stream/src/test/java/org/springframework/cloud/stream/binding/BindingServiceTests.java @@ -378,6 +378,7 @@ public class BindingServiceTests { public void testDefaultPropertyBehavior() { ConfigurableApplicationContext run = SpringApplication.run( DefaultConsumerPropertiesTestSink.class, + "--server.port=0", "--spring.cloud.stream.default.contentType=text/plain", "--spring.cloud.stream.bindings.input1.contentType=application/json", "--spring.cloud.stream.default.group=foo", @@ -416,6 +417,8 @@ public class BindingServiceTests { .isEqualTo("text/plain"); assertThat(bindings.get("input_snake_case").getContentType()) .isEqualTo("application/avro"); + + run.close(); } @Test