Search in sources :

Example 1 with MapConfigurationPropertySource

use of org.springframework.boot.context.properties.source.MapConfigurationPropertySource in project spring-cloud-open-service-broker by spring-cloud.

the class ServiceBrokerPropertiesTest method emptyCatalog.

@Test
public void emptyCatalog() {
    Map<String, String> map = new HashMap<>();
    ConfigurationPropertySource source = new MapConfigurationPropertySource(map);
    Binder binder = new Binder(source);
    ServiceBrokerProperties properties = new ServiceBrokerProperties();
    binder.bind("spring.cloud.openservicebroker", Bindable.ofInstance(properties));
    assertThat(properties.getCatalog()).isNull();
}
Also used : Binder(org.springframework.boot.context.properties.bind.Binder) HashMap(java.util.HashMap) ConfigurationPropertySource(org.springframework.boot.context.properties.source.ConfigurationPropertySource) MapConfigurationPropertySource(org.springframework.boot.context.properties.source.MapConfigurationPropertySource) MapConfigurationPropertySource(org.springframework.boot.context.properties.source.MapConfigurationPropertySource) Test(org.junit.Test)

Example 2 with MapConfigurationPropertySource

use of org.springframework.boot.context.properties.source.MapConfigurationPropertySource in project spring-cloud-gateway by spring-cloud.

the class ConfigurationUtils method bind.

public static void bind(Object o, Map<String, Object> properties, String configurationPropertyName, String bindingName, Validator validator) {
    Object toBind = getTargetObject(o);
    new Binder(new MapConfigurationPropertySource(properties)).bind(configurationPropertyName, Bindable.ofInstance(toBind));
    if (validator != null) {
        BindingResult errors = new BeanPropertyBindingResult(toBind, bindingName);
        validator.validate(toBind, errors);
        if (errors.hasErrors()) {
            throw new RuntimeException(new BindException(errors));
        }
    }
}
Also used : Binder(org.springframework.boot.context.properties.bind.Binder) BindingResult(org.springframework.validation.BindingResult) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) BindException(org.springframework.validation.BindException) MapConfigurationPropertySource(org.springframework.boot.context.properties.source.MapConfigurationPropertySource)

Example 3 with MapConfigurationPropertySource

use of org.springframework.boot.context.properties.source.MapConfigurationPropertySource in project spring-boot by spring-projects.

the class XADataSourceAutoConfiguration method getBinderSource.

private ConfigurationPropertySource getBinderSource(DataSourceProperties dataSourceProperties) {
    Map<Object, Object> properties = new HashMap<>();
    properties.putAll(dataSourceProperties.getXa().getProperties());
    properties.computeIfAbsent("user", (key) -> dataSourceProperties.determineUsername());
    properties.computeIfAbsent("password", (key) -> dataSourceProperties.determinePassword());
    try {
        properties.computeIfAbsent("url", (key) -> dataSourceProperties.determineUrl());
    } catch (DataSourceBeanCreationException ex) {
    // Continue as not all XA DataSource's require a URL
    }
    MapConfigurationPropertySource source = new MapConfigurationPropertySource(properties);
    ConfigurationPropertyNameAliases aliases = new ConfigurationPropertyNameAliases();
    aliases.addAliases("user", "username");
    return source.withAliases(aliases);
}
Also used : HashMap(java.util.HashMap) DataSourceBeanCreationException(org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.DataSourceBeanCreationException) MapConfigurationPropertySource(org.springframework.boot.context.properties.source.MapConfigurationPropertySource) ConfigurationPropertyNameAliases(org.springframework.boot.context.properties.source.ConfigurationPropertyNameAliases)

Example 4 with MapConfigurationPropertySource

use of org.springframework.boot.context.properties.source.MapConfigurationPropertySource in project spring-boot by spring-projects.

the class ConfigDataLocationBindHandlerTests method bindToArrayFromIndexedPropertiesSetsOrigin.

