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

View File

@ -0,0 +1,49 @@
package fr.xephi.authme.settings.domain;
import org.bukkit.configuration.file.FileConfiguration;
import org.yaml.snakeyaml.Yaml;
/**
* Enum property.
*
* @param <E> The enum class
*/
class EnumProperty<E extends Enum<E>> extends Property<E> {
private Class<E> clazz;
public EnumProperty(Class<E> 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;
}
}

View File

@ -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 <E> The enum class
*/
class EnumPropertyType<E extends Enum<E>> extends PropertyType<E> {
private Class<E> clazz;
public EnumPropertyType(Class<E> clazz) {
this.clazz = clazz;
}
@Override
public E getFromFile(Property<E> 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<E> 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;
}
}

View File

@ -10,45 +10,27 @@ import java.util.Objects;
/** /**
* Property class, representing a <i>setting</i> that is read from the config.yml file. * Property class, representing a <i>setting</i> that is read from the config.yml file.
*/ */
public class Property<T> { public abstract class Property<T> {
private final PropertyType<T> type;
private final String path; private final String path;
private final T defaultValue; private final T defaultValue;
private Property(PropertyType<T> type, String path, T defaultValue) { protected Property(String path, T defaultValue) {
Objects.requireNonNull(defaultValue); Objects.requireNonNull(defaultValue);
this.type = type;
this.path = path; this.path = path;
this.defaultValue = defaultValue; this.defaultValue = defaultValue;
} }
/** /**
* Create a new property. See also {@link #newProperty(PropertyType, String, Object[])} for lists and * Create a new string list property.
* {@link #newProperty(Class, String, Enum)}.
* *
* @param type The property type
* @param path The property's path * @param path The property's path
* @param defaultValue The default value * @param defaultValues The items in the default list
* @param <T> The type of the property
* @return The created property
*/
public static <T> Property<T> newProperty(PropertyType<T> 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 <U> The list type
* @return The created list property * @return The created list property
*/ */
@SafeVarargs public static Property<List<String>> newListProperty(String path, String... defaultValues) {
public static <U> Property<List<U>> newProperty(PropertyType<List<U>> type, String path, U... defaultValues) { // does not have the same name as not to clash with #newProperty(String, String)
return new Property<>(type, path, Arrays.asList(defaultValues)); return new StringListProperty(path, defaultValues);
} }
/** /**
@ -61,36 +43,28 @@ public class Property<T> {
* @return The created enum property * @return The created enum property
*/ */
public static <E extends Enum<E>> Property<E> newProperty(Class<E> clazz, String path, E defaultValue) { public static <E extends Enum<E>> Property<E> newProperty(Class<E> 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<Boolean> newProperty(String path, boolean defaultValue) { public static Property<Boolean> newProperty(String path, boolean defaultValue) {
return new Property<>(PropertyType.BOOLEAN, path, defaultValue); return new BooleanProperty(path, defaultValue);
} }
public static Property<Integer> newProperty(String path, int defaultValue) { public static Property<Integer> newProperty(String path, int defaultValue) {
return new Property<>(PropertyType.INTEGER, path, defaultValue); return new IntegerProperty(path, defaultValue);
} }
public static Property<String> newProperty(String path, String defaultValue) { public static Property<String> 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 &ndash; guaranteed to never return null. * Get the property value from the given configuration &ndash; guaranteed to never return null.
* *
* @param configuration The configuration to read the value from * @param configuration The configuration to read the value from
* @return The value, or default if not present * @return The value, or default if not present
*/ */
public T getFromFile(FileConfiguration configuration) { public abstract T getFromFile(FileConfiguration configuration);
return type.getFromFile(this, configuration);
}
/** /**
* Return whether or not the given configuration file contains the property. * Return whether or not the given configuration file contains the property.
@ -99,7 +73,7 @@ public class Property<T> {
* @return True if the property is present, false otherwise * @return True if the property is present, false otherwise
*/ */
public boolean isPresent(FileConfiguration configuration) { public boolean isPresent(FileConfiguration configuration) {
return type.contains(this, configuration); return configuration.contains(path);
} }
/** /**
@ -111,12 +85,9 @@ public class Property<T> {
* @return The generated YAML * @return The generated YAML
*/ */
public String toYaml(FileConfiguration configuration, Yaml simpleYaml, Yaml singleQuoteYaml) { 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. * Return the default value of the property.
* *
@ -140,4 +111,89 @@ public class Property<T> {
return "Property '" + path + "'"; return "Property '" + path + "'";
} }
/**
* Boolean property.
*/
private static final class BooleanProperty extends Property<Boolean> {
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<Integer> {
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<String> {
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<List<String>> {
public StringListProperty(String path, String[] defaultValues) {
super(path, Arrays.asList(defaultValues));
}
@Override
public List<String> 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<String> 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;
}
}
} }

View File

@ -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 <T> The value of the property
* @see Property
*/
public abstract class PropertyType<T> {
public static final PropertyType<Boolean> BOOLEAN = new BooleanProperty();
public static final PropertyType<Integer> INTEGER = new IntegerProperty();
public static final PropertyType<String> STRING = new StringProperty();
public static final PropertyType<List<String>> 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<T> 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<T> 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<Boolean> {
@Override
public Boolean getFromFile(Property<Boolean> property, FileConfiguration configuration) {
return configuration.getBoolean(property.getPath(), property.getDefaultValue());
}
}
/**
* Integer property.
*/
private static final class IntegerProperty extends PropertyType<Integer> {
@Override
public Integer getFromFile(Property<Integer> property, FileConfiguration configuration) {
return configuration.getInt(property.getPath(), property.getDefaultValue());
}
}
/**
* String property.
*/
private static final class StringProperty extends PropertyType<String> {
@Override
public String getFromFile(Property<String> 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<List<String>> {
@Override
public List<String> getFromFile(Property<List<String>> property, FileConfiguration configuration) {
if (!configuration.isList(property.getPath())) {
return property.getDefaultValue();
}
return configuration.getStringList(property.getPath());
}
@Override
public boolean contains(Property<List<String>> property, FileConfiguration configuration) {
return configuration.contains(property.getPath()) && configuration.isList(property.getPath());
}
@Override
public String toYaml(List<String> 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;
}
}
}

View File

@ -5,26 +5,24 @@ import fr.xephi.authme.settings.domain.Property;
import fr.xephi.authme.settings.domain.SettingsClass; import fr.xephi.authme.settings.domain.SettingsClass;
import static fr.xephi.authme.settings.domain.Property.newProperty; 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 { public class ConverterSettings implements SettingsClass {
@Comment("Rakamak file name") @Comment("Rakamak file name")
public static final Property<String> RAKAMAK_FILE_NAME = public static final Property<String> RAKAMAK_FILE_NAME =
newProperty(STRING, "Converter.Rakamak.fileName", "users.rak"); newProperty("Converter.Rakamak.fileName", "users.rak");
@Comment("Rakamak use IP?") @Comment("Rakamak use IP?")
public static final Property<Boolean> RAKAMAK_USE_IP = public static final Property<Boolean> RAKAMAK_USE_IP =
newProperty(BOOLEAN, "Converter.Rakamak.useIP", false); newProperty("Converter.Rakamak.useIP", false);
@Comment("Rakamak IP file name") @Comment("Rakamak IP file name")
public static final Property<String> RAKAMAK_IP_FILE_NAME = public static final Property<String> RAKAMAK_IP_FILE_NAME =
newProperty(STRING, "Converter.Rakamak.ipFileName", "UsersIp.rak"); newProperty("Converter.Rakamak.ipFileName", "UsersIp.rak");
@Comment("CrazyLogin database file name") @Comment("CrazyLogin database file name")
public static final Property<String> CRAZYLOGIN_FILE_NAME = public static final Property<String> CRAZYLOGIN_FILE_NAME =
newProperty(STRING, "Converter.CrazyLogin.fileName", "accounts.db"); newProperty("Converter.CrazyLogin.fileName", "accounts.db");
private ConverterSettings() { private ConverterSettings() {
} }

View File

@ -6,29 +6,26 @@ import fr.xephi.authme.settings.domain.SettingsClass;
import java.util.List; 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.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 { public class EmailSettings implements SettingsClass {
@Comment("Email SMTP server host") @Comment("Email SMTP server host")
public static final Property<String> SMTP_HOST = public static final Property<String> SMTP_HOST =
newProperty(STRING, "Email.mailSMTP", "smtp.gmail.com"); newProperty("Email.mailSMTP", "smtp.gmail.com");
@Comment("Email SMTP server port") @Comment("Email SMTP server port")
public static final Property<Integer> SMTP_PORT = public static final Property<Integer> SMTP_PORT =
newProperty(INTEGER, "Email.mailPort", 465); newProperty("Email.mailPort", 465);
@Comment("Email account which sends the mails") @Comment("Email account which sends the mails")
public static final Property<String> MAIL_ACCOUNT = public static final Property<String> MAIL_ACCOUNT =
newProperty(STRING, "Email.mailAccount", ""); newProperty("Email.mailAccount", "");
@Comment("Email account password") @Comment("Email account password")
public static final Property<String> MAIL_PASSWORD = public static final Property<String> MAIL_PASSWORD =
newProperty(STRING, "Email.mailPassword", ""); newProperty("Email.mailPassword", "");
@Comment("Custom sender name, replacing the mailAccount name in the email") @Comment("Custom sender name, replacing the mailAccount name in the email")
public static final Property<String> MAIL_SENDER_NAME = public static final Property<String> MAIL_SENDER_NAME =
@ -36,39 +33,39 @@ public class EmailSettings implements SettingsClass {
@Comment("Recovery password length") @Comment("Recovery password length")
public static final Property<Integer> RECOVERY_PASSWORD_LENGTH = public static final Property<Integer> RECOVERY_PASSWORD_LENGTH =
newProperty(INTEGER, "Email.RecoveryPasswordLength", 8); newProperty("Email.RecoveryPasswordLength", 8);
@Comment("Mail Subject") @Comment("Mail Subject")
public static final Property<String> RECOVERY_MAIL_SUBJECT = public static final Property<String> RECOVERY_MAIL_SUBJECT =
newProperty(STRING, "Email.mailSubject", "Your new AuthMe password"); newProperty("Email.mailSubject", "Your new AuthMe password");
@Comment("Like maxRegPerIP but with email") @Comment("Like maxRegPerIP but with email")
public static final Property<Integer> MAX_REG_PER_EMAIL = public static final Property<Integer> MAX_REG_PER_EMAIL =
newProperty(INTEGER, "Email.maxRegPerEmail", 1); newProperty("Email.maxRegPerEmail", 1);
@Comment("Recall players to add an email?") @Comment("Recall players to add an email?")
public static final Property<Boolean> RECALL_PLAYERS = public static final Property<Boolean> RECALL_PLAYERS =
newProperty(BOOLEAN, "Email.recallPlayers", false); newProperty("Email.recallPlayers", false);
@Comment("Delay in minute for the recall scheduler") @Comment("Delay in minute for the recall scheduler")
public static final Property<Integer> DELAY_RECALL = public static final Property<Integer> DELAY_RECALL =
newProperty(INTEGER, "Email.delayRecall", 5); newProperty("Email.delayRecall", 5);
@Comment("Blacklist these domains for emails") @Comment("Blacklist these domains for emails")
public static final Property<List<String>> DOMAIN_BLACKLIST = public static final Property<List<String>> DOMAIN_BLACKLIST =
newProperty(STRING_LIST, "Email.emailBlacklisted", "10minutemail.com"); newListProperty("Email.emailBlacklisted", "10minutemail.com");
@Comment("Whitelist ONLY these domains for emails") @Comment("Whitelist ONLY these domains for emails")
public static final Property<List<String>> DOMAIN_WHITELIST = public static final Property<List<String>> DOMAIN_WHITELIST =
newProperty(STRING_LIST, "Email.emailWhitelisted"); newListProperty("Email.emailWhitelisted");
@Comment("Send the new password drawn in an image?") @Comment("Send the new password drawn in an image?")
public static final Property<Boolean> PASSWORD_AS_IMAGE = public static final Property<Boolean> PASSWORD_AS_IMAGE =
newProperty(BOOLEAN, "Email.generateImage", false); newProperty("Email.generateImage", false);
@Comment("The OAuth2 token") @Comment("The OAuth2 token")
public static final Property<String> OAUTH2_TOKEN = public static final Property<String> OAUTH2_TOKEN =
newProperty(STRING, "Email.emailOauth2Token", ""); newProperty("Email.emailOauth2Token", "");
private EmailSettings() { private EmailSettings() {
} }

View File

@ -2,11 +2,11 @@ package fr.xephi.authme.settings.properties;
import fr.xephi.authme.settings.domain.Comment; import fr.xephi.authme.settings.domain.Comment;
import fr.xephi.authme.settings.domain.Property; 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.domain.SettingsClass;
import java.util.List; 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.Property.newProperty;
public class HooksSettings implements SettingsClass { 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)") @Comment("Other MySQL columns where we need to put the username (case-sensitive)")
public static final Property<List<String>> MYSQL_OTHER_USERNAME_COLS = public static final Property<List<String>> 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)") @Comment("How much log2 rounds needed in BCrypt (do not change if you do not know what it does)")
public static final Property<Integer> BCRYPT_LOG2_ROUND = public static final Property<Integer> BCRYPT_LOG2_ROUND =

View File

@ -6,39 +6,37 @@ import fr.xephi.authme.settings.domain.SettingsClass;
import java.util.List; 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.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 { public class ProtectionSettings implements SettingsClass {
@Comment("Enable some servers protection (country based login, antibot)") @Comment("Enable some servers protection (country based login, antibot)")
public static final Property<Boolean> ENABLE_PROTECTION = public static final Property<Boolean> 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", @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!"}) "PLEASE USE QUOTES!"})
public static final Property<List<String>> COUNTRIES_WHITELIST = public static final Property<List<String>> 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", @Comment({"Countries not allowed to join the server and register",
"PLEASE USE QUOTES!"}) "PLEASE USE QUOTES!"})
public static final Property<List<String>> COUNTRIES_BLACKLIST = public static final Property<List<String>> COUNTRIES_BLACKLIST =
newProperty(STRING_LIST, "Protection.countriesBlacklist"); newListProperty("Protection.countriesBlacklist");
@Comment("Do we need to enable automatic antibot system?") @Comment("Do we need to enable automatic antibot system?")
public static final Property<Boolean> ENABLE_ANTIBOT = public static final Property<Boolean> 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") @Comment("Max number of player allowed to login in 5 secs before enable AntiBot system automatically")
public static final Property<Integer> ANTIBOT_SENSIBILITY = public static final Property<Integer> ANTIBOT_SENSIBILITY =
newProperty(INTEGER, "Protection.antiBotSensibility", 5); newProperty("Protection.antiBotSensibility", 5);
@Comment("Duration in minutes of the antibot automatic system") @Comment("Duration in minutes of the antibot automatic system")
public static final Property<Integer> ANTIBOT_DURATION = public static final Property<Integer> ANTIBOT_DURATION =
newProperty(INTEGER, "Protection.antiBotDuration", 10); newProperty("Protection.antiBotDuration", 10);
private ProtectionSettings() { private ProtectionSettings() {
} }

View File

@ -2,11 +2,11 @@ package fr.xephi.authme.settings.properties;
import fr.xephi.authme.settings.domain.Comment; import fr.xephi.authme.settings.domain.Comment;
import fr.xephi.authme.settings.domain.Property; 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.domain.SettingsClass;
import java.util.List; 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.Property.newProperty;
public class RegistrationSettings implements SettingsClass { 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") @Comment("Force these commands after /login, without any '/', use %p to replace with player name")
public static final Property<List<String>> FORCE_COMMANDS = public static final Property<List<String>> FORCE_COMMANDS =
newProperty(PropertyType.STRING_LIST, "settings.forceCommands"); newListProperty("settings.forceCommands");
@Comment("Force these commands after /login as service console, without any '/'. " @Comment("Force these commands after /login as service console, without any '/'. "
+ "Use %p to replace with player name") + "Use %p to replace with player name")
public static final Property<List<String>> FORCE_COMMANDS_AS_CONSOLE = public static final Property<List<String>> 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") @Comment("Force these commands after /register, without any '/', use %p to replace with player name")
public static final Property<List<String>> FORCE_REGISTER_COMMANDS = public static final Property<List<String>> FORCE_REGISTER_COMMANDS =
newProperty(PropertyType.STRING_LIST, "settings.forceRegisterCommands"); newListProperty("settings.forceRegisterCommands");
@Comment("Force these commands after /register as a server console, without any '/'. " @Comment("Force these commands after /register as a server console, without any '/'. "
+ "Use %p to replace with player name") + "Use %p to replace with player name")
public static final Property<List<String>> FORCE_REGISTER_COMMANDS_AS_CONSOLE = public static final Property<List<String>> FORCE_REGISTER_COMMANDS_AS_CONSOLE =
newProperty(PropertyType.STRING_LIST, "settings.forceRegisterCommandsAsConsole"); newListProperty("settings.forceRegisterCommandsAsConsole");
@Comment({ @Comment({
"Enable to display the welcome message (welcome.txt) after a registration or a login", "Enable to display the welcome message (welcome.txt) after a registration or a login",

View File

@ -2,11 +2,11 @@ package fr.xephi.authme.settings.properties;
import fr.xephi.authme.settings.domain.Comment; import fr.xephi.authme.settings.domain.Comment;
import fr.xephi.authme.settings.domain.Property; 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.domain.SettingsClass;
import java.util.List; 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.Property.newProperty;
public class RestrictionSettings implements SettingsClass { public class RestrictionSettings implements SettingsClass {
@ -26,7 +26,7 @@ public class RestrictionSettings implements SettingsClass {
@Comment("Allowed commands for unauthenticated players") @Comment("Allowed commands for unauthenticated players")
public static final Property<List<String>> ALLOW_COMMANDS = public static final Property<List<String>> ALLOW_COMMANDS =
newProperty(PropertyType.STRING_LIST, "settings.restrictions.allowCommands", newListProperty("settings.restrictions.allowCommands",
"login", "register", "l", "reg", "email", "captcha"); "login", "register", "l", "reg", "email", "captcha");
@Comment("Max number of allowed registrations per IP") @Comment("Max number of allowed registrations per IP")
@ -75,7 +75,7 @@ public class RestrictionSettings implements SettingsClass {
" AllowedRestrictedUser:", " AllowedRestrictedUser:",
" - playername;127.0.0.1"}) " - playername;127.0.0.1"})
public static final Property<List<String>> ALLOWED_RESTRICTED_USERS = public static final Property<List<String>> ALLOWED_RESTRICTED_USERS =
newProperty(PropertyType.STRING_LIST, "settings.restrictions.AllowedRestrictedUser"); newListProperty("settings.restrictions.AllowedRestrictedUser");
@Comment("Should unregistered players be kicked immediately?") @Comment("Should unregistered players be kicked immediately?")
public static final Property<Boolean> KICK_NON_REGISTERED = public static final Property<Boolean> KICK_NON_REGISTERED =
@ -148,7 +148,7 @@ public class RestrictionSettings implements SettingsClass {
"WorldNames where we need to force the spawn location for ForceSpawnLocOnJoinEnabled", "WorldNames where we need to force the spawn location for ForceSpawnLocOnJoinEnabled",
"Case-sensitive!"}) "Case-sensitive!"})
public static final Property<List<String>> FORCE_SPAWN_ON_WORLDS = public static final Property<List<String>> FORCE_SPAWN_ON_WORLDS =
newProperty(PropertyType.STRING_LIST, "settings.restrictions.ForceSpawnOnTheseWorlds", newListProperty("settings.restrictions.ForceSpawnOnTheseWorlds",
"world", "world_nether", "world_the_end"); "world", "world_nether", "world_the_end");
@Comment("Ban ip when the ip is not the ip registered in database") @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!" "It is case-sensitive!"
}) })
public static final Property<List<String>> UNRESTRICTED_NAMES = public static final Property<List<String>> UNRESTRICTED_NAMES =
newProperty(PropertyType.STRING_LIST, "settings.unrestrictions.UnrestrictedName"); newListProperty("settings.unrestrictions.UnrestrictedName");
private RestrictionSettings() { private RestrictionSettings() {

View File

@ -7,8 +7,8 @@ import fr.xephi.authme.settings.domain.SettingsClass;
import java.util.List; 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.Property.newProperty;
import static fr.xephi.authme.settings.domain.PropertyType.STRING_LIST;
public class SecuritySettings implements SettingsClass { public class SecuritySettings implements SettingsClass {
@ -98,8 +98,7 @@ public class SecuritySettings implements SettingsClass {
"- '123456'", "- '123456'",
"- 'password'"}) "- 'password'"})
public static final Property<List<String>> UNSAFE_PASSWORDS = public static final Property<List<String>> UNSAFE_PASSWORDS =
newProperty(STRING_LIST, "settings.security.unsafePasswords", newListProperty("settings.security.unsafePasswords", "123456", "password", "qwerty", "12345", "54321");
"123456", "password", "qwerty", "12345", "54321");
private SecuritySettings() { private SecuritySettings() {
} }

View File

@ -9,20 +9,19 @@ import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
/** /**
* Test for {@link EnumPropertyType}. * Test for {@link EnumProperty}.
*/ */
public class EnumPropertyTypeTest { public class EnumPropertyTest {
@Test @Test
public void shouldReturnCorrectEnumValue() { public void shouldReturnCorrectEnumValue() {
// given // given
PropertyType<TestEnum> propertyType = new EnumPropertyType<>(TestEnum.class);
Property<TestEnum> property = Property.newProperty(TestEnum.class, "enum.path", TestEnum.ENTRY_C); Property<TestEnum> property = Property.newProperty(TestEnum.class, "enum.path", TestEnum.ENTRY_C);
YamlConfiguration configuration = mock(YamlConfiguration.class); YamlConfiguration configuration = mock(YamlConfiguration.class);
given(configuration.getString(property.getPath())).willReturn("Entry_B"); given(configuration.getString(property.getPath())).willReturn("Entry_B");
// when // when
TestEnum result = propertyType.getFromFile(property, configuration); TestEnum result = property.getFromFile(configuration);
// then // then
assertThat(result, equalTo(TestEnum.ENTRY_B)); assertThat(result, equalTo(TestEnum.ENTRY_B));
@ -31,13 +30,12 @@ public class EnumPropertyTypeTest {
@Test @Test
public void shouldFallBackToDefaultForInvalidValue() { public void shouldFallBackToDefaultForInvalidValue() {
// given // given
PropertyType<TestEnum> propertyType = new EnumPropertyType<>(TestEnum.class);
Property<TestEnum> property = Property.newProperty(TestEnum.class, "enum.path", TestEnum.ENTRY_C); Property<TestEnum> property = Property.newProperty(TestEnum.class, "enum.path", TestEnum.ENTRY_C);
YamlConfiguration configuration = mock(YamlConfiguration.class); YamlConfiguration configuration = mock(YamlConfiguration.class);
given(configuration.getString(property.getPath())).willReturn("Bogus"); given(configuration.getString(property.getPath())).willReturn("Bogus");
// when // when
TestEnum result = propertyType.getFromFile(property, configuration); TestEnum result = property.getFromFile(configuration);
// then // then
assertThat(result, equalTo(TestEnum.ENTRY_C)); assertThat(result, equalTo(TestEnum.ENTRY_C));
@ -46,13 +44,12 @@ public class EnumPropertyTypeTest {
@Test @Test
public void shouldFallBackToDefaultForNonExistentValue() { public void shouldFallBackToDefaultForNonExistentValue() {
// given // given
PropertyType<TestEnum> propertyType = new EnumPropertyType<>(TestEnum.class);
Property<TestEnum> property = Property.newProperty(TestEnum.class, "enum.path", TestEnum.ENTRY_C); Property<TestEnum> property = Property.newProperty(TestEnum.class, "enum.path", TestEnum.ENTRY_C);
YamlConfiguration configuration = mock(YamlConfiguration.class); YamlConfiguration configuration = mock(YamlConfiguration.class);
given(configuration.getString(property.getPath())).willReturn(null); given(configuration.getString(property.getPath())).willReturn(null);
// when // when
TestEnum result = propertyType.getFromFile(property, configuration); TestEnum result = property.getFromFile(configuration);
// then // then
assertThat(result, equalTo(TestEnum.ENTRY_C)); assertThat(result, equalTo(TestEnum.ENTRY_C));
@ -61,14 +58,13 @@ public class EnumPropertyTypeTest {
@Test @Test
public void shouldReturnTrueForContainsCheck() { public void shouldReturnTrueForContainsCheck() {
// given // given
PropertyType<TestEnum> propertyType = new EnumPropertyType<>(TestEnum.class);
Property<TestEnum> property = Property.newProperty(TestEnum.class, "my.test.path", TestEnum.ENTRY_C); Property<TestEnum> property = Property.newProperty(TestEnum.class, "my.test.path", TestEnum.ENTRY_C);
YamlConfiguration configuration = mock(YamlConfiguration.class); YamlConfiguration configuration = mock(YamlConfiguration.class);
given(configuration.contains(property.getPath())).willReturn(true); given(configuration.contains(property.getPath())).willReturn(true);
given(configuration.getString(property.getPath())).willReturn("ENTRY_B"); given(configuration.getString(property.getPath())).willReturn("ENTRY_B");
// when // when
boolean result = propertyType.contains(property, configuration); boolean result = property.isPresent(configuration);
// then // then
assertThat(result, equalTo(true)); assertThat(result, equalTo(true));
@ -77,13 +73,12 @@ public class EnumPropertyTypeTest {
@Test @Test
public void shouldReturnFalseForFileWithoutConfig() { public void shouldReturnFalseForFileWithoutConfig() {
// given // given
PropertyType<TestEnum> propertyType = new EnumPropertyType<>(TestEnum.class);
Property<TestEnum> property = Property.newProperty(TestEnum.class, "my.test.path", TestEnum.ENTRY_C); Property<TestEnum> property = Property.newProperty(TestEnum.class, "my.test.path", TestEnum.ENTRY_C);
YamlConfiguration configuration = mock(YamlConfiguration.class); YamlConfiguration configuration = mock(YamlConfiguration.class);
given(configuration.contains(property.getPath())).willReturn(false); given(configuration.contains(property.getPath())).willReturn(false);
// when // when
boolean result = propertyType.contains(property, configuration); boolean result = property.isPresent(configuration);
// then // then
assertThat(result, equalTo(false)); assertThat(result, equalTo(false));
@ -92,14 +87,13 @@ public class EnumPropertyTypeTest {
@Test @Test
public void shouldReturnFalseForUnknownValue() { public void shouldReturnFalseForUnknownValue() {
// given // given
PropertyType<TestEnum> propertyType = new EnumPropertyType<>(TestEnum.class);
Property<TestEnum> property = Property.newProperty(TestEnum.class, "my.test.path", TestEnum.ENTRY_C); Property<TestEnum> property = Property.newProperty(TestEnum.class, "my.test.path", TestEnum.ENTRY_C);
YamlConfiguration configuration = mock(YamlConfiguration.class); YamlConfiguration configuration = mock(YamlConfiguration.class);
given(configuration.contains(property.getPath())).willReturn(true); given(configuration.contains(property.getPath())).willReturn(true);
given(configuration.getString(property.getPath())).willReturn("wrong value"); given(configuration.getString(property.getPath())).willReturn("wrong value");
// when // when
boolean result = propertyType.contains(property, configuration); boolean result = property.isPresent(configuration);
// then // then
assertThat(result, equalTo(false)); assertThat(result, equalTo(false));

View File

@ -3,8 +3,7 @@ package fr.xephi.authme.settings.domain;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.mockito.invocation.InvocationOnMock; import org.mockito.internal.stubbing.answers.ReturnsArgumentAt;
import org.mockito.stubbing.Answer;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -20,9 +19,9 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; 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; private static YamlConfiguration configuration;
@ -31,11 +30,11 @@ public class PropertyTypeTest {
configuration = mock(YamlConfiguration.class); configuration = mock(YamlConfiguration.class);
when(configuration.getBoolean(eq("bool.path.test"), anyBoolean())).thenReturn(true); 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.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.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.isList("list.path.test")).thenReturn(true);
when(configuration.getStringList("list.path.test")).thenReturn(Arrays.asList("test1", "Test2", "3rd test")); when(configuration.getStringList("list.path.test")).thenReturn(Arrays.asList("test1", "Test2", "3rd test"));
when(configuration.isList("list.path.wrong")).thenReturn(false); when(configuration.isList("list.path.wrong")).thenReturn(false);
@ -120,7 +119,7 @@ public class PropertyTypeTest {
@Test @Test
public void shouldGetStringListValue() { public void shouldGetStringListValue() {
// given // 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 // when
List<String> result = property.getFromFile(configuration); List<String> result = property.getFromFile(configuration);
@ -133,7 +132,7 @@ public class PropertyTypeTest {
public void shouldGetStringListDefault() { public void shouldGetStringListDefault() {
// given // given
Property<List<String>> property = Property<List<String>> property =
Property.newProperty(PropertyType.STRING_LIST, "list.path.wrong", "default", "list", "elements"); Property.newListProperty("list.path.wrong", "default", "list", "elements");
// when // when
List<String> result = property.getFromFile(configuration); List<String> result = property.getFromFile(configuration);
@ -142,13 +141,4 @@ public class PropertyTypeTest {
assertThat(result, contains("default", "list", "elements")); 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];
}
};
}
} }

View File

@ -2,13 +2,13 @@ package fr.xephi.authme.settings.properties;
import fr.xephi.authme.ReflectionTestUtils; import fr.xephi.authme.ReflectionTestUtils;
import fr.xephi.authme.settings.domain.Property; 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.domain.SettingsClass;
import fr.xephi.authme.settings.propertymap.PropertyMap; import fr.xephi.authme.settings.propertymap.PropertyMap;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.List; 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.Property.newProperty;
/** /**
@ -26,7 +26,7 @@ public final class TestConfiguration implements SettingsClass {
newProperty(TestEnum.class, "sample.ratio.order", TestEnum.SECOND); newProperty(TestEnum.class, "sample.ratio.order", TestEnum.SECOND);
public static final Property<List<String>> RATIO_FIELDS = 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 = public static final Property<Integer> VERSION_NUMBER =
newProperty("version", 32046); newProperty("version", 32046);
@ -35,7 +35,7 @@ public final class TestConfiguration implements SettingsClass {
newProperty("features.boring.skip", false); newProperty("features.boring.skip", false);
public static final Property<List<String>> BORING_COLORS = 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 = public static final Property<Integer> DUST_LEVEL =
newProperty("features.boring.dustLevel", -1); newProperty("features.boring.dustLevel", -1);
@ -44,7 +44,7 @@ public final class TestConfiguration implements SettingsClass {
newProperty("features.cool.enabled", false); newProperty("features.cool.enabled", false);
public static final Property<List<String>> COOL_OPTIONS = 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() { private TestConfiguration() {