Browse Source

Polish "Tighten rules around profile naming"

See gh-43176
pull/43837/head
Moritz Halbritter 1 year ago
parent
commit
5322352919
  1. 9
      spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/StandardConfigDataLocationResolver.java
  2. 3
      spring-boot-project/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java
  3. 5
      spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/StandardConfigDataLocationResolverTests.java

9
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/StandardConfigDataLocationResolver.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 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.
@ -149,13 +149,13 @@ public class StandardConfigDataLocationResolver @@ -149,13 +149,13 @@ public class StandardConfigDataLocationResolver
@Override
public List<StandardConfigDataResource> resolveProfileSpecific(ConfigDataLocationResolverContext context,
ConfigDataLocation location, Profiles profiles) {
validateProfiles(profiles);
return resolve(getProfileSpecificReferences(context, location.split(), profiles));
}
private Set<StandardConfigDataReference> getProfileSpecificReferences(ConfigDataLocationResolverContext context,
ConfigDataLocation[] configDataLocations, Profiles profiles) {
Set<StandardConfigDataReference> references = new LinkedHashSet<>();
validateProfiles(profiles);
for (String profile : profiles) {
for (ConfigDataLocation configDataLocation : configDataLocations) {
String resourceLocation = getResourceLocation(context, configDataLocation);
@ -172,7 +172,7 @@ public class StandardConfigDataLocationResolver @@ -172,7 +172,7 @@ public class StandardConfigDataLocationResolver
}
private void validateProfile(String profile) {
Assert.hasText(profile, "Profile must contain text");
Assert.hasText(profile, "'profile' must contain text");
Assert.state(!profile.startsWith("-") && !profile.startsWith("_"),
() -> String.format("Invalid profile '%s': must not start with '-' or '_'", profile));
Assert.state(!profile.endsWith("-") && !profile.endsWith("_"),
@ -181,7 +181,8 @@ public class StandardConfigDataLocationResolver @@ -181,7 +181,8 @@ public class StandardConfigDataLocationResolver
if (codePoint == '-' || codePoint == '_' || Character.isLetterOrDigit(codePoint)) {
return;
}
throw new IllegalStateException(String.format("Invalid profile '%s': must contain only letters or digits or '-' or '_'", profile));
throw new IllegalStateException(
String.format("Invalid profile '%s': must contain only letters or digits or '-' or '_'", profile));
});
}

3
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java

@ -258,8 +258,7 @@ class SpringApplicationTests { @@ -258,8 +258,7 @@ class SpringApplicationTests {
application.setWebApplicationType(WebApplicationType.NONE);
application.setEnvironment(environment);
this.context = application.run();
assertThat(output)
.contains("No active profile set, falling back to 2 default profiles: \"p0\", \"default\"");
assertThat(output).contains("No active profile set, falling back to 2 default profiles: \"p0\", \"default\"");
}
@Test

5
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/StandardConfigDataLocationResolverTests.java

@ -309,7 +309,7 @@ class StandardConfigDataLocationResolverTests { @@ -309,7 +309,7 @@ class StandardConfigDataLocationResolverTests {
this.environment.setActiveProfiles("dev-테스트_123");
Profiles profiles = new Profiles(this.environment, this.environmentBinder, Collections.emptyList());
assertThatNoException()
.isThrownBy(() -> this.resolver.resolveProfileSpecific(this.context, location, profiles));
.isThrownBy(() -> this.resolver.resolveProfileSpecific(this.context, location, profiles));
}
@Test
@ -368,8 +368,7 @@ class StandardConfigDataLocationResolverTests { @@ -368,8 +368,7 @@ class StandardConfigDataLocationResolverTests {
Profiles profiles = new Profiles(this.environment, this.environmentBinder, Collections.emptyList());
assertThatIllegalStateException()
.isThrownBy(() -> this.resolver.resolveProfileSpecific(this.context, location, profiles))
.withMessageStartingWith(
"Invalid profile 'dev*test': must contain only letters or digits or '-' or '_'");
.withMessageStartingWith("Invalid profile 'dev*test': must contain only letters or digits or '-' or '_'");
}
private String filePath(String... components) {

Loading…
Cancel
Save