Skip to content

Commit c5307a8

Browse files
committed
Merge branch '2.6.x' into 2.7.x
Closes gh-29916
2 parents 5f2ae85 + c52f6f0 commit c5307a8

File tree

3 files changed

+42
-13
lines changed

3 files changed

+42
-13
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.Map;
2929
import java.util.Properties;
3030
import java.util.Set;
31+
import java.util.stream.Collectors;
3132

3233
import org.apache.commons.logging.Log;
3334
import org.apache.commons.logging.LogFactory;
@@ -619,19 +620,26 @@ protected void logStartupInfo(boolean isRoot) {
619620
protected void logStartupProfileInfo(ConfigurableApplicationContext context) {
620621
Log log = getApplicationLog();
621622
if (log.isInfoEnabled()) {
622-
String[] activeProfiles = context.getEnvironment().getActiveProfiles();
623+
List<String> activeProfiles = quoteProfiles(context.getEnvironment().getActiveProfiles());
623624
if (ObjectUtils.isEmpty(activeProfiles)) {
624-
String[] defaultProfiles = context.getEnvironment().getDefaultProfiles();
625-
log.info("No active profile set, falling back to default profiles: "
626-
+ StringUtils.arrayToCommaDelimitedString(defaultProfiles));
625+
List<String> defaultProfiles = quoteProfiles(context.getEnvironment().getDefaultProfiles());
626+
String message = String.format("%s default %s: ", defaultProfiles.size(),
627+
(defaultProfiles.size() <= 1) ? "profile" : "profiles");
628+
log.info("No active profile set, falling back to " + message
629+
+ StringUtils.collectionToDelimitedString(defaultProfiles, ", "));
627630
}
628631
else {
629-
log.info("The following profiles are active: "
630-
+ StringUtils.arrayToCommaDelimitedString(activeProfiles));
632+
String message = (activeProfiles.size() == 1) ? "1 profile is active: "
633+
: activeProfiles.size() + " profiles are active: ";
634+
log.info("The following " + message + StringUtils.collectionToDelimitedString(activeProfiles, ", "));
631635
}
632636
}
633637
}
634638

639+
private List<String> quoteProfiles(String[] profiles) {
640+
return Arrays.stream(profiles).map((profile) -> "\"" + profile + "\"").collect(Collectors.toList());
641+
}
642+
635643
/**
636644
* Returns the {@link Log} for the application. By default will be deduced.
637645
* @return the application log

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,19 +254,40 @@ void imageBannerLoads(CapturedOutput output) {
254254
}
255255

256256
@Test
257-
void logsNoActiveProfiles(CapturedOutput output) {
257+
void logsActiveProfilesWithoutProfileAndSingleDefault(CapturedOutput output) {
258258
SpringApplication application = new SpringApplication(ExampleConfig.class);
259259
application.setWebApplicationType(WebApplicationType.NONE);
260260
this.context = application.run();
261-
assertThat(output).contains("No active profile set, falling back to default profiles: default");
261+
assertThat(output).contains("No active profile set, falling back to 1 default profile: \"default\"");
262262
}
263263

264264
@Test
265-
void logsActiveProfiles(CapturedOutput output) {
265+
void logsActiveProfilesWithoutProfileAndMultipleDefaults(CapturedOutput output) {
266+
MockEnvironment environment = new MockEnvironment();
267+
environment.setDefaultProfiles("p0,p1", "default");
268+
SpringApplication application = new SpringApplication(ExampleConfig.class);
269+
application.setWebApplicationType(WebApplicationType.NONE);
270+
application.setEnvironment(environment);
271+
this.context = application.run();
272+
assertThat(output)
273+
.contains("No active profile set, falling back to 2 default profiles: \"p0,p1\", \"default\"");
274+
}
275+
276+
@Test
277+
void logsActiveProfilesWithSingleProfile(CapturedOutput output) {
266278
SpringApplication application = new SpringApplication(ExampleConfig.class);
267279
application.setWebApplicationType(WebApplicationType.NONE);
268280
this.context = application.run("--spring.profiles.active=myprofiles");
269-
assertThat(output).contains("The following profiles are active: myprofile");
281+
assertThat(output).contains("The following 1 profile is active: \"myprofiles\"");
282+
}
283+
284+
@Test
285+
void logsActiveProfilesWithMultipleProfiles(CapturedOutput output) {
286+
SpringApplication application = new SpringApplication(ExampleConfig.class);
287+
application.setWebApplicationType(WebApplicationType.NONE);
288+
application.setAdditionalProfiles("p1,p2", "p3");
289+
application.run();
290+
assertThat(output).contains("The following 2 profiles are active: \"p1,p2\", \"p3\"");
270291
}
271292

272293
@Test

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -815,8 +815,8 @@ void activateProfileFromProfileSpecificProperties(CapturedOutput output) {
815815
assertThat(environment).has(matchingProfile("morespecific"));
816816
assertThat(environment).has(matchingProfile("yetmorespecific"));
817817
assertThat(environment).doesNotHave(matchingProfile("missing"));
818-
assertThat(output)
819-
.contains("The following profiles are active: includeprofile,specific,morespecific,yetmorespecific");
818+
assertThat(output).contains(
819+
"The following 4 profiles are active: \"includeprofile\", \"specific\", \"morespecific\", \"yetmorespecific\"");
820820
}
821821

822822
@Test

0 commit comments

Comments
 (0)