Search in sources :

Example 31 with MockConfigurationPropertySource

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

the class MapBinderTests method bindToMapShouldBindToMapValue.

@Test
void bindToMapShouldBindToMapValue() {
    ResolvableType type = ResolvableType.forClassWithGenerics(Map.class, ResolvableType.forClass(String.class), STRING_INTEGER_MAP.getType());
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("foo.bar.baz", "1");
    source.put("foo.bar.bin", "2");
    source.put("foo.far.baz", "3");
    source.put("foo.far.bin", "4");
    source.put("faf.far.bin", "x");
    this.sources.add(source);
    Map<String, Map<String, Integer>> result = this.binder.bind("foo", Bindable.<Map<String, Map<String, Integer>>>of(type)).get();
    assertThat(result).hasSize(2);
    assertThat(result.get("bar")).containsEntry("baz", 1).containsEntry("bin", 2);
    assertThat(result.get("far")).containsEntry("baz", 3).containsEntry("bin", 4);
}
Also used : MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) ResolvableType(org.springframework.core.ResolvableType) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 32 with MockConfigurationPropertySource

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

the class MapBinderTests method bindToMapWhenHasExistingMapShouldReplaceOnlyNewContents.

@Test
void bindToMapWhenHasExistingMapShouldReplaceOnlyNewContents() {
    this.sources.add(new MockConfigurationPropertySource("foo.bar", "1"));
    Map<String, Integer> existing = new HashMap<>();
    existing.put("bar", 1000);
    existing.put("baz", 1001);
    Bindable<Map<String, Integer>> target = STRING_INTEGER_MAP.withExistingValue(existing);
    Map<String, Integer> result = this.binder.bind("foo", target).get();
    assertThat(result).isExactlyInstanceOf(HashMap.class);
    assertThat(result).hasSize(2);
    assertThat(result).containsEntry("bar", 1);
    assertThat(result).containsEntry("baz", 1001);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 33 with MockConfigurationPropertySource

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

the class MapBinderTests method bindToPropertiesShouldBeEquivalentToMapOfStringString.

@Test
void bindToPropertiesShouldBeEquivalentToMapOfStringString() {
    this.sources.add(new MockConfigurationPropertySource("foo.bar.baz", "1", "line1"));
    Bindable<Properties> target = Bindable.of(Properties.class);
    Properties properties = this.binder.bind("foo", target).get();
    assertThat(properties.getProperty("bar.baz")).isEqualTo("1");
}
Also used : MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) Properties(java.util.Properties) Test(org.junit.jupiter.api.Test)

Example 34 with MockConfigurationPropertySource

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

the class MapBinderTests method bindToMapStringArrayWithDotKeysAndCommaSeparatedShouldPreserveDot.

@Test
void bindToMapStringArrayWithDotKeysAndCommaSeparatedShouldPreserveDot() {
    MockConfigurationPropertySource mockSource = new MockConfigurationPropertySource();
    mockSource.put("foo.bar.baz", "a,b,c");
    this.sources.add(mockSource);
    Map<String, String[]> map = this.binder.bind("foo", STRING_ARRAY_MAP).get();
    assertThat(map.get("bar.baz")).containsExactly("a", "b", "c");
}
Also used : MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 35 with MockConfigurationPropertySource

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

the class MapBinderTests method bindToMapWithPlaceholdersShouldBeGreedyForScalars.

@Test
void bindToMapWithPlaceholdersShouldBeGreedyForScalars() {
    StandardEnvironment environment = new StandardEnvironment();
    TestPropertySourceUtils.addInlinedPropertiesToEnvironment(environment, "foo=boo");
    MockConfigurationPropertySource source = new MockConfigurationPropertySource("foo.aaa.bbb.ccc", "baz-${foo}");
    this.sources.add(source);
    this.binder = new Binder(this.sources, new PropertySourcesPlaceholdersResolver(environment));
    Map<String, ExampleEnum> result = this.binder.bind("foo", Bindable.mapOf(String.class, ExampleEnum.class)).get();
    assertThat(result).containsEntry("aaa.bbb.ccc", ExampleEnum.BAZ_BOO);
}
Also used : MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) StandardEnvironment(org.springframework.core.env.StandardEnvironment) ExampleEnum(org.springframework.boot.context.properties.bind.BinderTests.ExampleEnum) Test(org.junit.jupiter.api.Test)

Aggregations

MockConfigurationPropertySource (org.springframework.boot.context.properties.source.MockConfigurationPropertySource)207 Test (org.junit.jupiter.api.Test)204 Map (java.util.Map)18 HashMap (java.util.HashMap)17 LinkedHashMap (java.util.LinkedHashMap)17 ArrayList (java.util.ArrayList)16 List (java.util.List)16 Binder (org.springframework.boot.context.properties.bind.Binder)15 InOrder (org.mockito.InOrder)8 LinkedList (java.util.LinkedList)7 ResolvableType (org.springframework.core.ResolvableType)7 ValidationBindHandler (org.springframework.boot.context.properties.bind.validation.ValidationBindHandler)5 ConfigurationProperty (org.springframework.boot.context.properties.source.ConfigurationProperty)5 StandardEnvironment (org.springframework.core.env.StandardEnvironment)5 ConfigurationPropertyName (org.springframework.boot.context.properties.source.ConfigurationPropertyName)4 LocalDate (java.time.LocalDate)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 BindException (org.springframework.boot.context.properties.bind.BindException)3