|
|
@ -18,6 +18,7 @@ package org.springframework.boot.context.properties;
|
|
|
|
|
|
|
|
|
|
|
|
import java.beans.PropertyEditorSupport;
|
|
|
|
import java.beans.PropertyEditorSupport;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.File;
|
|
|
|
|
|
|
|
import java.text.ParseException;
|
|
|
|
import java.time.Duration;
|
|
|
|
import java.time.Duration;
|
|
|
|
import java.time.Period;
|
|
|
|
import java.time.Period;
|
|
|
|
import java.time.temporal.ChronoUnit;
|
|
|
|
import java.time.temporal.ChronoUnit;
|
|
|
@ -26,6 +27,7 @@ import java.util.Collections;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import java.util.Locale;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Properties;
|
|
|
|
import java.util.Properties;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.Set;
|
|
|
@ -81,6 +83,7 @@ import org.springframework.core.io.ClassPathResource;
|
|
|
|
import org.springframework.core.io.ProtocolResolver;
|
|
|
|
import org.springframework.core.io.ProtocolResolver;
|
|
|
|
import org.springframework.core.io.Resource;
|
|
|
|
import org.springframework.core.io.Resource;
|
|
|
|
import org.springframework.core.io.ResourceLoader;
|
|
|
|
import org.springframework.core.io.ResourceLoader;
|
|
|
|
|
|
|
|
import org.springframework.format.Formatter;
|
|
|
|
import org.springframework.mock.env.MockEnvironment;
|
|
|
|
import org.springframework.mock.env.MockEnvironment;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import org.springframework.test.context.support.TestPropertySourceUtils;
|
|
|
|
import org.springframework.test.context.support.TestPropertySourceUtils;
|
|
|
@ -618,7 +621,7 @@ class ConfigurationPropertiesTests {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
void loadShouldUseConfigurationConverter() {
|
|
|
|
void loadShouldUseConverterBean() {
|
|
|
|
prepareConverterContext(ConverterConfiguration.class, PersonProperties.class);
|
|
|
|
prepareConverterContext(ConverterConfiguration.class, PersonProperties.class);
|
|
|
|
Person person = this.context.getBean(PersonProperties.class).getPerson();
|
|
|
|
Person person = this.context.getBean(PersonProperties.class).getPerson();
|
|
|
|
assertThat(person.firstName).isEqualTo("John");
|
|
|
|
assertThat(person.firstName).isEqualTo("John");
|
|
|
@ -634,13 +637,21 @@ class ConfigurationPropertiesTests {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
void loadShouldUseGenericConfigurationConverter() {
|
|
|
|
void loadShouldUseGenericConverterBean() {
|
|
|
|
prepareConverterContext(GenericConverterConfiguration.class, PersonProperties.class);
|
|
|
|
prepareConverterContext(GenericConverterConfiguration.class, PersonProperties.class);
|
|
|
|
Person person = this.context.getBean(PersonProperties.class).getPerson();
|
|
|
|
Person person = this.context.getBean(PersonProperties.class).getPerson();
|
|
|
|
assertThat(person.firstName).isEqualTo("John");
|
|
|
|
assertThat(person.firstName).isEqualTo("John");
|
|
|
|
assertThat(person.lastName).isEqualTo("Smith");
|
|
|
|
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
|
|
|
|
@Test
|
|
|
|
void loadWhenGenericConfigurationConverterIsNotQualifiedShouldNotConvert() {
|
|
|
|
void loadWhenGenericConfigurationConverterIsNotQualifiedShouldNotConvert() {
|
|
|
|
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(
|
|
|
|
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(
|
|
|
@ -1326,6 +1337,17 @@ class ConfigurationPropertiesTests {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration(proxyBeanMethods = false)
|
|
|
|
|
|
|
|
static class FormatterConfiguration {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
|
|
|
@ConfigurationPropertiesBinding
|
|
|
|
|
|
|
|
Formatter<Person> personFormatter() {
|
|
|
|
|
|
|
|
return new PersonFormatter();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration(proxyBeanMethods = false)
|
|
|
|
@Configuration(proxyBeanMethods = false)
|
|
|
|
static class NonQualifiedGenericConverterConfiguration {
|
|
|
|
static class NonQualifiedGenericConverterConfiguration {
|
|
|
|
|
|
|
|
|
|
|
@ -2158,12 +2180,27 @@ class ConfigurationPropertiesTests {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 {
|
|
|
|
static class PersonPropertyEditor extends PropertyEditorSupport {
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void setAsText(String text) throws IllegalArgumentException {
|
|
|
|
public void setAsText(String text) throws IllegalArgumentException {
|
|
|
|
String[] split = text.split(",");
|
|
|
|
String[] content = text.split(",");
|
|
|
|
setValue(new Person(split[1], split[0]));
|
|
|
|
setValue(new Person(content[1], content[0]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -2179,6 +2216,14 @@ class ConfigurationPropertiesTests {
|
|
|
|
this.lastName = lastName;
|
|
|
|
this.lastName = lastName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String getFirstName() {
|
|
|
|
|
|
|
|
return this.firstName;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String getLastName() {
|
|
|
|
|
|
|
|
return this.lastName;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static class Foo {
|
|
|
|
static class Foo {
|
|
|
|