#347 Create consistency and integration tests for NewSetting

- Ensure that the project's config.yml contains all settings that NewSetting is aware of
- Add extensive, more generic integration tests
This commit is contained in:
ljacqu
2016-01-08 23:20:42 +01:00
parent 69c225c850
commit d0b7d0ff06
13 changed files with 329 additions and 338 deletions
@@ -0,0 +1,35 @@
package fr.xephi.authme.settings.custom;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.junit.Assume.assumeThat;
/**
* Test for {@link NewSetting} and the project's config.yml,
* verifying that no settings are missing from the file.
*/
public class ConfigFileConsistencyTest {
@Test
public void shouldHaveAllConfigs() throws IOException {
URL url = this.getClass().getResource("/config.yml");
File configFile = new File(url.getFile());
// given
assumeThat(configFile.exists(), equalTo(true));
NewSetting settings = new NewSetting(configFile);
// when
boolean result = settings.containsAllSettings(SettingsFieldRetriever.getAllPropertyFields());
// then
assertThat(result, equalTo(true));
}
}
@@ -0,0 +1,113 @@
package fr.xephi.authme.settings.custom;
import com.google.common.collect.ImmutableMap;
import fr.xephi.authme.ReflectionTestUtils;
import fr.xephi.authme.settings.domain.Property;
import fr.xephi.authme.settings.propertymap.PropertyMap;
import org.bukkit.configuration.file.YamlConfiguration;
import org.junit.BeforeClass;
import org.junit.Test;
import java.io.File;
import java.lang.reflect.Field;
import java.net.URL;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.junit.Assume.assumeThat;
/**
* Integration test for {@link NewSetting}.
*/
public class NewSettingIntegrationTest {
/** File name of the sample config including all {@link TestConfiguration} values. */
private static final String COMPLETE_FILE = "config-sample-values.yml";
/** File name of the sample config missing certain {@link TestConfiguration} values. */
private static final String INCOMPLETE_FILE = "config-incomplete-sample.yml";
private static PropertyMap propertyMap;
@BeforeClass
public static void generatePropertyMap() {
propertyMap = new PropertyMap();
for (Field field : TestConfiguration.class.getDeclaredFields()) {
Property<?> property =
(Property<?>) ReflectionTestUtils.getFieldValue(TestConfiguration.class, null, field.getName());
String[] comments = new String[]{"Comment for '" + property.getPath() + "'"};
propertyMap.put(property, comments);
}
}
@Test
public void shouldLoadAndReadAllProperties() {
// given
YamlConfiguration configuration = YamlConfiguration.loadConfiguration(getConfigFile(COMPLETE_FILE));
File file = new File("unused");
assumeThat(file.exists(), equalTo(false));
// when / then
NewSetting settings = new NewSetting(configuration, file, propertyMap);
Map<Property<?>, Object> expectedValues = ImmutableMap.<Property<?>, Object>builder()
.put(TestConfiguration.DURATION_IN_SECONDS, 22)
.put(TestConfiguration.SYSTEM_NAME, "Custom sys name")
.put(TestConfiguration.RATIO_LIMIT, -4.1)
.put(TestConfiguration.RATIO_FIELDS, Arrays.asList("Australia", "Burundi", "Colombia"))
.put(TestConfiguration.VERSION_NUMBER, 2492)
.put(TestConfiguration.SKIP_BORING_FEATURES, false)
.put(TestConfiguration.BORING_COLORS, Arrays.asList("beige", "gray"))
.put(TestConfiguration.DUST_LEVEL, 0.81)
.put(TestConfiguration.USE_COOL_FEATURES, true)
.put(TestConfiguration.COOL_OPTIONS, Arrays.asList("Dinosaurs", "Explosions", "Big trucks"))
.build();
for (Map.Entry<Property<?>, Object> entry : expectedValues.entrySet()) {
assertThat("Property '" + entry.getKey().getPath() + "' has expected value",
settings.getOption(entry.getKey()), equalTo(entry.getValue()));
}
assertThat(file.exists(), equalTo(false));
}
@Test
public void shouldWriteMissingProperties() {
// given/when
File file = getConfigFile(INCOMPLETE_FILE);
YamlConfiguration configuration = YamlConfiguration.loadConfiguration(file);
assumeThat(configuration.contains(TestConfiguration.BORING_COLORS.getPath()), equalTo(false));
// Expectation: File is rewritten to since it does not have all configurations
new NewSetting(configuration, file, propertyMap);
// 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);
Map<Property<?>, Object> expectedValues = ImmutableMap.<Property<?>, Object>builder()
.put(TestConfiguration.DURATION_IN_SECONDS, 22)
.put(TestConfiguration.SYSTEM_NAME, "[TestDefaultValue]")
.put(TestConfiguration.RATIO_LIMIT, 3.0)
.put(TestConfiguration.RATIO_FIELDS, Arrays.asList("Australia", "Burundi", "Colombia"))
.put(TestConfiguration.VERSION_NUMBER, 32046)
.put(TestConfiguration.SKIP_BORING_FEATURES, false)
.put(TestConfiguration.BORING_COLORS, Collections.EMPTY_LIST)
.put(TestConfiguration.DUST_LEVEL, 0.2)
.put(TestConfiguration.USE_COOL_FEATURES, false)
.put(TestConfiguration.COOL_OPTIONS, Arrays.asList("Dinosaurs", "Explosions", "Big trucks"))
.build();
for (Map.Entry<Property<?>, Object> entry : expectedValues.entrySet()) {
assertThat("Property '" + entry.getKey().getPath() + "' has expected value",
settings.getOption(entry.getKey()), equalTo(entry.getValue()));
}
}
private File getConfigFile(String file) {
URL url = getClass().getClassLoader().getResource(file);
if (url == null) {
throw new IllegalStateException("File '" + file + "' could not be loaded");
}
return new File(url.getFile());
}
}
@@ -1,114 +1,84 @@
package fr.xephi.authme.settings.custom;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.settings.domain.Property;
import fr.xephi.authme.settings.domain.PropertyType;
import org.bukkit.configuration.file.YamlConfiguration;
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import java.io.File;
import java.net.URL;
import java.util.List;
import static fr.xephi.authme.settings.domain.Property.newProperty;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;
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.when;
/**
* Test for {@link NewSetting}.
*/
public class NewSettingTest {
private static final String CONFIG_FILE = "437-config-test.yml";
@Test
public void shouldReturnIntegerFromFile() {
// given
YamlConfiguration file = mock(YamlConfiguration.class);
Property<Integer> config = TestConfiguration.DURATION_IN_SECONDS;
given(file.getInt("test.duration", 4)).willReturn(18);
NewSetting settings = new NewSetting(file, "conf.txt");
// when
int retrieve = settings.getOption(config);
// then
assertThat(retrieve, equalTo(18));
}
@Test
public void shouldLoadAllConfigs() {
// given
YamlConfiguration file = mock(YamlConfiguration.class);
given(file.getString(anyString(), anyString())).willAnswer(withDefaultArgument());
given(file.getBoolean(anyString(), anyBoolean())).willAnswer(withDefaultArgument());
given(file.getDouble(anyString(), anyDouble())).willAnswer(withDefaultArgument());
given(file.getInt(anyString(), anyInt())).willAnswer(withDefaultArgument());
given(file.getString(anyString(), anyString())).willAnswer(new Answer<String>() {
@Override
public String answer(InvocationOnMock invocation) throws Throwable {
// Return the second parameter -> the default
return (String) invocation.getArguments()[1];
}
});
setReturnValue(file, TestConfiguration.VERSION_NUMBER, 20);
setReturnValue(file, TestConfiguration.SKIP_BORING_FEATURES, true);
setReturnValue(file, TestConfiguration.RATIO_LIMIT, 4.25);
setReturnValue(file, TestConfiguration.SYSTEM_NAME, "myTestSys");
given(file.getInt(eq(EmailSettings.RECOVERY_PASSWORD_LENGTH.getPath()), anyInt()))
.willReturn(20);
given(file.getBoolean(eq(SecuritySettings.REMOVE_PASSWORD_FROM_CONSOLE.getPath()), anyBoolean()))
.willReturn(false);
// when / then
NewSetting settings = new NewSetting(file, new File("conf.txt"), null);
// when
NewSetting settings = new NewSetting(file, "conf.txt");
assertThat(settings.getOption(TestConfiguration.VERSION_NUMBER), equalTo(20));
assertThat(settings.getOption(TestConfiguration.SKIP_BORING_FEATURES), equalTo(true));
assertThat(settings.getOption(TestConfiguration.RATIO_LIMIT), equalTo(4.25));
assertThat(settings.getOption(TestConfiguration.SYSTEM_NAME), equalTo("myTestSys"));
// then
// Expect the value we told the YAML mock to return:
assertThat(settings.getOption(EmailSettings.RECOVERY_PASSWORD_LENGTH), equalTo(20));
// Expect the default:
assertThat(settings.getOption(EmailSettings.SMTP_HOST), equalTo(EmailSettings.SMTP_HOST.getDefaultValue()));
// Expect the value we told the YAML mock to return:
assertThat(settings.getOption(SecuritySettings.REMOVE_PASSWORD_FROM_CONSOLE), equalTo(false));
assertDefaultValue(TestConfiguration.DURATION_IN_SECONDS, settings);
assertDefaultValue(TestConfiguration.DUST_LEVEL, settings);
assertDefaultValue(TestConfiguration.COOL_OPTIONS, settings);
}
@Test
public void executeIntegrationTest() {
// given
YamlConfiguration yamlFile = YamlConfiguration.loadConfiguration(getConfigFile());
NewSetting settings = new NewSetting(yamlFile, "conf.txt");
// when
int result = settings.getOption(TestConfiguration.DURATION_IN_SECONDS);
String systemName = settings.getOption(TestConfiguration.SYSTEM_NAME);
String helpHeader = settings.getOption(newProperty("settings.helpHeader", ""));
List<String> unsafePasswords = settings.getOption(
newProperty(PropertyType.STRING_LIST, "Security.unsafePasswords"));
DataSource.DataSourceType dataSourceType = settings.getOption(DatabaseSettings.BACKEND);
// then
assertThat(result, equalTo(22));
assertThat(systemName, equalTo(TestConfiguration.SYSTEM_NAME.getDefaultValue()));
assertThat(helpHeader, equalTo("AuthMeReloaded"));
assertThat(unsafePasswords, contains("123456", "qwerty", "54321"));
assertThat(dataSourceType, equalTo(DataSource.DataSourceType.MYSQL));
}
private File getConfigFile() {
URL url = getClass().getClassLoader().getResource(CONFIG_FILE);
if (url == null) {
throw new RuntimeException("File '" + CONFIG_FILE + "' could not be loaded");
private static <T> void setReturnValue(YamlConfiguration config, Property<T> property, T value) {
if (value instanceof String) {
when(config.getString(eq(property.getPath()), anyString())).thenReturn((String) value);
} else if (value instanceof Integer) {
when(config.getInt(eq(property.getPath()), anyInt())).thenReturn((Integer) value);
} else if (value instanceof Boolean) {
when(config.getBoolean(eq(property.getPath()), anyBoolean())).thenReturn((Boolean) value);
} else if (value instanceof Double) {
when(config.getDouble(eq(property.getPath()), anyDouble())).thenReturn((Double) value);
} else {
throw new UnsupportedOperationException("Value has unsupported type '"
+ (value == null ? "null" : value.getClass().getSimpleName()) + "'");
}
return new File(url.getFile());
}
private static class TestConfiguration {
private static void assertDefaultValue(Property<?> property, NewSetting setting) {
assertThat(property.getPath() + " has default value",
setting.getOption(property).equals(property.getDefaultValue()), equalTo(true));
}
public static final Property<Integer> DURATION_IN_SECONDS =
newProperty("test.duration", 4);
public static final Property<String> SYSTEM_NAME =
newProperty("test.systemName", "[TestDefaultValue]");
private static <T> Answer<T> withDefaultArgument() {
return new Answer<T>() {
@Override
public T answer(InvocationOnMock invocation) throws Throwable {
// Return the second parameter -> the default
return (T) invocation.getArguments()[1];
}
};
}
}
@@ -1,41 +0,0 @@
package fr.xephi.authme.settings.custom;
import org.junit.Test;
import java.io.File;
import java.net.URL;
import static org.junit.Assert.assertThat;
import static org.hamcrest.Matchers.equalTo;
/**
* Test for the save() function of new settings
*/
public class NewSettingsWriteTest {
private static final String CONFIG_FILE = "437-write-test.yml";
@Test
public void shouldWriteProperties() {
File file = getConfigFile();
NewSetting setting = new NewSetting(file);
setting.save();
// assert that we can load the file again -- i.e. that it's valid YAML!
NewSetting newSetting = new NewSetting(file);
assertThat(newSetting.getOption(SecuritySettings.CAPTCHA_LENGTH),
equalTo(SecuritySettings.CAPTCHA_LENGTH.getDefaultValue()));
assertThat(newSetting.getOption(ProtectionSettings.COUNTRIES_BLACKLIST),
equalTo(ProtectionSettings.COUNTRIES_BLACKLIST.getDefaultValue()));
}
private File getConfigFile() {
URL url = getClass().getClassLoader().getResource(CONFIG_FILE);
if (url == null) {
throw new RuntimeException("File '" + CONFIG_FILE + "' could not be loaded");
}
return new File(url.getFile());
}
}
@@ -0,0 +1,46 @@
package fr.xephi.authme.settings.custom;
import fr.xephi.authme.settings.domain.Property;
import fr.xephi.authme.settings.domain.PropertyType;
import fr.xephi.authme.settings.domain.SettingsClass;
import java.util.List;
import static fr.xephi.authme.settings.domain.Property.newProperty;
/**
* Sample properties for testing purposes.
*/
class TestConfiguration implements SettingsClass {
public static final Property<Integer> DURATION_IN_SECONDS =
newProperty("test.duration", 4);
public static final Property<String> SYSTEM_NAME =
newProperty("test.systemName", "[TestDefaultValue]");
public static final Property<Double> RATIO_LIMIT =
newProperty(PropertyType.DOUBLE, "sample.ratio.limit", 3.0);
public static final Property<List<String>> RATIO_FIELDS =
newProperty(PropertyType.STRING_LIST, "sample.ratio.fields", "a", "b", "c");
public static final Property<Integer> VERSION_NUMBER =
newProperty("version", 32046);
public static final Property<Boolean> SKIP_BORING_FEATURES =
newProperty("features.boring.skip", false);
public static final Property<List<String>> BORING_COLORS =
newProperty(PropertyType.STRING_LIST, "features.boring.colors");
public static final Property<Double> DUST_LEVEL =
newProperty(PropertyType.DOUBLE, "features.boring.dustLevel", 0.2);
public static final Property<Boolean> USE_COOL_FEATURES =
newProperty("features.cool.enabled", false);
public static final Property<List<String>> COOL_OPTIONS =
newProperty(PropertyType.STRING_LIST, "features.cool.options", "Sparks", "Sprinkles");
}