diff --git a/spring-core/src/main/java/org/springframework/core/env/Environment.java b/spring-core/src/main/java/org/springframework/core/env/Environment.java index c996040a44..6210d17810 100644 --- a/spring-core/src/main/java/org/springframework/core/env/Environment.java +++ b/spring-core/src/main/java/org/springframework/core/env/Environment.java @@ -57,6 +57,7 @@ package org.springframework.core.env; * * @author Chris Beams * @author Phillip Webb + * @author Sam Brannen * @since 3.1 * @see PropertyResolver * @see EnvironmentCapable @@ -108,20 +109,42 @@ public interface Environment extends PropertyResolver { * whitespace only * @see #getActiveProfiles * @see #getDefaultProfiles + * @see #matchesProfiles(String...) * @see #acceptsProfiles(Profiles) - * @deprecated as of 5.1 in favor of {@link #acceptsProfiles(Profiles)} + * @deprecated as of 5.1 in favor of {@link #acceptsProfiles(Profiles)} or + * {@link #matchesProfiles(String...)} */ @Deprecated boolean acceptsProfiles(String... profiles); + /** + * Determine whether one of the given profile expressions matches the + * {@linkplain #getActiveProfiles() active profiles} — or in the case + * of no explicit active profiles, whether one of the given profile expressions + * matches the {@linkplain #getDefaultProfiles() default profiles}. + *
Profile expressions allow for complex, boolean profile logic to be + * expressed — for example {@code "p1 & p2"}, {@code "(p1 & p2) | p3"}, + * etc. See {@link Profiles#of(String...)} for details on the supported + * expression syntax. + *
This method is a convenient shortcut for + * {@code env.acceptsProfiles(Profiles.of(profileExpressions))}. + * @since 5.3.28 + * @see Profiles#of(String...) + * @see #acceptsProfiles(Profiles) + */ + default boolean matchesProfiles(String... profileExpressions) { + return acceptsProfiles(Profiles.of(profileExpressions)); + } + /** * Determine whether the given {@link Profiles} predicate matches the * {@linkplain #getActiveProfiles() active profiles} — or in the case * of no explicit active profiles, whether the given {@code Profiles} predicate * matches the {@linkplain #getDefaultProfiles() default profiles}. - *
If you wish to check a single profile expression, consider using - * {@link #acceptsProfiles(String)} instead. + *
If you wish provide profile expressions directly as strings, use + * {@link #matchesProfiles(String...)} instead. * @since 5.1 + * @see #matchesProfiles(String...) * @see Profiles#of(String...) */ boolean acceptsProfiles(Profiles profiles); diff --git a/spring-core/src/test/java/org/springframework/core/env/StandardEnvironmentTests.java b/spring-core/src/test/java/org/springframework/core/env/StandardEnvironmentTests.java index 466606efcd..36c7915ac0 100644 --- a/spring-core/src/test/java/org/springframework/core/env/StandardEnvironmentTests.java +++ b/spring-core/src/test/java/org/springframework/core/env/StandardEnvironmentTests.java @@ -450,4 +450,116 @@ class StandardEnvironmentTests { } + @Nested + class MatchesProfilesTests { + + @Test + @SuppressWarnings("deprecation") + void withEmptyArgumentList() { + assertThatIllegalArgumentException().isThrownBy(environment::matchesProfiles); + } + + @Test + @SuppressWarnings("deprecation") + void withNullArgumentList() { + assertThatIllegalArgumentException().isThrownBy(() -> environment.matchesProfiles((String[]) null)); + } + + @Test + @SuppressWarnings("deprecation") + void withNullArgument() { + assertThatIllegalArgumentException().isThrownBy(() -> environment.matchesProfiles((String) null)); + assertThatIllegalArgumentException().isThrownBy(() -> environment.matchesProfiles("p1", null)); + } + + @Test + @SuppressWarnings("deprecation") + void withEmptyArgument() { + assertThatIllegalArgumentException().isThrownBy(() -> environment.matchesProfiles("")); + assertThatIllegalArgumentException().isThrownBy(() -> environment.matchesProfiles("p1", "")); + assertThatIllegalArgumentException().isThrownBy(() -> environment.matchesProfiles("p1", " ")); + } + + @Test + @SuppressWarnings("deprecation") + void withInvalidNotOperator() { + assertThatIllegalArgumentException().isThrownBy(() -> environment.matchesProfiles("p1", "!")); + } + + @Test + @SuppressWarnings("deprecation") + void withInvalidCompoundExpressionGrouping() { + assertThatIllegalArgumentException().isThrownBy(() -> environment.matchesProfiles("p1 | p2 & p3")); + assertThatIllegalArgumentException().isThrownBy(() -> environment.matchesProfiles("p1 & p2 | p3")); + assertThatIllegalArgumentException().isThrownBy(() -> environment.matchesProfiles("p1 & (p2 | p3) | p4")); + } + + @Test + @SuppressWarnings("deprecation") + void activeProfileSetProgrammatically() { + assertThat(environment.matchesProfiles("p1", "p2")).isFalse(); + + environment.setActiveProfiles("p1"); + assertThat(environment.matchesProfiles("p1", "p2")).isTrue(); + + environment.setActiveProfiles("p2"); + assertThat(environment.matchesProfiles("p1", "p2")).isTrue(); + + environment.setActiveProfiles("p1", "p2"); + assertThat(environment.matchesProfiles("p1", "p2")).isTrue(); + } + + @Test + @SuppressWarnings("deprecation") + void activeProfileSetViaProperty() { + assertThat(environment.matchesProfiles("p1")).isFalse(); + + environment.getPropertySources().addFirst(new MockPropertySource().withProperty(ACTIVE_PROFILES_PROPERTY_NAME, "p1")); + assertThat(environment.matchesProfiles("p1")).isTrue(); + } + + @Test + @SuppressWarnings("deprecation") + void defaultProfile() { + assertThat(environment.matchesProfiles("pd")).isFalse(); + + environment.setDefaultProfiles("pd"); + assertThat(environment.matchesProfiles("pd")).isTrue(); + + environment.setActiveProfiles("p1"); + assertThat(environment.matchesProfiles("pd")).isFalse(); + assertThat(environment.matchesProfiles("p1")).isTrue(); + } + + @Test + @SuppressWarnings("deprecation") + void withNotOperator() { + assertThat(environment.matchesProfiles("p1")).isFalse(); + assertThat(environment.matchesProfiles("!p1")).isTrue(); + + environment.addActiveProfile("p1"); + assertThat(environment.matchesProfiles("p1")).isTrue(); + assertThat(environment.matchesProfiles("!p1")).isFalse(); + } + + @Test + void withProfileExpressions() { + assertThat(environment.matchesProfiles("p1 & p2")).isFalse(); + + environment.addActiveProfile("p1"); + assertThat(environment.matchesProfiles("p1 | p2")).isTrue(); + assertThat(environment.matchesProfiles("p1 & p2")).isFalse(); + + environment.addActiveProfile("p2"); + assertThat(environment.matchesProfiles("p1 & p2")).isTrue(); + assertThat(environment.matchesProfiles("p1 | p2")).isTrue(); + assertThat(environment.matchesProfiles("foo | p1", "p2")).isTrue(); + assertThat(environment.matchesProfiles("foo | p2", "p1")).isTrue(); + assertThat(environment.matchesProfiles("foo | (p2 & p1)")).isTrue(); + assertThat(environment.matchesProfiles("p2 & (foo | p1)")).isTrue(); + assertThat(environment.matchesProfiles("foo", "(p2 & p1)")).isTrue(); + } + + } + }