Skip to content

Commit

Permalink
Merge branch '2.1.x' into 2.2.x
Browse files Browse the repository at this point in the history
Closes gh-23613
  • Loading branch information
philwebb committed Oct 7, 2020
2 parents c523295 + 1db2f5f commit ce70e7d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.format.Formatter;

/**
* Utility to deduce the {@link ConversionService} to use for configuration properties
Expand Down Expand Up @@ -62,9 +63,13 @@ private static class Factory {

private final List<GenericConverter> genericConverters;

@SuppressWarnings("rawtypes")
private final List<Formatter> formatters;

Factory(BeanFactory beanFactory) {
this.converters = beans(beanFactory, Converter.class, ConfigurationPropertiesBinding.VALUE);
this.genericConverters = beans(beanFactory, GenericConverter.class, ConfigurationPropertiesBinding.VALUE);
this.formatters = beans(beanFactory, Formatter.class, ConfigurationPropertiesBinding.VALUE);
}

private <T> List<T> beans(BeanFactory beanFactory, Class<T> type, String qualifier) {
Expand All @@ -80,7 +85,7 @@ private <T> List<T> beans(Class<T> type, String qualifier, ListableBeanFactory b
}

ConversionService create() {
if (this.converters.isEmpty() && this.genericConverters.isEmpty()) {
if (this.converters.isEmpty() && this.genericConverters.isEmpty() && this.formatters.isEmpty()) {
return ApplicationConversionService.getSharedInstance();
}
ApplicationConversionService conversionService = new ApplicationConversionService();
Expand All @@ -90,6 +95,9 @@ ConversionService create() {
for (GenericConverter genericConverter : this.genericConverters) {
conversionService.addConverter(genericConverter);
}
for (Formatter<?> formatter : this.formatters) {
conversionService.addFormatter(formatter);
}
return conversionService;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@

import java.beans.PropertyEditorSupport;
import java.io.File;
import java.text.ParseException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
Expand Down Expand Up @@ -72,6 +74,7 @@
import org.springframework.core.io.ProtocolResolver;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.format.Formatter;
import org.springframework.mock.env.MockEnvironment;
import org.springframework.stereotype.Component;
import org.springframework.test.context.support.TestPropertySourceUtils;
Expand Down Expand Up @@ -609,7 +612,7 @@ void customProtocolResolver() {
}

@Test
void loadShouldUseConfigurationConverter() {
void loadShouldUseConverterBean() {
prepareConverterContext(ConverterConfiguration.class, PersonProperties.class);
Person person = this.context.getBean(PersonProperties.class).getPerson();
assertThat(person.firstName).isEqualTo("John");
Expand All @@ -625,13 +628,21 @@ void loadWhenConfigurationConverterIsNotQualifiedShouldNotConvert() {
}

@Test
void loadShouldUseGenericConfigurationConverter() {
void loadShouldUseGenericConverterBean() {
prepareConverterContext(GenericConverterConfiguration.class, PersonProperties.class);
Person person = this.context.getBean(PersonProperties.class).getPerson();
assertThat(person.firstName).isEqualTo("John");
assertThat(person.lastName).isEqualTo("Smith");
}

@Test
void loadShouldUseFormatterBean() {
prepareConverterContext(FormatterConfiguration.class, PersonProperties.class);
Person person = this.context.getBean(PersonProperties.class).getPerson();
assertThat(person.firstName).isEqualTo("John");
assertThat(person.lastName).isEqualTo("Smith");
}

@Test
void loadWhenGenericConfigurationConverterIsNotQualifiedShouldNotConvert() {
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(
Expand Down Expand Up @@ -1238,6 +1249,17 @@ GenericConverter genericPersonConverter() {

}

@Configuration(proxyBeanMethods = false)
static class FormatterConfiguration {

@Bean
@ConfigurationPropertiesBinding
Formatter<Person> personFormatter() {
return new PersonFormatter();
}

}

@Configuration(proxyBeanMethods = false)
static class NonQualifiedGenericConverterConfiguration {

Expand Down Expand Up @@ -2003,12 +2025,27 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t

}

static class PersonFormatter implements Formatter<Person> {

@Override
public String print(Person person, Locale locale) {
return person.getFirstName() + " " + person.getLastName();
}

@Override
public Person parse(String text, Locale locale) throws ParseException {
String[] content = text.split(" ");
return new Person(content[0], content[1]);
}

}

static class PersonPropertyEditor extends PropertyEditorSupport {

@Override
public void setAsText(String text) throws IllegalArgumentException {
String[] split = text.split(",");
setValue(new Person(split[1], split[0]));
String[] content = text.split(",");
setValue(new Person(content[1], content[0]));
}

}
Expand All @@ -2024,6 +2061,14 @@ static class Person {
this.lastName = lastName;
}

String getFirstName() {
return this.firstName;
}

String getLastName() {
return this.lastName;
}

}

static class Foo {
Expand Down

0 comments on commit ce70e7d

Please sign in to comment.