Merge branch '2.0.x'
This commit is contained in:
commit
3665917d44
|
|
@ -23,30 +23,44 @@ import org.springframework.util.Assert;
|
|||
|
||||
/**
|
||||
* An identifier for an actuator endpoint. Endpoint IDs may contain only letters, numbers
|
||||
* and {@code '.'}. They must begin with a lower-case letter. Case is ignored when
|
||||
* comparing endpoint IDs.
|
||||
* {@code '.'} and {@code '-'}. They must begin with a lower-case letter. Case and syntax
|
||||
* characters are ignored when comparing endpoint IDs.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @since 2.0.6
|
||||
*/
|
||||
public final class EndpointId {
|
||||
|
||||
private static final Pattern VALID_CHARS = Pattern.compile("[a-zA-Z0-9\\.]+");
|
||||
private static final Pattern VALID_CHARS = Pattern.compile("[a-zA-Z0-9\\.\\-]+");
|
||||
|
||||
private final String value;
|
||||
|
||||
private final String lowerCaseValue;
|
||||
|
||||
private final String lowerCaseAlphaNumeric;
|
||||
|
||||
private EndpointId(String value) {
|
||||
Assert.hasText(value, "Value must not be empty");
|
||||
Assert.isTrue(VALID_CHARS.matcher(value).matches(),
|
||||
"Value must be alpha-numeric or '.'");
|
||||
"Value must only contain valid chars");
|
||||
Assert.isTrue(!Character.isDigit(value.charAt(0)),
|
||||
"Value must not start with a number");
|
||||
Assert.isTrue(!Character.isUpperCase(value.charAt(0)),
|
||||
"Value must not start with an uppercase letter");
|
||||
this.value = value;
|
||||
this.lowerCaseValue = value.toLowerCase(Locale.ENGLISH);
|
||||
this.lowerCaseAlphaNumeric = getAlphaNumerics(this.lowerCaseValue);
|
||||
}
|
||||
|
||||
private String getAlphaNumerics(String value) {
|
||||
StringBuilder result = new StringBuilder(value.length());
|
||||
for (int i = 0; i < value.length(); i++) {
|
||||
char ch = value.charAt(i);
|
||||
if (ch >= 'a' && ch <= 'z' || ch >= '0' && ch <= '9') {
|
||||
result.append(ch);
|
||||
}
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -57,12 +71,13 @@ public final class EndpointId {
|
|||
if (obj == null || getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
return toLowerCaseString().equals(((EndpointId) obj).toLowerCaseString());
|
||||
return this.lowerCaseAlphaNumeric
|
||||
.equals(((EndpointId) obj).lowerCaseAlphaNumeric);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return toLowerCaseString().hashCode();
|
||||
return this.lowerCaseAlphaNumeric.hashCode();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -41,15 +41,15 @@ public class EndpointIdTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void ofWhenContainsDashThrowsException() {
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> EndpointId.of("foo-bar"))
|
||||
.withMessageContaining("Value must be alpha-numeric");
|
||||
public void ofWhenContainsInvalidCharThrowsException() {
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> EndpointId.of("foo!bar"))
|
||||
.withMessage("Value must only contain valid chars");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ofWhenHasBadCharThrowsException() {
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> EndpointId.of("foo!bar"))
|
||||
.withMessageContaining("Value must be alpha-numeric");
|
||||
.withMessage("Value must only contain valid chars");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -72,13 +72,25 @@ public class EndpointIdTests {
|
|||
assertThat(endpointId.toString()).isEqualTo("foo.bar");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ofWhenContainsDashIsValid() {
|
||||
// Ideally we wouldn't support this but there are existing endpoints using the
|
||||
// pattern. See gh-14773
|
||||
EndpointId endpointId = EndpointId.of("foo-bar");
|
||||
assertThat(endpointId.toString()).isEqualTo("foo-bar");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equalsAndHashCode() {
|
||||
EndpointId one = EndpointId.of("foobar");
|
||||
EndpointId two = EndpointId.of("fooBar");
|
||||
EndpointId three = EndpointId.of("barfoo");
|
||||
EndpointId one = EndpointId.of("foobar1");
|
||||
EndpointId two = EndpointId.of("fooBar1");
|
||||
EndpointId three = EndpointId.of("foo-bar1");
|
||||
EndpointId four = EndpointId.of("foo.bar1");
|
||||
EndpointId five = EndpointId.of("barfoo1");
|
||||
EndpointId six = EndpointId.of("foobar2");
|
||||
assertThat(one.hashCode()).isEqualTo(two.hashCode());
|
||||
assertThat(one).isEqualTo(one).isEqualTo(two).isNotEqualTo(three);
|
||||
assertThat(one).isEqualTo(one).isEqualTo(two).isEqualTo(two).isEqualTo(three)
|
||||
.isEqualTo(four).isNotEqualTo(five).isNotEqualTo(six);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
|||
Loading…
Reference in New Issue