diff --git a/spring-cloud-stream-test-support/src/main/java/org/springframework/cloud/stream/test/binder/TestBinderEnvironmentPostProcessor.java b/spring-cloud-stream-test-support/src/main/java/org/springframework/cloud/stream/test/binder/TestBinderEnvironmentPostProcessor.java new file mode 100644 index 000000000..1879617c6 --- /dev/null +++ b/spring-cloud-stream-test-support/src/main/java/org/springframework/cloud/stream/test/binder/TestBinderEnvironmentPostProcessor.java @@ -0,0 +1,40 @@ +/* + * Copyright 2017 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.cloud.stream.test.binder; + +import java.util.HashMap; +import java.util.Map; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.env.EnvironmentPostProcessor; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.MapPropertySource; + +/** + * An {@link EnvironmentPostProcessor} that sets some configuration properties for {@link TestSupportBinder}. + * + * @author Ilayaperumal Gopinathan + */ +public class TestBinderEnvironmentPostProcessor implements EnvironmentPostProcessor { + + @Override + public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { + Map propertiesToAdd = new HashMap<>(); + propertiesToAdd.put("spring.cloud.stream.binders.test.defaultCandidate", "false"); + environment.getPropertySources().addLast(new MapPropertySource("testBinderConfig", propertiesToAdd)); + } +} diff --git a/spring-cloud-stream-test-support/src/main/resources/META-INF/spring.factories b/spring-cloud-stream-test-support/src/main/resources/META-INF/spring.factories index 5ce24e778..94bec2048 100644 --- a/spring-cloud-stream-test-support/src/main/resources/META-INF/spring.factories +++ b/spring-cloud-stream-test-support/src/main/resources/META-INF/spring.factories @@ -1,2 +1,4 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration:\ -org.springframework.cloud.stream.test.binder.TestSupportBinderAutoConfiguration \ No newline at end of file +org.springframework.cloud.stream.test.binder.TestSupportBinderAutoConfiguration +org.springframework.boot.env.EnvironmentPostProcessor=\ + org.springframework.cloud.stream.test.binder.TestBinderEnvironmentPostProcessor diff --git a/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/config/BinderFactoryConfiguration.java b/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/config/BinderFactoryConfiguration.java index 84d1ae87a..edcb7b189 100644 --- a/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/config/BinderFactoryConfiguration.java +++ b/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/config/BinderFactoryConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2015-2016 the original author or authors. + * Copyright 2015-2017 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. @@ -23,6 +23,7 @@ import java.util.Collection; import java.util.Enumeration; import java.util.HashMap; import java.util.Iterator; +import java.util.List; import java.util.Map; import java.util.Properties; @@ -70,6 +71,7 @@ public class BinderFactoryConfiguration { while (!defaultCandidatesExist && binderPropertiesIterator.hasNext()) { defaultCandidatesExist = binderPropertiesIterator.next().getValue().isDefaultCandidate(); } + List existingBinderConfigurations = new ArrayList<>(); for (Map.Entry binderEntry : declaredBinders.entrySet()) { BinderProperties binderProperties = binderEntry.getValue(); if (binderTypeRegistry.get(binderEntry.getKey()) != null) { @@ -77,6 +79,7 @@ public class BinderFactoryConfiguration { new BinderConfiguration(binderTypeRegistry.get(binderEntry.getKey()), binderProperties.getEnvironment(), binderProperties.isInheritEnvironment(), binderProperties.isDefaultCandidate())); + existingBinderConfigurations.add(binderEntry.getKey()); } else { Assert.hasText(binderProperties.getType(), @@ -86,12 +89,20 @@ public class BinderFactoryConfiguration { binderConfigurations.put(binderEntry.getKey(), new BinderConfiguration(binderType, binderProperties.getEnvironment(), binderProperties.isInheritEnvironment(), binderProperties.isDefaultCandidate())); + existingBinderConfigurations.add(binderEntry.getKey()); + } + } + for (Map.Entry configurationEntry: binderConfigurations.entrySet()) { + if (configurationEntry.getValue().isDefaultCandidate()) { + defaultCandidatesExist = true; } } if (!defaultCandidatesExist) { - for (Map.Entry entry : binderTypeRegistry.getAll().entrySet()) { - binderConfigurations.put(entry.getKey(), - new BinderConfiguration(entry.getValue(), new Properties(), true, true)); + for (Map.Entry binderEntry : binderTypeRegistry.getAll().entrySet()) { + if (!existingBinderConfigurations.contains(binderEntry.getKey())) { + binderConfigurations.put(binderEntry.getKey(), new BinderConfiguration(binderEntry.getValue(), + new Properties(), true, true)); + } } } DefaultBinderFactory binderFactory = new DefaultBinderFactory(binderConfigurations); @@ -117,7 +128,7 @@ public class BinderFactoryConfiguration { URL url = resources.nextElement(); UrlResource resource = new UrlResource(url); for (BinderType binderType : parseBinderConfigurations(classLoader, resource)) { - binderTypes.put(binderType.getDefaultName(), binderType); + binderTypes.put(binderType.getDefaultName(), binderType); } } }