Skip to content

Commit 47ba819

Browse files
committed
Polish Environment and StandardEnvironmentTests
See spring-projectsgh-30206
1 parent ea4c64e commit 47ba819

File tree

5 files changed

+242
-190
lines changed

5 files changed

+242
-190
lines changed

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

+20-10
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
* of property sources prior to application context {@code refresh()}.
5757
*
5858
* @author Chris Beams
59+
* @author Phillip Webb
5960
* @since 3.1
6061
* @see PropertyResolver
6162
* @see EnvironmentCapable
@@ -94,14 +95,17 @@ public interface Environment extends PropertyResolver {
9495
String[] getDefaultProfiles();
9596

9697
/**
97-
* Return whether one or more of the given profiles is active or, in the case of no
98-
* explicit active profiles, whether one or more of the given profiles is included in
99-
* the set of default profiles. If a profile begins with '!' the logic is inverted,
100-
* i.e. the method will return {@code true} if the given profile is <em>not</em> active.
101-
* For example, {@code env.acceptsProfiles("p1", "!p2")} will return {@code true} if
102-
* profile 'p1' is active or 'p2' is not active.
103-
* @throws IllegalArgumentException if called with zero arguments
104-
* or if any profile is {@code null}, empty, or whitespace only
98+
* Determine whether one or more of the given profiles is active &mdash; or
99+
* in the case of no explicit {@linkplain #getActiveProfiles() active profiles},
100+
* whether one or more of the given profiles is included in the set of
101+
* {@linkplain #getDefaultProfiles() default profiles}.
102+
* <p>If a profile begins with '!' the logic is inverted, meaning this method
103+
* will return {@code true} if the given profile is <em>not</em> active. For
104+
* example, {@code env.acceptsProfiles("p1", "!p2")} will return {@code true}
105+
* if profile 'p1' is active or 'p2' is not active.
106+
* @throws IllegalArgumentException if called with a {@code null} array, an
107+
* empty array, zero arguments or if any profile is {@code null}, empty, or
108+
* whitespace only
105109
* @see #getActiveProfiles
106110
* @see #getDefaultProfiles
107111
* @see #acceptsProfiles(Profiles)
@@ -111,8 +115,14 @@ public interface Environment extends PropertyResolver {
111115
boolean acceptsProfiles(String... profiles);
112116

113117
/**
114-
* Return whether the {@linkplain #getActiveProfiles() active profiles}
115-
* match the given {@link Profiles} predicate.
118+
* Determine whether the given {@link Profiles} predicate matches the
119+
* {@linkplain #getActiveProfiles() active profiles} &mdash; or in the case
120+
* of no explicit active profiles, whether the given {@code Profiles} predicate
121+
* matches the {@linkplain #getDefaultProfiles() default profiles}.
122+
* <p>If you wish to check a single profile expression, consider using
123+
* {@link #acceptsProfiles(String)} instead.
124+
* @since 5.1
125+
* @see Profiles#of(String...)
116126
*/
117127
boolean acceptsProfiles(Profiles profiles);
118128

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

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -43,32 +43,32 @@ public interface Profiles {
4343

4444
/**
4545
* Create a new {@link Profiles} instance that checks for matches against
46-
* the given <em>profile strings</em>.
46+
* the given <em>profile expressions</em>.
4747
* <p>The returned instance will {@linkplain Profiles#matches(Predicate) match}
48-
* if any one of the given profile strings matches.
49-
* <p>A profile string may contain a simple profile name (for example
50-
* {@code "production"}) or a profile expression. A profile expression allows
48+
* if any one of the given profile expressions matches.
49+
* <p>A profile expression may contain a simple profile name (for example
50+
* {@code "production"}) or a compound expression. A compound expression allows
5151
* for more complicated profile logic to be expressed, for example
5252
* {@code "production & cloud"}.
5353
* <p>The following operators are supported in profile expressions.
5454
* <ul>
55-
* <li>{@code !} - A logical <em>NOT</em> of the profile or profile expression</li>
56-
* <li>{@code &} - A logical <em>AND</em> of the profiles or profile expressions</li>
57-
* <li>{@code |} - A logical <em>OR</em> of the profiles or profile expressions</li>
55+
* <li>{@code !} - A logical <em>NOT</em> of the profile name or compound expression</li>
56+
* <li>{@code &} - A logical <em>AND</em> of the profile names or compound expressions</li>
57+
* <li>{@code |} - A logical <em>OR</em> of the profile names or compound expressions</li>
5858
* </ul>
5959
* <p>Please note that the {@code &} and {@code |} operators may not be mixed
60-
* without using parentheses. For example {@code "a & b | c"} is not a valid
61-
* expression; it must be expressed as {@code "(a & b) | c"} or
60+
* without using parentheses. For example, {@code "a & b | c"} is not a valid
61+
* expression: it must be expressed as {@code "(a & b) | c"} or
6262
* {@code "a & (b | c)"}.
6363
* <p>As of Spring Framework 5.1.17, two {@code Profiles} instances returned
6464
* by this method are considered equivalent to each other (in terms of
6565
* {@code equals()} and {@code hashCode()} semantics) if they are created
66-
* with identical <em>profile strings</em>.
67-
* @param profiles the <em>profile strings</em> to include
66+
* with identical <em>profile expressions</em>.
67+
* @param profileExpressions the <em>profile expressions</em> to include
6868
* @return a new {@link Profiles} instance
6969
*/
70-
static Profiles of(String... profiles) {
71-
return ProfilesParser.parse(profiles);
70+
static Profiles of(String... profileExpressions) {
71+
return ProfilesParser.parse(profileExpressions);
7272
}
7373

7474
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ private ProfilesParser() {
4343

4444

4545
static Profiles parse(String... expressions) {
46-
Assert.notEmpty(expressions, "Must specify at least one profile");
46+
Assert.notEmpty(expressions, "Must specify at least one profile expression");
4747
Profiles[] parsed = new Profiles[expressions.length];
4848
for (int i = 0; i < expressions.length; i++) {
4949
parsed[i] = parseExpression(expressions[i]);
@@ -136,8 +136,8 @@ private static Profiles equals(String profile) {
136136
return activeProfile -> activeProfile.test(profile);
137137
}
138138

139-
private static Predicate<Profiles> isMatch(Predicate<String> activeProfile) {
140-
return profiles -> profiles.matches(activeProfile);
139+
private static Predicate<Profiles> isMatch(Predicate<String> activeProfiles) {
140+
return profiles -> profiles.matches(activeProfiles);
141141
}
142142

143143

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

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -40,30 +40,30 @@ class ProfilesTests {
4040

4141
@Test
4242
void ofWhenNullThrowsException() {
43-
assertThatIllegalArgumentException().isThrownBy(() ->
44-
Profiles.of((String[]) null))
45-
.withMessageContaining("Must specify at least one profile");
43+
assertThatIllegalArgumentException()
44+
.isThrownBy(() -> Profiles.of((String[]) null))
45+
.withMessage("Must specify at least one profile expression");
4646
}
4747

4848
@Test
4949
void ofWhenEmptyThrowsException() {
5050
assertThatIllegalArgumentException()
5151
.isThrownBy(Profiles::of)
52-
.withMessageContaining("Must specify at least one profile");
52+
.withMessage("Must specify at least one profile expression");
5353
}
5454

5555
@Test
5656
void ofNullElement() {
57-
assertThatIllegalArgumentException().isThrownBy(() ->
58-
Profiles.of((String) null))
59-
.withMessageContaining("must contain text");
57+
assertThatIllegalArgumentException()
58+
.isThrownBy(() -> Profiles.of((String) null))
59+
.withMessage("Invalid profile expression [null]: must contain text");
6060
}
6161

6262
@Test
6363
void ofEmptyElement() {
64-
assertThatIllegalArgumentException().isThrownBy(() ->
65-
Profiles.of(" "))
66-
.withMessageContaining("must contain text");
64+
assertThatIllegalArgumentException()
65+
.isThrownBy(() -> Profiles.of(" "))
66+
.withMessage("Invalid profile expression [ ]: must contain text");
6767
}
6868

6969
@Test
@@ -356,7 +356,7 @@ void equalsAndHashCodeAreNotBasedOnLogicalStructureOfNodesWithinExpressionTree()
356356
private static void assertMalformed(Supplier<Profiles> supplier) {
357357
assertThatIllegalArgumentException()
358358
.isThrownBy(supplier::get)
359-
.withMessageContaining("Malformed");
359+
.withMessageStartingWith("Malformed profile expression");
360360
}
361361

362362
private static Predicate<String> activeProfiles(String... profiles) {

0 commit comments

Comments
 (0)