From aef18a894ac4ec04bb90ccd7302cce89c5bb6266 Mon Sep 17 00:00:00 2001 From: ljacqu Date: Wed, 16 Mar 2016 19:07:00 +0100 Subject: [PATCH] Move PropertyType functionality into Property class --- .../authme/settings/domain/EnumProperty.java | 49 ++++++ .../settings/domain/EnumPropertyType.java | 48 ------ .../authme/settings/domain/Property.java | 142 ++++++++++++------ .../authme/settings/domain/PropertyType.java | 116 -------------- .../properties/ConverterSettings.java | 10 +- .../settings/properties/EmailSettings.java | 31 ++-- .../settings/properties/HooksSettings.java | 4 +- .../properties/ProtectionSettings.java | 16 +- .../properties/RegistrationSettings.java | 10 +- .../properties/RestrictionSettings.java | 10 +- .../settings/properties/SecuritySettings.java | 5 +- ...rtyTypeTest.java => EnumPropertyTest.java} | 22 +-- ...ropertyTypeTest.java => PropertyTest.java} | 26 +--- .../properties/TestConfiguration.java | 8 +- 14 files changed, 207 insertions(+), 290 deletions(-) create mode 100644 src/main/java/fr/xephi/authme/settings/domain/EnumProperty.java delete mode 100644 src/main/java/fr/xephi/authme/settings/domain/EnumPropertyType.java delete mode 100644 src/main/java/fr/xephi/authme/settings/domain/PropertyType.java rename src/test/java/fr/xephi/authme/settings/domain/{EnumPropertyTypeTest.java => EnumPropertyTest.java} (75%) rename src/test/java/fr/xephi/authme/settings/domain/{PropertyTypeTest.java => PropertyTest.java} (82%) diff --git a/src/main/java/fr/xephi/authme/settings/domain/EnumProperty.java b/src/main/java/fr/xephi/authme/settings/domain/EnumProperty.java new file mode 100644 index 00000000..14d09329 --- /dev/null +++ b/src/main/java/fr/xephi/authme/settings/domain/EnumProperty.java @@ -0,0 +1,49 @@ +package fr.xephi.authme.settings.domain; + +import org.bukkit.configuration.file.FileConfiguration; +import org.yaml.snakeyaml.Yaml; + +/** + * Enum property. + * + * @param The enum class + */ +class EnumProperty> extends Property { + + private Class clazz; + + public EnumProperty(Class clazz, String path, E defaultValue) { + super(path, defaultValue); + this.clazz = clazz; + } + + @Override + public E getFromFile(FileConfiguration configuration) { + String textValue = configuration.getString(getPath()); + if (textValue == null) { + return getDefaultValue(); + } + E mappedValue = mapToEnum(textValue); + return mappedValue != null ? mappedValue : getDefaultValue(); + } + + @Override + public boolean isPresent(FileConfiguration configuration) { + return super.isPresent(configuration) && mapToEnum(configuration.getString(getPath())) != null; + } + + @Override + public String toYaml(FileConfiguration configuration, Yaml simpleYaml, Yaml singleQuoteYaml) { + E value = getFromFile(configuration); + return singleQuoteYaml.dump(value.name()); + } + + private E mapToEnum(String value) { + for (E entry : clazz.getEnumConstants()) { + if (entry.name().equalsIgnoreCase(value)) { + return entry; + } + } + return null; + } +} diff --git a/src/main/java/fr/xephi/authme/settings/domain/EnumPropertyType.java b/src/main/java/fr/xephi/authme/settings/domain/EnumPropertyType.java deleted file mode 100644 index d38d0649..00000000 --- a/src/main/java/fr/xephi/authme/settings/domain/EnumPropertyType.java +++ /dev/null @@ -1,48 +0,0 @@ -package fr.xephi.authme.settings.domain; - -import org.bukkit.configuration.file.FileConfiguration; -import org.yaml.snakeyaml.Yaml; - -/** - * Enum property type. - * - * @param The enum class - */ -class EnumPropertyType> extends PropertyType { - - private Class clazz; - - public EnumPropertyType(Class clazz) { - this.clazz = clazz; - } - - @Override - public E getFromFile(Property property, FileConfiguration configuration) { - String textValue = configuration.getString(property.getPath()); - if (textValue == null) { - return property.getDefaultValue(); - } - E mappedValue = mapToEnum(textValue); - return mappedValue != null ? mappedValue : property.getDefaultValue(); - } - - @Override - public boolean contains(Property property, FileConfiguration configuration) { - return super.contains(property, configuration) - && mapToEnum(configuration.getString(property.getPath())) != null; - } - - @Override - public String toYaml(E value, Yaml simpleYaml, Yaml singleQuoteYaml) { - return singleQuoteYaml.dump(value.name()); - } - - private E mapToEnum(String value) { - for (E entry : clazz.getEnumConstants()) { - if (entry.name().equalsIgnoreCase(value)) { - return entry; - } - } - return null; - } -} 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 f9637a7b..1d5100af 100644 --- a/src/main/java/fr/xephi/authme/settings/domain/Property.java +++ b/src/main/java/fr/xephi/authme/settings/domain/Property.java @@ -10,45 +10,27 @@ import java.util.Objects; /** * Property class, representing a setting that is read from the config.yml file. */ -public class Property { +public abstract class Property { - private final PropertyType type; private final String path; private final T defaultValue; - private Property(PropertyType type, String path, T defaultValue) { + protected Property(String path, T defaultValue) { Objects.requireNonNull(defaultValue); - this.type = type; this.path = path; this.defaultValue = defaultValue; } /** - * Create a new property. See also {@link #newProperty(PropertyType, String, Object[])} for lists and - * {@link #newProperty(Class, String, Enum)}. + * Create a new string list property. * - * @param type The property type * @param path The property's path - * @param defaultValue The default value - * @param The type of the property - * @return The created property - */ - public static Property newProperty(PropertyType type, String path, T defaultValue) { - return new Property<>(type, path, defaultValue); - } - - /** - * Create a new list property. - * - * @param type The list type of the property - * @param path The property's path - * @param defaultValues The default value's items - * @param The list type + * @param defaultValues The items in the default list * @return The created list property */ - @SafeVarargs - public static Property> newProperty(PropertyType> type, String path, U... defaultValues) { - return new Property<>(type, path, Arrays.asList(defaultValues)); + public static Property> newListProperty(String path, String... defaultValues) { + // does not have the same name as not to clash with #newProperty(String, String) + return new StringListProperty(path, defaultValues); } /** @@ -61,36 +43,28 @@ public class Property { * @return The created enum property */ public static > Property newProperty(Class clazz, String path, E defaultValue) { - return new Property<>(new EnumPropertyType<>(clazz), path, defaultValue); + return new EnumProperty<>(clazz, path, defaultValue); } - // ----- - // Overloaded convenience methods for specific types - // ----- public static Property newProperty(String path, boolean defaultValue) { - return new Property<>(PropertyType.BOOLEAN, path, defaultValue); + return new BooleanProperty(path, defaultValue); } public static Property newProperty(String path, int defaultValue) { - return new Property<>(PropertyType.INTEGER, path, defaultValue); + return new IntegerProperty(path, defaultValue); } public static Property newProperty(String path, String defaultValue) { - return new Property<>(PropertyType.STRING, path, defaultValue); + return new StringProperty(path, defaultValue); } - // ----- - // Hooks to the PropertyType methods - // ----- /** * Get the property value from the given configuration – guaranteed to never return null. * * @param configuration The configuration to read the value from * @return The value, or default if not present */ - public T getFromFile(FileConfiguration configuration) { - return type.getFromFile(this, configuration); - } + public abstract T getFromFile(FileConfiguration configuration); /** * Return whether or not the given configuration file contains the property. @@ -99,7 +73,7 @@ public class Property { * @return True if the property is present, false otherwise */ public boolean isPresent(FileConfiguration configuration) { - return type.contains(this, configuration); + return configuration.contains(path); } /** @@ -111,12 +85,9 @@ public class Property { * @return The generated YAML */ public String toYaml(FileConfiguration configuration, Yaml simpleYaml, Yaml singleQuoteYaml) { - return type.toYaml(getFromFile(configuration), simpleYaml, singleQuoteYaml); + return simpleYaml.dump(getFromFile(configuration)); } - // ----- - // Trivial getters - // ----- /** * Return the default value of the property. * @@ -140,4 +111,89 @@ public class Property { return "Property '" + path + "'"; } + + /** + * Boolean property. + */ + private static final class BooleanProperty extends Property { + + public BooleanProperty(String path, Boolean defaultValue) { + super(path, defaultValue); + } + + @Override + public Boolean getFromFile(FileConfiguration configuration) { + return configuration.getBoolean(getPath(), getDefaultValue()); + } + } + + /** + * Integer property. + */ + private static final class IntegerProperty extends Property { + + public IntegerProperty(String path, Integer defaultValue) { + super(path, defaultValue); + } + + @Override + public Integer getFromFile(FileConfiguration configuration) { + return configuration.getInt(getPath(), getDefaultValue()); + } + } + + /** + * String property. + */ + private static final class StringProperty extends Property { + + public StringProperty(String path, String defaultValue) { + super(path, defaultValue); + } + + @Override + public String getFromFile(FileConfiguration configuration) { + return configuration.getString(getPath(), getDefaultValue()); + } + + @Override + public String toYaml(FileConfiguration configuration, Yaml simpleYaml, Yaml singleQuoteYaml) { + return singleQuoteYaml.dump(getFromFile(configuration)); + } + } + + /** + * String list property. + */ + private static final class StringListProperty extends Property> { + + public StringListProperty(String path, String[] defaultValues) { + super(path, Arrays.asList(defaultValues)); + } + + @Override + public List getFromFile(FileConfiguration configuration) { + if (!configuration.isList(getPath())) { + return getDefaultValue(); + } + return configuration.getStringList(getPath()); + } + + @Override + public boolean isPresent(FileConfiguration configuration) { + return configuration.isList(getPath()); + } + + @Override + public String toYaml(FileConfiguration configuration, Yaml simpleYaml, Yaml singleQuoteYaml) { + List value = getFromFile(configuration); + 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/main/java/fr/xephi/authme/settings/domain/PropertyType.java b/src/main/java/fr/xephi/authme/settings/domain/PropertyType.java deleted file mode 100644 index 28a505cf..00000000 --- a/src/main/java/fr/xephi/authme/settings/domain/PropertyType.java +++ /dev/null @@ -1,116 +0,0 @@ -package fr.xephi.authme.settings.domain; - -import org.bukkit.configuration.file.FileConfiguration; -import org.yaml.snakeyaml.Yaml; - -import java.util.List; - -/** - * Handles a certain property type and provides type-specific functionality. - * - * @param The value of the property - * @see Property - */ -public abstract class PropertyType { - - public static final PropertyType BOOLEAN = new BooleanProperty(); - public static final PropertyType INTEGER = new IntegerProperty(); - public static final PropertyType STRING = new StringProperty(); - public static final PropertyType> STRING_LIST = new StringListProperty(); - - /** - * Get the property's value from the given YAML configuration. - * - * @param property The property to retrieve - * @param configuration The YAML configuration to read from - * @return The read value, or the default value if absent - */ - public abstract T getFromFile(Property property, FileConfiguration configuration); - - /** - * Return whether the property is present in the given configuration. - * - * @param property The property to search for - * @param configuration The configuration to verify - * @return True if the property is present, false otherwise - */ - public boolean contains(Property property, FileConfiguration configuration) { - return configuration.contains(property.getPath()); - } - - /** - * Format the value as YAML. - * - * @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 String toYaml(T value, Yaml simpleYaml, Yaml singleQuoteYaml) { - return simpleYaml.dump(value); - } - - - /** - * Boolean property. - */ - private static final class BooleanProperty extends PropertyType { - @Override - public Boolean getFromFile(Property property, FileConfiguration configuration) { - return configuration.getBoolean(property.getPath(), property.getDefaultValue()); - } - } - - /** - * Integer property. - */ - private static final class IntegerProperty extends PropertyType { - @Override - public Integer getFromFile(Property property, FileConfiguration configuration) { - return configuration.getInt(property.getPath(), property.getDefaultValue()); - } - } - - /** - * String property. - */ - private static final class StringProperty extends PropertyType { - @Override - public String getFromFile(Property property, FileConfiguration configuration) { - return configuration.getString(property.getPath(), property.getDefaultValue()); - } - @Override - public String toYaml(String value, Yaml simpleYaml, Yaml singleQuoteYaml) { - return singleQuoteYaml.dump(value); - } - } - - /** - * String list property. - */ - private static final class StringListProperty extends PropertyType> { - @Override - public List getFromFile(Property> property, FileConfiguration configuration) { - if (!configuration.isList(property.getPath())) { - return property.getDefaultValue(); - } - return configuration.getStringList(property.getPath()); - } - - @Override - public boolean contains(Property> property, FileConfiguration configuration) { - return configuration.contains(property.getPath()) && configuration.isList(property.getPath()); - } - - @Override - 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/main/java/fr/xephi/authme/settings/properties/ConverterSettings.java b/src/main/java/fr/xephi/authme/settings/properties/ConverterSettings.java index 9dc4ad60..35e9cef9 100644 --- a/src/main/java/fr/xephi/authme/settings/properties/ConverterSettings.java +++ b/src/main/java/fr/xephi/authme/settings/properties/ConverterSettings.java @@ -5,26 +5,24 @@ import fr.xephi.authme.settings.domain.Property; import fr.xephi.authme.settings.domain.SettingsClass; import static fr.xephi.authme.settings.domain.Property.newProperty; -import static fr.xephi.authme.settings.domain.PropertyType.BOOLEAN; -import static fr.xephi.authme.settings.domain.PropertyType.STRING; public class ConverterSettings implements SettingsClass { @Comment("Rakamak file name") public static final Property RAKAMAK_FILE_NAME = - newProperty(STRING, "Converter.Rakamak.fileName", "users.rak"); + newProperty("Converter.Rakamak.fileName", "users.rak"); @Comment("Rakamak use IP?") public static final Property RAKAMAK_USE_IP = - newProperty(BOOLEAN, "Converter.Rakamak.useIP", false); + newProperty("Converter.Rakamak.useIP", false); @Comment("Rakamak IP file name") public static final Property RAKAMAK_IP_FILE_NAME = - newProperty(STRING, "Converter.Rakamak.ipFileName", "UsersIp.rak"); + newProperty("Converter.Rakamak.ipFileName", "UsersIp.rak"); @Comment("CrazyLogin database file name") public static final Property CRAZYLOGIN_FILE_NAME = - newProperty(STRING, "Converter.CrazyLogin.fileName", "accounts.db"); + newProperty("Converter.CrazyLogin.fileName", "accounts.db"); private ConverterSettings() { } diff --git a/src/main/java/fr/xephi/authme/settings/properties/EmailSettings.java b/src/main/java/fr/xephi/authme/settings/properties/EmailSettings.java index 7b7ee3ce..c6b6959d 100644 --- a/src/main/java/fr/xephi/authme/settings/properties/EmailSettings.java +++ b/src/main/java/fr/xephi/authme/settings/properties/EmailSettings.java @@ -6,29 +6,26 @@ import fr.xephi.authme.settings.domain.SettingsClass; import java.util.List; +import static fr.xephi.authme.settings.domain.Property.newListProperty; import static fr.xephi.authme.settings.domain.Property.newProperty; -import static fr.xephi.authme.settings.domain.PropertyType.BOOLEAN; -import static fr.xephi.authme.settings.domain.PropertyType.INTEGER; -import static fr.xephi.authme.settings.domain.PropertyType.STRING; -import static fr.xephi.authme.settings.domain.PropertyType.STRING_LIST; public class EmailSettings implements SettingsClass { @Comment("Email SMTP server host") public static final Property SMTP_HOST = - newProperty(STRING, "Email.mailSMTP", "smtp.gmail.com"); + newProperty("Email.mailSMTP", "smtp.gmail.com"); @Comment("Email SMTP server port") public static final Property SMTP_PORT = - newProperty(INTEGER, "Email.mailPort", 465); + newProperty("Email.mailPort", 465); @Comment("Email account which sends the mails") public static final Property MAIL_ACCOUNT = - newProperty(STRING, "Email.mailAccount", ""); + newProperty("Email.mailAccount", ""); @Comment("Email account password") public static final Property MAIL_PASSWORD = - newProperty(STRING, "Email.mailPassword", ""); + newProperty("Email.mailPassword", ""); @Comment("Custom sender name, replacing the mailAccount name in the email") public static final Property MAIL_SENDER_NAME = @@ -36,39 +33,39 @@ public class EmailSettings implements SettingsClass { @Comment("Recovery password length") public static final Property RECOVERY_PASSWORD_LENGTH = - newProperty(INTEGER, "Email.RecoveryPasswordLength", 8); + newProperty("Email.RecoveryPasswordLength", 8); @Comment("Mail Subject") public static final Property RECOVERY_MAIL_SUBJECT = - newProperty(STRING, "Email.mailSubject", "Your new AuthMe password"); + newProperty("Email.mailSubject", "Your new AuthMe password"); @Comment("Like maxRegPerIP but with email") public static final Property MAX_REG_PER_EMAIL = - newProperty(INTEGER, "Email.maxRegPerEmail", 1); + newProperty("Email.maxRegPerEmail", 1); @Comment("Recall players to add an email?") public static final Property RECALL_PLAYERS = - newProperty(BOOLEAN, "Email.recallPlayers", false); + newProperty("Email.recallPlayers", false); @Comment("Delay in minute for the recall scheduler") public static final Property DELAY_RECALL = - newProperty(INTEGER, "Email.delayRecall", 5); + newProperty("Email.delayRecall", 5); @Comment("Blacklist these domains for emails") public static final Property> DOMAIN_BLACKLIST = - newProperty(STRING_LIST, "Email.emailBlacklisted", "10minutemail.com"); + newListProperty("Email.emailBlacklisted", "10minutemail.com"); @Comment("Whitelist ONLY these domains for emails") public static final Property> DOMAIN_WHITELIST = - newProperty(STRING_LIST, "Email.emailWhitelisted"); + newListProperty("Email.emailWhitelisted"); @Comment("Send the new password drawn in an image?") public static final Property PASSWORD_AS_IMAGE = - newProperty(BOOLEAN, "Email.generateImage", false); + newProperty("Email.generateImage", false); @Comment("The OAuth2 token") public static final Property OAUTH2_TOKEN = - newProperty(STRING, "Email.emailOauth2Token", ""); + newProperty("Email.emailOauth2Token", ""); private EmailSettings() { } diff --git a/src/main/java/fr/xephi/authme/settings/properties/HooksSettings.java b/src/main/java/fr/xephi/authme/settings/properties/HooksSettings.java index b20d2868..95d91f85 100644 --- a/src/main/java/fr/xephi/authme/settings/properties/HooksSettings.java +++ b/src/main/java/fr/xephi/authme/settings/properties/HooksSettings.java @@ -2,11 +2,11 @@ package fr.xephi.authme.settings.properties; import fr.xephi.authme.settings.domain.Comment; import fr.xephi.authme.settings.domain.Property; -import fr.xephi.authme.settings.domain.PropertyType; import fr.xephi.authme.settings.domain.SettingsClass; import java.util.List; +import static fr.xephi.authme.settings.domain.Property.newListProperty; import static fr.xephi.authme.settings.domain.Property.newProperty; public class HooksSettings implements SettingsClass { @@ -48,7 +48,7 @@ public class HooksSettings implements SettingsClass { @Comment("Other MySQL columns where we need to put the username (case-sensitive)") public static final Property> MYSQL_OTHER_USERNAME_COLS = - newProperty(PropertyType.STRING_LIST, "ExternalBoardOptions.mySQLOtherUsernameColumns"); + newListProperty("ExternalBoardOptions.mySQLOtherUsernameColumns"); @Comment("How much log2 rounds needed in BCrypt (do not change if you do not know what it does)") public static final Property BCRYPT_LOG2_ROUND = diff --git a/src/main/java/fr/xephi/authme/settings/properties/ProtectionSettings.java b/src/main/java/fr/xephi/authme/settings/properties/ProtectionSettings.java index f5a51215..d4b5f00f 100644 --- a/src/main/java/fr/xephi/authme/settings/properties/ProtectionSettings.java +++ b/src/main/java/fr/xephi/authme/settings/properties/ProtectionSettings.java @@ -6,39 +6,37 @@ import fr.xephi.authme.settings.domain.SettingsClass; import java.util.List; +import static fr.xephi.authme.settings.domain.Property.newListProperty; import static fr.xephi.authme.settings.domain.Property.newProperty; -import static fr.xephi.authme.settings.domain.PropertyType.BOOLEAN; -import static fr.xephi.authme.settings.domain.PropertyType.INTEGER; -import static fr.xephi.authme.settings.domain.PropertyType.STRING_LIST; public class ProtectionSettings implements SettingsClass { @Comment("Enable some servers protection (country based login, antibot)") public static final Property ENABLE_PROTECTION = - newProperty(BOOLEAN, "Protection.enableProtection", false); + newProperty("Protection.enableProtection", false); @Comment({"Countries allowed to join the server and register, see http://dev.bukkit.org/bukkit-plugins/authme-reloaded/pages/countries-codes/ for countries' codes", "PLEASE USE QUOTES!"}) public static final Property> COUNTRIES_WHITELIST = - newProperty(STRING_LIST, "Protection.countries", "US", "GB", "A1"); + newListProperty("Protection.countries", "US", "GB", "A1"); @Comment({"Countries not allowed to join the server and register", "PLEASE USE QUOTES!"}) public static final Property> COUNTRIES_BLACKLIST = - newProperty(STRING_LIST, "Protection.countriesBlacklist"); + newListProperty("Protection.countriesBlacklist"); @Comment("Do we need to enable automatic antibot system?") public static final Property ENABLE_ANTIBOT = - newProperty(BOOLEAN, "Protection.enableAntiBot", false); + newProperty("Protection.enableAntiBot", false); @Comment("Max number of player allowed to login in 5 secs before enable AntiBot system automatically") public static final Property ANTIBOT_SENSIBILITY = - newProperty(INTEGER, "Protection.antiBotSensibility", 5); + newProperty("Protection.antiBotSensibility", 5); @Comment("Duration in minutes of the antibot automatic system") public static final Property ANTIBOT_DURATION = - newProperty(INTEGER, "Protection.antiBotDuration", 10); + newProperty("Protection.antiBotDuration", 10); private ProtectionSettings() { } diff --git a/src/main/java/fr/xephi/authme/settings/properties/RegistrationSettings.java b/src/main/java/fr/xephi/authme/settings/properties/RegistrationSettings.java index 14ef825e..c2c2bbe3 100644 --- a/src/main/java/fr/xephi/authme/settings/properties/RegistrationSettings.java +++ b/src/main/java/fr/xephi/authme/settings/properties/RegistrationSettings.java @@ -2,11 +2,11 @@ package fr.xephi.authme.settings.properties; import fr.xephi.authme.settings.domain.Comment; import fr.xephi.authme.settings.domain.Property; -import fr.xephi.authme.settings.domain.PropertyType; import fr.xephi.authme.settings.domain.SettingsClass; import java.util.List; +import static fr.xephi.authme.settings.domain.Property.newListProperty; import static fr.xephi.authme.settings.domain.Property.newProperty; public class RegistrationSettings implements SettingsClass { @@ -50,21 +50,21 @@ public class RegistrationSettings implements SettingsClass { @Comment("Force these commands after /login, without any '/', use %p to replace with player name") public static final Property> FORCE_COMMANDS = - newProperty(PropertyType.STRING_LIST, "settings.forceCommands"); + newListProperty("settings.forceCommands"); @Comment("Force these commands after /login as service console, without any '/'. " + "Use %p to replace with player name") public static final Property> FORCE_COMMANDS_AS_CONSOLE = - newProperty(PropertyType.STRING_LIST, "settings.forceCommandsAsConsole"); + newListProperty("settings.forceCommandsAsConsole"); @Comment("Force these commands after /register, without any '/', use %p to replace with player name") public static final Property> FORCE_REGISTER_COMMANDS = - newProperty(PropertyType.STRING_LIST, "settings.forceRegisterCommands"); + newListProperty("settings.forceRegisterCommands"); @Comment("Force these commands after /register as a server console, without any '/'. " + "Use %p to replace with player name") public static final Property> FORCE_REGISTER_COMMANDS_AS_CONSOLE = - newProperty(PropertyType.STRING_LIST, "settings.forceRegisterCommandsAsConsole"); + newListProperty("settings.forceRegisterCommandsAsConsole"); @Comment({ "Enable to display the welcome message (welcome.txt) after a registration or a login", diff --git a/src/main/java/fr/xephi/authme/settings/properties/RestrictionSettings.java b/src/main/java/fr/xephi/authme/settings/properties/RestrictionSettings.java index dd26b9e7..c2b2cb68 100644 --- a/src/main/java/fr/xephi/authme/settings/properties/RestrictionSettings.java +++ b/src/main/java/fr/xephi/authme/settings/properties/RestrictionSettings.java @@ -2,11 +2,11 @@ package fr.xephi.authme.settings.properties; import fr.xephi.authme.settings.domain.Comment; import fr.xephi.authme.settings.domain.Property; -import fr.xephi.authme.settings.domain.PropertyType; import fr.xephi.authme.settings.domain.SettingsClass; import java.util.List; +import static fr.xephi.authme.settings.domain.Property.newListProperty; import static fr.xephi.authme.settings.domain.Property.newProperty; public class RestrictionSettings implements SettingsClass { @@ -26,7 +26,7 @@ public class RestrictionSettings implements SettingsClass { @Comment("Allowed commands for unauthenticated players") public static final Property> ALLOW_COMMANDS = - newProperty(PropertyType.STRING_LIST, "settings.restrictions.allowCommands", + newListProperty("settings.restrictions.allowCommands", "login", "register", "l", "reg", "email", "captcha"); @Comment("Max number of allowed registrations per IP") @@ -75,7 +75,7 @@ public class RestrictionSettings implements SettingsClass { " AllowedRestrictedUser:", " - playername;127.0.0.1"}) public static final Property> ALLOWED_RESTRICTED_USERS = - newProperty(PropertyType.STRING_LIST, "settings.restrictions.AllowedRestrictedUser"); + newListProperty("settings.restrictions.AllowedRestrictedUser"); @Comment("Should unregistered players be kicked immediately?") public static final Property KICK_NON_REGISTERED = @@ -148,7 +148,7 @@ public class RestrictionSettings implements SettingsClass { "WorldNames where we need to force the spawn location for ForceSpawnLocOnJoinEnabled", "Case-sensitive!"}) public static final Property> FORCE_SPAWN_ON_WORLDS = - newProperty(PropertyType.STRING_LIST, "settings.restrictions.ForceSpawnOnTheseWorlds", + newListProperty("settings.restrictions.ForceSpawnOnTheseWorlds", "world", "world_nether", "world_the_end"); @Comment("Ban ip when the ip is not the ip registered in database") @@ -189,7 +189,7 @@ public class RestrictionSettings implements SettingsClass { "It is case-sensitive!" }) public static final Property> UNRESTRICTED_NAMES = - newProperty(PropertyType.STRING_LIST, "settings.unrestrictions.UnrestrictedName"); + newListProperty("settings.unrestrictions.UnrestrictedName"); private RestrictionSettings() { diff --git a/src/main/java/fr/xephi/authme/settings/properties/SecuritySettings.java b/src/main/java/fr/xephi/authme/settings/properties/SecuritySettings.java index 6a2ace71..9a52ca82 100644 --- a/src/main/java/fr/xephi/authme/settings/properties/SecuritySettings.java +++ b/src/main/java/fr/xephi/authme/settings/properties/SecuritySettings.java @@ -7,8 +7,8 @@ import fr.xephi.authme.settings.domain.SettingsClass; import java.util.List; +import static fr.xephi.authme.settings.domain.Property.newListProperty; import static fr.xephi.authme.settings.domain.Property.newProperty; -import static fr.xephi.authme.settings.domain.PropertyType.STRING_LIST; public class SecuritySettings implements SettingsClass { @@ -98,8 +98,7 @@ public class SecuritySettings implements SettingsClass { "- '123456'", "- 'password'"}) public static final Property> UNSAFE_PASSWORDS = - newProperty(STRING_LIST, "settings.security.unsafePasswords", - "123456", "password", "qwerty", "12345", "54321"); + newListProperty("settings.security.unsafePasswords", "123456", "password", "qwerty", "12345", "54321"); private SecuritySettings() { } diff --git a/src/test/java/fr/xephi/authme/settings/domain/EnumPropertyTypeTest.java b/src/test/java/fr/xephi/authme/settings/domain/EnumPropertyTest.java similarity index 75% rename from src/test/java/fr/xephi/authme/settings/domain/EnumPropertyTypeTest.java rename to src/test/java/fr/xephi/authme/settings/domain/EnumPropertyTest.java index 461314e8..5fe55362 100644 --- a/src/test/java/fr/xephi/authme/settings/domain/EnumPropertyTypeTest.java +++ b/src/test/java/fr/xephi/authme/settings/domain/EnumPropertyTest.java @@ -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 propertyType = new EnumPropertyType<>(TestEnum.class); Property 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 propertyType = new EnumPropertyType<>(TestEnum.class); Property 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 propertyType = new EnumPropertyType<>(TestEnum.class); Property 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 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); + boolean result = property.isPresent(configuration); // then assertThat(result, equalTo(true)); @@ -77,13 +73,12 @@ public class EnumPropertyTypeTest { @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); + boolean result = property.isPresent(configuration); // then assertThat(result, equalTo(false)); @@ -92,14 +87,13 @@ public class EnumPropertyTypeTest { @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); + boolean result = property.isPresent(configuration); // then assertThat(result, equalTo(false)); diff --git a/src/test/java/fr/xephi/authme/settings/domain/PropertyTypeTest.java b/src/test/java/fr/xephi/authme/settings/domain/PropertyTest.java similarity index 82% rename from src/test/java/fr/xephi/authme/settings/domain/PropertyTypeTest.java rename to src/test/java/fr/xephi/authme/settings/domain/PropertyTest.java index df012063..de33c53c 100644 --- a/src/test/java/fr/xephi/authme/settings/domain/PropertyTypeTest.java +++ b/src/test/java/fr/xephi/authme/settings/domain/PropertyTest.java @@ -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> property = Property.newProperty(PropertyType.STRING_LIST, "list.path.test", "1", "b"); + Property> property = Property.newListProperty("list.path.test", "1", "b"); // when List result = property.getFromFile(configuration); @@ -133,7 +132,7 @@ public class PropertyTypeTest { public void shouldGetStringListDefault() { // given Property> property = - Property.newProperty(PropertyType.STRING_LIST, "list.path.wrong", "default", "list", "elements"); + Property.newListProperty("list.path.wrong", "default", "list", "elements"); // when List result = property.getFromFile(configuration); @@ -142,13 +141,4 @@ public class PropertyTypeTest { assertThat(result, contains("default", "list", "elements")); } - private static Answer secondParameter() { - return new Answer() { - @Override - public T answer(InvocationOnMock invocation) throws Throwable { - // Return the second parameter -> the default - return (T) invocation.getArguments()[1]; - } - }; - } } 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 1e2e5f9f..0f16e1e8 100644 --- a/src/test/java/fr/xephi/authme/settings/properties/TestConfiguration.java +++ b/src/test/java/fr/xephi/authme/settings/properties/TestConfiguration.java @@ -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> RATIO_FIELDS = - newProperty(PropertyType.STRING_LIST, "sample.ratio.fields", "a", "b", "c"); + newListProperty("sample.ratio.fields", "a", "b", "c"); public static final Property VERSION_NUMBER = newProperty("version", 32046); @@ -35,7 +35,7 @@ public final class TestConfiguration implements SettingsClass { newProperty("features.boring.skip", false); public static final Property> BORING_COLORS = - newProperty(PropertyType.STRING_LIST, "features.boring.colors"); + newListProperty("features.boring.colors"); public static final Property 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> COOL_OPTIONS = - newProperty(PropertyType.STRING_LIST, "features.cool.options", "Sparks", "Sprinkles"); + newListProperty("features.cool.options", "Sparks", "Sprinkles"); private TestConfiguration() {