Skip to content

Commit

Permalink
Merge branch '2.4.x'
Browse files Browse the repository at this point in the history
Closes gh-26346
  • Loading branch information
scottfrederick committed May 3, 2021
2 parents 199bc2d + dbee0cf commit 0893df4
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 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.
Expand All @@ -16,7 +16,10 @@

package org.springframework.boot.context.config;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import org.springframework.boot.context.properties.bind.AbstractBindHandler;
import org.springframework.boot.context.properties.bind.BindContext;
Expand All @@ -30,6 +33,7 @@
* objects.
*
* @author Phillip Webb
* @author Scott Frederick
*/
class ConfigDataLocationBindHandler extends AbstractBindHandler {

Expand All @@ -40,19 +44,22 @@ public Object onSuccess(ConfigurationPropertyName name, Bindable<?> target, Bind
return withOrigin(context, (ConfigDataLocation) result);
}
if (result instanceof List) {
List<Object> list = (List<Object>) result;
List<Object> list = ((List<Object>) result).stream().filter(Objects::nonNull).collect(Collectors.toList());
for (int i = 0; i < list.size(); i++) {
Object element = list.get(i);
if (element instanceof ConfigDataLocation) {
list.set(i, withOrigin(context, (ConfigDataLocation) element));
}
}
return list;
}
if (result instanceof ConfigDataLocation[]) {
ConfigDataLocation[] locations = (ConfigDataLocation[]) result;
ConfigDataLocation[] locations = Arrays.stream((ConfigDataLocation[]) result).filter(Objects::nonNull)
.toArray(ConfigDataLocation[]::new);
for (int i = 0; i < locations.length; i++) {
locations[i] = withOrigin(context, locations[i]);
}
return locations;
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@
*
* @author Phillip Webb
* @author Madhura Bhave
* @author Scott Frederick
*/
class ConfigDataEnvironmentContributorTests {

private static final ConfigDataLocation TEST_LOCATION = ConfigDataLocation.of("test");

private ConfigDataActivationContext activationContext = new ConfigDataActivationContext(CloudPlatform.KUBERNETES,
null);
private final ConfigDataActivationContext activationContext = new ConfigDataActivationContext(
CloudPlatform.KUBERNETES, null);

@Test
void getKindReturnsKind() {
Expand Down Expand Up @@ -125,6 +126,16 @@ void getImportsReturnsImports() {
ConfigDataLocation.of("boot"));
}

@Test
void getImportsIgnoresEmptyElements() {
MockPropertySource propertySource = new MockPropertySource();
propertySource.setProperty("spring.config.import", "spring,,boot,");
ConfigData configData = new ConfigData(Collections.singleton(propertySource));
ConfigDataEnvironmentContributor contributor = createBoundContributor(null, configData, 0);
assertThat(contributor.getImports()).containsExactly(ConfigDataLocation.of("spring"),
ConfigDataLocation.of("boot"));
}

@Test
void hasUnprocessedImportsWhenNoImportsReturnsFalse() {
ConfigData configData = new ConfigData(Collections.singleton(new MockPropertySource()));
Expand Down Expand Up @@ -205,9 +216,9 @@ void iteratorWhenTypicalStructureReturnsCorrectlyOrderedIterator() {
"classpath:application-profile.properties");
ConfigDataEnvironmentContributor classpathImports = createBoundContributor("classpath:/");
classpathImports = classpathImports.withChildren(ImportPhase.BEFORE_PROFILE_ACTIVATION,
Arrays.asList(classpathApplication));
Collections.singletonList(classpathApplication));
classpathImports = classpathImports.withChildren(ImportPhase.AFTER_PROFILE_ACTIVATION,
Arrays.asList(classpathProfile));
Collections.singletonList(classpathProfile));
ConfigDataEnvironmentContributor root = createBoundContributor("root");
root = root.withChildren(ImportPhase.BEFORE_PROFILE_ACTIVATION, Arrays.asList(fileImports, classpathImports));
assertThat(asLocationsList(root.iterator())).containsExactly("file:application-profile.properties",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 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.
Expand Down Expand Up @@ -30,6 +30,7 @@
* Tests for {@link ConfigDataLocationBindHandler}.
*
* @author Phillip Webb
* @author Scott Frederick
*/
class ConfigDataLocationBindHandlerTests {

Expand All @@ -54,6 +55,21 @@ void bindToArrayFromCommaStringPropertySetsOrigin() {
assertThat(bound[2].getOrigin()).hasToString(expectedLocation);
}

@Test
void bindToArrayFromCommaStringPropertyIgnoresEmptyElements() {
MapConfigurationPropertySource source = new MapConfigurationPropertySource();
source.put("locations", ",a,,b,c,");
Binder binder = new Binder(source);
ConfigDataLocation[] bound = binder.bind("locations", ARRAY, this.handler).get();
String expectedLocation = "\"locations\" from property source \"source\"";
assertThat(bound[0]).hasToString("a");
assertThat(bound[0].getOrigin()).hasToString(expectedLocation);
assertThat(bound[1]).hasToString("b");
assertThat(bound[1].getOrigin()).hasToString(expectedLocation);
assertThat(bound[2]).hasToString("c");
assertThat(bound[2].getOrigin()).hasToString(expectedLocation);
}

@Test
void bindToArrayFromIndexedPropertiesSetsOrigin() {
MapConfigurationPropertySource source = new MapConfigurationPropertySource();
Expand Down Expand Up @@ -85,6 +101,21 @@ void bindToValueObjectFromCommaStringPropertySetsOrigin() {
assertThat(bound.getLocation(2).getOrigin()).hasToString(expectedLocation);
}

@Test
void bindToValueObjectFromCommaStringPropertyIgnoresEmptyElements() {
MapConfigurationPropertySource source = new MapConfigurationPropertySource();
source.put("test.locations", ",a,b,,c,");
Binder binder = new Binder(source);
ValueObject bound = binder.bind("test", VALUE_OBJECT, this.handler).get();
String expectedLocation = "\"test.locations\" from property source \"source\"";
assertThat(bound.getLocation(0)).hasToString("a");
assertThat(bound.getLocation(0).getOrigin()).hasToString(expectedLocation);
assertThat(bound.getLocation(1)).hasToString("b");
assertThat(bound.getLocation(1).getOrigin()).hasToString(expectedLocation);
assertThat(bound.getLocation(2)).hasToString("c");
assertThat(bound.getLocation(2).getOrigin()).hasToString(expectedLocation);
}

@Test
void bindToValueObjectFromIndexedPropertiesSetsOrigin() {
MapConfigurationPropertySource source = new MapConfigurationPropertySource();
Expand Down

0 comments on commit 0893df4

Please sign in to comment.