Skip to content

Commit 3f28207

Browse files
committed
Switch to String#isBlank and String#strip
Since we now have a Java 17 baseline, this commit makes use of String#isBlank and String#strip (instead of String#trim) throughout the code base. Closes #4697
1 parent 9a6b0a7 commit 3f28207

File tree

40 files changed

+88
-87
lines changed

40 files changed

+88
-87
lines changed

documentation/src/docs/asciidoc/user-guide/running-tests.adoc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -961,18 +961,18 @@ own annotation or other means for users to specify tags.
961961
Regardless how a tag is specified, the JUnit Platform enforces the following rules:
962962

963963
* A tag must not be `null` or _blank_.
964-
* A _trimmed_ tag must not contain whitespace.
965-
* A _trimmed_ tag must not contain ISO control characters.
966-
* A _trimmed_ tag must not contain any of the following _reserved characters_.
964+
* A _stripped_ tag must not contain whitespace.
965+
* A _stripped_ tag must not contain ISO control characters.
966+
* A _stripped_ tag must not contain any of the following _reserved characters_.
967967
- `,`: _comma_
968968
- `(`: _left parenthesis_
969969
- `)`: _right parenthesis_
970970
- `&`: _ampersand_
971971
- `|`: _vertical bar_
972972
- `!`: _exclamation point_
973973

974-
NOTE: In the above context, "trimmed" means that leading and trailing whitespace
975-
characters have been removed.
974+
NOTE: In the above context, "stripped" means that leading and trailing whitespace
975+
characters have been removed using `java.lang.String.strip()`.
976976

977977
[[running-tests-tag-expressions]]
978978
==== Tag Expressions

documentation/src/test/java/example/ParameterizedTestDemo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class NullAndEmptySource_1 {
108108
@EmptySource
109109
@ValueSource(strings = { " ", " ", "\t", "\n" })
110110
void nullEmptyAndBlankStrings(String text) {
111-
assertTrue(text == null || text.trim().isEmpty());
111+
assertTrue(text == null || text.isBlank());
112112
}
113113
// end::NullAndEmptySource_example1[]
114114
}
@@ -121,7 +121,7 @@ class NullAndEmptySource_2 {
121121
@NullAndEmptySource
122122
@ValueSource(strings = { " ", " ", "\t", "\n" })
123123
void nullEmptyAndBlankStrings(String text) {
124-
assertTrue(text == null || text.trim().isEmpty());
124+
assertTrue(text == null || text.isBlank());
125125
}
126126
// end::NullAndEmptySource_example2[]
127127
}

junit-jupiter-api/src/main/java/org/junit/jupiter/api/AssertLinesMatch.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,13 +198,13 @@ void fail(String format, Object... args) {
198198
}
199199

200200
static boolean isFastForwardLine(String line) {
201-
line = line.trim();
201+
line = line.strip();
202202
return line.length() >= 4 && line.startsWith(">>") && line.endsWith(">>");
203203
}
204204

