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(..)
This commit is contained in:
Walliee
2019-05-09 01:30:02 -04:00
committed by Oleg Zhurakousky
parent 3d192b8fb5
commit 576297c88e
3 changed files with 36 additions and 0 deletions

View File

@@ -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);
}

View File

@@ -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()

View File

@@ -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