From 1bd52bc4324a1089c311ffd4adeacfabf05803c4 Mon Sep 17 00:00:00 2001 From: Dmytro Nosan Date: Fri, 20 Jul 2018 18:14:56 +0300 Subject: [PATCH 1/2] Add PropertyMapper.from(value) See gh-13837 --- .../batch/BasicBatchConfigurer.java | 2 +- ...fkaListenerContainerFactoryConfigurer.java | 5 ++-- .../TomcatWebServerFactoryCustomizer.java | 2 +- .../UndertowWebServerFactoryCustomizer.java | 2 +- .../web/servlet/MultipartProperties.java | 8 +++--- .../context/properties/PropertyMapper.java | 12 ++++++++ .../boot/task/TaskExecutorBuilder.java | 15 +++++----- .../boot/task/TaskSchedulerBuilder.java | 4 +-- .../client/WebServiceTemplateBuilder.java | 11 ++++---- .../properties/PropertyMapperTests.java | 28 ++++++++++++++++++- 10 files changed, 62 insertions(+), 27 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BasicBatchConfigurer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BasicBatchConfigurer.java index f14aed862d..6a91a053be 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BasicBatchConfigurer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BasicBatchConfigurer.java @@ -122,7 +122,7 @@ public class BasicBatchConfigurer implements BatchConfigurer { protected JobRepository createJobRepository() throws Exception { JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean(); PropertyMapper map = PropertyMapper.get(); - map.from(() -> this.dataSource).to(factory::setDataSource); + map.from(this.dataSource).to(factory::setDataSource); map.from(this::determineIsolationLevel).whenNonNull() .to(factory::setIsolationLevelForCreate); map.from(this.properties::getTablePrefix).whenHasText() diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/kafka/ConcurrentKafkaListenerContainerFactoryConfigurer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/kafka/ConcurrentKafkaListenerContainerFactoryConfigurer.java index 5a12fad006..bdc00693f5 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/kafka/ConcurrentKafkaListenerContainerFactoryConfigurer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/kafka/ConcurrentKafkaListenerContainerFactoryConfigurer.java @@ -85,9 +85,8 @@ public class ConcurrentKafkaListenerContainerFactoryConfigurer { PropertyMapper map = PropertyMapper.get(); Listener properties = this.properties.getListener(); map.from(properties::getConcurrency).whenNonNull().to(factory::setConcurrency); - map.from(() -> this.messageConverter).whenNonNull() - .to(factory::setMessageConverter); - map.from(() -> this.replyTemplate).whenNonNull().to(factory::setReplyTemplate); + map.from(this.messageConverter).whenNonNull().to(factory::setMessageConverter); + map.from(this.replyTemplate).whenNonNull().to(factory::setReplyTemplate); map.from(properties::getType).whenEqualTo(Listener.Type.BATCH) .toCall(() -> factory.setBatchListener(true)); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizer.java index 3563696112..06a4dc21fc 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizer.java @@ -84,7 +84,7 @@ public class TomcatWebServerFactoryCustomizer implements tomcatProperties.getMaxThreads())); propertyMapper.from(tomcatProperties::getMinSpareThreads).when(this::isPositive) .to((minSpareThreads) -> customizeMinThreads(factory, minSpareThreads)); - propertyMapper.from(() -> determineMaxHttpHeaderSize()).when(this::isPositive) + propertyMapper.from(this::determineMaxHttpHeaderSize).when(this::isPositive) .to((maxHttpHeaderSize) -> customizeMaxHttpHeaderSize(factory, maxHttpHeaderSize)); propertyMapper.from(tomcatProperties::getMaxSwallowSize).whenNonNull() diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/UndertowWebServerFactoryCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/UndertowWebServerFactoryCustomizer.java index 43f350d59b..985128cfd3 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/UndertowWebServerFactoryCustomizer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/UndertowWebServerFactoryCustomizer.java @@ -81,7 +81,7 @@ public class UndertowWebServerFactoryCustomizer implements .to(factory::setAccessLogSuffix); propertyMapper.from(accesslogProperties::isRotate) .to(factory::setAccessLogRotate); - propertyMapper.from(() -> getOrDeduceUseForwardHeaders()) + propertyMapper.from(this::getOrDeduceUseForwardHeaders) .to(factory::setUseForwardHeaders); propertyMapper.from(properties::getMaxHttpHeaderSize).when(this::isPositive) .to((maxHttpHeaderSize) -> customizeMaxHttpHeaderSize(factory, diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/MultipartProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/MultipartProperties.java index 8331ff0bf6..8a43adbd2f 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/MultipartProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/MultipartProperties.java @@ -138,10 +138,10 @@ public class MultipartProperties { public MultipartConfigElement createMultipartConfig() { MultipartConfigFactory factory = new MultipartConfigFactory(); PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull(); - map.from(() -> this.fileSizeThreshold).to(factory::setFileSizeThreshold); - map.from(() -> this.location).whenHasText().to(factory::setLocation); - map.from(() -> this.maxRequestSize).to(factory::setMaxRequestSize); - map.from(() -> this.maxFileSize).to(factory::setMaxFileSize); + map.from(this.fileSizeThreshold).to(factory::setFileSizeThreshold); + map.from(this.location).whenHasText().to(factory::setLocation); + map.from(this.maxRequestSize).to(factory::setMaxRequestSize); + map.from(this.maxFileSize).to(factory::setMaxFileSize); return factory.createMultipartConfig(); } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/PropertyMapper.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/PropertyMapper.java index 75ba1d9d9b..cbe9ad0ddb 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/PropertyMapper.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/PropertyMapper.java @@ -91,6 +91,18 @@ public final class PropertyMapper { return new PropertyMapper(this, operator); } + /** + * Return a new {@link Source} from the specified value that can be used to perform + * the mapping. + * @param the source type + * @param value the value + * @return a {@link Source} that can be used to complete the mapping + * @see #from(Supplier) + */ + public Source from(T value) { + return from(() -> value); + } + /** * Return a new {@link Source} from the specified value supplier that can be used to * perform the mapping. diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/task/TaskExecutorBuilder.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/task/TaskExecutorBuilder.java index ca11bb530c..2e959f2864 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/task/TaskExecutorBuilder.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/task/TaskExecutorBuilder.java @@ -279,16 +279,15 @@ public class TaskExecutorBuilder { */ public T configure(T taskExecutor) { PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull(); - map.from(() -> this.queueCapacity).to(taskExecutor::setQueueCapacity); - map.from(() -> this.corePoolSize).to(taskExecutor::setCorePoolSize); - map.from(() -> this.maxPoolSize).to(taskExecutor::setMaxPoolSize); - map.from(() -> this.keepAlive).asInt(Duration::getSeconds) + map.from(this.queueCapacity).to(taskExecutor::setQueueCapacity); + map.from(this.corePoolSize).to(taskExecutor::setCorePoolSize); + map.from(this.maxPoolSize).to(taskExecutor::setMaxPoolSize); + map.from(this.keepAlive).asInt(Duration::getSeconds) .to(taskExecutor::setKeepAliveSeconds); - map.from(() -> this.allowCoreThreadTimeOut) - .to(taskExecutor::setAllowCoreThreadTimeOut); - map.from(() -> this.threadNamePrefix).whenHasText() + map.from(this.allowCoreThreadTimeOut).to(taskExecutor::setAllowCoreThreadTimeOut); + map.from(this.threadNamePrefix).whenHasText() .to(taskExecutor::setThreadNamePrefix); - map.from(() -> this.taskDecorator).to(taskExecutor::setTaskDecorator); + map.from(this.taskDecorator).to(taskExecutor::setTaskDecorator); if (!CollectionUtils.isEmpty(this.taskExecutorCustomizers)) { for (TaskExecutorCustomizer customizer : this.taskExecutorCustomizers) { diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/task/TaskSchedulerBuilder.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/task/TaskSchedulerBuilder.java index 228ca4c437..ed4f71916c 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/task/TaskSchedulerBuilder.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/task/TaskSchedulerBuilder.java @@ -167,8 +167,8 @@ public class TaskSchedulerBuilder { */ public T configure(T taskScheduler) { PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull(); - map.from(() -> this.poolSize).to(taskScheduler::setPoolSize); - map.from(() -> this.threadNamePrefix).to(taskScheduler::setThreadNamePrefix); + map.from(this.poolSize).to(taskScheduler::setPoolSize); + map.from(this.threadNamePrefix).to(taskScheduler::setThreadNamePrefix); if (!CollectionUtils.isEmpty(this.taskSchedulerCustomizers)) { for (TaskSchedulerCustomizer customizer : this.taskSchedulerCustomizers) { diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/webservices/client/WebServiceTemplateBuilder.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/webservices/client/WebServiceTemplateBuilder.java index 1baf8d7196..5481a84702 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/webservices/client/WebServiceTemplateBuilder.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/webservices/client/WebServiceTemplateBuilder.java @@ -502,13 +502,12 @@ public class WebServiceTemplateBuilder { configureMessageSenders(webServiceTemplate); PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull(); applyCustomizers(webServiceTemplate, this.internalCustomizers); - map.from(() -> this.marshaller).to(webServiceTemplate::setMarshaller); - map.from(() -> this.unmarshaller).to(webServiceTemplate::setUnmarshaller); - map.from(() -> this.destinationProvider) - .to(webServiceTemplate::setDestinationProvider); - map.from(() -> this.transformerFactoryClass) + map.from(this.marshaller).to(webServiceTemplate::setMarshaller); + map.from(this.unmarshaller).to(webServiceTemplate::setUnmarshaller); + map.from(this.destinationProvider).to(webServiceTemplate::setDestinationProvider); + map.from(this.transformerFactoryClass) .to(webServiceTemplate::setTransformerFactoryClass); - map.from(() -> this.messageFactory).to(webServiceTemplate::setMessageFactory); + map.from(this.messageFactory).to(webServiceTemplate::setMessageFactory); if (!CollectionUtils.isEmpty(this.interceptors)) { Set merged = new LinkedHashSet<>(this.interceptors); if (webServiceTemplate.getInterceptors() != null) { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/PropertyMapperTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/PropertyMapperTests.java index c293176517..5318986d6d 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/PropertyMapperTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/PropertyMapperTests.java @@ -37,11 +37,37 @@ public class PropertyMapperTests { @Rule public ExpectedException thrown = ExpectedException.none(); + @Test + public void fromNullValue() { + ExampleDest dest = new ExampleDest(); + this.map.from((String) null).to(dest::setName); + assertThat(dest.getName()).isNull(); + } + + @Test + public void fromValue() { + ExampleDest dest = new ExampleDest(); + this.map.from("Hello World").to(dest::setName); + assertThat(dest.getName()).isEqualTo("Hello World"); + } + + @Test + public void fromValueAsIntShouldAdaptSupplier() { + Integer result = this.map.from("123").asInt(Long::valueOf) + .toInstance(Integer::new); + assertThat(result).isEqualTo(123); + } + + @Test + public void fromValueAlwaysApplyingWhenNonNullShouldAlwaysApplyNonNullToSource() { + this.map.alwaysApplyingWhenNonNull().from((String) null).toCall(Assert::fail); + } + @Test public void fromWhenSupplierIsNullShouldThrowException() { this.thrown.expect(IllegalArgumentException.class); this.thrown.expectMessage("Supplier must not be null"); - this.map.from(null); + this.map.from((Supplier) null); } @Test From 597fe237b5c84f24cfa64a92c8650a32e6842dc2 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Wed, 22 Aug 2018 15:35:52 +0200 Subject: [PATCH 2/2] Polish "Add PropertyMapper.from(value)" Closes gh-13837 --- .../batch/BasicBatchConfigurer.java | 2 +- .../context/properties/PropertyMapper.java | 24 +++++++++---------- .../properties/PropertyMapperTests.java | 24 +++++++++---------- 3 files changed, 24 insertions(+), 26 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BasicBatchConfigurer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BasicBatchConfigurer.java index 6a91a053be..862d6a7266 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BasicBatchConfigurer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BasicBatchConfigurer.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-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. diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/PropertyMapper.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/PropertyMapper.java index cbe9ad0ddb..fb5a301a2d 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/PropertyMapper.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/PropertyMapper.java @@ -91,24 +91,13 @@ public final class PropertyMapper { return new PropertyMapper(this, operator); } - /** - * Return a new {@link Source} from the specified value that can be used to perform - * the mapping. - * @param the source type - * @param value the value - * @return a {@link Source} that can be used to complete the mapping - * @see #from(Supplier) - */ - public Source from(T value) { - return from(() -> value); - } - /** * Return a new {@link Source} from the specified value supplier that can be used to * perform the mapping. * @param the source type * @param supplier the value supplier * @return a {@link Source} that can be used to complete the mapping + * @see #from(Object) */ public Source from(Supplier supplier) { Assert.notNull(supplier, "Supplier must not be null"); @@ -119,6 +108,17 @@ public final class PropertyMapper { return source; } + /** + * Return a new {@link Source} from the specified value that can be used to perform + * the mapping. + * @param the source type + * @param value the value + * @return a {@link Source} that can be used to complete the mapping + */ + public Source from(T value) { + return from(() -> value); + } + @SuppressWarnings("unchecked") private Source getSource(Supplier supplier) { if (this.parent != null) { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/PropertyMapperTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/PropertyMapperTests.java index 5318986d6d..7b0d5ef985 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/PropertyMapperTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/PropertyMapperTests.java @@ -120,24 +120,24 @@ public class PropertyMapperTests { @Test public void whenTrueWhenValueIsTrueShouldMap() { - Boolean result = this.map.from(() -> true).whenTrue().toInstance(Boolean::new); + Boolean result = this.map.from(true).whenTrue().toInstance(Boolean::new); assertThat(result).isTrue(); } @Test public void whenTrueWhenValueIsFalseShouldNotMap() { - this.map.from(() -> false).whenTrue().toCall(Assert::fail); + this.map.from(false).whenTrue().toCall(Assert::fail); } @Test public void whenFalseWhenValueIsFalseShouldMap() { - Boolean result = this.map.from(() -> false).whenFalse().toInstance(Boolean::new); + Boolean result = this.map.from(false).whenFalse().toInstance(Boolean::new); assertThat(result).isFalse(); } @Test public void whenFalseWhenValueIsTrueShouldNotMap() { - this.map.from(() -> true).whenFalse().toCall(Assert::fail); + this.map.from(true).whenFalse().toCall(Assert::fail); } @Test @@ -147,30 +147,29 @@ public class PropertyMapperTests { @Test public void whenHasTextWhenValueIsEmptyShouldNotMap() { - this.map.from(() -> "").whenHasText().toCall(Assert::fail); + this.map.from("").whenHasText().toCall(Assert::fail); } @Test public void whenHasTextWhenValueHasTextShouldMap() { - Integer result = this.map.from(() -> 123).whenHasText().toInstance(Integer::new); + Integer result = this.map.from(123).whenHasText().toInstance(Integer::new); assertThat(result).isEqualTo(123); } @Test public void whenEqualToWhenValueIsEqualShouldMatch() { - String result = this.map.from(() -> "123").whenEqualTo("123") - .toInstance(String::new); + String result = this.map.from("123").whenEqualTo("123").toInstance(String::new); assertThat(result).isEqualTo("123"); } @Test public void whenEqualToWhenValueIsNotEqualShouldNotMatch() { - this.map.from(() -> "123").whenEqualTo("321").toCall(Assert::fail); + this.map.from("123").whenEqualTo("321").toCall(Assert::fail); } @Test public void whenInstanceOfWhenValueIsTargetTypeShouldMatch() { - Long result = this.map.from(() -> 123L).whenInstanceOf(Long.class) + Long result = this.map.from(123L).whenInstanceOf(Long.class) .toInstance((value) -> value + 1); assertThat(result).isEqualTo(124L); } @@ -183,14 +182,13 @@ public class PropertyMapperTests { @Test public void whenWhenValueMatchesShouldMap() { - String result = this.map.from(() -> "123").when("123"::equals) - .toInstance(String::new); + String result = this.map.from("123").when("123"::equals).toInstance(String::new); assertThat(result).isEqualTo("123"); } @Test public void whenWhenValueDoesNotMatchShouldNotMap() { - this.map.from(() -> "123").when("321"::equals).toCall(Assert::fail); + this.map.from("123").when("321"::equals).toCall(Assert::fail); } @Test