Update configme (#1631)

* Upgrade to ConfigMe 1.0.1
* Use ConfigMe reader whenever possible, minor simplifications
This commit is contained in:
ljacqu
2018-09-09 15:45:00 +02:00
committed by GitHub
parent 6676a39447
commit ee764c0a6e
42 changed files with 516 additions and 555 deletions
@@ -1,11 +1,11 @@
package fr.xephi.authme.message;
import ch.jalu.configme.resource.PropertyReader;
import ch.jalu.configme.resource.YamlFileReader;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.command.help.HelpMessage;
import fr.xephi.authme.command.help.HelpSection;
import fr.xephi.authme.permission.DefaultPermission;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.hamcrest.Matcher;
import org.junit.Before;
import org.junit.Test;
@@ -49,27 +49,27 @@ public class HelpMessageConsistencyTest {
public void shouldHaveRequiredEntries() {
for (File file : helpFiles) {
// given
FileConfiguration configuration = YamlConfiguration.loadConfiguration(file);
PropertyReader reader = new YamlFileReader(file);
// when / then
assertHasAllHelpSectionEntries(file.getName(), configuration);
assertHasAllHelpSectionEntries(file.getName(), reader);
}
}
private void assertHasAllHelpSectionEntries(String filename, FileConfiguration configuration) {
private void assertHasAllHelpSectionEntries(String filename, PropertyReader reader) {
for (HelpSection section : HelpSection.values()) {
assertThat(filename + " should have entry for HelpSection '" + section + "'",
configuration.getString(section.getKey()), notEmptyString());
reader.getString(section.getKey()), notEmptyString());
}
for (HelpMessage message : HelpMessage.values()) {
assertThat(filename + " should have entry for HelpMessage '" + message + "'",
configuration.getString(message.getKey()), notEmptyString());
reader.getString(message.getKey()), notEmptyString());
}
for (DefaultPermission defaultPermission : DefaultPermission.values()) {
assertThat(filename + " should have entry for DefaultPermission '" + defaultPermission + "'",
configuration.getString(getPathForDefaultPermission(defaultPermission)), notEmptyString());
reader.getString(getPathForDefaultPermission(defaultPermission)), notEmptyString());
}
}
@@ -1,10 +1,10 @@
package fr.xephi.authme.message;
import ch.jalu.configme.resource.PropertyReader;
import ch.jalu.configme.resource.YamlFileReader;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Multimap;
import fr.xephi.authme.TestHelper;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@@ -51,12 +51,12 @@ public class MessageFilePlaceholderTest {
@Test
public void shouldHaveAllPlaceholders() {
// given
FileConfiguration configuration = YamlConfiguration.loadConfiguration(messagesFile);
List<String> errors = new ArrayList<>();
PropertyReader reader = new YamlFileReader(messagesFile);
List<String> errors = new ArrayList<>(0);
// when
for (MessageKey key : MessageKey.values()) {
List<String> missingTags = findMissingTags(key, configuration);
List<String> missingTags = findMissingTags(key, reader);
if (!missingTags.isEmpty()) {
errors.add("Message key '" + key + "' should have tags: " + String.join(", ", missingTags));
}
@@ -68,9 +68,9 @@ public class MessageFilePlaceholderTest {
}
}
private List<String> findMissingTags(MessageKey key, FileConfiguration configuration) {
if (key.getTags().length > 0 && configuration.contains(key.getKey())) {
String message = configuration.getString(key.getKey());
private List<String> findMissingTags(MessageKey key, PropertyReader reader) {
if (key.getTags().length > 0 && reader.contains(key.getKey())) {
String message = reader.getString(key.getKey());
return Arrays.stream(key.getTags())
.filter(tag -> !EXCLUSIONS.get(key).contains(tag) && !message.contains(tag))
.collect(Collectors.toList());
@@ -1,9 +1,9 @@
package fr.xephi.authme.message;
import ch.jalu.configme.resource.PropertyReader;
import ch.jalu.configme.resource.YamlFileReader;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.util.StringUtils;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.junit.Test;
import java.io.File;
@@ -24,10 +24,10 @@ public class MessagesFileConsistencyTest {
@Test
public void shouldHaveAllMessages() {
File file = TestHelper.getJarFile(MESSAGES_FILE);
FileConfiguration configuration = YamlConfiguration.loadConfiguration(file);
PropertyReader reader = new YamlFileReader(file);
List<String> errors = new ArrayList<>();
for (MessageKey messageKey : MessageKey.values()) {
validateMessage(messageKey, configuration, errors);
validateMessage(messageKey, reader, errors);
}
if (!errors.isEmpty()) {
@@ -36,9 +36,9 @@ public class MessagesFileConsistencyTest {
}
}
private static void validateMessage(MessageKey messageKey, FileConfiguration configuration, List<String> errors) {
private static void validateMessage(MessageKey messageKey, PropertyReader reader, List<String> errors) {
final String key = messageKey.getKey();
final String message = configuration.getString(key);
final String message = reader.getString(key);
if (StringUtils.isEmpty(message)) {
errors.add("Messages file should have message for key '" + key + "'");
@@ -1,10 +1,11 @@
package fr.xephi.authme.message;
import ch.jalu.configme.resource.PropertyReader;
import ch.jalu.configme.resource.YamlFileReader;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.command.help.HelpSection;
import fr.xephi.authme.util.ExceptionUtils;
import fr.xephi.authme.util.StringUtils;
import org.bukkit.configuration.file.YamlConfiguration;
import org.junit.BeforeClass;
import org.junit.Test;
@@ -81,8 +82,8 @@ public class YamlTextFileCheckerTest {
*/
private void checkFile(File file, String mandatoryKey, List<String> errors) {
try {
YamlConfiguration configuration = YamlConfiguration.loadConfiguration(file);
if (StringUtils.isEmpty(configuration.getString(mandatoryKey))) {
PropertyReader reader = new YamlFileReader(file);
if (StringUtils.isEmpty(reader.getString(mandatoryKey))) {
errors.add("Message for '" + mandatoryKey + "' is empty");
}
} catch (Exception e) {
@@ -1,13 +1,11 @@
package fr.xephi.authme.message.updater;
import ch.jalu.configme.configurationdata.ConfigurationData;
import ch.jalu.configme.properties.Property;
import ch.jalu.configme.resource.PropertyReader;
import ch.jalu.configme.resource.YamlFileReader;
import com.google.common.io.Files;
import fr.xephi.authme.ReflectionTestUtils;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.message.MessageKey;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
@@ -16,6 +14,7 @@ import org.junit.rules.TemporaryFolder;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
@@ -65,13 +64,13 @@ public class MessageUpdaterTest {
// then
assertThat(wasChanged, equalTo(true));
FileConfiguration configuration = YamlConfiguration.loadConfiguration(messagesFile);
PropertyReader reader = new YamlFileReader(messagesFile);
// Existing keys should not be overridden
assertThat(configuration.getString(MessageKey.LOGIN_SUCCESS.getKey()), equalTo("&cHere we have&bdefined some colors &dand some other &lthings"));
assertThat(configuration.getString(MessageKey.EMAIL_ALREADY_USED_ERROR.getKey()), equalTo(""));
assertThat(reader.getString(MessageKey.LOGIN_SUCCESS.getKey()), equalTo("&cHere we have&bdefined some colors &dand some other &lthings"));
assertThat(reader.getString(MessageKey.EMAIL_ALREADY_USED_ERROR.getKey()), equalTo(""));
// Check that new keys were added
assertThat(configuration.getString(MessageKey.SECOND.getKey()), equalTo("second"));
assertThat(configuration.getString(MessageKey.ERROR.getKey()), equalTo("&4An unexpected error occurred, please contact an administrator!"));
assertThat(reader.getString(MessageKey.SECOND.getKey()), equalTo("second"));
assertThat(reader.getString(MessageKey.ERROR.getKey()), equalTo("&4An unexpected error occurred, please contact an administrator!"));
}
@Test
@@ -85,18 +84,18 @@ public class MessageUpdaterTest {
// then
assertThat(wasChanged, equalTo(true));
FileConfiguration configuration = YamlConfiguration.loadConfiguration(messagesFile);
assertThat(configuration.getString(MessageKey.PASSWORD_MATCH_ERROR.getKey()),
PropertyReader reader = new YamlFileReader(messagesFile);
assertThat(reader.getString(MessageKey.PASSWORD_MATCH_ERROR.getKey()),
equalTo("Password error message"));
assertThat(configuration.getString(MessageKey.INVALID_NAME_CHARACTERS.getKey()),
assertThat(reader.getString(MessageKey.INVALID_NAME_CHARACTERS.getKey()),
equalTo("not valid username: Allowed chars are %valid_chars"));
assertThat(configuration.getString(MessageKey.INVALID_OLD_EMAIL.getKey()),
assertThat(reader.getString(MessageKey.INVALID_OLD_EMAIL.getKey()),
equalTo("Email (old) is not valid!!"));
assertThat(configuration.getString(MessageKey.CAPTCHA_WRONG_ERROR.getKey()),
assertThat(reader.getString(MessageKey.CAPTCHA_WRONG_ERROR.getKey()),
equalTo("The captcha code is %captcha_code for you"));
assertThat(configuration.getString(MessageKey.CAPTCHA_FOR_REGISTRATION_REQUIRED.getKey()),
assertThat(reader.getString(MessageKey.CAPTCHA_FOR_REGISTRATION_REQUIRED.getKey()),
equalTo("Now type /captcha %captcha_code"));
assertThat(configuration.getString(MessageKey.SECONDS.getKey()),
assertThat(reader.getString(MessageKey.SECONDS.getKey()),
equalTo("seconds in plural"));
}
@@ -111,10 +110,10 @@ public class MessageUpdaterTest {
// then
assertThat(wasChanged, equalTo(true));
FileConfiguration configuration = YamlConfiguration.loadConfiguration(messagesFile);
assertThat(configuration.getString(MessageKey.TWO_FACTOR_CREATE.getKey()), equalTo("Old 2fa create text"));
assertThat(configuration.getString(MessageKey.WRONG_PASSWORD.getKey()), equalTo("test2 - wrong password")); // from pre-5.5 key
assertThat(configuration.getString(MessageKey.SECOND.getKey()), equalTo("second")); // from messages_en.yml
PropertyReader reader = new YamlFileReader(messagesFile);
assertThat(reader.getString(MessageKey.TWO_FACTOR_CREATE.getKey()), equalTo("Old 2fa create text"));
assertThat(reader.getString(MessageKey.WRONG_PASSWORD.getKey()), equalTo("test2 - wrong password")); // from pre-5.5 key
assertThat(reader.getString(MessageKey.SECOND.getKey()), equalTo("second")); // from messages_en.yml
}
@Test
@@ -125,7 +124,7 @@ public class MessageUpdaterTest {
.collect(Collectors.toSet());
// when
Set<String> messageKeysFromConfigData = MessageUpdater.getConfigurationData().getProperties().stream()
Set<String> messageKeysFromConfigData = MessageUpdater.createConfigurationData().getProperties().stream()
.map(Property::getPath)
.collect(Collectors.toSet());
@@ -141,8 +140,7 @@ public class MessageUpdaterTest {
.collect(Collectors.toSet());
// when
Map<String, String[]> comments = ReflectionTestUtils.getFieldValue(
ConfigurationData.class, MessageUpdater.getConfigurationData(), "sectionComments");
Map<String, List<String>> comments = MessageUpdater.createConfigurationData().getAllComments();
// then
assertThat(comments.keySet(), equalTo(rootPaths));
@@ -1,8 +1,10 @@
package fr.xephi.authme.message.updater;
import ch.jalu.configme.configurationdata.ConfigurationData;
import ch.jalu.configme.configurationdata.ConfigurationDataBuilder;
import ch.jalu.configme.properties.Property;
import ch.jalu.configme.properties.StringProperty;
import ch.jalu.configme.resource.PropertyReader;
import com.google.common.io.Files;
import fr.xephi.authme.TestHelper;
import org.junit.Rule;
@@ -31,14 +33,15 @@ public class MigraterYamlFileResourceTest {
public void shouldReadChineseFile() {
// given
File file = TestHelper.getJarFile(CHINESE_MESSAGES_FILE);
// when
MigraterYamlFileResource resource = new MigraterYamlFileResource(file);
// when
PropertyReader reader = resource.createReader();
// then
assertThat(resource.getString("first"), equalTo("错误的密码"));
assertThat(resource.getString("second"), equalTo("为了验证您的身份,您需要将一个电子邮件地址与您的帐户绑定!"));
assertThat(resource.getString("third"), equalTo("您已经可以在当前会话中执行任何敏感命令!"));
assertThat(reader.getString("first"), equalTo("错误的密码"));
assertThat(reader.getString("second"), equalTo("为了验证您的身份,您需要将一个电子邮件地址与您的帐户绑定!"));
assertThat(reader.getString("third"), equalTo("您已经可以在当前会话中执行任何敏感命令!"));
}
@Test
@@ -47,24 +50,26 @@ public class MigraterYamlFileResourceTest {
File file = temporaryFolder.newFile();
Files.copy(TestHelper.getJarFile(CHINESE_MESSAGES_FILE), file);
MigraterYamlFileResource resource = new MigraterYamlFileResource(file);
ConfigurationData configurationData = buildConfigurationData();
configurationData.initializeValues(resource.createReader());
String newMessage = "您当前并没有任何邮箱与该账号绑定";
resource.setValue("third", newMessage);
configurationData.setValue(new StringProperty("third", ""), newMessage);
// when
resource.exportProperties(buildConfigurationData());
resource.exportProperties(configurationData);
// then
resource = new MigraterYamlFileResource(file);
assertThat(resource.getString("first"), equalTo("错误的密码"));
assertThat(resource.getString("second"), equalTo("为了验证您的身份,您需要将一个电子邮件地址与您的帐户绑定!"));
assertThat(resource.getString("third"), equalTo(newMessage));
PropertyReader reader = resource.createReader();
assertThat(reader.getString("first"), equalTo("错误的密码"));
assertThat(reader.getString("second"), equalTo("为了验证您的身份,您需要将一个电子邮件地址与您的帐户绑定!"));
assertThat(reader.getString("third"), equalTo(newMessage));
}
private static ConfigurationData buildConfigurationData() {
List<Property<?>> properties = Arrays.asList(
List<Property<String>> properties = Arrays.asList(
new StringProperty("first", "first"),
new StringProperty("second", "second"),
new StringProperty("third", "third"));
return new ConfigurationData(properties);
return ConfigurationDataBuilder.createConfiguration(properties);
}
}