Skip to content

Commit

Permalink
Improve toString() for Profiles.of() implementation
Browse files Browse the repository at this point in the history
Prior to this commit, the toString() implementation in ProfilesParser
(which is the internal implementation of Profiles.of()) concatenated
profile expressions with " or ".

For example, the string representation of
Profiles.of("spring & framework", "java | kotlin") was
"spring & framework or java | kotlin".

This commit improves the toString() implementation by wrapping
individual profile expressions in parentheses and concatenating them
with " | ".

For example, the string representation from the previous example is now
"(spring & framework) | (java | kotlin)".

This makes it easier to read (for example, when debugging) and
comprehend.

As an additional benefit, the result of invoking toString() can even be
used as a logically equivalent composite profile expression, though
that is not the primary goal of this change.

Closes gh-30374
  • Loading branch information
sbrannen committed Apr 25, 2023
1 parent b924b7b commit 845488a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
import java.util.Set;
import java.util.StringTokenizer;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

/**
* Internal parser used by {@link Profiles#of}.
Expand Down Expand Up @@ -189,7 +189,14 @@ public boolean equals(@Nullable Object obj) {

@Override
public String toString() {
return StringUtils.collectionToDelimitedString(this.expressions, " or ");
if (this.expressions.size() == 1) {
return this.expressions.iterator().next();
}
return this.expressions.stream().map(this::wrap).collect(Collectors.joining(" | "));
}

private String wrap(String str) {
return "(" + str + ")";
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -272,6 +272,12 @@ void ofComplexExpressionWithoutSpaces() {
assertComplexExpression(profiles);
}

@Test
void ofComplexExpressionEnclosedInParentheses() {
Profiles profiles = Profiles.of("((spring & framework) | (spring & java))");
assertComplexExpression(profiles);
}

private void assertComplexExpression(Profiles profiles) {
assertThat(profiles.matches(activeProfiles("spring"))).isFalse();
assertThat(profiles.matches(activeProfiles("spring", "framework"))).isTrue();
Expand All @@ -291,8 +297,27 @@ void sensibleToString() {
assertThat(Profiles.of("spring")).hasToString("spring");
assertThat(Profiles.of("(spring & framework) | (spring & java)")).hasToString("(spring & framework) | (spring & java)");
assertThat(Profiles.of("(spring&framework)|(spring&java)")).hasToString("(spring&framework)|(spring&java)");
assertThat(Profiles.of("spring & framework", "java | kotlin")).hasToString("spring & framework or java | kotlin");
assertThat(Profiles.of("java | kotlin", "spring & framework")).hasToString("java | kotlin or spring & framework");
assertThat(Profiles.of("spring & framework", "java | kotlin")).hasToString("(spring & framework) | (java | kotlin)");
assertThat(Profiles.of("java | kotlin", "spring & framework")).hasToString("(java | kotlin) | (spring & framework)");
assertThat(Profiles.of("java | kotlin", "spring & framework", "cat | dog")).hasToString("(java | kotlin) | (spring & framework) | (cat | dog)");
}

@Test
void toStringGeneratesValidCompositeProfileExpression() {
assertThatToStringGeneratesValidCompositeProfileExpression("spring");
assertThatToStringGeneratesValidCompositeProfileExpression("(spring & kotlin) | (spring & java)");
assertThatToStringGeneratesValidCompositeProfileExpression("spring & kotlin", "spring & java");
assertThatToStringGeneratesValidCompositeProfileExpression("spring & kotlin", "spring & java", "cat | dog");
}

private static void assertThatToStringGeneratesValidCompositeProfileExpression(String... profileExpressions) {
Profiles profiles = Profiles.of(profileExpressions);
assertThat(profiles.matches(activeProfiles("spring", "java"))).isTrue();
assertThat(profiles.matches(activeProfiles("kotlin"))).isFalse();

Profiles compositeProfiles = Profiles.of(profiles.toString());
assertThat(compositeProfiles.matches(activeProfiles("spring", "java"))).isTrue();
assertThat(compositeProfiles.matches(activeProfiles("kotlin"))).isFalse();
}

@Test
Expand Down

0 comments on commit 845488a

Please sign in to comment.