Exclude test support binder from binder type registry

- Given the TestSupportBinder has its own BinderFactory that returns the TestSupportBinder, we need to exclude the BinderType entry of TestSupportBinder detected from classpath
  - This will make sure to exclude test support binder when TestSupportBinderAutoConfiguration is excluded when using other explicit binder in tests

Resolves #827

Set defaultCandidate to false for TestSupportBinder

 - Set `defaultCandidate` to false for the TestSupportBinder using an environment post processor
 - At BinderFactoryConfiguration, don't override the 'BinderProperties' set via declared binders
   - Set `defaultCandidate` to `true` only for the binders set via BinderTypeRegistry without overriding their binder properties
This commit is contained in:
Ilayaperumal Gopinathan
2017-03-09 17:55:34 +05:30
committed by Marius Bogoevici
parent 8e87f2bece
commit 69cbc7f05b
3 changed files with 59 additions and 6 deletions

View File

@@ -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<String, Object> propertiesToAdd = new HashMap<>();
propertiesToAdd.put("spring.cloud.stream.binders.test.defaultCandidate", "false");
environment.getPropertySources().addLast(new MapPropertySource("testBinderConfig", propertiesToAdd));
}
}

View File

@@ -1,2 +1,4 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration:\
org.springframework.cloud.stream.test.binder.TestSupportBinderAutoConfiguration
org.springframework.cloud.stream.test.binder.TestSupportBinderAutoConfiguration
org.springframework.boot.env.EnvironmentPostProcessor=\
org.springframework.cloud.stream.test.binder.TestBinderEnvironmentPostProcessor

View File

@@ -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<String> existingBinderConfigurations = new ArrayList<>();
for (Map.Entry<String, BinderProperties> 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<String, BinderConfiguration> configurationEntry: binderConfigurations.entrySet()) {
if (configurationEntry.getValue().isDefaultCandidate()) {
defaultCandidatesExist = true;
}
}
if (!defaultCandidatesExist) {
for (Map.Entry<String, BinderType> entry : binderTypeRegistry.getAll().entrySet()) {
binderConfigurations.put(entry.getKey(),
new BinderConfiguration(entry.getValue(), new Properties(), true, true));
for (Map.Entry<String, BinderType> 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);
}
}
}