@Test
void bindToArrayFromIndexedPropertiesSetsOrigin() {
    MapConfigurationPropertySource source = new MapConfigurationPropertySource();
    source.put("locations[0]", "a");
    source.put("locations[1]", "b");
    source.put("locations[2]", "c");
    Binder binder = new Binder(source);
    ConfigDataLocation[] bound = binder.bind("locations", ARRAY, this.handler).get();
    assertThat(bound[0]).hasToString("a");
    assertThat(bound[0].getOrigin()).hasToString("\"locations[0]\" from property source \"source\"");
    assertThat(bound[1]).hasToString("b");
    assertThat(bound[1].getOrigin()).hasToString("\"locations[1]\" from property source \"source\"");
    assertThat(bound[2]).hasToString("c");
    assertThat(bound[2].getOrigin()).hasToString("\"locations[2]\" from property source \"source\"");
}
Also used : Binder(org.springframework.boot.context.properties.bind.Binder) MapConfigurationPropertySource(org.springframework.boot.context.properties.source.MapConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 5 with MapConfigurationPropertySource

use of org.springframework.boot.context.properties.source.MapConfigurationPropertySource in project spring-boot by spring-projects.

the class ConfigDataLocationBindHandlerTests method bindToValueObjectFromIndexedPropertiesSetsOrigin.

@Test
void bindToValueObjectFromIndexedPropertiesSetsOrigin() {
    MapConfigurationPropertySource source = new MapConfigurationPropertySource();
    source.put("test.locations[0]", "a");
    source.put("test.locations[1]", "b");
    source.put("test.locations[2]", "c");
    Binder binder = new Binder(source);
    ValueObject bound = binder.bind("test", VALUE_OBJECT, this.handler).get();
    assertThat(bound.getLocation(0)).hasToString("a");
    assertThat(bound.getLocation(0).getOrigin()).hasToString("\"test.locations[0]\" from property source \"source\"");
    assertThat(bound.getLocation(1)).hasToString("b");
    assertThat(bound.getLocation(1).getOrigin()).hasToString("\"test.locations[1]\" from property source \"source\"");
    assertThat(bound.getLocation(2)).hasToString("c");
    assertThat(bound.getLocation(2).getOrigin()).hasToString("\"test.locations[2]\" from property source \"source\"");
}
Also used : Binder(org.springframework.boot.context.properties.bind.Binder) MapConfigurationPropertySource(org.springframework.boot.context.properties.source.MapConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Aggregations

MapConfigurationPropertySource (org.springframework.boot.context.properties.source.MapConfigurationPropertySource)34 Binder (org.springframework.boot.context.properties.bind.Binder)31 Test (org.junit.jupiter.api.Test)21 ConfigurationPropertySource (org.springframework.boot.context.properties.source.ConfigurationPropertySource)10 MockEnvironment (org.springframework.mock.env.MockEnvironment)7 HashMap (java.util.HashMap)4 Test (org.junit.Test)4 LinkedHashMap (java.util.LinkedHashMap)3 Environment (org.springframework.core.env.Environment)3 ArrayList (java.util.ArrayList)2 ApolloClientProperties (com.ctrip.framework.apollo.config.data.extension.properties.ApolloClientProperties)1 DataSourceBeanCreationException (org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.DataSourceBeanCreationException)1 ConfigurationPropertyNameAliases (org.springframework.boot.context.properties.source.ConfigurationPropertyNameAliases)1 Catalog (org.springframework.cloud.servicebroker.model.catalog.Catalog)1 Binder (org.springframework.cloud.stream.binder.Binder)1 ExtendedPropertiesBinder (org.springframework.cloud.stream.binder.ExtendedPropertiesBinder)1 BindingServiceProperties (org.springframework.cloud.stream.config.BindingServiceProperties)1 StandardEnvironment (org.springframework.core.env.StandardEnvironment)1 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)1 BindException (org.springframework.validation.BindException)1