Search in sources :

Example 1 with Property

use of ch.jalu.configme.properties.Property in project AuthMeReloaded by AuthMe.

the class AbstractResourceClosingTest method initializeSettings.

/** Initialize the settings mock and makes it return the default of any given property by default. */
@SuppressWarnings({ "unchecked", "rawtypes" })
@BeforeClass
public static void initializeSettings() throws IOException, ClassNotFoundException {
    settings = mock(Settings.class);
    given(settings.getProperty(any(Property.class))).willAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) {
            return ((Property<?>) invocation.getArguments()[0]).getDefaultValue();
        }
    });
    TestHelper.setupLogger();
}
Also used : Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Property(ch.jalu.configme.properties.Property) SecuritySettings(fr.xephi.authme.settings.properties.SecuritySettings) Settings(fr.xephi.authme.settings.Settings) BeforeClass(org.junit.BeforeClass)

Example 2 with Property

use of ch.jalu.configme.properties.Property in project AuthMeReloaded by AuthMe.

the class SettingsIntegrationTest method shouldLoadAndReadAllProperties.

@Test
public void shouldLoadAndReadAllProperties() throws IOException {
    // given
    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(testPluginFolder, resource, new PlainMigrationService(), CONFIG_DATA);
    Map<Property<?>, Object> expectedValues = ImmutableMap.<Property<?>, Object>builder().put(TestConfiguration.DURATION_IN_SECONDS, 22).put(TestConfiguration.SYSTEM_NAME, "Custom sys name").put(TestConfiguration.RATIO_ORDER, TestEnum.FIRST).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, 2).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.getProperty(entry.getKey()), equalTo(entry.getValue()));
    }
    assertThat(newFile.length(), equalTo(0L));
}
Also used : YamlFileResource(ch.jalu.configme.resource.YamlFileResource) PlainMigrationService(ch.jalu.configme.migration.PlainMigrationService) PropertyResource(ch.jalu.configme.resource.PropertyResource) File(java.io.File) TestHelper.getJarFile(fr.xephi.authme.TestHelper.getJarFile) Property(ch.jalu.configme.properties.Property) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) Test(org.junit.Test)

Example 3 with Property

use of ch.jalu.configme.properties.Property in project AuthMeReloaded by AuthMe.

the class MessageUpdater method createConfigurationData.

/**
 * Constructs the {@link ConfigurationData} for exporting a messages file in its entirety.
 *
 * @return the configuration data to export with
 */
public static MessageKeyConfigurationData createConfigurationData() {
    Map<String, String> comments = ImmutableMap.<String, String>builder().put("registration", "Registration").put("password", "Password errors on registration").put("login", "Login").put("error", "Errors").put("antibot", "AntiBot").put("unregister", "Unregister").put("misc", "Other messages").put("session", "Session messages").put("on_join_validation", "Error messages when joining").put("email", "Email").put("recovery", "Password recovery by email").put("captcha", "Captcha").put("verification", "Verification code").put("time", "Time units").put("two_factor", "Two-factor authentication").build();
    Set<String> addedKeys = new HashSet<>();
    MessageKeyPropertyListBuilder builder = new MessageKeyPropertyListBuilder();
    // Add one key per section based on the comments map above so that the order is clear
    for (String path : comments.keySet()) {
        MessageKey key = Arrays.stream(MessageKey.values()).filter(p -> p.getKey().startsWith(path + ".")).findFirst().orElseThrow(() -> new IllegalStateException(path));
        builder.addMessageKey(key);
        addedKeys.add(key.getKey());
    }
    // Add all remaining keys to the property list builder
    Arrays.stream(MessageKey.values()).filter(key -> !addedKeys.contains(key.getKey())).forEach(builder::addMessageKey);
    // Create ConfigurationData instance
    Map<String, List<String>> commentsMap = comments.entrySet().stream().collect(Collectors.toMap(e -> e.getKey(), e -> singletonList(e.getValue())));
    return new MessageKeyConfigurationData(builder, commentsMap);
}
Also used : ConsoleLoggerFactory(fr.xephi.authme.output.ConsoleLoggerFactory) Arrays(java.util.Arrays) ConvertErrorRecorder(ch.jalu.configme.properties.convertresult.ConvertErrorRecorder) ImmutableMap(com.google.common.collect.ImmutableMap) PropertyReader(ch.jalu.configme.resource.PropertyReader) Set(java.util.Set) IOException(java.io.IOException) MessageKey(fr.xephi.authme.message.MessageKey) Collectors(java.util.stream.Collectors) File(java.io.File) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) HashSet(java.util.HashSet) List(java.util.List) FileUtils(fr.xephi.authme.util.FileUtils) Property(ch.jalu.configme.properties.Property) Files(com.google.common.io.Files) Map(java.util.Map) ConfigurationData(ch.jalu.configme.configurationdata.ConfigurationData) PropertyListBuilder(ch.jalu.configme.configurationdata.PropertyListBuilder) PropertyResource(ch.jalu.configme.resource.PropertyResource) StringProperty(ch.jalu.configme.properties.StringProperty) ConsoleLogger(fr.xephi.authme.ConsoleLogger) MessageKey(fr.xephi.authme.message.MessageKey) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) HashSet(java.util.HashSet)

