#927 Integrate ConfigMe into AuthMe (work in progress)
- Replace own code with ConfigMe
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
package fr.xephi.authme.settings;
|
||||
|
||||
import com.github.authme.configme.migration.MigrationService;
|
||||
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.resource.PropertyResource;
|
||||
import com.github.authme.configme.resource.YamlFileResource;
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import fr.xephi.authme.settings.domain.Property;
|
||||
import fr.xephi.authme.settings.properties.SettingsFieldRetriever;
|
||||
import fr.xephi.authme.settings.propertymap.PropertyMap;
|
||||
import fr.xephi.authme.settings.properties.AuthMeSettingsRetriever;
|
||||
import fr.xephi.authme.util.StringUtils;
|
||||
import org.bukkit.configuration.MemorySection;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
@@ -35,18 +39,18 @@ public class ConfigFileConsistencyTest {
|
||||
public void shouldHaveAllConfigs() throws IOException {
|
||||
// given
|
||||
File configFile = TestHelper.getJarFile(CONFIG_FILE);
|
||||
FileConfiguration configuration = YamlConfiguration.loadConfiguration(configFile);
|
||||
SettingsMigrationService migration = new SettingsMigrationService();
|
||||
PropertyResource resource = new YamlFileResource(configFile);
|
||||
MigrationService migration = new PlainMigrationService();
|
||||
|
||||
// when
|
||||
boolean result = migration.containsAllSettings(configuration, SettingsFieldRetriever.getAllPropertyFields());
|
||||
boolean result = migration.checkAndMigrate(resource, AuthMeSettingsRetriever.getAllPropertyFields());
|
||||
|
||||
// then
|
||||
if (!result) {
|
||||
if (result) {
|
||||
Set<String> knownProperties = getAllKnownPropertyPaths();
|
||||
List<String> missingProperties = new ArrayList<>();
|
||||
for (String path : knownProperties) {
|
||||
if (!configuration.contains(path)) {
|
||||
if (!resource.contains(path)) {
|
||||
missingProperties.add(path);
|
||||
}
|
||||
}
|
||||
@@ -82,21 +86,22 @@ public class ConfigFileConsistencyTest {
|
||||
public void shouldHaveValueCorrespondingToPropertyDefault() {
|
||||
// given
|
||||
File configFile = TestHelper.getJarFile(CONFIG_FILE);
|
||||
FileConfiguration configuration = YamlConfiguration.loadConfiguration(configFile);
|
||||
PropertyMap propertyMap = SettingsFieldRetriever.getAllPropertyFields();
|
||||
PropertyResource resource = new YamlFileResource(configFile);
|
||||
List<PropertyEntry> knownProperties = AuthMeSettingsRetriever.getAllPropertyFields();
|
||||
|
||||
// when / then
|
||||
for (Property<?> property : propertyMap.keySet()) {
|
||||
for (PropertyEntry propertyEntry : knownProperties) {
|
||||
Property<?> property = propertyEntry.getProperty();
|
||||
assertThat("Default value of '" + property.getPath() + "' in config.yml should be the same as in Property",
|
||||
property.getFromFile(configuration).equals(property.getDefaultValue()), equalTo(true));
|
||||
property.getValue(resource).equals(property.getDefaultValue()), equalTo(true));
|
||||
}
|
||||
}
|
||||
|
||||
private static Set<String> getAllKnownPropertyPaths() {
|
||||
PropertyMap propertyMap = SettingsFieldRetriever.getAllPropertyFields();
|
||||
Set<String> paths = new HashSet<>(propertyMap.size());
|
||||
for (Property<?> property : propertyMap.keySet()) {
|
||||
paths.add(property.getPath());
|
||||
List<PropertyEntry> knownProperties = AuthMeSettingsRetriever.getAllPropertyFields();
|
||||
Set<String> paths = new HashSet<>(knownProperties.size());
|
||||
for (PropertyEntry propertyEntry : knownProperties) {
|
||||
paths.add(propertyEntry.getProperty().getPath());
|
||||
}
|
||||
return paths;
|
||||
}
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
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.resource.PropertyResource;
|
||||
import com.github.authme.configme.resource.YamlFileResource;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.io.Files;
|
||||
import fr.xephi.authme.TestHelper;
|
||||
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.YamlConfiguration;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Rule;
|
||||
@@ -21,8 +23,6 @@ import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static fr.xephi.authme.settings.TestSettingsMigrationServices.checkAllPropertiesPresent;
|
||||
import static fr.xephi.authme.settings.domain.Property.newProperty;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
@@ -35,10 +35,8 @@ public class SettingsIntegrationTest {
|
||||
private static final String COMPLETE_FILE = TestHelper.PROJECT_ROOT + "settings/config-sample-values.yml";
|
||||
/** 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";
|
||||
/** File name for testing difficult values. */
|
||||
private static final String DIFFICULT_FILE = TestHelper.PROJECT_ROOT + "settings/config-difficult-values.yml";
|
||||
|
||||
private static PropertyMap propertyMap = TestConfiguration.generatePropertyMap();
|
||||
private static List<PropertyEntry> propertyMap = TestConfiguration.generatePropertyMap();
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
@@ -58,13 +56,13 @@ public class SettingsIntegrationTest {
|
||||
@Test
|
||||
public void shouldLoadAndReadAllProperties() throws IOException {
|
||||
// given
|
||||
YamlConfiguration configuration = YamlConfiguration.loadConfiguration(copyFileFromResources(COMPLETE_FILE));
|
||||
PropertyResource resource = new YamlFileResource(copyFileFromResources(COMPLETE_FILE));
|
||||
// Pass another, non-existent file to check if the settings had to be rewritten
|
||||
File newFile = temporaryFolder.newFile();
|
||||
|
||||
// when / then
|
||||
Settings settings = new Settings(configuration, newFile, testPluginFolder, propertyMap,
|
||||
checkAllPropertiesPresent());
|
||||
Settings settings = new Settings(testPluginFolder, propertyMap, resource,
|
||||
new PlainMigrationService());
|
||||
Map<Property<?>, Object> expectedValues = ImmutableMap.<Property<?>, Object>builder()
|
||||
.put(TestConfiguration.DURATION_IN_SECONDS, 22)
|
||||
.put(TestConfiguration.SYSTEM_NAME, "Custom sys name")
|
||||
@@ -88,16 +86,16 @@ public class SettingsIntegrationTest {
|
||||
public void shouldWriteMissingProperties() {
|
||||
// given/when
|
||||
File file = copyFileFromResources(INCOMPLETE_FILE);
|
||||
YamlConfiguration configuration = YamlConfiguration.loadConfiguration(file);
|
||||
PropertyResource resource = new YamlFileResource(file);
|
||||
// Expectation: File is rewritten to since it does not have all configurations
|
||||
new Settings(configuration, file, testPluginFolder, propertyMap, checkAllPropertiesPresent());
|
||||
new Settings(testPluginFolder, propertyMap, resource, new PlainMigrationService());
|
||||
|
||||
// Load the settings again -> checks that what we wrote can be loaded again
|
||||
configuration = YamlConfiguration.loadConfiguration(file);
|
||||
resource = new YamlFileResource(file);
|
||||
|
||||
// then
|
||||
Settings settings = new Settings(configuration, file, testPluginFolder, propertyMap,
|
||||
checkAllPropertiesPresent());
|
||||
Settings settings = new Settings(testPluginFolder, propertyMap, resource,
|
||||
new PlainMigrationService());
|
||||
Map<Property<?>, Object> expectedValues = ImmutableMap.<Property<?>, Object>builder()
|
||||
.put(TestConfiguration.DURATION_IN_SECONDS, 22)
|
||||
.put(TestConfiguration.SYSTEM_NAME, "[TestDefaultValue]")
|
||||
@@ -116,62 +114,11 @@ public class SettingsIntegrationTest {
|
||||
}
|
||||
}
|
||||
|
||||
/** Verify that "difficult cases" such as apostrophes in strings etc. are handled properly. */
|
||||
@Test
|
||||
public void shouldProperlyExportAnyValues() {
|
||||
// given
|
||||
File file = copyFileFromResources(DIFFICULT_FILE);
|
||||
YamlConfiguration configuration = YamlConfiguration.loadConfiguration(file);
|
||||
|
||||
// Additional string properties
|
||||
List<Property<String>> additionalProperties = Arrays.asList(
|
||||
newProperty("more.string1", "it's a text with some \\'apostrophes'"),
|
||||
newProperty("more.string2", "\tthis one\nhas some\nnew '' lines-test")
|
||||
);
|
||||
for (Property<?> property : additionalProperties) {
|
||||
propertyMap.put(property, new String[0]);
|
||||
}
|
||||
|
||||
// when
|
||||
new Settings(configuration, file, testPluginFolder, propertyMap, checkAllPropertiesPresent());
|
||||
// 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");
|
||||
Settings settings = new Settings(configuration, unusedFile, testPluginFolder, propertyMap,
|
||||
checkAllPropertiesPresent());
|
||||
assertThat(unusedFile.exists(), equalTo(false));
|
||||
assertThat(configuration.contains(TestConfiguration.DUST_LEVEL.getPath()), equalTo(true));
|
||||
|
||||
Map<Property<?>, Object> expectedValues = ImmutableMap.<Property<?>, Object>builder()
|
||||
.put(TestConfiguration.DURATION_IN_SECONDS, 20)
|
||||
.put(TestConfiguration.SYSTEM_NAME, "A 'test' name")
|
||||
.put(TestConfiguration.RATIO_ORDER, TestEnum.FOURTH)
|
||||
.put(TestConfiguration.RATIO_FIELDS, Arrays.asList("Australia\\", "\tBurundi'", "Colombia?\n''"))
|
||||
.put(TestConfiguration.VERSION_NUMBER, -1337)
|
||||
.put(TestConfiguration.SKIP_BORING_FEATURES, false)
|
||||
.put(TestConfiguration.BORING_COLORS, Arrays.asList("it's a difficult string!", "gray\nwith new lines\n"))
|
||||
.put(TestConfiguration.DUST_LEVEL, -1)
|
||||
.put(TestConfiguration.USE_COOL_FEATURES, true)
|
||||
.put(TestConfiguration.COOL_OPTIONS, Collections.EMPTY_LIST)
|
||||
.put(additionalProperties.get(0), additionalProperties.get(0).getDefaultValue())
|
||||
.put(additionalProperties.get(1), additionalProperties.get(1).getDefaultValue())
|
||||
.build();
|
||||
for (Map.Entry<Property<?>, Object> entry : expectedValues.entrySet()) {
|
||||
assertThat("Property '" + entry.getKey().getPath() + "' has expected value"
|
||||
+ entry.getValue() + " but found " + settings.getProperty(entry.getKey()),
|
||||
settings.getProperty(entry.getKey()), equalTo(entry.getValue()));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReloadSettings() throws IOException {
|
||||
// given
|
||||
YamlConfiguration configuration = YamlConfiguration.loadConfiguration(temporaryFolder.newFile());
|
||||
File fullConfigFile = copyFileFromResources(COMPLETE_FILE);
|
||||
Settings settings = new Settings(configuration, fullConfigFile, testPluginFolder, propertyMap,
|
||||
PropertyResource resource = new YamlFileResource(temporaryFolder.newFile());
|
||||
Settings settings = new Settings(testPluginFolder, propertyMap, resource,
|
||||
TestSettingsMigrationServices.alwaysFulfilled());
|
||||
|
||||
// when
|
||||
|
||||
@@ -1,17 +1,18 @@
|
||||
package fr.xephi.authme.settings;
|
||||
|
||||
import com.github.authme.configme.propertymap.PropertyEntry;
|
||||
import com.github.authme.configme.resource.PropertyResource;
|
||||
import com.github.authme.configme.resource.YamlFileResource;
|
||||
import com.google.common.io.Files;
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import fr.xephi.authme.settings.properties.SettingsFieldRetriever;
|
||||
import fr.xephi.authme.settings.propertymap.PropertyMap;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import fr.xephi.authme.settings.properties.AuthMeSettingsRetriever;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import static org.hamcrest.Matchers.arrayWithSize;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
@@ -39,13 +40,13 @@ public class SettingsMigrationServiceTest {
|
||||
public void shouldNotRewriteJarConfig() throws IOException {
|
||||
// given
|
||||
copyConfigToTestFolder();
|
||||
FileConfiguration configuration = YamlConfiguration.loadConfiguration(configTestFile);
|
||||
PropertyMap propertyMap = SettingsFieldRetriever.getAllPropertyFields();
|
||||
PropertyResource resource = new YamlFileResource(configTestFile);
|
||||
List<PropertyEntry> propertyMap = AuthMeSettingsRetriever.getAllPropertyFields();
|
||||
assumeThat(testFolder.listFiles(), arrayWithSize(1));
|
||||
SettingsMigrationService migrationService = new SettingsMigrationService();
|
||||
SettingsMigrationService migrationService = new SettingsMigrationService(testFolder);
|
||||
|
||||
// when
|
||||
boolean result = migrationService.checkAndMigrate(configuration, propertyMap, testFolder);
|
||||
boolean result = migrationService.checkAndMigrate(resource, propertyMap);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(false));
|
||||
|
||||
@@ -1,22 +1,23 @@
|
||||
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.resource.PropertyResource;
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import fr.xephi.authme.settings.domain.Property;
|
||||
import fr.xephi.authme.settings.properties.RegistrationSettings;
|
||||
import fr.xephi.authme.settings.properties.TestConfiguration;
|
||||
import fr.xephi.authme.settings.properties.TestEnum;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.mockito.internal.stubbing.answers.ReturnsArgumentAt;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static fr.xephi.authme.settings.properties.PluginSettings.MESSAGES_LANGUAGE;
|
||||
@@ -28,13 +29,9 @@ 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.Matchers.anyBoolean;
|
||||
import static org.mockito.Matchers.anyDouble;
|
||||
import static org.mockito.Matchers.anyInt;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
@@ -56,38 +53,12 @@ public class SettingsTest {
|
||||
testPluginFolder = temporaryFolder.newFolder();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldLoadAllConfigs() {
|
||||
// given
|
||||
YamlConfiguration configuration = mock(YamlConfiguration.class);
|
||||
given(configuration.getString(anyString(), anyString())).willAnswer(new ReturnsArgumentAt(1));
|
||||
given(configuration.getBoolean(anyString(), anyBoolean())).willAnswer(new ReturnsArgumentAt(1));
|
||||
given(configuration.getDouble(anyString(), anyDouble())).willAnswer(new ReturnsArgumentAt(1));
|
||||
given(configuration.getInt(anyString(), anyInt())).willAnswer(new ReturnsArgumentAt(1));
|
||||
|
||||
setReturnValue(configuration, TestConfiguration.VERSION_NUMBER, 20);
|
||||
setReturnValue(configuration, TestConfiguration.SKIP_BORING_FEATURES, true);
|
||||
setReturnValue(configuration, TestConfiguration.RATIO_ORDER, TestEnum.THIRD);
|
||||
setReturnValue(configuration, TestConfiguration.SYSTEM_NAME, "myTestSys");
|
||||
|
||||
// when / then
|
||||
Settings settings = new Settings(configuration, null, null, null, null);
|
||||
|
||||
assertThat(settings.getProperty(TestConfiguration.VERSION_NUMBER), equalTo(20));
|
||||
assertThat(settings.getProperty(TestConfiguration.SKIP_BORING_FEATURES), equalTo(true));
|
||||
assertThat(settings.getProperty(TestConfiguration.RATIO_ORDER), equalTo(TestEnum.THIRD));
|
||||
assertThat(settings.getProperty(TestConfiguration.SYSTEM_NAME), equalTo("myTestSys"));
|
||||
|
||||
assertDefaultValue(TestConfiguration.DURATION_IN_SECONDS, settings);
|
||||
assertDefaultValue(TestConfiguration.DUST_LEVEL, settings);
|
||||
assertDefaultValue(TestConfiguration.COOL_OPTIONS, settings);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnDefaultFile() throws IOException {
|
||||
// given
|
||||
YamlConfiguration configuration = mock(YamlConfiguration.class);
|
||||
Settings settings = new Settings(configuration, null, null, null, null);
|
||||
PropertyResource resource = mock(PropertyResource.class);
|
||||
List<PropertyEntry> knownProperties = Collections.emptyList();
|
||||
Settings settings = new Settings(testPluginFolder, knownProperties, resource, new PlainMigrationService());
|
||||
|
||||
// when
|
||||
String defaultFile = settings.getDefaultMessagesFile();
|
||||
@@ -99,19 +70,6 @@ public class SettingsTest {
|
||||
assertThat(stream.read(), not(equalTo(0)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldSetProperty() {
|
||||
// given
|
||||
YamlConfiguration configuration = mock(YamlConfiguration.class);
|
||||
Settings settings = new Settings(configuration, null, null, null, null);
|
||||
|
||||
// when
|
||||
settings.setProperty(TestConfiguration.DUST_LEVEL, -4);
|
||||
|
||||
// then
|
||||
verify(configuration).set(TestConfiguration.DUST_LEVEL.getPath(), -4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnMessagesFile() {
|
||||
// given
|
||||
@@ -120,11 +78,11 @@ public class SettingsTest {
|
||||
File file = new File(testPluginFolder, makePath("messages", "messages_" + languageCode + ".yml"));
|
||||
createFile(file);
|
||||
|
||||
YamlConfiguration configuration = mock(YamlConfiguration.class);
|
||||
given(configuration.contains(anyString())).willReturn(true);
|
||||
setReturnValue(configuration, MESSAGES_LANGUAGE, languageCode);
|
||||
Settings settings = new Settings(configuration, null, testPluginFolder,
|
||||
TestConfiguration.generatePropertyMap(), TestSettingsMigrationServices.alwaysFulfilled());
|
||||
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());
|
||||
|
||||
// when
|
||||
File messagesFile = settings.getMessagesFile();
|
||||
@@ -137,11 +95,11 @@ public class SettingsTest {
|
||||
@Test
|
||||
public void shouldCopyDefaultForUnknownLanguageCode() {
|
||||
// given
|
||||
YamlConfiguration configuration = mock(YamlConfiguration.class);
|
||||
given(configuration.contains(anyString())).willReturn(true);
|
||||
setReturnValue(configuration, MESSAGES_LANGUAGE, "doesntexist");
|
||||
Settings settings = new Settings(configuration, null, testPluginFolder,
|
||||
TestConfiguration.generatePropertyMap(), TestSettingsMigrationServices.alwaysFulfilled());
|
||||
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());
|
||||
|
||||
// when
|
||||
File messagesFile = settings.getMessagesFile();
|
||||
@@ -159,10 +117,10 @@ public class SettingsTest {
|
||||
createFile(welcomeFile);
|
||||
Files.write(welcomeFile.toPath(), welcomeMessage.getBytes());
|
||||
|
||||
YamlConfiguration configuration = mock(YamlConfiguration.class);
|
||||
setReturnValue(configuration, RegistrationSettings.USE_WELCOME_MESSAGE, true);
|
||||
Settings settings = new Settings(configuration, null, testPluginFolder,
|
||||
TestConfiguration.generatePropertyMap(), TestSettingsMigrationServices.alwaysFulfilled());
|
||||
PropertyResource resource = mock(PropertyResource.class);
|
||||
setReturnValue(resource, RegistrationSettings.USE_WELCOME_MESSAGE, true);
|
||||
Settings settings = new Settings(testPluginFolder, TestConfiguration.generatePropertyMap(),
|
||||
resource, TestSettingsMigrationServices.alwaysFulfilled());
|
||||
|
||||
// when
|
||||
List<String> result = settings.getWelcomeMessage();
|
||||
@@ -181,9 +139,9 @@ public class SettingsTest {
|
||||
createFile(emailFile);
|
||||
Files.write(emailFile.toPath(), emailMessage.getBytes());
|
||||
|
||||
YamlConfiguration configuration = mock(YamlConfiguration.class);
|
||||
Settings settings = new Settings(configuration, null, testPluginFolder,
|
||||
TestConfiguration.generatePropertyMap(), TestSettingsMigrationServices.alwaysFulfilled());
|
||||
PropertyResource resource = mock(PropertyResource.class);
|
||||
Settings settings = new Settings(testPluginFolder, TestConfiguration.generatePropertyMap(),
|
||||
resource, TestSettingsMigrationServices.alwaysFulfilled());
|
||||
|
||||
// when
|
||||
String result = settings.getEmailMessage();
|
||||
@@ -192,26 +150,21 @@ public class SettingsTest {
|
||||
assertThat(result, equalTo(emailMessage));
|
||||
}
|
||||
|
||||
private static <T> void setReturnValue(YamlConfiguration config, Property<T> property, T value) {
|
||||
private static <T> void setReturnValue(PropertyResource resource, Property<T> property, T value) {
|
||||
if (value instanceof String) {
|
||||
when(config.getString(eq(property.getPath()), anyString())).thenReturn((String) value);
|
||||
when(resource.getString(eq(property.getPath()))).thenReturn((String) value);
|
||||
} else if (value instanceof Integer) {
|
||||
when(config.getInt(eq(property.getPath()), anyInt())).thenReturn((Integer) value);
|
||||
when(resource.getInt(eq(property.getPath()))).thenReturn((Integer) value);
|
||||
} else if (value instanceof Boolean) {
|
||||
when(config.getBoolean(eq(property.getPath()), anyBoolean())).thenReturn((Boolean) value);
|
||||
when(resource.getBoolean(eq(property.getPath()))).thenReturn((Boolean) value);
|
||||
} else if (value instanceof Enum<?>) {
|
||||
when(config.getString(property.getPath())).thenReturn(((Enum<?>) value).name());
|
||||
when(resource.getString(property.getPath())).thenReturn(((Enum<?>) value).name());
|
||||
} else {
|
||||
throw new UnsupportedOperationException("Value has unsupported type '"
|
||||
+ (value == null ? "null" : value.getClass().getSimpleName()) + "'");
|
||||
}
|
||||
}
|
||||
|
||||
private static void assertDefaultValue(Property<?> property, Settings setting) {
|
||||
assertThat(property.getPath() + " has default value",
|
||||
setting.getProperty(property).equals(property.getDefaultValue()), equalTo(true));
|
||||
}
|
||||
|
||||
private static void createFile(File file) {
|
||||
try {
|
||||
file.getParentFile().mkdirs();
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
package fr.xephi.authme.settings;
|
||||
|
||||
import fr.xephi.authme.settings.propertymap.PropertyMap;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import com.github.authme.configme.migration.MigrationService;
|
||||
import com.github.authme.configme.propertymap.PropertyEntry;
|
||||
import com.github.authme.configme.resource.PropertyResource;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Provides {@link SettingsMigrationService} implementations for testing.
|
||||
* Provides {@link MigrationService} implementations for testing.
|
||||
*/
|
||||
public final class TestSettingsMigrationServices {
|
||||
|
||||
@@ -18,32 +19,12 @@ public final class TestSettingsMigrationServices {
|
||||
*
|
||||
* @return test settings migration service
|
||||
*/
|
||||
public static SettingsMigrationService alwaysFulfilled() {
|
||||
return new SettingsMigrationService() {
|
||||
public static MigrationService alwaysFulfilled() {
|
||||
return new MigrationService() {
|
||||
@Override
|
||||
public boolean checkAndMigrate(FileConfiguration configuration, PropertyMap propertyMap, File pluginFolder) {
|
||||
public boolean checkAndMigrate(PropertyResource propertyResource, List<PropertyEntry> list) {
|
||||
return false;
|
||||
}
|
||||
@Override
|
||||
public boolean containsAllSettings(FileConfiguration configuration, PropertyMap propertyMap) {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a simple settings migration service which is fulfilled if all properties are present.
|
||||
*
|
||||
* @return test settings migration service
|
||||
*/
|
||||
public static SettingsMigrationService checkAllPropertiesPresent() {
|
||||
return new 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);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,112 +0,0 @@
|
||||
package fr.xephi.authme.settings.domain;
|
||||
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Test for {@link EnumProperty}.
|
||||
*/
|
||||
public class EnumPropertyTest {
|
||||
|
||||
@Test
|
||||
public void shouldReturnCorrectEnumValue() {
|
||||
// given
|
||||
Property<TestEnum> property = Property.newProperty(TestEnum.class, "enum.path", TestEnum.ENTRY_C);
|
||||
YamlConfiguration configuration = mock(YamlConfiguration.class);
|
||||
given(configuration.getString(property.getPath())).willReturn("Entry_B");
|
||||
|
||||
// when
|
||||
TestEnum result = property.getFromFile(configuration);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(TestEnum.ENTRY_B));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFallBackToDefaultForInvalidValue() {
|
||||
// given
|
||||
Property<TestEnum> property = Property.newProperty(TestEnum.class, "enum.path", TestEnum.ENTRY_C);
|
||||
YamlConfiguration configuration = mock(YamlConfiguration.class);
|
||||
given(configuration.getString(property.getPath())).willReturn("Bogus");
|
||||
|
||||
// when
|
||||
TestEnum result = property.getFromFile(configuration);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(TestEnum.ENTRY_C));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFallBackToDefaultForNonExistentValue() {
|
||||
// given
|
||||
Property<TestEnum> property = Property.newProperty(TestEnum.class, "enum.path", TestEnum.ENTRY_C);
|
||||
YamlConfiguration configuration = mock(YamlConfiguration.class);
|
||||
given(configuration.getString(property.getPath())).willReturn(null);
|
||||
|
||||
// when
|
||||
TestEnum result = property.getFromFile(configuration);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(TestEnum.ENTRY_C));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnTrueForContainsCheck() {
|
||||
// given
|
||||
Property<TestEnum> property = Property.newProperty(TestEnum.class, "my.test.path", TestEnum.ENTRY_C);
|
||||
YamlConfiguration configuration = mock(YamlConfiguration.class);
|
||||
given(configuration.contains(property.getPath())).willReturn(true);
|
||||
given(configuration.getString(property.getPath())).willReturn("ENTRY_B");
|
||||
|
||||
// when
|
||||
boolean result = property.isPresent(configuration);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnFalseForFileWithoutConfig() {
|
||||
// given
|
||||
Property<TestEnum> property = Property.newProperty(TestEnum.class, "my.test.path", TestEnum.ENTRY_C);
|
||||
YamlConfiguration configuration = mock(YamlConfiguration.class);
|
||||
given(configuration.contains(property.getPath())).willReturn(false);
|
||||
|
||||
// when
|
||||
boolean result = property.isPresent(configuration);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnFalseForUnknownValue() {
|
||||
// given
|
||||
Property<TestEnum> property = Property.newProperty(TestEnum.class, "my.test.path", TestEnum.ENTRY_C);
|
||||
YamlConfiguration configuration = mock(YamlConfiguration.class);
|
||||
given(configuration.contains(property.getPath())).willReturn(true);
|
||||
given(configuration.getString(property.getPath())).willReturn("wrong value");
|
||||
|
||||
// when
|
||||
boolean result = property.isPresent(configuration);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(false));
|
||||
}
|
||||
|
||||
|
||||
private enum TestEnum {
|
||||
|
||||
ENTRY_A,
|
||||
|
||||
ENTRY_B,
|
||||
|
||||
ENTRY_C
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,172 +0,0 @@
|
||||
package fr.xephi.authme.settings.domain;
|
||||
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.mockito.internal.stubbing.answers.ReturnsArgumentAt;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.Matchers.anyBoolean;
|
||||
import static org.mockito.Matchers.anyInt;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Test for {@link Property} and the contained subtypes.
|
||||
*/
|
||||
public class PropertyTest {
|
||||
|
||||
private static YamlConfiguration configuration;
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpYamlConfigurationMock() {
|
||||
configuration = mock(YamlConfiguration.class);
|
||||
|
||||
when(configuration.getBoolean(eq("bool.path.test"), anyBoolean())).thenReturn(true);
|
||||
when(configuration.getBoolean(eq("bool.path.wrong"), anyBoolean())).thenAnswer(new ReturnsArgumentAt(1));
|
||||
when(configuration.getInt(eq("int.path.test"), anyInt())).thenReturn(27);
|
||||
when(configuration.getInt(eq("int.path.wrong"), anyInt())).thenAnswer(new ReturnsArgumentAt(1));
|
||||
when(configuration.getString(eq("str.path.test"), anyString())).thenReturn("Test value");
|
||||
when(configuration.getString(eq("str.path.wrong"), anyString())).thenAnswer(new ReturnsArgumentAt(1));
|
||||
when(configuration.isList("list.path.test")).thenReturn(true);
|
||||
when(configuration.getStringList("list.path.test")).thenReturn(Arrays.asList("test1", "Test2", "3rd test"));
|
||||
when(configuration.isList("list.path.wrong")).thenReturn(false);
|
||||
when(configuration.isList("lowercaselist.path.test")).thenReturn(true);
|
||||
when(configuration.getStringList("lowercaselist.path.test")).thenReturn(Arrays.asList("test1", "Test2", "3rd test"));
|
||||
when(configuration.isList("lowercaselist.path.wrong")).thenReturn(false);
|
||||
}
|
||||
|
||||
/* Boolean */
|
||||
@Test
|
||||
public void shouldGetBoolValue() {
|
||||
// given
|
||||
Property<Boolean> property = Property.newProperty("bool.path.test", false);
|
||||
|
||||
// when
|
||||
boolean result = property.getFromFile(configuration);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetBoolDefault() {
|
||||
// given
|
||||
Property<Boolean> property = Property.newProperty("bool.path.wrong", true);
|
||||
|
||||
// when
|
||||
boolean result = property.getFromFile(configuration);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(true));
|
||||
}
|
||||
|
||||
/* Integer */
|
||||
@Test
|
||||
public void shouldGetIntValue() {
|
||||
// given
|
||||
Property<Integer> property = Property.newProperty("int.path.test", 3);
|
||||
|
||||
// when
|
||||
int result = property.getFromFile(configuration);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(27));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetIntDefault() {
|
||||
// given
|
||||
Property<Integer> property = Property.newProperty("int.path.wrong", -10);
|
||||
|
||||
// when
|
||||
int result = property.getFromFile(configuration);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(-10));
|
||||
}
|
||||
|
||||
/* String */
|
||||
@Test
|
||||
public void shouldGetStringValue() {
|
||||
// given
|
||||
Property<String> property = Property.newProperty("str.path.test", "unused default");
|
||||
|
||||
// when
|
||||
String result = property.getFromFile(configuration);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo("Test value"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetStringDefault() {
|
||||
// given
|
||||
Property<String> property = Property.newProperty("str.path.wrong", "given default value");
|
||||
|
||||
// when
|
||||
String result = property.getFromFile(configuration);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo("given default value"));
|
||||
}
|
||||
|
||||
/* String list */
|
||||
@Test
|
||||
public void shouldGetStringListValue() {
|
||||
// given
|
||||
Property<List<String>> property = Property.newListProperty("list.path.test", "1", "b");
|
||||
|
||||
// when
|
||||
List<String> result = property.getFromFile(configuration);
|
||||
|
||||
// then
|
||||
assertThat(result, contains("test1", "Test2", "3rd test"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetStringListDefault() {
|
||||
// given
|
||||
Property<List<String>> property =
|
||||
Property.newListProperty("list.path.wrong", "default", "list", "elements");
|
||||
|
||||
// when
|
||||
List<String> result = property.getFromFile(configuration);
|
||||
|
||||
// then
|
||||
assertThat(result, contains("default", "list", "elements"));
|
||||
}
|
||||
|
||||
/* Lowercase String list */
|
||||
@Test
|
||||
public void shouldGetLowercaseStringListValue() {
|
||||
// given
|
||||
Property<List<String>> property = Property.newLowercaseListProperty("lowercaselist.path.test", "1", "b");
|
||||
|
||||
// when
|
||||
List<String> result = property.getFromFile(configuration);
|
||||
|
||||
// then
|
||||
assertThat(result, contains("test1", "test2", "3rd test"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetLowercaseStringListDefault() {
|
||||
// given
|
||||
Property<List<String>> property =
|
||||
Property.newLowercaseListProperty("lowercaselist.path.wrong", "default", "list", "elements");
|
||||
|
||||
// when
|
||||
List<String> result = property.getFromFile(configuration);
|
||||
|
||||
// then
|
||||
assertThat(result, contains("default", "list", "elements"));
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
package fr.xephi.authme.settings.properties;
|
||||
|
||||
import com.github.authme.configme.SettingsHolder;
|
||||
import com.github.authme.configme.properties.Property;
|
||||
import fr.xephi.authme.ReflectionTestUtils;
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import fr.xephi.authme.settings.domain.Property;
|
||||
import fr.xephi.authme.settings.domain.SettingsClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -20,12 +20,12 @@ import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* Test for {@link SettingsClass} implementations.
|
||||
* Test for {@link SettingsHolder} implementations.
|
||||
*/
|
||||
public class SettingsClassConsistencyTest {
|
||||
|
||||
private static final String SETTINGS_FOLDER = "src/main/java/fr/xephi/authme/settings/properties";
|
||||
private static List<Class<? extends SettingsClass>> classes;
|
||||
private static List<Class<? extends SettingsHolder>> classes;
|
||||
|
||||
@BeforeClass
|
||||
public static void scanForSettingsClasses() {
|
||||
@@ -37,7 +37,7 @@ public class SettingsClassConsistencyTest {
|
||||
|
||||
classes = new ArrayList<>();
|
||||
for (File file : filesInFolder) {
|
||||
Class<? extends SettingsClass> clazz = getSettingsClassFromFile(file);
|
||||
Class<? extends SettingsHolder> clazz = getSettingsClassFromFile(file);
|
||||
if (clazz != null) {
|
||||
classes.add(clazz);
|
||||
}
|
||||
@@ -95,15 +95,15 @@ public class SettingsClassConsistencyTest {
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static Class<? extends SettingsClass> getSettingsClassFromFile(File file) {
|
||||
private static Class<? extends SettingsHolder> getSettingsClassFromFile(File file) {
|
||||
String fileName = file.getPath();
|
||||
String className = fileName
|
||||
.substring("src/main/java/".length(), fileName.length() - ".java".length())
|
||||
.replace(File.separator, ".");
|
||||
try {
|
||||
Class<?> clazz = SettingsClassConsistencyTest.class.getClassLoader().loadClass(className);
|
||||
if (SettingsClass.class.isAssignableFrom(clazz)) {
|
||||
return (Class<? extends SettingsClass>) clazz;
|
||||
if (SettingsHolder.class.isAssignableFrom(clazz)) {
|
||||
return (Class<? extends SettingsHolder>) clazz;
|
||||
}
|
||||
return null;
|
||||
} catch (ClassNotFoundException e) {
|
||||
|
||||
@@ -1,20 +1,21 @@
|
||||
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 fr.xephi.authme.settings.domain.Property;
|
||||
import fr.xephi.authme.settings.domain.SettingsClass;
|
||||
import fr.xephi.authme.settings.propertymap.PropertyMap;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static fr.xephi.authme.settings.domain.Property.newListProperty;
|
||||
import static fr.xephi.authme.settings.domain.Property.newProperty;
|
||||
import static com.github.authme.configme.properties.PropertyInitializer.newListProperty;
|
||||
import static com.github.authme.configme.properties.PropertyInitializer.newProperty;
|
||||
|
||||
/**
|
||||
* Sample properties for testing purposes.
|
||||
*/
|
||||
public final class TestConfiguration implements SettingsClass {
|
||||
public final class TestConfiguration implements SettingsHolder {
|
||||
|
||||
public static final Property<Integer> DURATION_IN_SECONDS =
|
||||
newProperty("test.duration", 4);
|
||||
@@ -55,17 +56,17 @@ public final class TestConfiguration implements SettingsClass {
|
||||
*
|
||||
* @return The generated property map
|
||||
*/
|
||||
public static PropertyMap generatePropertyMap() {
|
||||
PropertyMap propertyMap = new PropertyMap();
|
||||
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() + "'"};
|
||||
propertyMap.put(property, comments);
|
||||
properties.add(new PropertyEntry(property, comments));
|
||||
}
|
||||
}
|
||||
return propertyMap;
|
||||
return properties;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,53 +0,0 @@
|
||||
package fr.xephi.authme.settings.propertymap;
|
||||
|
||||
import fr.xephi.authme.settings.domain.Property;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Test for {@link PropertyMap}.
|
||||
*/
|
||||
public class PropertyMapTest {
|
||||
|
||||
@Test
|
||||
public void shouldKeepEntriesByInsertionAndGroup() {
|
||||
// given
|
||||
List<String> paths = Arrays.asList("japan", "indonesia.jakarta", "japan.tokyo", "china.shanghai", "egypt.cairo",
|
||||
"china.shenzhen", "china", "indonesia.jakarta.tugu", "egypt", "japan.nagoya", "japan.tokyo.taito");
|
||||
PropertyMap map = new PropertyMap();
|
||||
|
||||
// when
|
||||
for (String path : paths) {
|
||||
Property<?> property = createPropertyWithPath(path);
|
||||
map.put(property, new String[0]);
|
||||
}
|
||||
|
||||
// then
|
||||
Set<Map.Entry<Property<?>, String[]>> entrySet = map.entrySet();
|
||||
List<String> resultPaths = new ArrayList<>(entrySet.size());
|
||||
for (Map.Entry<Property<?>, String[]> entry : entrySet) {
|
||||
resultPaths.add(entry.getKey().getPath());
|
||||
}
|
||||
|
||||
Assert.assertThat(resultPaths, contains("japan", "japan.tokyo", "japan.tokyo.taito", "japan.nagoya",
|
||||
"indonesia.jakarta", "indonesia.jakarta.tugu", "china", "china.shanghai", "china.shenzhen",
|
||||
"egypt", "egypt.cairo"));
|
||||
}
|
||||
|
||||
private static Property<?> createPropertyWithPath(String path) {
|
||||
Property<?> property = mock(Property.class);
|
||||
when(property.getPath()).thenReturn(path);
|
||||
return property;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user