From 30db03837a32f3f365d8dbd57cbf6447a190117d Mon Sep 17 00:00:00 2001 From: ljacqu Date: Thu, 7 Jan 2016 21:58:28 +0100 Subject: [PATCH] #347 Add 'contains' method to PropertyType --- .../settings/domain/EnumPropertyType.java | 6 +++ .../authme/settings/domain/PropertyType.java | 4 ++ .../settings/domain/EnumPropertyTypeTest.java | 47 +++++++++++++++++++ 3 files changed, 57 insertions(+) diff --git a/src/main/java/fr/xephi/authme/settings/domain/EnumPropertyType.java b/src/main/java/fr/xephi/authme/settings/domain/EnumPropertyType.java index 8455a893..357067d8 100644 --- a/src/main/java/fr/xephi/authme/settings/domain/EnumPropertyType.java +++ b/src/main/java/fr/xephi/authme/settings/domain/EnumPropertyType.java @@ -33,6 +33,12 @@ class EnumPropertyType> extends PropertyType { return asList("'" + value + "'"); } + @Override + public boolean contains(Property property, YamlConfiguration configuration) { + return super.contains(property, configuration) + && mapToEnum(configuration.getString(property.getPath())) != null; + } + private E mapToEnum(String value) { for (E entry : clazz.getEnumConstants()) { if (entry.name().equalsIgnoreCase(value)) { diff --git a/src/main/java/fr/xephi/authme/settings/domain/PropertyType.java b/src/main/java/fr/xephi/authme/settings/domain/PropertyType.java index e4196564..b243ef65 100644 --- a/src/main/java/fr/xephi/authme/settings/domain/PropertyType.java +++ b/src/main/java/fr/xephi/authme/settings/domain/PropertyType.java @@ -49,6 +49,10 @@ public abstract class PropertyType { */ protected abstract List asYaml(T value); + protected boolean contains(Property property, YamlConfiguration configuration) { + return configuration.contains(property.getPath()); + } + /** * Boolean property. diff --git a/src/test/java/fr/xephi/authme/settings/domain/EnumPropertyTypeTest.java b/src/test/java/fr/xephi/authme/settings/domain/EnumPropertyTypeTest.java index 1b9d3909..461314e8 100644 --- a/src/test/java/fr/xephi/authme/settings/domain/EnumPropertyTypeTest.java +++ b/src/test/java/fr/xephi/authme/settings/domain/EnumPropertyTypeTest.java @@ -58,6 +58,53 @@ public class EnumPropertyTypeTest { assertThat(result, equalTo(TestEnum.ENTRY_C)); } + @Test + public void shouldReturnTrueForContainsCheck() { + // given + PropertyType propertyType = new EnumPropertyType<>(TestEnum.class); + Property 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 propertyType = new EnumPropertyType<>(TestEnum.class); + Property 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 propertyType = new EnumPropertyType<>(TestEnum.class); + Property 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 {