Example 4 with Property

use of ch.jalu.configme.properties.Property in project AuthMeReloaded by AuthMe.

the class SettingsIntegrationTest method shouldWriteMissingProperties.

@Test
public void shouldWriteMissingProperties() {
    // given/when
    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, resource, new PlainMigrationService(), CONFIG_DATA);
    // Load the settings again -> checks that what we wrote can be loaded again
    resource = new YamlFileResource(file);
    // then
    Settings settings = new Settings(testPluginFolder, resource, new PlainMigrationService(), CONFIG_DATA);
    Map<Property<?>, Object> expectedValues = ImmutableMap.<Property<?>, Object>builder().put(TestConfiguration.DURATION_IN_SECONDS, 22).put(TestConfiguration.SYSTEM_NAME, "[TestDefaultValue]").put(TestConfiguration.RATIO_ORDER, TestEnum.SECOND).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, -1).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.getProperty(entry.getKey()), equalTo(entry.getValue()));
    }
}
Also used : YamlFileResource(ch.jalu.configme.resource.YamlFileResource) PlainMigrationService(ch.jalu.configme.migration.PlainMigrationService) PropertyResource(ch.jalu.configme.resource.PropertyResource) File(java.io.File) TestHelper.getJarFile(fr.xephi.authme.TestHelper.getJarFile) Property(ch.jalu.configme.properties.Property) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

Property (ch.jalu.configme.properties.Property)4 PropertyResource (ch.jalu.configme.resource.PropertyResource)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 File (java.io.File)3 Map (java.util.Map)3 PlainMigrationService (ch.jalu.configme.migration.PlainMigrationService)2 YamlFileResource (ch.jalu.configme.resource.YamlFileResource)2 TestHelper.getJarFile (fr.xephi.authme.TestHelper.getJarFile)2 Test (org.junit.Test)2 ConfigurationData (ch.jalu.configme.configurationdata.ConfigurationData)1 PropertyListBuilder (ch.jalu.configme.configurationdata.PropertyListBuilder)1 StringProperty (ch.jalu.configme.properties.StringProperty)1 ConvertErrorRecorder (ch.jalu.configme.properties.convertresult.ConvertErrorRecorder)1 PropertyReader (ch.jalu.configme.resource.PropertyReader)1 Files (com.google.common.io.Files)1 ConsoleLogger (fr.xephi.authme.ConsoleLogger)1 MessageKey (fr.xephi.authme.message.MessageKey)1 ConsoleLoggerFactory (fr.xephi.authme.output.ConsoleLoggerFactory)1 Settings (fr.xephi.authme.settings.Settings)1 SecuritySettings (fr.xephi.authme.settings.properties.SecuritySettings)1