Skip to content

Commit 08fe123

Browse files
committed
Introduce Environment.matchesProfiles() for profile expressions
Environment.acceptsProfiles(String...) was deprecated in 5.1 in conjunction with gh-17063 which introduced a new acceptsProfiles(Profiles) method to replace it. The deprecated method only supports OR semantics; whereas, the new method supports profile expressions. Thus, the goal was to encourage people to use the more powerful profile expressions instead of the limited OR support with profile names. However, there are use cases where it is difficult (if not impossible) to provide a Profiles instance, and there are use cases where it is simply preferable to provide profile expressions directly as strings. To address these issues, this commit introduces a new matchesProfiles() method in Environment that accepts a var-args list of profile expressions. See gh-30206 Closes gh-30226
1 parent 2194487 commit 08fe123

File tree

2 files changed

+138
-3
lines changed

2 files changed

+138
-3
lines changed

spring-core/src/main/java/org/springframework/core/env/Environment.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
*
5959
* @author Chris Beams
6060
* @author Phillip Webb
61+
* @author Sam Brannen
6162
* @since 3.1
6263
* @see PropertyResolver
6364
* @see EnvironmentCapable
@@ -109,20 +110,42 @@ public interface Environment extends PropertyResolver {
109110
* whitespace only
110111
* @see #getActiveProfiles
111112
* @see #getDefaultProfiles
113+
* @see #matchesProfiles(String...)
112114
* @see #acceptsProfiles(Profiles)
113-
* @deprecated as of 5.1 in favor of {@link #acceptsProfiles(Profiles)}
115+
* @deprecated as of 5.1 in favor of {@link #acceptsProfiles(Profiles)} or
116+
* {@link #matchesProfiles(String...)}
114117
*/
115118
@Deprecated
116119
boolean acceptsProfiles(String... profiles);
117120

121+
/**
122+
* Determine whether one of the given profile expressions matches the
123+
* {@linkplain #getActiveProfiles() active profiles} — or in the case
124+
* of no explicit active profiles, whether one of the given profile expressions
125+
* matches the {@linkplain #getDefaultProfiles() default profiles}.
126+
* <p>Profile expressions allow for complex, boolean profile logic to be
127+
* expressed &mdash; for example {@code "p1 & p2"}, {@code "(p1 & p2) | p3"},
128+
* etc. See {@link Profiles#of(String...)} for details on the supported
129+
* expression syntax.
130+
* <p>This method is a convenient shortcut for
131+
* {@code env.acceptsProfiles(Profiles.of(profileExpressions))}.
132+
* @since 5.3.28
133+
* @see Profiles#of(String...)
134+
* @see #acceptsProfiles(Profiles)
135+
*/
136+
default boolean matchesProfiles(String... profileExpressions) {
137+
return acceptsProfiles(Profiles.of(profileExpressions));
138+
}
139+
118140
/**
119141
* Determine whether the given {@link Profiles} predicate matches the
120142
* {@linkplain #getActiveProfiles() active profiles} &mdash; or in the case
121143
* of no explicit active profiles, whether the given {@code Profiles} predicate
122144
* matches the {@linkplain #getDefaultProfiles() default profiles}.
123-
* <p>If you wish to check a single profile expression, consider using
124-
* {@link #acceptsProfiles(String)} instead.
145+
* <p>If you wish provide profile expressions directly as strings, use
146+
* {@link #matchesProfiles(String...)} instead.
125147
* @since 5.1
148+
* @see #matchesProfiles(String...)
126149
* @see Profiles#of(String...)
127150
*/
128151
boolean acceptsProfiles(Profiles profiles);

spring-core/src/test/java/org/springframework/core/env/StandardEnvironmentTests.java

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,4 +541,116 @@ void withProfileExpression() {
541541

542542
}
543543

544+
@Nested
545+
class MatchesProfilesTests {
546+
547+
@Test
548+
@SuppressWarnings("deprecation")
549+
void withEmptyArgumentList() {
550+
assertThatIllegalArgumentException().isThrownBy(environment::matchesProfiles);
551+
}
552+
553+
@Test
554+
@SuppressWarnings("deprecation")
555+
void withNullArgumentList() {
556+
assertThatIllegalArgumentException().isThrownBy(() -> environment.matchesProfiles((String[]) null));
557+
}
558+
559+
@Test
560+
@SuppressWarnings("deprecation")
561+
void withNullArgument() {
562+
assertThatIllegalArgumentException().isThrownBy(() -> environment.matchesProfiles((String) null));
563+
assertThatIllegalArgumentException().isThrownBy(() -> environment.matchesProfiles("p1", null));
564+
}
565+
566+
@Test
567+
@SuppressWarnings("deprecation")
568+
void withEmptyArgument() {
569+
assertThatIllegalArgumentException().isThrownBy(() -> environment.matchesProfiles(""));
570+
assertThatIllegalArgumentException().isThrownBy(() -> environment.matchesProfiles("p1", ""));
571+
assertThatIllegalArgumentException().isThrownBy(() -> environment.matchesProfiles("p1", " "));
572+
}
573+
574+
@Test
575+
@SuppressWarnings("deprecation")
576+
void withInvalidNotOperator() {
577+
assertThatIllegalArgumentException().isThrownBy(() -> environment.matchesProfiles("p1", "!"));
578+
}
579+
580+
@Test
581+
@SuppressWarnings("deprecation")
582+
void withInvalidCompoundExpressionGrouping() {
583+
assertThatIllegalArgumentException().isThrownBy(() -> environment.matchesProfiles("p1 | p2 & p3"));
584+
assertThatIllegalArgumentException().isThrownBy(() -> environment.matchesProfiles("p1 & p2 | p3"));
585+
assertThatIllegalArgumentException().isThrownBy(() -> environment.matchesProfiles("p1 & (p2 | p3) | p4"));
586+
}
587+
588+
@Test
589+
@SuppressWarnings("deprecation")
590+
void activeProfileSetProgrammatically() {
591+
assertThat(environment.matchesProfiles("p1", "p2")).isFalse();
592+
593+
environment.setActiveProfiles("p1");
594+
assertThat(environment.matchesProfiles("p1", "p2")).isTrue();
595+
596+
environment.setActiveProfiles("p2");
597+
assertThat(environment.matchesProfiles("p1", "p2")).isTrue();
598+
599+
environment.setActiveProfiles("p1", "p2");
600+
assertThat(environment.matchesProfiles("p1", "p2")).isTrue();
601+
}
602+
603+
@Test
604+
@SuppressWarnings("deprecation")
605+
void activeProfileSetViaProperty() {
606+
assertThat(environment.matchesProfiles("p1")).isFalse();
607+
608+
environment.getPropertySources().addFirst(new MockPropertySource().withProperty(ACTIVE_PROFILES_PROPERTY_NAME, "p1"));
609+
assertThat(environment.matchesProfiles("p1")).isTrue();
610+
}
611+
612+
@Test
613+
@SuppressWarnings("deprecation")
614+
void defaultProfile() {
615+
assertThat(environment.matchesProfiles("pd")).isFalse();
616+
617+
environment.setDefaultProfiles("pd");
618+
assertThat(environment.matchesProfiles("pd")).isTrue();
619+
620+
environment.setActiveProfiles("p1");
621+
assertThat(environment.matchesProfiles("pd")).isFalse();
622+
assertThat(environment.matchesProfiles("p1")).isTrue();
623+
}
624+
625+
@Test
626+
@SuppressWarnings("deprecation")
627+
void withNotOperator() {
628+
assertThat(environment.matchesProfiles("p1")).isFalse();
629+
assertThat(environment.matchesProfiles("!p1")).isTrue();
630+
631+
environment.addActiveProfile("p1");
632+
assertThat(environment.matchesProfiles("p1")).isTrue();
633+
assertThat(environment.matchesProfiles("!p1")).isFalse();
634+
}
635+
636+
@Test
637+
void withProfileExpressions() {
638+
assertThat(environment.matchesProfiles("p1 & p2")).isFalse();
639+
640+
environment.addActiveProfile("p1");
641+
assertThat(environment.matchesProfiles("p1 | p2")).isTrue();
642+
assertThat(environment.matchesProfiles("p1 & p2")).isFalse();
643+
644+
environment.addActiveProfile("p2");
645+
assertThat(environment.matchesProfiles("p1 & p2")).isTrue();
646+
assertThat(environment.matchesProfiles("p1 | p2")).isTrue();
647+
assertThat(environment.matchesProfiles("foo | p1", "p2")).isTrue();
648+
assertThat(environment.matchesProfiles("foo | p2", "p1")).isTrue();
649+
assertThat(environment.matchesProfiles("foo | (p2 & p1)")).isTrue();
650+
assertThat(environment.matchesProfiles("p2 & (foo | p1)")).isTrue();
651+
assertThat(environment.matchesProfiles("foo", "(p2 & p1)")).isTrue();
652+
}
653+
654+
}
655+
544656
}

0 commit comments

Comments
 (0)