Move PropertyType functionality into Property class

This commit is contained in:
ljacqu
2016-03-16 19:07:00 +01:00
parent 89767b120c
commit aef18a894a
14 changed files with 207 additions and 290 deletions
@@ -9,20 +9,19 @@ import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
/**
* Test for {@link EnumPropertyType}.
* Test for {@link EnumProperty}.
*/
public class EnumPropertyTypeTest {
public class EnumPropertyTest {
@Test
public void shouldReturnCorrectEnumValue() {
// given
PropertyType<TestEnum> propertyType = new EnumPropertyType<>(TestEnum.class);
Property<TestEnum> property = Property.newProperty(TestEnum.class, "enum.path", TestEnum.ENTRY_C);
YamlConfiguration configuration = mock(YamlConfiguration.class);
given(configuration.getString(property.getPath())).willReturn("Entry_B");
// when
TestEnum result = propertyType.getFromFile(property, configuration);
TestEnum result = property.getFromFile(configuration);
// then
assertThat(result, equalTo(TestEnum.ENTRY_B));
@@ -31,13 +30,12 @@ public class EnumPropertyTypeTest {
@Test
public void shouldFallBackToDefaultForInvalidValue() {
// given
PropertyType<TestEnum> propertyType = new EnumPropertyType<>(TestEnum.class);
Property<TestEnum> property = Property.newProperty(TestEnum.class, "enum.path", TestEnum.ENTRY_C);
YamlConfiguration configuration = mock(YamlConfiguration.class);
given(configuration.getString(property.getPath())).willReturn("Bogus");
// when
TestEnum result = propertyType.getFromFile(property, configuration);
TestEnum result = property.getFromFile(configuration);
// then
assertThat(result, equalTo(TestEnum.ENTRY_C));
@@ -46,13 +44,12 @@ public class EnumPropertyTypeTest {
@Test
public void shouldFallBackToDefaultForNonExistentValue() {
// given
PropertyType<TestEnum> propertyType = new EnumPropertyType<>(TestEnum.class);
Property<TestEnum> property = Property.newProperty(TestEnum.class, "enum.path", TestEnum.ENTRY_C);
YamlConfiguration configuration = mock(YamlConfiguration.class);
given(configuration.getString(property.getPath())).willReturn(null);
// when
TestEnum result = propertyType.getFromFile(property, configuration);
TestEnum result = property.getFromFile(configuration);
// then
assertThat(result, equalTo(TestEnum.ENTRY_C));
@@ -61,14 +58,13 @@ public class EnumPropertyTypeTest {
@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);
boolean result = property.isPresent(configuration);
// then
assertThat(result, equalTo(true));
@@ -77,13 +73,12 @@ public class EnumPropertyTypeTest {
@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);
boolean result = property.isPresent(configuration);
// then
assertThat(result, equalTo(false));
@@ -92,14 +87,13 @@ public class EnumPropertyTypeTest {
@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);
boolean result = property.isPresent(configuration);
// then
assertThat(result, equalTo(false));
@@ -3,8 +3,7 @@ package fr.xephi.authme.settings.domain;
import org.bukkit.configuration.file.YamlConfiguration;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.mockito.internal.stubbing.answers.ReturnsArgumentAt;
import java.util.Arrays;
import java.util.List;
@@ -20,9 +19,9 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* Test for {@link PropertyType} and the contained subtypes.
* Test for {@link Property} and the contained subtypes.
*/
public class PropertyTypeTest {
public class PropertyTest {
private static YamlConfiguration configuration;
@@ -31,11 +30,11 @@ public class PropertyTypeTest {
configuration = mock(YamlConfiguration.class);
when(configuration.getBoolean(eq("bool.path.test"), anyBoolean())).thenReturn(true);
when(configuration.getBoolean(eq("bool.path.wrong"), anyBoolean())).thenAnswer(secondParameter());
when(configuration.getBoolean(eq("bool.path.wrong"), anyBoolean())).thenAnswer(new ReturnsArgumentAt(1));
when(configuration.getInt(eq("int.path.test"), anyInt())).thenReturn(27);
when(configuration.getInt(eq("int.path.wrong"), anyInt())).thenAnswer(secondParameter());
when(configuration.getInt(eq("int.path.wrong"), anyInt())).thenAnswer(new ReturnsArgumentAt(1));
when(configuration.getString(eq("str.path.test"), anyString())).thenReturn("Test value");
when(configuration.getString(eq("str.path.wrong"), anyString())).thenAnswer(secondParameter());
when(configuration.getString(eq("str.path.wrong"), anyString())).thenAnswer(new ReturnsArgumentAt(1));
when(configuration.isList("list.path.test")).thenReturn(true);
when(configuration.getStringList("list.path.test")).thenReturn(Arrays.asList("test1", "Test2", "3rd test"));
when(configuration.isList("list.path.wrong")).thenReturn(false);
@@ -120,7 +119,7 @@ public class PropertyTypeTest {
@Test
public void shouldGetStringListValue() {
// given
Property<List<String>> property = Property.newProperty(PropertyType.STRING_LIST, "list.path.test", "1", "b");
Property<List<String>> property = Property.newListProperty("list.path.test", "1", "b");
// when
List<String> result = property.getFromFile(configuration);
@@ -133,7 +132,7 @@ public class PropertyTypeTest {
public void shouldGetStringListDefault() {
// given
Property<List<String>> property =
Property.newProperty(PropertyType.STRING_LIST, "list.path.wrong", "default", "list", "elements");
Property.newListProperty("list.path.wrong", "default", "list", "elements");
// when
List<String> result = property.getFromFile(configuration);
@@ -142,13 +141,4 @@ public class PropertyTypeTest {
assertThat(result, contains("default", "list", "elements"));
}
private static <T> Answer<T> secondParameter() {
return new Answer<T>() {
@Override
public T answer(InvocationOnMock invocation) throws Throwable {
// Return the second parameter -> the default
return (T) invocation.getArguments()[1];
}
};
}
}
@@ -2,13 +2,13 @@ package fr.xephi.authme.settings.properties;
import fr.xephi.authme.ReflectionTestUtils;
import fr.xephi.authme.settings.domain.Property;
import fr.xephi.authme.settings.domain.PropertyType;
import fr.xephi.authme.settings.domain.SettingsClass;
import fr.xephi.authme.settings.propertymap.PropertyMap;
import java.lang.reflect.Field;
import java.util.List;
import static fr.xephi.authme.settings.domain.Property.newListProperty;
import static fr.xephi.authme.settings.domain.Property.newProperty;
/**
@@ -26,7 +26,7 @@ public final class TestConfiguration implements SettingsClass {
newProperty(TestEnum.class, "sample.ratio.order", TestEnum.SECOND);
public static final Property<List<String>> RATIO_FIELDS =
newProperty(PropertyType.STRING_LIST, "sample.ratio.fields", "a", "b", "c");
newListProperty("sample.ratio.fields", "a", "b", "c");
public static final Property<Integer> VERSION_NUMBER =
newProperty("version", 32046);
@@ -35,7 +35,7 @@ public final class TestConfiguration implements SettingsClass {
newProperty("features.boring.skip", false);
public static final Property<List<String>> BORING_COLORS =
newProperty(PropertyType.STRING_LIST, "features.boring.colors");
newListProperty("features.boring.colors");
public static final Property<Integer> DUST_LEVEL =
newProperty("features.boring.dustLevel", -1);
@@ -44,7 +44,7 @@ public final class TestConfiguration implements SettingsClass {
newProperty("features.cool.enabled", false);
public static final Property<List<String>> COOL_OPTIONS =
newProperty(PropertyType.STRING_LIST, "features.cool.options", "Sparks", "Sprinkles");
newListProperty("features.cool.options", "Sparks", "Sprinkles");
private TestConfiguration() {