diff --git a/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binder/AbstractExtendedBindingProperties.java b/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binder/AbstractExtendedBindingProperties.java index f064e65cb..e0b1d6fe5 100644 --- a/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binder/AbstractExtendedBindingProperties.java +++ b/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binder/AbstractExtendedBindingProperties.java @@ -32,7 +32,7 @@ import org.springframework.context.support.GenericApplicationContext; import org.springframework.integration.support.utils.IntegrationUtils; /** - * Vase implementation of {@link ExtendedBindingProperties} + * Base implementation of {@link ExtendedBindingProperties} * * @author Oleg Zhurakousky * diff --git a/spring-cloud-stream/src/test/java/org/springframework/cloud/stream/binder/ExtendedPropertiesDefaultTests.java b/spring-cloud-stream/src/test/java/org/springframework/cloud/stream/binder/ExtendedPropertiesDefaultTests.java new file mode 100644 index 000000000..72d527342 --- /dev/null +++ b/spring-cloud-stream/src/test/java/org/springframework/cloud/stream/binder/ExtendedPropertiesDefaultTests.java @@ -0,0 +1,69 @@ +/* + * Copyright 2018 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.binder; + +import java.util.Collections; +import java.util.HashMap; + +import org.junit.Test; + +import org.springframework.cloud.stream.utils.FooConsumerProperties; +import org.springframework.cloud.stream.utils.FooProducerProperties; +import org.springframework.cloud.stream.utils.MockExtendedBinderConfiguration; +import org.springframework.messaging.MessageChannel; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Soby Chacko + */ +public class ExtendedPropertiesDefaultTests { + + @Test + public void testExtendedDefaultProducerProperties() { + DefaultBinderFactory binderFactory = createMockExtendedBinderFactory(); + Binder binder = binderFactory.getBinder(null, MessageChannel.class); + FooProducerProperties fooProducerProperties = + (FooProducerProperties) ((ExtendedPropertiesBinder) binder) + .getExtendedProducerProperties("output"); + // Expectations are set in the mock configuration for the binder factory + assertThat(fooProducerProperties.getExtendedProperty()).isEqualTo("someFancyExtension"); + } + + @Test + public void testExtendedDefaultConsumerProperties() { + DefaultBinderFactory binderFactory = createMockExtendedBinderFactory(); + Binder binder = binderFactory.getBinder(null, MessageChannel.class); + FooConsumerProperties fooConsumerProperties = + (FooConsumerProperties)((ExtendedPropertiesBinder)binder) + .getExtendedConsumerProperties("input"); + // Expectations are set in the mock configuration for the binder factory + assertThat(fooConsumerProperties.getExtendedProperty()).isEqualTo("someFancyExtension"); + } + + private DefaultBinderFactory createMockExtendedBinderFactory() { + BinderTypeRegistry binderTypeRegistry = createMockExtendedBinderTypeRegistry(); + return new DefaultBinderFactory( + Collections.singletonMap("mock", new BinderConfiguration("mock", new HashMap<>(), true, true)), + binderTypeRegistry); + } + + private DefaultBinderTypeRegistry createMockExtendedBinderTypeRegistry() { + return new DefaultBinderTypeRegistry(Collections.singletonMap("mock", + new BinderType("mock", new Class[] { MockExtendedBinderConfiguration.class }))); + } +} 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 db0d17e52..4c99174cd 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 @@ -27,7 +27,6 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; -import org.junit.Ignore; import org.junit.Test; import org.mockito.ArgumentCaptor; import org.mockito.Mockito; @@ -59,15 +58,9 @@ import org.springframework.cloud.stream.config.BindingServiceConfiguration; import org.springframework.cloud.stream.config.BindingServiceProperties; import org.springframework.cloud.stream.converter.CompositeMessageConverterFactory; import org.springframework.cloud.stream.reflection.GenericsUtils; -import org.springframework.cloud.stream.utils.FooExtendedConsumerProperties; -import org.springframework.cloud.stream.utils.FooExtendedProducerProperties; import org.springframework.cloud.stream.utils.MockBinderConfiguration; -import org.springframework.cloud.stream.utils.MockExtendedBinderConfiguration; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Bean; -import org.springframework.core.env.ConfigurableEnvironment; -import org.springframework.core.env.MapPropertySource; -import org.springframework.core.env.StandardEnvironment; import org.springframework.integration.channel.DirectChannel; import org.springframework.integration.test.util.TestUtils; import org.springframework.messaging.MessageChannel; @@ -365,72 +358,6 @@ public class BindingServiceTests { } } - @Test - @Ignore - public void testExtendedDefaultProducerProperties() { - BindingServiceProperties serviceProperties = new BindingServiceProperties(); - Map bindingProperties = new HashMap<>(); - BindingProperties props = new BindingProperties(); - ProducerProperties producerProperties = new ProducerProperties(); - props.setDestination("dest"); - props.setProducer(producerProperties); - final String outputChannelName = "output"; - bindingProperties.put(outputChannelName, props); - serviceProperties.setBindings(bindingProperties); - - DefaultBinderFactory binderFactory = createMockExtendedBinderFactory(); - - ConfigurableEnvironment environment = new StandardEnvironment(); - Map propertiesToAdd = new HashMap<>(); - propertiesToAdd.put("spring.cloud.stream.foo.default.producer.extendedProperty", "someFancyExtension"); - environment.getPropertySources().addLast(new MapPropertySource("extPropertiesConfig", propertiesToAdd)); - - BindingService service = new BindingService(serviceProperties, binderFactory, null); - MessageChannel outputChannel = new DirectChannel(); - - Binder binder = binderFactory.getBinder(null, MessageChannel.class); - FooExtendedProducerProperties fooExtendedProducerProperties = - (FooExtendedProducerProperties)((ExtendedPropertiesBinder)binder).getExtendedProducerProperties("output"); - assertThat(fooExtendedProducerProperties.getExtendedProperty()).isNull(); - - service.bindProducer(outputChannel, outputChannelName); - - assertThat(fooExtendedProducerProperties.getExtendedProperty()).isEqualTo("someFancyExtension"); - } - - @Test - @Ignore - public void testExtendedDefaultConsumerProperties() { - BindingServiceProperties serviceProperties = new BindingServiceProperties(); - Map bindingProperties = new HashMap<>(); - BindingProperties props = new BindingProperties(); - ConsumerProperties consumerProperties = new ConsumerProperties(); - props.setDestination("dest"); - props.setConsumer(consumerProperties); - final String inputChannelName = "input"; - bindingProperties.put(inputChannelName, props); - serviceProperties.setBindings(bindingProperties); - - DefaultBinderFactory binderFactory = createMockExtendedBinderFactory(); - - ConfigurableEnvironment environment = new StandardEnvironment(); - Map propertiesToAdd = new HashMap<>(); - propertiesToAdd.put("spring.cloud.stream.foo.default.consumer.extendedProperty", "someFancyExtension"); - environment.getPropertySources().addLast(new MapPropertySource("extPropertiesConfig", propertiesToAdd)); - - BindingService service = new BindingService(serviceProperties, binderFactory, null); - MessageChannel inputChannel = new DirectChannel(); - - Binder binder = binderFactory.getBinder(null, MessageChannel.class); - FooExtendedConsumerProperties fooExtendedConsumerProperties = - (FooExtendedConsumerProperties)((ExtendedPropertiesBinder)binder).getExtendedConsumerProperties("input"); - assertThat(fooExtendedConsumerProperties.getExtendedProperty()).isNull(); - - service.bindConsumer(inputChannel, inputChannelName); - - assertThat(fooExtendedConsumerProperties.getExtendedProperty()).isEqualTo("someFancyExtension"); - } - @Test public void testDefaultPropertyBehavior() { ConfigurableApplicationContext run = SpringApplication.run(DefaultConsumerPropertiesTestSink.class, @@ -689,24 +616,11 @@ public class BindingServiceTests { binderTypeRegistry); } - private DefaultBinderFactory createMockExtendedBinderFactory() { - BinderTypeRegistry binderTypeRegistry = createMockExtendedBinderTypeRegistry(); - return new DefaultBinderFactory( - Collections.singletonMap("mock", new BinderConfiguration("mock", new HashMap<>(), true, true)), - binderTypeRegistry); - } - - private DefaultBinderTypeRegistry createMockBinderTypeRegistry() { return new DefaultBinderTypeRegistry(Collections.singletonMap("mock", new BinderType("mock", new Class[] { MockBinderConfiguration.class }))); } - private DefaultBinderTypeRegistry createMockExtendedBinderTypeRegistry() { - return new DefaultBinderTypeRegistry(Collections.singletonMap("mock", - new BinderType("mock", new Class[] { MockExtendedBinderConfiguration.class }))); - } - private BindingServiceProperties createBindingServiceProperties(HashMap properties) { BindingServiceProperties bindingServiceProperties = new BindingServiceProperties(); org.springframework.boot.context.properties.bind.Binder propertiesBinder = new org.springframework.boot.context.properties.bind.Binder(new MapConfigurationPropertySource(properties)); diff --git a/spring-cloud-stream/src/test/java/org/springframework/cloud/stream/utils/FooBindingProperties.java b/spring-cloud-stream/src/test/java/org/springframework/cloud/stream/utils/FooBindingProperties.java index 10f9c8a7c..c0c7ab006 100644 --- a/spring-cloud-stream/src/test/java/org/springframework/cloud/stream/utils/FooBindingProperties.java +++ b/spring-cloud-stream/src/test/java/org/springframework/cloud/stream/utils/FooBindingProperties.java @@ -23,23 +23,23 @@ import org.springframework.cloud.stream.binder.BinderSpecificPropertiesProvider; */ public class FooBindingProperties implements BinderSpecificPropertiesProvider { - private FooExtendedProducerProperties producer = new FooExtendedProducerProperties(); + private FooProducerProperties producer = new FooProducerProperties(); - private FooExtendedConsumerProperties consumer = new FooExtendedConsumerProperties(); + private FooConsumerProperties consumer = new FooConsumerProperties(); - public FooExtendedProducerProperties getProducer() { + public FooProducerProperties getProducer() { return producer; } - public void setProducer(FooExtendedProducerProperties producer) { + public void setProducer(FooProducerProperties producer) { this.producer = producer; } - public FooExtendedConsumerProperties getConsumer() { + public FooConsumerProperties getConsumer() { return consumer; } - public void setConsumer(FooExtendedConsumerProperties consumer) { + public void setConsumer(FooConsumerProperties consumer) { this.consumer = consumer; } } diff --git a/spring-cloud-stream/src/test/java/org/springframework/cloud/stream/utils/FooExtendedConsumerProperties.java b/spring-cloud-stream/src/test/java/org/springframework/cloud/stream/utils/FooConsumerProperties.java similarity index 95% rename from spring-cloud-stream/src/test/java/org/springframework/cloud/stream/utils/FooExtendedConsumerProperties.java rename to spring-cloud-stream/src/test/java/org/springframework/cloud/stream/utils/FooConsumerProperties.java index 8f3cfaf96..6bd3cb2e4 100644 --- a/spring-cloud-stream/src/test/java/org/springframework/cloud/stream/utils/FooExtendedConsumerProperties.java +++ b/spring-cloud-stream/src/test/java/org/springframework/cloud/stream/utils/FooConsumerProperties.java @@ -16,11 +16,10 @@ package org.springframework.cloud.stream.utils; - /** * @author Soby Chacko */ -public class FooExtendedConsumerProperties { +public class FooConsumerProperties { String extendedProperty; diff --git a/spring-cloud-stream/src/test/java/org/springframework/cloud/stream/utils/FooExtendedBindingProperties.java b/spring-cloud-stream/src/test/java/org/springframework/cloud/stream/utils/FooExtendedBindingProperties.java new file mode 100644 index 000000000..7df1746ea --- /dev/null +++ b/spring-cloud-stream/src/test/java/org/springframework/cloud/stream/utils/FooExtendedBindingProperties.java @@ -0,0 +1,41 @@ +/* + * Copyright 2018 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.utils; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.cloud.stream.binder.AbstractExtendedBindingProperties; +import org.springframework.cloud.stream.binder.BinderSpecificPropertiesProvider; + +/** + * @author Soby Chacko + */ +@ConfigurationProperties("spring.cloud.stream.foo") +public class FooExtendedBindingProperties extends AbstractExtendedBindingProperties { + + private static final String DEFAULTS_PREFIX = "spring.cloud.stream.foo.default"; + + @Override + public String getDefaultsPrefix() { + return DEFAULTS_PREFIX; + } + + @Override + public Class getExtendedPropertiesEntryClass() { + return FooBindingProperties.class; + } +} + diff --git a/spring-cloud-stream/src/test/java/org/springframework/cloud/stream/utils/FooExtendedProducerProperties.java b/spring-cloud-stream/src/test/java/org/springframework/cloud/stream/utils/FooProducerProperties.java similarity index 95% rename from spring-cloud-stream/src/test/java/org/springframework/cloud/stream/utils/FooExtendedProducerProperties.java rename to spring-cloud-stream/src/test/java/org/springframework/cloud/stream/utils/FooProducerProperties.java index 585650a32..69d4c2a42 100644 --- a/spring-cloud-stream/src/test/java/org/springframework/cloud/stream/utils/FooExtendedProducerProperties.java +++ b/spring-cloud-stream/src/test/java/org/springframework/cloud/stream/utils/FooProducerProperties.java @@ -16,11 +16,10 @@ package org.springframework.cloud.stream.utils; - /** * @author Soby Chacko */ -public class FooExtendedProducerProperties { +public class FooProducerProperties { String extendedProperty; diff --git a/spring-cloud-stream/src/test/java/org/springframework/cloud/stream/utils/MockExtendedBinderConfiguration.java b/spring-cloud-stream/src/test/java/org/springframework/cloud/stream/utils/MockExtendedBinderConfiguration.java index 8767f06c4..c55c58e11 100644 --- a/spring-cloud-stream/src/test/java/org/springframework/cloud/stream/utils/MockExtendedBinderConfiguration.java +++ b/spring-cloud-stream/src/test/java/org/springframework/cloud/stream/utils/MockExtendedBinderConfiguration.java @@ -16,12 +16,20 @@ package org.springframework.cloud.stream.utils; +import java.util.HashMap; +import java.util.Map; + import org.mockito.Mockito; import org.springframework.cloud.stream.binder.Binder; import org.springframework.cloud.stream.binder.ExtendedPropertiesBinder; +import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.support.GenericApplicationContext; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.MapPropertySource; +import org.springframework.core.env.StandardEnvironment; import static org.mockito.Mockito.when; @@ -36,14 +44,27 @@ public class MockExtendedBinderConfiguration { public Binder extendedPropertiesBinder() { Binder mock = Mockito.mock(Binder.class, Mockito.withSettings().defaultAnswer(Mockito.RETURNS_MOCKS) .extraInterfaces(ExtendedPropertiesBinder.class)); - when (((ExtendedPropertiesBinder)mock).getExtendedProducerProperties("output")) - .thenReturn(new FooExtendedProducerProperties()); + ConfigurableEnvironment environment = new StandardEnvironment(); + Map propertiesToAdd = new HashMap<>(); + propertiesToAdd.put("spring.cloud.stream.foo.default.consumer.extendedProperty", "someFancyExtension"); + propertiesToAdd.put("spring.cloud.stream.foo.default.producer.extendedProperty", "someFancyExtension"); + environment.getPropertySources().addLast(new MapPropertySource("extPropertiesConfig", propertiesToAdd)); + + ConfigurableApplicationContext applicationContext = new GenericApplicationContext(); + applicationContext.setEnvironment(environment); + + FooExtendedBindingProperties fooExtendedBindingProperties = new FooExtendedBindingProperties(); + fooExtendedBindingProperties.setApplicationContext(applicationContext); + + final FooConsumerProperties fooConsumerProperties = fooExtendedBindingProperties.getExtendedConsumerProperties("input"); + final FooProducerProperties fooProducerProperties = fooExtendedBindingProperties.getExtendedProducerProperties("output"); + when (((ExtendedPropertiesBinder)mock).getExtendedConsumerProperties("input")) - .thenReturn(new FooExtendedConsumerProperties()); - when (((ExtendedPropertiesBinder)mock).getDefaultsPrefix()) - .thenReturn("spring.cloud.stream.foo.default"); - when (((ExtendedPropertiesBinder)mock).getExtendedPropertiesEntryClass()) - .thenReturn(FooBindingProperties.class); + .thenReturn(fooConsumerProperties); + + when (((ExtendedPropertiesBinder)mock).getExtendedProducerProperties("output")) + .thenReturn(fooProducerProperties); + return mock; }