mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2025-01-04 06:57:41 +01:00
#347 Add 'contains' method to PropertyType
This commit is contained in:
parent
7d749801f9
commit
30db03837a
@ -33,6 +33,12 @@ class EnumPropertyType<E extends Enum<E>> extends PropertyType<E> {
|
|||||||
return asList("'" + value + "'");
|
return asList("'" + value + "'");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean contains(Property<E> property, YamlConfiguration configuration) {
|
||||||
|
return super.contains(property, configuration)
|
||||||
|
&& mapToEnum(configuration.getString(property.getPath())) != null;
|
||||||
|
}
|
||||||
|
|
||||||
private E mapToEnum(String value) {
|
private E mapToEnum(String value) {
|
||||||
for (E entry : clazz.getEnumConstants()) {
|
for (E entry : clazz.getEnumConstants()) {
|
||||||
if (entry.name().equalsIgnoreCase(value)) {
|
if (entry.name().equalsIgnoreCase(value)) {
|
||||||
|
@ -49,6 +49,10 @@ public abstract class PropertyType<T> {
|
|||||||
*/
|
*/
|
||||||
protected abstract List<String> asYaml(T value);
|
protected abstract List<String> asYaml(T value);
|
||||||
|
|
||||||
|
protected boolean contains(Property<T> property, YamlConfiguration configuration) {
|
||||||
|
return configuration.contains(property.getPath());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Boolean property.
|
* Boolean property.
|
||||||
|
@ -58,6 +58,53 @@ public class EnumPropertyTypeTest {
|
|||||||
assertThat(result, equalTo(TestEnum.ENTRY_C));
|
assertThat(result, equalTo(TestEnum.ENTRY_C));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturnTrueForContainsCheck() {
|
||||||
|
// given
|
||||||
|
PropertyType<TestEnum> propertyType = new EnumPropertyType<>(TestEnum.class);
|
||||||
|
Property<TestEnum> property = Property.newProperty(TestEnum.class, "my.test.path", TestEnum.ENTRY_C);
|
||||||
|
YamlConfiguration configuration = mock(YamlConfiguration.class);
|
||||||
|
given(configuration.contains(property.getPath())).willReturn(true);
|
||||||
|
given(configuration.getString(property.getPath())).willReturn("ENTRY_B");
|
||||||
|
|
||||||
|
// when
|
||||||
|
boolean result = propertyType.contains(property, configuration);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(result, equalTo(true));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturnFalseForFileWithoutConfig() {
|
||||||
|
// given
|
||||||
|
PropertyType<TestEnum> propertyType = new EnumPropertyType<>(TestEnum.class);
|
||||||
|
Property<TestEnum> property = Property.newProperty(TestEnum.class, "my.test.path", TestEnum.ENTRY_C);
|
||||||
|
YamlConfiguration configuration = mock(YamlConfiguration.class);
|
||||||
|
given(configuration.contains(property.getPath())).willReturn(false);
|
||||||
|
|
||||||
|
// when
|
||||||
|
boolean result = propertyType.contains(property, configuration);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(result, equalTo(false));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturnFalseForUnknownValue() {
|
||||||
|
// given
|
||||||
|
PropertyType<TestEnum> propertyType = new EnumPropertyType<>(TestEnum.class);
|
||||||
|
Property<TestEnum> property = Property.newProperty(TestEnum.class, "my.test.path", TestEnum.ENTRY_C);
|
||||||
|
YamlConfiguration configuration = mock(YamlConfiguration.class);
|
||||||
|
given(configuration.contains(property.getPath())).willReturn(true);
|
||||||
|
given(configuration.getString(property.getPath())).willReturn("wrong value");
|
||||||
|
|
||||||
|
// when
|
||||||
|
boolean result = propertyType.contains(property, configuration);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(result, equalTo(false));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private enum TestEnum {
|
private enum TestEnum {
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user