Update configme (#1631)
* Upgrade to ConfigMe 1.0.1 * Use ConfigMe reader whenever possible, minor simplifications
This commit is contained in:
@@ -41,6 +41,7 @@ import static org.hamcrest.Matchers.not;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
@@ -91,7 +92,7 @@ public class AuthMeInitializationTest {
|
||||
public void shouldInitializeAllServices() {
|
||||
// given
|
||||
Settings settings =
|
||||
new Settings(dataFolder, mock(PropertyResource.class), null, buildConfigurationData());
|
||||
new Settings(dataFolder, mock(PropertyResource.class, RETURNS_DEEP_STUBS), null, buildConfigurationData());
|
||||
|
||||
Injector injector = new InjectorBuilder()
|
||||
.addDefaultHandlers("fr.xephi.authme")
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package fr.xephi.authme.command.help;
|
||||
|
||||
import ch.jalu.configme.resource.PropertyReader;
|
||||
import ch.jalu.configme.resource.YamlFileReader;
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import fr.xephi.authme.command.CommandDescription;
|
||||
import fr.xephi.authme.command.CommandInitializer;
|
||||
@@ -61,16 +63,16 @@ public class HelpMessagesConsistencyTest {
|
||||
@Test
|
||||
public void shouldHaveEntryForEachHelpMessageKey() {
|
||||
// given
|
||||
FileConfiguration configuration = YamlConfiguration.loadConfiguration(DEFAULT_MESSAGES_FILE);
|
||||
PropertyReader reader = new YamlFileReader(DEFAULT_MESSAGES_FILE);
|
||||
|
||||
// when / then
|
||||
for (HelpMessage message : HelpMessage.values()) {
|
||||
assertThat("Default configuration has entry for message '" + message + "'",
|
||||
configuration.contains(message.getKey()), equalTo(true));
|
||||
assertThat("Default configuration should have entry for message '" + message + "'",
|
||||
reader.contains(message.getKey()), equalTo(true));
|
||||
}
|
||||
for (HelpSection section : HelpSection.values()) {
|
||||
assertThat("Default configuration has entry for section '" + section + "'",
|
||||
configuration.contains(section.getKey()), equalTo(true));
|
||||
assertThat("Default configuration should have entry for section '" + section + "'",
|
||||
reader.contains(section.getKey()), equalTo(true));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -57,6 +57,7 @@ import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
||||
import static com.google.common.collect.Sets.newHashSet;
|
||||
import static fr.xephi.authme.listener.EventCancelVerifier.withServiceMock;
|
||||
import static fr.xephi.authme.service.BukkitServiceTestHelper.setBukkitServiceToScheduleSyncDelayedTaskWithDelay;
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
@@ -206,8 +207,7 @@ public class PlayerListenerTest {
|
||||
public void shouldNotStopAllowedCommand() {
|
||||
// given
|
||||
given(settings.getProperty(HooksSettings.USE_ESSENTIALS_MOTD)).willReturn(true);
|
||||
given(settings.getProperty(RestrictionSettings.ALLOW_COMMANDS))
|
||||
.willReturn(Arrays.asList("/plugins", "/mail", "/msg"));
|
||||
given(settings.getProperty(RestrictionSettings.ALLOW_COMMANDS)).willReturn(newHashSet("/plugins", "/mail", "/msg"));
|
||||
PlayerCommandPreprocessEvent event = mockCommandEvent("/Mail send test Test");
|
||||
|
||||
// when
|
||||
@@ -222,7 +222,7 @@ public class PlayerListenerTest {
|
||||
public void shouldNotCancelEventForAuthenticatedPlayer() {
|
||||
// given
|
||||
given(settings.getProperty(HooksSettings.USE_ESSENTIALS_MOTD)).willReturn(false);
|
||||
given(settings.getProperty(RestrictionSettings.ALLOW_COMMANDS)).willReturn(Collections.emptyList());
|
||||
given(settings.getProperty(RestrictionSettings.ALLOW_COMMANDS)).willReturn(Collections.emptySet());
|
||||
Player player = playerWithMockedServer();
|
||||
// PlayerCommandPreprocessEvent#getPlayer is final, so create a spy instead of a mock
|
||||
PlayerCommandPreprocessEvent event = spy(new PlayerCommandPreprocessEvent(player, "/hub"));
|
||||
@@ -243,7 +243,7 @@ public class PlayerListenerTest {
|
||||
public void shouldCancelCommandEvent() {
|
||||
// given
|
||||
given(settings.getProperty(HooksSettings.USE_ESSENTIALS_MOTD)).willReturn(false);
|
||||
given(settings.getProperty(RestrictionSettings.ALLOW_COMMANDS)).willReturn(Arrays.asList("/spawn", "/help"));
|
||||
given(settings.getProperty(RestrictionSettings.ALLOW_COMMANDS)).willReturn(newHashSet("/spawn", "/help"));
|
||||
Player player = playerWithMockedServer();
|
||||
PlayerCommandPreprocessEvent event = spy(new PlayerCommandPreprocessEvent(player, "/hub"));
|
||||
given(listenerService.shouldCancelEvent(player)).willReturn(true);
|
||||
@@ -262,7 +262,7 @@ public class PlayerListenerTest {
|
||||
public void shouldCancelFastCommandEvent() {
|
||||
// given
|
||||
given(settings.getProperty(HooksSettings.USE_ESSENTIALS_MOTD)).willReturn(false);
|
||||
given(settings.getProperty(RestrictionSettings.ALLOW_COMMANDS)).willReturn(Arrays.asList("/spawn", "/help"));
|
||||
given(settings.getProperty(RestrictionSettings.ALLOW_COMMANDS)).willReturn(newHashSet("/spawn", "/help"));
|
||||
Player player = playerWithMockedServer();
|
||||
PlayerCommandPreprocessEvent event = spy(new PlayerCommandPreprocessEvent(player, "/hub"));
|
||||
given(quickCommandsProtectionManager.isAllowed(player.getName())).willReturn(false);
|
||||
|
||||
@@ -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 <hings"));
|
||||
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 <hings"));
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,10 +22,10 @@ import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mock;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static com.google.common.collect.Sets.newHashSet;
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
@@ -57,10 +57,9 @@ public class ValidationServiceTest {
|
||||
given(settings.getProperty(RestrictionSettings.ALLOWED_PASSWORD_REGEX)).willReturn("[a-zA-Z]+");
|
||||
given(settings.getProperty(SecuritySettings.MIN_PASSWORD_LENGTH)).willReturn(3);
|
||||
given(settings.getProperty(SecuritySettings.MAX_PASSWORD_LENGTH)).willReturn(20);
|
||||
given(settings.getProperty(SecuritySettings.UNSAFE_PASSWORDS))
|
||||
.willReturn(asList("unsafe", "other-unsafe"));
|
||||
given(settings.getProperty(SecuritySettings.UNSAFE_PASSWORDS)).willReturn(newHashSet("unsafe", "other-unsafe"));
|
||||
given(settings.getProperty(EmailSettings.MAX_REG_PER_EMAIL)).willReturn(3);
|
||||
given(settings.getProperty(RestrictionSettings.UNRESTRICTED_NAMES)).willReturn(asList("name01", "npc"));
|
||||
given(settings.getProperty(RestrictionSettings.UNRESTRICTED_NAMES)).willReturn(newHashSet("name01", "npc"));
|
||||
given(settings.getProperty(RestrictionSettings.ENABLE_RESTRICTED_USERS)).willReturn(false);
|
||||
}
|
||||
|
||||
@@ -261,7 +260,7 @@ public class ValidationServiceTest {
|
||||
assertThat(validationService.isUnrestricted("NAME01"), equalTo(true));
|
||||
|
||||
// Check reloading
|
||||
given(settings.getProperty(RestrictionSettings.UNRESTRICTED_NAMES)).willReturn(asList("new", "names"));
|
||||
given(settings.getProperty(RestrictionSettings.UNRESTRICTED_NAMES)).willReturn(newHashSet("new", "names"));
|
||||
validationService.reload();
|
||||
assertThat(validationService.isUnrestricted("npc"), equalTo(false));
|
||||
assertThat(validationService.isUnrestricted("New"), equalTo(true));
|
||||
@@ -350,7 +349,7 @@ public class ValidationServiceTest {
|
||||
// given
|
||||
given(settings.getProperty(RestrictionSettings.ENABLE_RESTRICTED_USERS)).willReturn(true);
|
||||
given(settings.getProperty(RestrictionSettings.RESTRICTED_USERS))
|
||||
.willReturn(Arrays.asList("Bobby;127.0.0.4", "Tamara;32.24.16.8", "Gabriel;regex:93\\.23\\.44\\..*", "emanuel;94.65.24.*", "imyourisp;*.yourisp.net"));
|
||||
.willReturn(newHashSet("Bobby;127.0.0.4", "Tamara;32.24.16.8", "Gabriel;regex:93\\.23\\.44\\..*", "emanuel;94.65.24.*", "imyourisp;*.yourisp.net"));
|
||||
validationService.reload();
|
||||
|
||||
Player bobby = mockPlayer("bobby", "127.0.0.4");
|
||||
@@ -389,7 +388,7 @@ public class ValidationServiceTest {
|
||||
Logger logger = TestHelper.setupLogger();
|
||||
given(settings.getProperty(RestrictionSettings.ENABLE_RESTRICTED_USERS)).willReturn(true);
|
||||
given(settings.getProperty(RestrictionSettings.RESTRICTED_USERS))
|
||||
.willReturn(Arrays.asList("Bobby;127.0.0.4", "Tamara;"));
|
||||
.willReturn(newHashSet("Bobby;127.0.0.4", "Tamara;"));
|
||||
|
||||
// when
|
||||
validationService.reload();
|
||||
|
||||
@@ -26,7 +26,7 @@ public class YamlFileResourceProviderTest {
|
||||
YamlFileResource resource = YamlFileResourceProvider.loadFromFile(yamlFile);
|
||||
|
||||
// then
|
||||
assertThat(resource.getString("test.jkl"), equalTo("Test test"));
|
||||
assertThat(resource.createReader().getString("test.jkl"), equalTo("Test test"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -36,7 +36,7 @@ public class YamlFileResourceProviderTest {
|
||||
|
||||
// when
|
||||
try {
|
||||
YamlFileResourceProvider.loadFromFile(yamlFile);
|
||||
YamlFileResourceProvider.loadFromFile(yamlFile).createReader();
|
||||
|
||||
// then
|
||||
fail("Expected exception to be thrown");
|
||||
|
||||
@@ -1,30 +1,22 @@
|
||||
package fr.xephi.authme.settings;
|
||||
|
||||
import ch.jalu.configme.SectionComments;
|
||||
import ch.jalu.configme.SettingsHolder;
|
||||
import ch.jalu.configme.configurationdata.ConfigurationData;
|
||||
import ch.jalu.configme.properties.EnumProperty;
|
||||
import ch.jalu.configme.properties.Property;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import fr.xephi.authme.ClassCollector;
|
||||
import fr.xephi.authme.ReflectionTestUtils;
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import fr.xephi.authme.settings.properties.AuthMeSettingsRetriever;
|
||||
import fr.xephi.authme.settings.properties.SecuritySettings;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
import static fr.xephi.authme.ReflectionTestUtils.getFieldValue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
@@ -58,7 +50,7 @@ public class SettingsConsistencyTest {
|
||||
|
||||
// when / then
|
||||
for (Property<?> property : properties) {
|
||||
if (configurationData.getCommentsForSection(property.getPath()).length == 0) {
|
||||
if (configurationData.getCommentsForSection(property.getPath()).isEmpty()) {
|
||||
fail("No comment defined for " + property);
|
||||
}
|
||||
}
|
||||
@@ -67,83 +59,27 @@ public class SettingsConsistencyTest {
|
||||
@Test
|
||||
public void shouldNotHaveVeryLongCommentLines() {
|
||||
// given
|
||||
List<Property<?>> properties = configurationData.getProperties();
|
||||
List<Property<?>> badProperties = new ArrayList<>();
|
||||
Map<String, List<String>> commentEntries = configurationData.getAllComments();
|
||||
List<String> badPaths = new ArrayList<>(0);
|
||||
|
||||
// when
|
||||
for (Property<?> property : properties) {
|
||||
for (String comment : configurationData.getCommentsForSection(property.getPath())) {
|
||||
for (Map.Entry<String, List<String>> commentEntry : commentEntries.entrySet()) {
|
||||
for (String comment : commentEntry.getValue()) {
|
||||
if (comment.length() > MAX_COMMENT_LENGTH) {
|
||||
badProperties.add(property);
|
||||
badPaths.add(commentEntry.getKey());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// then
|
||||
if (!badProperties.isEmpty()) {
|
||||
if (!badPaths.isEmpty()) {
|
||||
fail("Comment lines should not be longer than " + MAX_COMMENT_LENGTH + " chars, "
|
||||
+ "but found too long comments for:\n- "
|
||||
+ badProperties.stream().map(Property::getPath).collect(Collectors.joining("\n- ")));
|
||||
+ "but found too long comments for paths:\n- "
|
||||
+ String.join("\n- ", badPaths));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldNotHaveVeryLongSectionCommentLines() {
|
||||
// given
|
||||
List<Method> sectionCommentMethods = getSectionCommentMethods();
|
||||
Set<Method> badMethods = new HashSet<>();
|
||||
|
||||
// when
|
||||
for (Method method : sectionCommentMethods) {
|
||||
boolean hasTooLongLine = getSectionComments(method).stream()
|
||||
.anyMatch(line -> line.length() > MAX_COMMENT_LENGTH);
|
||||
if (hasTooLongLine) {
|
||||
badMethods.add(method);
|
||||
}
|
||||
}
|
||||
|
||||
// then
|
||||
if (!badMethods.isEmpty()) {
|
||||
String methodList = badMethods.stream()
|
||||
.map(m -> m.getName() + " in " + m.getDeclaringClass().getSimpleName())
|
||||
.collect(Collectors.joining("\n- "));
|
||||
fail("Found SectionComments methods with too long comments:\n- " + methodList);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all {@link SectionComments} methods from {@link SettingsHolder} implementations.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private List<Method> getSectionCommentMethods() {
|
||||
// Find all SettingsHolder classes
|
||||
List<Class<? extends SettingsHolder>> settingsClasses =
|
||||
new ClassCollector(TestHelper.SOURCES_FOLDER, TestHelper.PROJECT_ROOT + "settings/properties/")
|
||||
.collectClasses(SettingsHolder.class);
|
||||
checkArgument(!settingsClasses.isEmpty(), "Could not find any SettingsHolder classes");
|
||||
|
||||
// Find all @SectionComments methods in these classes
|
||||
return settingsClasses.stream()
|
||||
.map(Class::getDeclaredMethods)
|
||||
.flatMap(Arrays::stream)
|
||||
.filter(method -> method.isAnnotationPresent(SectionComments.class))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all comments returned from the given SectionComments method, flattened into one list.
|
||||
*
|
||||
* @param sectionCommentsMethod the method whose comments should be retrieved
|
||||
* @return flattened list of all comments provided by the method
|
||||
*/
|
||||
private static List<String> getSectionComments(Method sectionCommentsMethod) {
|
||||
// @SectionComments methods are static
|
||||
Map<String, String[]> comments = ReflectionTestUtils.invokeMethod(sectionCommentsMethod, null);
|
||||
return comments.values().stream()
|
||||
.flatMap(Arrays::stream)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks that enum properties have all possible enum values listed in their comment
|
||||
|
||||
@@ -38,7 +38,7 @@ public class SettingsIntegrationTest {
|
||||
private static final String INCOMPLETE_FILE = TestHelper.PROJECT_ROOT + "settings/config-incomplete-sample.yml";
|
||||
|
||||
private static ConfigurationData CONFIG_DATA =
|
||||
ConfigurationDataBuilder.collectData(TestConfiguration.class);
|
||||
ConfigurationDataBuilder.createConfiguration(TestConfiguration.class);
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package fr.xephi.authme.settings;
|
||||
|
||||
import ch.jalu.configme.configurationdata.ConfigurationData;
|
||||
import ch.jalu.configme.properties.Property;
|
||||
import ch.jalu.configme.resource.PropertyReader;
|
||||
import ch.jalu.configme.resource.PropertyResource;
|
||||
import ch.jalu.configme.resource.YamlFileResource;
|
||||
import com.google.common.io.Files;
|
||||
@@ -108,7 +108,7 @@ public class SettingsMigrationServiceTest {
|
||||
SettingsMigrationService migrationService = new SettingsMigrationService(dataFolder);
|
||||
|
||||
// when
|
||||
migrationService.performMigrations(resource, AuthMeSettingsRetriever.buildConfigurationData().getProperties());
|
||||
migrationService.performMigrations(resource.createReader(), AuthMeSettingsRetriever.buildConfigurationData());
|
||||
|
||||
// then
|
||||
assertThat(migrationService.hasOldOtherAccountsCommand(), equalTo(true));
|
||||
@@ -146,8 +146,8 @@ public class SettingsMigrationServiceTest {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean performMigrations(PropertyResource resource, List<Property<?>> properties) {
|
||||
boolean result = super.performMigrations(resource, properties);
|
||||
protected boolean performMigrations(PropertyReader reader, ConfigurationData configurationData) {
|
||||
boolean result = super.performMigrations(reader, configurationData);
|
||||
returnedValues.add(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ import java.nio.file.Files;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
@@ -25,7 +26,7 @@ import static org.mockito.Mockito.mock;
|
||||
public class SettingsTest {
|
||||
|
||||
private static final ConfigurationData CONFIG_DATA =
|
||||
ConfigurationDataBuilder.collectData(TestConfiguration.class);
|
||||
ConfigurationDataBuilder.createConfiguration(TestConfiguration.class);
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
@@ -49,7 +50,7 @@ public class SettingsTest {
|
||||
createFile(emailFile);
|
||||
Files.write(emailFile.toPath(), emailMessage.getBytes());
|
||||
|
||||
PropertyResource resource = mock(PropertyResource.class);
|
||||
PropertyResource resource = mock(PropertyResource.class, RETURNS_DEEP_STUBS);
|
||||
Settings settings = new Settings(testPluginFolder, resource, null, CONFIG_DATA);
|
||||
|
||||
// when
|
||||
@@ -67,7 +68,7 @@ public class SettingsTest {
|
||||
createFile(emailFile);
|
||||
Files.write(emailFile.toPath(), emailMessage.getBytes());
|
||||
|
||||
PropertyResource resource = mock(PropertyResource.class);
|
||||
PropertyResource resource = mock(PropertyResource.class, RETURNS_DEEP_STUBS);
|
||||
Settings settings = new Settings(testPluginFolder, resource, null, CONFIG_DATA);
|
||||
|
||||
// when
|
||||
@@ -85,7 +86,7 @@ public class SettingsTest {
|
||||
createFile(emailFile);
|
||||
Files.write(emailFile.toPath(), emailMessage.getBytes());
|
||||
|
||||
PropertyResource resource = mock(PropertyResource.class);
|
||||
PropertyResource resource = mock(PropertyResource.class, RETURNS_DEEP_STUBS);
|
||||
Settings settings = new Settings(testPluginFolder, resource, null, CONFIG_DATA);
|
||||
|
||||
// when
|
||||
|
||||
+11
-9
@@ -1,7 +1,8 @@
|
||||
package fr.xephi.authme.settings.commandconfig;
|
||||
|
||||
import ch.jalu.configme.beanmapper.BeanDescriptionFactory;
|
||||
import ch.jalu.configme.beanmapper.BeanPropertyDescription;
|
||||
import ch.jalu.configme.beanmapper.propertydescription.BeanDescriptionFactoryImpl;
|
||||
import ch.jalu.configme.beanmapper.propertydescription.BeanPropertyDescription;
|
||||
import ch.jalu.configme.configurationdata.ConfigurationData;
|
||||
import ch.jalu.configme.configurationdata.ConfigurationDataBuilder;
|
||||
import ch.jalu.configme.resource.PropertyResource;
|
||||
import ch.jalu.configme.resource.YamlFileResource;
|
||||
@@ -52,7 +53,7 @@ public class CommandMigrationServiceTest {
|
||||
|
||||
// when
|
||||
boolean result = commandMigrationService.checkAndMigrate(
|
||||
resource, ConfigurationDataBuilder.collectData(CommandSettingsHolder.class).getProperties());
|
||||
resource.createReader(), ConfigurationDataBuilder.createConfiguration(CommandSettingsHolder.class));
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(true));
|
||||
@@ -66,7 +67,7 @@ public class CommandMigrationServiceTest {
|
||||
|
||||
// when
|
||||
boolean result = commandMigrationService.checkAndMigrate(
|
||||
resource, ConfigurationDataBuilder.collectData(CommandSettingsHolder.class).getProperties());
|
||||
resource.createReader(), ConfigurationDataBuilder.createConfiguration(CommandSettingsHolder.class));
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(true));
|
||||
@@ -80,7 +81,7 @@ public class CommandMigrationServiceTest {
|
||||
|
||||
// when
|
||||
boolean result = commandMigrationService.checkAndMigrate(
|
||||
resource, ConfigurationDataBuilder.collectData(CommandSettingsHolder.class).getProperties());
|
||||
resource.createReader(), ConfigurationDataBuilder.createConfiguration(CommandSettingsHolder.class));
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(false));
|
||||
@@ -93,8 +94,8 @@ public class CommandMigrationServiceTest {
|
||||
@Test
|
||||
public void shouldHaveAllPropertiesFromCommandConfig() {
|
||||
// given
|
||||
String[] properties = new BeanDescriptionFactory()
|
||||
.collectWritableFields(CommandConfig.class)
|
||||
String[] properties = new BeanDescriptionFactoryImpl()
|
||||
.getAllProperties(CommandConfig.class)
|
||||
.stream()
|
||||
.map(BeanPropertyDescription::getName)
|
||||
.toArray(String[]::new);
|
||||
@@ -112,13 +113,14 @@ public class CommandMigrationServiceTest {
|
||||
given(settingsMigrationService.getOldOtherAccountsCommandThreshold()).willReturn(3);
|
||||
File commandFile = TestHelper.getJarFile(TestHelper.PROJECT_ROOT + "settings/commandconfig/commands.complete.yml");
|
||||
PropertyResource resource = new YamlFileResource(commandFile);
|
||||
ConfigurationData configurationData = ConfigurationDataBuilder.createConfiguration(CommandSettingsHolder.class);
|
||||
|
||||
// when
|
||||
commandMigrationService.checkAndMigrate(
|
||||
resource, ConfigurationDataBuilder.collectData(CommandSettingsHolder.class).getProperties());
|
||||
resource.createReader(), configurationData);
|
||||
|
||||
// then
|
||||
Map<String, OnLoginCommand> onLoginCommands = CommandSettingsHolder.COMMANDS.getValue(resource).getOnLogin();
|
||||
Map<String, OnLoginCommand> onLoginCommands = configurationData.getValue(CommandSettingsHolder.COMMANDS).getOnLogin();
|
||||
assertThat(onLoginCommands, aMapWithSize(6)); // 5 in the file + the newly migrated on
|
||||
OnLoginCommand newCommand = getUnknownOnLoginCommand(onLoginCommands);
|
||||
assertThat(newCommand.getCommand(), equalTo("helpop %p (%ip) has other accounts!"));
|
||||
|
||||
@@ -42,7 +42,7 @@ public class CommandYmlConsistencyTest {
|
||||
|
||||
// when
|
||||
boolean result = commandMigrationService.checkAndMigrate(
|
||||
resource, ConfigurationDataBuilder.collectData(CommandSettingsHolder.class).getProperties());
|
||||
resource.createReader(), ConfigurationDataBuilder.createConfiguration(CommandSettingsHolder.class));
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(false));
|
||||
|
||||
Reference in New Issue
Block a user