#927 Update code to recent changes in ConfigMe

This commit is contained in:
ljacqu 2016-08-31 22:19:27 +02:00
parent c7bb7b460e
commit 36dfab636a
7 changed files with 30 additions and 49 deletions

View File

@ -67,7 +67,7 @@ public class Initializer {
List<PropertyEntry> 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");
}

View File

@ -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<PropertyEntry> knownProperties, PropertyResource resource,
MigrationService migrationService) {
super(knownProperties, resource, migrationService);
public Settings(File pluginFolder, PropertyResource resource, MigrationService migrationService,
List<PropertyEntry> knownProperties) {
super(resource, migrationService, knownProperties);
this.pluginFolder = pluginFolder;
}

View File

@ -22,12 +22,10 @@ public final class AuthMeSettingsRetriever {
* @return list of all known properties
*/
public static List<PropertyEntry> 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();
}
}

View File

@ -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);

View File

@ -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<PropertyEntry> propertyMap = TestConfiguration.generatePropertyMap();
private static List<PropertyEntry> 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<Property<?>, Object> expectedValues = ImmutableMap.<Property<?>, 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<Property<?>, Object> expectedValues = ImmutableMap.<Property<?>, 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),

View File

@ -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<PropertyEntry> 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<PropertyEntry> 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<String> 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();

View File

@ -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<PropertyEntry> generatePropertyMap() {
List<PropertyEntry> 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;
}
}