Merge branch '2.0.x'
commit
1bf97e9b5e
@ -0,0 +1,100 @@
|
||||
/*
|
||||
* Copyright 2012-2018 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.actuate.endpoint;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* An identifier for an actuator endpoint. Endpoint IDs may contain only letters and
|
||||
* numbers and must begin with a lower-case letter. Case is ignored when comparing
|
||||
* endpoint IDs.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @since 2.0.6
|
||||
*/
|
||||
public final class EndpointId {
|
||||
|
||||
private static final Pattern ALPHA_NUMERIC = Pattern.compile("[a-zA-Z0-9]+");
|
||||
|
||||
private final String value;
|
||||
|
||||
private final String lowerCaseValue;
|
||||
|
||||
private EndpointId(String value) {
|
||||
Assert.hasText(value, "Value must not be empty");
|
||||
Assert.isTrue(ALPHA_NUMERIC.matcher(value).matches(),
|
||||
"Value must be alpha-numeric");
|
||||
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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null || getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
return toLowerCaseString().equals(((EndpointId) obj).toLowerCaseString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return toLowerCaseString().hashCode();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a lower-case version of the endpoint ID.
|
||||
* @return the lower-case endpoint ID
|
||||
*/
|
||||
public String toLowerCaseString() {
|
||||
return this.lowerCaseValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory method to create a new {@link EndpointId} of the specified value.
|
||||
* @param value the endpoint ID value
|
||||
* @return an {@link EndpointId} instance
|
||||
*/
|
||||
public static EndpointId of(String value) {
|
||||
return new EndpointId(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory method to create a new {@link EndpointId} from a property value. Is more
|
||||
* lenient that {@link #of(String)} to allow for common "relaxed" property variants.
|
||||
* @param value the property value to convert
|
||||
* @return an {@link EndpointId} instance
|
||||
*/
|
||||
public static EndpointId fromPropertyValue(String value) {
|
||||
return new EndpointId(value.replace("-", ""));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,92 @@
|
||||
/*
|
||||
* Copyright 2012-2018 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.actuate.endpoint;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Tests for {@link EndpointId}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class EndpointIdTests {
|
||||
|
||||
@Test
|
||||
public void ofWhenNullThorowsException() {
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> EndpointId.of(null))
|
||||
.withMessage("Value must not be empty");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ofWhenEmptyThrowsException() {
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> EndpointId.of(""))
|
||||
.withMessage("Value must not be empty");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ofWhenContainsDashThrowsException() {
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> EndpointId.of("foo-bar"))
|
||||
.withMessage("Value must be alpha-numeric");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ofWhenHasBadCharThrowsException() {
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> EndpointId.of("foo!bar"))
|
||||
.withMessage("Value must be alpha-numeric");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ofWhenStartsWithNumberThrowsException() {
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> EndpointId.of("1foo"))
|
||||
.withMessage("Value must not start with a number");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ofWhenStartsWithUppercaseLetterThrowsException() {
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> EndpointId.of("Foo"))
|
||||
.withMessage("Value must not start with an uppercase letter");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equalsAndHashCode() {
|
||||
EndpointId one = EndpointId.of("foobar");
|
||||
EndpointId two = EndpointId.of("fooBar");
|
||||
EndpointId three = EndpointId.of("barfoo");
|
||||
assertThat(one.hashCode()).isEqualTo(two.hashCode());
|
||||
assertThat(one).isEqualTo(one).isEqualTo(two).isNotEqualTo(three);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toLowerCaseStringReturnsLowercase() {
|
||||
assertThat(EndpointId.of("fooBar").toLowerCaseString()).isEqualTo("foobar");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toStringReturnsString() {
|
||||
assertThat(EndpointId.of("fooBar").toString()).isEqualTo("fooBar");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fromPropertyValueStripsDashes() {
|
||||
EndpointId fromPropertyValue = EndpointId.fromPropertyValue("foo-bar");
|
||||
assertThat(fromPropertyValue).isEqualTo(EndpointId.of("fooBar"));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue