From 36dfab636a4614a3c991b932e6b3dd33af7383e1 Mon Sep 17 00:00:00 2001 From: ljacqu Date: Wed, 31 Aug 2016 22:19:27 +0200 Subject: [PATCH] #927 Update code to recent changes in ConfigMe --- .../authme/initialization/Initializer.java | 2 +- .../fr/xephi/authme/settings/Settings.java | 8 +++---- .../properties/AuthMeSettingsRetriever.java | 4 +--- .../authme/AuthMeInitializationTest.java | 3 ++- .../settings/SettingsIntegrationTest.java | 17 +++++++------- .../xephi/authme/settings/SettingsTest.java | 22 ++++++++++-------- .../properties/TestConfiguration.java | 23 ------------------- 7 files changed, 30 insertions(+), 49 deletions(-) diff --git a/src/main/java/fr/xephi/authme/initialization/Initializer.java b/src/main/java/fr/xephi/authme/initialization/Initializer.java index 16612e1e5..13f7ae2be 100644 --- a/src/main/java/fr/xephi/authme/initialization/Initializer.java +++ b/src/main/java/fr/xephi/authme/initialization/Initializer.java @@ -67,7 +67,7 @@ public class Initializer { List knownProperties = AuthMeSettingsRetriever.getAllPropertyFields(); if (FileUtils.copyFileFromResource(configFile, "config.yml")) { - return new Settings(authMe.getDataFolder(), knownProperties, resource, migrationService); + return new Settings(authMe.getDataFolder(), resource, migrationService, knownProperties); } throw new Exception("Could not copy config.yml from JAR to plugin folder"); } diff --git a/src/main/java/fr/xephi/authme/settings/Settings.java b/src/main/java/fr/xephi/authme/settings/Settings.java index b00b38bab..46f47052b 100644 --- a/src/main/java/fr/xephi/authme/settings/Settings.java +++ b/src/main/java/fr/xephi/authme/settings/Settings.java @@ -33,13 +33,13 @@ public class Settings extends SettingsManager { * Constructor. * * @param pluginFolder the AuthMe plugin folder - * @param knownProperties collection of all available settings * @param resource the property resource to read and write properties to * @param migrationService migration service to check the settings file with + * @param knownProperties collection of all available settings */ - public Settings(File pluginFolder, List knownProperties, PropertyResource resource, - MigrationService migrationService) { - super(knownProperties, resource, migrationService); + public Settings(File pluginFolder, PropertyResource resource, MigrationService migrationService, + List knownProperties) { + super(resource, migrationService, knownProperties); this.pluginFolder = pluginFolder; } diff --git a/src/main/java/fr/xephi/authme/settings/properties/AuthMeSettingsRetriever.java b/src/main/java/fr/xephi/authme/settings/properties/AuthMeSettingsRetriever.java index c76b326ee..3fb8385fc 100644 --- a/src/main/java/fr/xephi/authme/settings/properties/AuthMeSettingsRetriever.java +++ b/src/main/java/fr/xephi/authme/settings/properties/AuthMeSettingsRetriever.java @@ -22,12 +22,10 @@ public final class AuthMeSettingsRetriever { * @return list of all known properties */ public static List getAllPropertyFields() { - SettingsFieldRetriever retriever = new SettingsFieldRetriever( + return SettingsFieldRetriever.getAllProperties( DatabaseSettings.class, ConverterSettings.class, PluginSettings.class, RestrictionSettings.class, EmailSettings.class, HooksSettings.class, ProtectionSettings.class, PurgeSettings.class, SecuritySettings.class, RegistrationSettings.class, BackupSettings.class); - - return retriever.getAllPropertyFields(); } } diff --git a/src/test/java/fr/xephi/authme/AuthMeInitializationTest.java b/src/test/java/fr/xephi/authme/AuthMeInitializationTest.java index f00f234a9..d20424a06 100644 --- a/src/test/java/fr/xephi/authme/AuthMeInitializationTest.java +++ b/src/test/java/fr/xephi/authme/AuthMeInitializationTest.java @@ -94,7 +94,8 @@ public class AuthMeInitializationTest { @Test public void shouldInitializeAllServices() { // given - Settings settings = new Settings(dataFolder, getAllPropertyFields(), mock(PropertyResource.class), alwaysFulfilled()); + Settings settings = + new Settings(dataFolder, mock(PropertyResource.class), alwaysFulfilled(), getAllPropertyFields()); Injector injector = new InjectorBuilder().addDefaultHandlers("fr.xephi.authme").create(); injector.provide(DataFolder.class, dataFolder); diff --git a/src/test/java/fr/xephi/authme/settings/SettingsIntegrationTest.java b/src/test/java/fr/xephi/authme/settings/SettingsIntegrationTest.java index f8f87ac02..4e95b37ef 100644 --- a/src/test/java/fr/xephi/authme/settings/SettingsIntegrationTest.java +++ b/src/test/java/fr/xephi/authme/settings/SettingsIntegrationTest.java @@ -3,6 +3,7 @@ package fr.xephi.authme.settings; import com.github.authme.configme.migration.PlainMigrationService; import com.github.authme.configme.properties.Property; import com.github.authme.configme.propertymap.PropertyEntry; +import com.github.authme.configme.propertymap.SettingsFieldRetriever; import com.github.authme.configme.resource.PropertyResource; import com.github.authme.configme.resource.YamlFileResource; import com.google.common.collect.ImmutableMap; @@ -36,7 +37,7 @@ public class SettingsIntegrationTest { /** File name of the sample config missing certain {@link TestConfiguration} values. */ private static final String INCOMPLETE_FILE = TestHelper.PROJECT_ROOT + "settings/config-incomplete-sample.yml"; - private static List propertyMap = TestConfiguration.generatePropertyMap(); + private static List knownProperties = SettingsFieldRetriever.getAllProperties(TestConfiguration.class); @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder(); @@ -61,8 +62,8 @@ public class SettingsIntegrationTest { File newFile = temporaryFolder.newFile(); // when / then - Settings settings = new Settings(testPluginFolder, propertyMap, resource, - new PlainMigrationService()); + Settings settings = new Settings(testPluginFolder, resource, + new PlainMigrationService(), knownProperties); Map, Object> expectedValues = ImmutableMap., Object>builder() .put(TestConfiguration.DURATION_IN_SECONDS, 22) .put(TestConfiguration.SYSTEM_NAME, "Custom sys name") @@ -88,14 +89,14 @@ public class SettingsIntegrationTest { File file = copyFileFromResources(INCOMPLETE_FILE); PropertyResource resource = new YamlFileResource(file); // Expectation: File is rewritten to since it does not have all configurations - new Settings(testPluginFolder, propertyMap, resource, new PlainMigrationService()); + new Settings(testPluginFolder, resource, new PlainMigrationService(), knownProperties); // Load the settings again -> checks that what we wrote can be loaded again resource = new YamlFileResource(file); // then - Settings settings = new Settings(testPluginFolder, propertyMap, resource, - new PlainMigrationService()); + Settings settings = new Settings(testPluginFolder, resource, + new PlainMigrationService(), knownProperties); Map, Object> expectedValues = ImmutableMap., Object>builder() .put(TestConfiguration.DURATION_IN_SECONDS, 22) .put(TestConfiguration.SYSTEM_NAME, "[TestDefaultValue]") @@ -118,8 +119,8 @@ public class SettingsIntegrationTest { public void shouldReloadSettings() throws IOException { // given PropertyResource resource = new YamlFileResource(temporaryFolder.newFile()); - Settings settings = new Settings(testPluginFolder, propertyMap, resource, - TestSettingsMigrationServices.alwaysFulfilled()); + Settings settings = new Settings(testPluginFolder, resource, + TestSettingsMigrationServices.alwaysFulfilled(), knownProperties); // when assertThat(settings.getProperty(TestConfiguration.RATIO_ORDER), diff --git a/src/test/java/fr/xephi/authme/settings/SettingsTest.java b/src/test/java/fr/xephi/authme/settings/SettingsTest.java index ce17ebd58..7c9acf70e 100644 --- a/src/test/java/fr/xephi/authme/settings/SettingsTest.java +++ b/src/test/java/fr/xephi/authme/settings/SettingsTest.java @@ -3,6 +3,7 @@ package fr.xephi.authme.settings; import com.github.authme.configme.migration.PlainMigrationService; import com.github.authme.configme.properties.Property; import com.github.authme.configme.propertymap.PropertyEntry; +import com.github.authme.configme.propertymap.SettingsFieldRetriever; import com.github.authme.configme.resource.PropertyResource; import fr.xephi.authme.TestHelper; import fr.xephi.authme.settings.properties.RegistrationSettings; @@ -38,6 +39,9 @@ import static org.mockito.Mockito.when; * Unit tests for {@link Settings}. */ public class SettingsTest { + + private final List knownProperties = + SettingsFieldRetriever.getAllProperties(TestConfiguration.class); @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder(); @@ -58,7 +62,7 @@ public class SettingsTest { // given PropertyResource resource = mock(PropertyResource.class); List knownProperties = Collections.emptyList(); - Settings settings = new Settings(testPluginFolder, knownProperties, resource, new PlainMigrationService()); + Settings settings = new Settings(testPluginFolder, resource, new PlainMigrationService(), knownProperties); // when String defaultFile = settings.getDefaultMessagesFile(); @@ -81,8 +85,8 @@ public class SettingsTest { PropertyResource resource = mock(PropertyResource.class); given(resource.contains(anyString())).willReturn(true); setReturnValue(resource, MESSAGES_LANGUAGE, languageCode); - Settings settings = new Settings(testPluginFolder, TestConfiguration.generatePropertyMap(), - resource, TestSettingsMigrationServices.alwaysFulfilled()); + Settings settings = new Settings(testPluginFolder, resource, + TestSettingsMigrationServices.alwaysFulfilled(), knownProperties); // when File messagesFile = settings.getMessagesFile(); @@ -98,8 +102,8 @@ public class SettingsTest { PropertyResource resource = mock(PropertyResource.class); given(resource.contains(anyString())).willReturn(true); setReturnValue(resource, MESSAGES_LANGUAGE, "doesntexist"); - Settings settings = new Settings(testPluginFolder, TestConfiguration.generatePropertyMap(), - resource, TestSettingsMigrationServices.alwaysFulfilled()); + Settings settings = new Settings(testPluginFolder, resource, + TestSettingsMigrationServices.alwaysFulfilled(), knownProperties); // when File messagesFile = settings.getMessagesFile(); @@ -119,8 +123,8 @@ public class SettingsTest { PropertyResource resource = mock(PropertyResource.class); setReturnValue(resource, RegistrationSettings.USE_WELCOME_MESSAGE, true); - Settings settings = new Settings(testPluginFolder, TestConfiguration.generatePropertyMap(), - resource, TestSettingsMigrationServices.alwaysFulfilled()); + Settings settings = new Settings(testPluginFolder, resource, + TestSettingsMigrationServices.alwaysFulfilled(), knownProperties); // when List result = settings.getWelcomeMessage(); @@ -140,8 +144,8 @@ public class SettingsTest { Files.write(emailFile.toPath(), emailMessage.getBytes()); PropertyResource resource = mock(PropertyResource.class); - Settings settings = new Settings(testPluginFolder, TestConfiguration.generatePropertyMap(), - resource, TestSettingsMigrationServices.alwaysFulfilled()); + Settings settings = new Settings(testPluginFolder, resource, + TestSettingsMigrationServices.alwaysFulfilled(), knownProperties); // when String result = settings.getEmailMessage(); diff --git a/src/test/java/fr/xephi/authme/settings/properties/TestConfiguration.java b/src/test/java/fr/xephi/authme/settings/properties/TestConfiguration.java index 7019dc2f7..043a597bb 100644 --- a/src/test/java/fr/xephi/authme/settings/properties/TestConfiguration.java +++ b/src/test/java/fr/xephi/authme/settings/properties/TestConfiguration.java @@ -2,11 +2,7 @@ package fr.xephi.authme.settings.properties; import com.github.authme.configme.SettingsHolder; import com.github.authme.configme.properties.Property; -import com.github.authme.configme.propertymap.PropertyEntry; -import fr.xephi.authme.ReflectionTestUtils; -import java.lang.reflect.Field; -import java.util.ArrayList; import java.util.List; import static com.github.authme.configme.properties.PropertyInitializer.newListProperty; @@ -50,23 +46,4 @@ public final class TestConfiguration implements SettingsHolder { private TestConfiguration() { } - - /** - * Generate a property map with all properties in {@link TestConfiguration}. - * - * @return The generated property map - */ - public static List generatePropertyMap() { - List properties = new ArrayList<>(); - for (Field field : TestConfiguration.class.getDeclaredFields()) { - Object fieldValue = ReflectionTestUtils.getFieldValue(TestConfiguration.class, null, field.getName()); - if (fieldValue instanceof Property) { - Property property = (Property) fieldValue; - String[] comments = new String[]{"Comment for '" + property.getPath() + "'"}; - properties.add(new PropertyEntry(property, comments)); - } - } - return properties; - } - }