diff --git a/src/main/java/fr/xephi/authme/AuthMe.java b/src/main/java/fr/xephi/authme/AuthMe.java index 4d7c9461..03a7a6bc 100644 --- a/src/main/java/fr/xephi/authme/AuthMe.java +++ b/src/main/java/fr/xephi/authme/AuthMe.java @@ -224,7 +224,7 @@ public class AuthMe extends JavaPlugin { return; } - messages = new Messages(Settings.messageFile); + messages = new Messages(newSettings.getMessagesFile()); // Connect to the database and setup tables try { diff --git a/src/main/java/fr/xephi/authme/settings/NewSetting.java b/src/main/java/fr/xephi/authme/settings/NewSetting.java index 25a5c640..5f393cb9 100644 --- a/src/main/java/fr/xephi/authme/settings/NewSetting.java +++ b/src/main/java/fr/xephi/authme/settings/NewSetting.java @@ -171,19 +171,7 @@ public class NewSetting { } private String toYaml(Property property, int indent, Yaml simpleYaml, Yaml singleQuoteYaml) { - T value = property.getFromFile(configuration); - String representation = property.hasSingleQuotes() - ? singleQuoteYaml.dump(value) - : simpleYaml.dump(value); - - // If the property is a non-empty list we need to append a new line because it will be - // something like the following, which requires a new line: - // - 'item 1' - // - 'second item in list' - if (property.isList() && !((List) value).isEmpty()) { - representation = "\n" + representation; - } - + String representation = property.toYaml(configuration, simpleYaml, singleQuoteYaml); return join("\n" + indent(indent), representation.split("\\n")); } @@ -197,7 +185,7 @@ public class NewSetting { } private File buildMessagesFileFromCode(String language) { - return new File(pluginFolder.getName(), + return new File(pluginFolder, makePath("messages", "messages_" + language + ".yml")); } 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 c71739a5..d38d0649 100644 --- a/src/main/java/fr/xephi/authme/settings/domain/EnumPropertyType.java +++ b/src/main/java/fr/xephi/authme/settings/domain/EnumPropertyType.java @@ -1,6 +1,7 @@ package fr.xephi.authme.settings.domain; import org.bukkit.configuration.file.FileConfiguration; +import org.yaml.snakeyaml.Yaml; /** * Enum property type. @@ -32,8 +33,8 @@ class EnumPropertyType> extends PropertyType { } @Override - public boolean hasSingleQuotes() { - return true; + public String toYaml(E value, Yaml simpleYaml, Yaml singleQuoteYaml) { + return singleQuoteYaml.dump(value.name()); } private E mapToEnum(String value) { diff --git a/src/main/java/fr/xephi/authme/settings/domain/Property.java b/src/main/java/fr/xephi/authme/settings/domain/Property.java index a67c6a49..f9637a7b 100644 --- a/src/main/java/fr/xephi/authme/settings/domain/Property.java +++ b/src/main/java/fr/xephi/authme/settings/domain/Property.java @@ -1,6 +1,7 @@ package fr.xephi.authme.settings.domain; import org.bukkit.configuration.file.FileConfiguration; +import org.yaml.snakeyaml.Yaml; import java.util.Arrays; import java.util.List; @@ -102,24 +103,15 @@ public class Property { } /** - * Return whether the property should be represented wrapped in single quotes in YAML. - * The YAML format allows both using single quotes and not for all types but for certain - * types one representation makes more sense. Typically we wrap string-like types in - * single quotes and all other ones not. + * Format the property's value as YAML. * - * @return True if single quotes should be used, false otherwise + * @param configuration The file configuration + * @param simpleYaml YAML object (default) + * @param singleQuoteYaml YAML object using single quotes + * @return The generated YAML */ - public boolean hasSingleQuotes() { - return type.hasSingleQuotes(); - } - - /** - * Return whether the property has a list type. - * - * @return True if the property type is a list, false otherwise - */ - public boolean isList() { - return type.isList(); + public String toYaml(FileConfiguration configuration, Yaml simpleYaml, Yaml singleQuoteYaml) { + return type.toYaml(getFromFile(configuration), simpleYaml, singleQuoteYaml); } // ----- 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 4be93415..28a505cf 100644 --- a/src/main/java/fr/xephi/authme/settings/domain/PropertyType.java +++ b/src/main/java/fr/xephi/authme/settings/domain/PropertyType.java @@ -1,6 +1,7 @@ package fr.xephi.authme.settings.domain; import org.bukkit.configuration.file.FileConfiguration; +import org.yaml.snakeyaml.Yaml; import java.util.List; @@ -13,7 +14,6 @@ import java.util.List; public abstract class PropertyType { public static final PropertyType BOOLEAN = new BooleanProperty(); - public static final PropertyType DOUBLE = new DoubleProperty(); public static final PropertyType INTEGER = new IntegerProperty(); public static final PropertyType STRING = new StringProperty(); public static final PropertyType> STRING_LIST = new StringListProperty(); @@ -39,16 +39,15 @@ public abstract class PropertyType { } /** - * Return whether the property type should be wrapped in single quotes in YAML. + * Format the value as YAML. * - * @return True if single quotes should be used, false if not + * @param value The value to export + * @param simpleYaml YAML object (default) + * @param singleQuoteYaml YAML object set to use single quotes + * @return The generated YAML */ - public boolean hasSingleQuotes() { - return false; - } - - public boolean isList() { - return false; + public String toYaml(T value, Yaml simpleYaml, Yaml singleQuoteYaml) { + return simpleYaml.dump(value); } @@ -62,16 +61,6 @@ public abstract class PropertyType { } } - /** - * Double property. - */ - private static final class DoubleProperty extends PropertyType { - @Override - public Double getFromFile(Property property, FileConfiguration configuration) { - return configuration.getDouble(property.getPath(), property.getDefaultValue()); - } - } - /** * Integer property. */ @@ -91,8 +80,8 @@ public abstract class PropertyType { return configuration.getString(property.getPath(), property.getDefaultValue()); } @Override - public boolean hasSingleQuotes() { - return true; + public String toYaml(String value, Yaml simpleYaml, Yaml singleQuoteYaml) { + return singleQuoteYaml.dump(value); } } @@ -114,13 +103,13 @@ public abstract class PropertyType { } @Override - public boolean hasSingleQuotes() { - return true; - } - - @Override - public boolean isList() { - return true; + public String toYaml(List value, Yaml simpleYaml, Yaml singleQuoteYaml) { + String yaml = singleQuoteYaml.dump(value); + // If the property is a non-empty list we need to append a new line because it will be + // something like the following, which requires a new line: + // - 'item 1' + // - 'second item in list' + return value.isEmpty() ? yaml : "\n" + yaml; } } diff --git a/src/test/java/fr/xephi/authme/settings/NewSettingIntegrationTest.java b/src/test/java/fr/xephi/authme/settings/NewSettingIntegrationTest.java index 41b2cf2b..d01902b5 100644 --- a/src/test/java/fr/xephi/authme/settings/NewSettingIntegrationTest.java +++ b/src/test/java/fr/xephi/authme/settings/NewSettingIntegrationTest.java @@ -4,6 +4,7 @@ import com.google.common.collect.ImmutableMap; import fr.xephi.authme.ReflectionTestUtils; import fr.xephi.authme.settings.domain.Property; import fr.xephi.authme.settings.properties.TestConfiguration; +import fr.xephi.authme.settings.properties.TestEnum; import fr.xephi.authme.settings.propertymap.PropertyMap; import fr.xephi.authme.util.WrapperMock; import org.bukkit.configuration.file.YamlConfiguration; @@ -48,12 +49,12 @@ public class NewSettingIntegrationTest { Map, Object> expectedValues = ImmutableMap., Object>builder() .put(TestConfiguration.DURATION_IN_SECONDS, 22) .put(TestConfiguration.SYSTEM_NAME, "Custom sys name") - .put(TestConfiguration.RATIO_LIMIT, -4.1) + .put(TestConfiguration.RATIO_ORDER, TestEnum.FIRST) .put(TestConfiguration.RATIO_FIELDS, Arrays.asList("Australia", "Burundi", "Colombia")) .put(TestConfiguration.VERSION_NUMBER, 2492) .put(TestConfiguration.SKIP_BORING_FEATURES, false) .put(TestConfiguration.BORING_COLORS, Arrays.asList("beige", "gray")) - .put(TestConfiguration.DUST_LEVEL, 0.81) + .put(TestConfiguration.DUST_LEVEL, 2) .put(TestConfiguration.USE_COOL_FEATURES, true) .put(TestConfiguration.COOL_OPTIONS, Arrays.asList("Dinosaurs", "Explosions", "Big trucks")) .build(); @@ -81,12 +82,12 @@ public class NewSettingIntegrationTest { Map, Object> expectedValues = ImmutableMap., Object>builder() .put(TestConfiguration.DURATION_IN_SECONDS, 22) .put(TestConfiguration.SYSTEM_NAME, "[TestDefaultValue]") - .put(TestConfiguration.RATIO_LIMIT, 3.0) + .put(TestConfiguration.RATIO_ORDER, TestEnum.SECOND) .put(TestConfiguration.RATIO_FIELDS, Arrays.asList("Australia", "Burundi", "Colombia")) .put(TestConfiguration.VERSION_NUMBER, 32046) .put(TestConfiguration.SKIP_BORING_FEATURES, false) .put(TestConfiguration.BORING_COLORS, Collections.EMPTY_LIST) - .put(TestConfiguration.DUST_LEVEL, 0.2) + .put(TestConfiguration.DUST_LEVEL, -1) .put(TestConfiguration.USE_COOL_FEATURES, false) .put(TestConfiguration.COOL_OPTIONS, Arrays.asList("Dinosaurs", "Explosions", "Big trucks")) .build(); @@ -129,12 +130,12 @@ public class NewSettingIntegrationTest { Map, Object> expectedValues = ImmutableMap., Object>builder() .put(TestConfiguration.DURATION_IN_SECONDS, 20) .put(TestConfiguration.SYSTEM_NAME, "A 'test' name") - .put(TestConfiguration.RATIO_LIMIT, -41.8) + .put(TestConfiguration.RATIO_ORDER, TestEnum.FOURTH) .put(TestConfiguration.RATIO_FIELDS, Arrays.asList("Australia\\", "\tBurundi'", "Colombia?\n''")) .put(TestConfiguration.VERSION_NUMBER, -1337) .put(TestConfiguration.SKIP_BORING_FEATURES, false) .put(TestConfiguration.BORING_COLORS, Arrays.asList("it's a difficult string!", "gray\nwith new lines\n")) - .put(TestConfiguration.DUST_LEVEL, 0.2) + .put(TestConfiguration.DUST_LEVEL, -1) .put(TestConfiguration.USE_COOL_FEATURES, true) .put(TestConfiguration.COOL_OPTIONS, Collections.EMPTY_LIST) .put(additionalProperties.get(0), additionalProperties.get(0).getDefaultValue()) diff --git a/src/test/java/fr/xephi/authme/settings/NewSettingTest.java b/src/test/java/fr/xephi/authme/settings/NewSettingTest.java index 232401bf..8ae65128 100644 --- a/src/test/java/fr/xephi/authme/settings/NewSettingTest.java +++ b/src/test/java/fr/xephi/authme/settings/NewSettingTest.java @@ -2,6 +2,7 @@ package fr.xephi.authme.settings; import fr.xephi.authme.settings.domain.Property; import fr.xephi.authme.settings.properties.TestConfiguration; +import fr.xephi.authme.settings.properties.TestEnum; import fr.xephi.authme.settings.propertymap.PropertyMap; import org.bukkit.configuration.file.YamlConfiguration; import org.junit.Test; @@ -37,7 +38,7 @@ public class NewSettingTest { setReturnValue(file, TestConfiguration.VERSION_NUMBER, 20); setReturnValue(file, TestConfiguration.SKIP_BORING_FEATURES, true); - setReturnValue(file, TestConfiguration.RATIO_LIMIT, 4.25); + setReturnValue(file, TestConfiguration.RATIO_ORDER, TestEnum.THIRD); setReturnValue(file, TestConfiguration.SYSTEM_NAME, "myTestSys"); // when / then @@ -45,7 +46,7 @@ public class NewSettingTest { assertThat(settings.getProperty(TestConfiguration.VERSION_NUMBER), equalTo(20)); assertThat(settings.getProperty(TestConfiguration.SKIP_BORING_FEATURES), equalTo(true)); - assertThat(settings.getProperty(TestConfiguration.RATIO_LIMIT), equalTo(4.25)); + assertThat(settings.getProperty(TestConfiguration.RATIO_ORDER), equalTo(TestEnum.THIRD)); assertThat(settings.getProperty(TestConfiguration.SYSTEM_NAME), equalTo("myTestSys")); assertDefaultValue(TestConfiguration.DURATION_IN_SECONDS, settings); @@ -60,8 +61,8 @@ public class NewSettingTest { when(config.getInt(eq(property.getPath()), anyInt())).thenReturn((Integer) value); } else if (value instanceof Boolean) { when(config.getBoolean(eq(property.getPath()), anyBoolean())).thenReturn((Boolean) value); - } else if (value instanceof Double) { - when(config.getDouble(eq(property.getPath()), anyDouble())).thenReturn((Double) value); + } else if (value instanceof Enum) { + when(config.getString(property.getPath())).thenReturn(((Enum) value).name()); } else { throw new UnsupportedOperationException("Value has unsupported type '" + (value == null ? "null" : value.getClass().getSimpleName()) + "'"); diff --git a/src/test/java/fr/xephi/authme/settings/domain/PropertyTypeTest.java b/src/test/java/fr/xephi/authme/settings/domain/PropertyTypeTest.java index 6dce2ad3..df012063 100644 --- a/src/test/java/fr/xephi/authme/settings/domain/PropertyTypeTest.java +++ b/src/test/java/fr/xephi/authme/settings/domain/PropertyTypeTest.java @@ -13,7 +13,6 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.equalTo; import static org.mockito.Matchers.anyBoolean; -import static org.mockito.Matchers.anyDouble; import static org.mockito.Matchers.anyInt; import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.eq; @@ -33,8 +32,6 @@ public class PropertyTypeTest { when(configuration.getBoolean(eq("bool.path.test"), anyBoolean())).thenReturn(true); when(configuration.getBoolean(eq("bool.path.wrong"), anyBoolean())).thenAnswer(secondParameter()); - when(configuration.getDouble(eq("double.path.test"), anyDouble())).thenReturn(-6.4); - when(configuration.getDouble(eq("double.path.wrong"), anyDouble())).thenAnswer(secondParameter()); when(configuration.getInt(eq("int.path.test"), anyInt())).thenReturn(27); when(configuration.getInt(eq("int.path.wrong"), anyInt())).thenAnswer(secondParameter()); when(configuration.getString(eq("str.path.test"), anyString())).thenReturn("Test value"); @@ -69,31 +66,6 @@ public class PropertyTypeTest { assertThat(result, equalTo(true)); } - /* Double */ - @Test - public void shouldGetDoubleValue() { - // given - Property property = Property.newProperty(PropertyType.DOUBLE, "double.path.test", 3.8); - - // when - double result = property.getFromFile(configuration); - - // then - assertThat(result, equalTo(-6.4)); - } - - @Test - public void shouldGetDoubleDefault() { - // given - Property property = Property.newProperty(PropertyType.DOUBLE, "double.path.wrong", 12.0); - - // when - double result = property.getFromFile(configuration); - - // then - assertThat(result, equalTo(12.0)); - } - /* Integer */ @Test public void shouldGetIntValue() { diff --git a/src/test/java/fr/xephi/authme/settings/properties/TestConfiguration.java b/src/test/java/fr/xephi/authme/settings/properties/TestConfiguration.java index 449627f2..dc02e9cc 100644 --- a/src/test/java/fr/xephi/authme/settings/properties/TestConfiguration.java +++ b/src/test/java/fr/xephi/authme/settings/properties/TestConfiguration.java @@ -19,8 +19,8 @@ public final class TestConfiguration implements SettingsClass { public static final Property SYSTEM_NAME = newProperty("test.systemName", "[TestDefaultValue]"); - public static final Property RATIO_LIMIT = - newProperty(PropertyType.DOUBLE, "sample.ratio.limit", 3.0); + public static final Property RATIO_ORDER = + newProperty(TestEnum.class, "sample.ratio.order", TestEnum.SECOND); public static final Property> RATIO_FIELDS = newProperty(PropertyType.STRING_LIST, "sample.ratio.fields", "a", "b", "c"); @@ -34,8 +34,8 @@ public final class TestConfiguration implements SettingsClass { public static final Property> BORING_COLORS = newProperty(PropertyType.STRING_LIST, "features.boring.colors"); - public static final Property DUST_LEVEL = - newProperty(PropertyType.DOUBLE, "features.boring.dustLevel", 0.2); + public static final Property DUST_LEVEL = + newProperty(PropertyType.INTEGER, "features.boring.dustLevel", -1); public static final Property USE_COOL_FEATURES = newProperty("features.cool.enabled", false); diff --git a/src/test/java/fr/xephi/authme/settings/properties/TestEnum.java b/src/test/java/fr/xephi/authme/settings/properties/TestEnum.java new file mode 100644 index 00000000..e02d6825 --- /dev/null +++ b/src/test/java/fr/xephi/authme/settings/properties/TestEnum.java @@ -0,0 +1,16 @@ +package fr.xephi.authme.settings.properties; + +/** + * Test enum used in {@link TestConfiguration}. + */ +public enum TestEnum { + + FIRST, + + SECOND, + + THIRD, + + FOURTH + +} diff --git a/src/test/resources/config-difficult-values.yml b/src/test/resources/config-difficult-values.yml index 157971bc..29bc0e37 100644 --- a/src/test/resources/config-difficult-values.yml +++ b/src/test/resources/config-difficult-values.yml @@ -5,7 +5,7 @@ test: systemName: 'A ''test'' name' sample: ratio: - limit: -41.8 + order: Fourth fields: - Australia\ - ' Burundi''' @@ -23,7 +23,7 @@ features: - | gray with new lines - # dustLevel: 0.81 <-- missing property triggering rewrite + # dustLevel: 8 <-- missing property triggering rewrite cool: enabled: yes options: [] diff --git a/src/test/resources/config-incomplete-sample.yml b/src/test/resources/config-incomplete-sample.yml index a2987972..65990e84 100644 --- a/src/test/resources/config-incomplete-sample.yml +++ b/src/test/resources/config-incomplete-sample.yml @@ -6,7 +6,7 @@ test: # systemName: 'Custom sys name' sample: ratio: -# limit: 3.0 +# order: 'THIRD' fields: - 'Australia' - 'Burundi' @@ -18,7 +18,7 @@ features: # colors: # - 'beige' # - 'gray' -# dustLevel: 0.81 +# dustLevel: 1 cool: # enabled: true options: diff --git a/src/test/resources/config-sample-values.yml b/src/test/resources/config-sample-values.yml index 1bd99d77..72cdeb79 100644 --- a/src/test/resources/config-sample-values.yml +++ b/src/test/resources/config-sample-values.yml @@ -6,7 +6,7 @@ test: systemName: 'Custom sys name' sample: ratio: - limit: -4.1 + order: 'first' fields: - 'Australia' - 'Burundi' @@ -18,7 +18,7 @@ features: colors: - 'beige' - 'gray' - dustLevel: 0.81 + dustLevel: 2 cool: enabled: true options: