commit
3b49bdecb3
@ -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,102 @@
|
||||
/*
|
||||
* 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.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link EndpointId}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class EndpointIdTests {
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
@Test
|
||||
public void ofWhenNullThorowsException() {
|
||||
this.thrown.expect(IllegalArgumentException.class);
|
||||
this.thrown.expectMessage("Value must not be empty");
|
||||
EndpointId.of(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ofWhenEmptyThrowsException() {
|
||||
this.thrown.expect(IllegalArgumentException.class);
|
||||
this.thrown.expectMessage("Value must not be empty");
|
||||
EndpointId.of("");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ofWhenContainsDashThrowsException() {
|
||||
this.thrown.expect(IllegalArgumentException.class);
|
||||
this.thrown.expectMessage("Value must be alpha-numeric");
|
||||
EndpointId.of("foo-bar");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ofWhenHasBadCharThrowsException() {
|
||||
this.thrown.expect(IllegalArgumentException.class);
|
||||
this.thrown.expectMessage("Value must be alpha-numeric");
|
||||
EndpointId.of("foo!bar");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ofWhenStartsWithNumberThrowsException() {
|
||||
this.thrown.expect(IllegalArgumentException.class);
|
||||
this.thrown.expectMessage("Value must not start with a number");
|
||||
EndpointId.of("1foo");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ofWhenStartsWithUppercaseLetterThrowsException() {
|
||||
this.thrown.expect(IllegalArgumentException.class);
|
||||
this.thrown.expectMessage("Value must not start with an uppercase letter");
|
||||
EndpointId.of("Foo");
|
||||
}
|
||||
|
||||
@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