Add StandardConfigDataResource.getProfile method

Add a `StandardConfigDataResource.getProfile()` method so that it's
possible to tell the profile used when reading a profile specific
resource.

Fixes gh-25940
This commit is contained in:
Phillip Webb 2021-04-15 12:00:00 -07:00
parent 270bf0f937
commit 302d500ee9
3 changed files with 20 additions and 0 deletions

View File

@ -74,6 +74,10 @@ class StandardConfigDataReference {
return this.directory;
}
String getProfile() {
return this.profile;
}
boolean isSkippable() {
return this.configDataLocation.isOptional() || this.directory != null || this.profile != null;
}

View File

@ -74,6 +74,15 @@ public class StandardConfigDataResource extends ConfigDataResource {
return this.resource;
}
/**
* Return the profile or {@code null} if the resource is not profile specific.
* @return the profile or {@code null}
* @since 2.4.6
*/
public String getProfile() {
return this.reference.getProfile();
}
boolean isEmptyDirectory() {
return this.emptyDirectory;
}

View File

@ -127,6 +127,13 @@ class ConfigDataEnvironmentPostProcessorTests {
assertThat(this.environment.getActiveProfiles()).containsExactly("dev");
assertThat(listener.getAddedPropertySources()).hasSizeGreaterThan(0);
assertThat(listener.getProfiles().getActive()).containsExactly("dev");
assertThat(listener.getAddedPropertySources().stream().anyMatch((added) -> hasDevProfile(added.getResource())))
.isTrue();
}
private boolean hasDevProfile(ConfigDataResource resource) {
return (resource instanceof StandardConfigDataResource)
&& "dev".equals(((StandardConfigDataResource) resource).getProfile());
}
}