#603 Pass settings migration service as constructor parameter (work in progress)

This commit is contained in:
ljacqu
2016-03-14 23:18:16 +01:00
parent 4634213d75
commit 89767b120c
8 changed files with 68 additions and 38 deletions
@@ -35,10 +35,10 @@ public class ConfigFileConsistencyTest {
// given
File configFile = TestHelper.getJarFile(CONFIG_FILE);
FileConfiguration configuration = YamlConfiguration.loadConfiguration(configFile);
SettingsMigrationService migration = new SettingsMigrationService();
// when
boolean result = SettingsMigrationService.containsAllSettings(
configuration, SettingsFieldRetriever.getAllPropertyFields());
boolean result = migration.containsAllSettings(configuration, SettingsFieldRetriever.getAllPropertyFields());
// then
if (!result) {
@@ -8,9 +8,9 @@ import fr.xephi.authme.settings.domain.Property;
import fr.xephi.authme.settings.properties.TestConfiguration;
import fr.xephi.authme.settings.properties.TestEnum;
import fr.xephi.authme.settings.propertymap.PropertyMap;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
@@ -25,6 +25,9 @@ import java.util.Map;
import static fr.xephi.authme.settings.domain.Property.newProperty;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
/**
* Integration test for {@link NewSetting}.
@@ -56,7 +59,7 @@ public class NewSettingIntegrationTest {
File newFile = temporaryFolder.newFile();
// when / then
NewSetting settings = new NewSetting(configuration, newFile, propertyMap);
NewSetting settings = new NewSetting(configuration, newFile, propertyMap, new PlainSettingsMigrationService());
Map<Property<?>, Object> expectedValues = ImmutableMap.<Property<?>, Object>builder()
.put(TestConfiguration.DURATION_IN_SECONDS, 22)
.put(TestConfiguration.SYSTEM_NAME, "Custom sys name")
@@ -82,13 +85,13 @@ public class NewSettingIntegrationTest {
File file = copyFileFromResources(INCOMPLETE_FILE);
YamlConfiguration configuration = YamlConfiguration.loadConfiguration(file);
// Expectation: File is rewritten to since it does not have all configurations
new NewSetting(configuration, file, propertyMap);
new NewSetting(configuration, file, propertyMap, new PlainSettingsMigrationService());
// Load the settings again -> checks that what we wrote can be loaded again
configuration = YamlConfiguration.loadConfiguration(file);
// then
NewSetting settings = new NewSetting(configuration, file, propertyMap);
NewSetting settings = new NewSetting(configuration, file, propertyMap, new PlainSettingsMigrationService());
Map<Property<?>, Object> expectedValues = ImmutableMap.<Property<?>, Object>builder()
.put(TestConfiguration.DURATION_IN_SECONDS, 22)
.put(TestConfiguration.SYSTEM_NAME, "[TestDefaultValue]")
@@ -125,14 +128,14 @@ public class NewSettingIntegrationTest {
}
// when
new NewSetting(configuration, file, propertyMap);
new NewSetting(configuration, file, propertyMap, new PlainSettingsMigrationService());
// reload the file as settings should have been rewritten
configuration = YamlConfiguration.loadConfiguration(file);
// then
// assert that we won't rewrite the settings again! One rewrite should produce a valid, complete configuration
File unusedFile = new File("config-difficult-values.unused.yml");
NewSetting settings = new NewSetting(configuration, unusedFile, propertyMap);
NewSetting settings = new NewSetting(configuration, unusedFile, propertyMap, new PlainSettingsMigrationService());
assertThat(unusedFile.exists(), equalTo(false));
assertThat(configuration.contains(TestConfiguration.DUST_LEVEL.getPath()), equalTo(true));
@@ -158,13 +161,15 @@ public class NewSettingIntegrationTest {
}
@Test
@Ignore
// TODO #603: Un-ignore once migration service is passed to settings
public void shouldReloadSettings() throws IOException {
// given
YamlConfiguration configuration = YamlConfiguration.loadConfiguration(temporaryFolder.newFile());
File fullConfigFile = copyFileFromResources(COMPLETE_FILE);
NewSetting settings = new NewSetting(configuration, fullConfigFile, null);
SettingsMigrationService migrationService = mock(SettingsMigrationService.class);
given(migrationService.checkAndMigrate(any(FileConfiguration.class), any(PropertyMap.class), any(File.class)))
.willReturn(false);
NewSetting settings = new NewSetting(configuration, fullConfigFile, TestConfiguration.generatePropertyMap(),
new PlainSettingsMigrationService());
// when
assertThat(settings.getProperty(TestConfiguration.RATIO_ORDER),
@@ -6,7 +6,6 @@ import fr.xephi.authme.settings.properties.TestConfiguration;
import fr.xephi.authme.settings.properties.TestEnum;
import org.bukkit.configuration.file.YamlConfiguration;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
@@ -59,7 +58,7 @@ public class NewSettingTest {
setReturnValue(configuration, TestConfiguration.SYSTEM_NAME, "myTestSys");
// when / then
NewSetting settings = new NewSetting(configuration, null, null);
NewSetting settings = new NewSetting(configuration, null, null, null);
assertThat(settings.getProperty(TestConfiguration.VERSION_NUMBER), equalTo(20));
assertThat(settings.getProperty(TestConfiguration.SKIP_BORING_FEATURES), equalTo(true));
@@ -75,7 +74,7 @@ public class NewSettingTest {
public void shouldReturnDefaultFile() throws IOException {
// given
YamlConfiguration configuration = mock(YamlConfiguration.class);
NewSetting settings = new NewSetting(configuration, null, null);
NewSetting settings = new NewSetting(configuration, null, null, null);
// when
String defaultFile = settings.getDefaultMessagesFile();
@@ -91,7 +90,7 @@ public class NewSettingTest {
public void shouldSetProperty() {
// given
YamlConfiguration configuration = mock(YamlConfiguration.class);
NewSetting settings = new NewSetting(configuration, null, null);
NewSetting settings = new NewSetting(configuration, null, null, null);
// when
settings.setProperty(TestConfiguration.DUST_LEVEL, -4);
@@ -101,14 +100,13 @@ public class NewSettingTest {
}
@Test
@Ignore
// TODO #603: Un-ignore once migration service is injected into settings
public void shouldReturnMessagesFile() {
// given
YamlConfiguration configuration = mock(YamlConfiguration.class);
given(configuration.contains(anyString())).willReturn(true);
given(configuration.getString(eq(MESSAGES_LANGUAGE.getPath()), anyString())).willReturn("fr");
NewSetting settings = new NewSetting(configuration, null, TestConfiguration.generatePropertyMap());
NewSetting settings = new NewSetting(configuration, null, TestConfiguration.generatePropertyMap(),
new PlainSettingsMigrationService());
// when
File messagesFile = settings.getMessagesFile();
@@ -0,0 +1,20 @@
package fr.xephi.authme.settings;
import fr.xephi.authme.settings.propertymap.PropertyMap;
import org.bukkit.configuration.file.FileConfiguration;
import java.io.File;
/**
* Simple settings migration service simply returning {@code true} if all properties are present,
* {@code false} otherwise.
*/
public class PlainSettingsMigrationService extends SettingsMigrationService {
// See parent javadoc: true = some migration had to be done, false = config file is up-to-date
@Override
public boolean checkAndMigrate(FileConfiguration configuration, PropertyMap propertyMap, File pluginFolder) {
return !super.containsAllSettings(configuration, propertyMap);
}
}
@@ -42,9 +42,10 @@ public class SettingsMigrationServiceTest {
FileConfiguration configuration = YamlConfiguration.loadConfiguration(configTestFile);
PropertyMap propertyMap = SettingsFieldRetriever.getAllPropertyFields();
assumeThat(testFolder.listFiles(), arrayWithSize(1));
SettingsMigrationService migrationService = new SettingsMigrationService();
// when
boolean result = SettingsMigrationService.checkAndMigrate(configuration, propertyMap, testFolder);
boolean result = migrationService.checkAndMigrate(configuration, propertyMap, testFolder);
// then
assertThat(result, equalTo(false));