205205
static int parseFastForwardLimit(String fastForwardLine) {
206-
fastForwardLine = fastForwardLine.trim();
207-
String text = fastForwardLine.substring(2, fastForwardLine.length() - 2).trim();
206+
fastForwardLine = fastForwardLine.strip();
207+
String text = fastForwardLine.substring(2, fastForwardLine.length() - 2).strip();
208208
try {
209209
int limit = Integer.parseInt(text);
210210
condition(limit > 0, () -> "fast-forward(%d) limit must be greater than zero".formatted(limit));

junit-jupiter-api/src/main/java/org/junit/jupiter/api/DisplayNameGenerator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ private String getSentenceBeginning(Class<?> testClass, List<Class<?>> enclosing
391391

392392
String sentenceFragment = findAnnotation(testClass, DisplayName.class)//
393393
.map(DisplayName::value)//
394-
.map(String::trim)//
394+
.map(String::strip)//
395395
.orElseGet(() -> getSentenceFragment(testClass));
396396

397397
if (enclosingClass == null || isStatic(testClass)) { // top-level class
@@ -508,7 +508,7 @@ private static Optional<IndicativeSentencesGeneration> findIndicativeSentencesGe
508508
.map(sentenceFragment -> {
509509
Preconditions.notBlank(sentenceFragment,
510510
"@SentenceFragment on [%s] must be declared with a non-blank value.".formatted(element));
511-
return sentenceFragment.trim();
511+
return sentenceFragment.strip();
512512
}) //
513513
.orElse(null);
514514
}

junit-jupiter-api/src/main/java/org/junit/jupiter/api/Tag.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
/**
6666
* The <em>tag</em>.
6767
*
68-
* <p>Note: the tag will first be {@linkplain String#trim() trimmed}. If the
68+
* <p>Note: the tag will first be {@linkplain String#strip() stripped}. If the
6969
* supplied tag is syntactically invalid after trimming, the error will be
7070
* logged as a warning, and the invalid tag will be effectively ignored. See
7171
* {@linkplain Tag Syntax Rules for Tags}.

junit-jupiter-api/src/main/java/org/junit/jupiter/api/condition/DisabledIfEnvironmentVariableCondition.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ protected ConditionEvaluationResult getNoDisabledConditionsEncounteredResult() {
4141

4242
@Override
4343
protected ConditionEvaluationResult evaluate(DisabledIfEnvironmentVariable annotation) {
44-
String name = annotation.named().trim();
44+
String name = annotation.named().strip();
4545
String regex = annotation.matches();
4646
Preconditions.notBlank(name, () -> "The 'named' attribute must not be blank in " + annotation);
4747
Preconditions.notBlank(regex, () -> "The 'matches' attribute must not be blank in " + annotation);

junit-jupiter-api/src/main/java/org/junit/jupiter/api/condition/DisabledIfSystemPropertyCondition.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ protected ConditionEvaluationResult getNoDisabledConditionsEncounteredResult() {
3939

4040
@Override
4141
protected ConditionEvaluationResult evaluate(DisabledIfSystemProperty annotation) {
42-
String name = annotation.named().trim();
42+
String name = annotation.named().strip();
4343
String regex = annotation.matches();
4444
Preconditions.notBlank(name, () -> "The 'named' attribute must not be blank in " + annotation);
4545
Preconditions.notBlank(regex, () -> "The 'matches' attribute must not be blank in " + annotation);

junit-jupiter-api/src/main/java/org/junit/jupiter/api/condition/EnabledIfEnvironmentVariableCondition.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ protected ConditionEvaluationResult getNoDisabledConditionsEncounteredResult() {
4242
@Override
4343
protected ConditionEvaluationResult evaluate(EnabledIfEnvironmentVariable annotation) {
4444

45-
String name = annotation.named().trim();
45+
String name = annotation.named().strip();
4646
String regex = annotation.matches();
4747
Preconditions.notBlank(name, () -> "The 'named' attribute must not be blank in " + annotation);
4848
Preconditions.notBlank(regex, () -> "The 'matches' attribute must not be blank in " + annotation);

junit-jupiter-api/src/main/java/org/junit/jupiter/api/condition/EnabledIfSystemPropertyCondition.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ protected ConditionEvaluationResult getNoDisabledConditionsEncounteredResult() {
4040
@Override
4141
protected ConditionEvaluationResult evaluate(EnabledIfSystemProperty annotation) {
4242

43-
String name = annotation.named().trim();
43+
String name = annotation.named().strip();
4444
String regex = annotation.matches();
4545
Preconditions.notBlank(name, () -> "The 'named' attribute must not be blank in " + annotation);
4646
Preconditions.notBlank(regex, () -> "The 'matches' attribute must not be blank in " + annotation);

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/config/EnumConfigurationParameterConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public E get(String key, Function<String, Optional<String>> lookup, E defaultVal
5151
if (value.isPresent()) {
5252
String constantName = null;
5353
try {
54-
constantName = value.get().trim().toUpperCase(Locale.ROOT);
54+
constantName = value.get().strip().toUpperCase(Locale.ROOT);
5555
E result = Enum.valueOf(enumType, constantName);
5656
logger.config(() -> "Using %s '%s' set via the '%s' configuration parameter.".formatted(enumDisplayName,
5757
result, key));

0 commit comments

Comments
 (0)