Search in sources :

Example 6 with PropertyReader

use of ch.jalu.configme.resource.PropertyReader in project AuthMeReloaded by AuthMe.

the class MessageUpdaterTest method shouldPerformNewerMigrations.

@Test
public void shouldPerformNewerMigrations() throws IOException {
    // given
    File messagesFile = temporaryFolder.newFile();
    Files.copy(TestHelper.getJarFile(TestHelper.PROJECT_ROOT + "message/messages_test2.yml"), messagesFile);
    // when
    boolean wasChanged = messageUpdater.migrateAndSave(messagesFile, DEFAULT_MESSAGES_FILE, DEFAULT_MESSAGES_FILE);
    // then
    assertThat(wasChanged, equalTo(true));
    PropertyReader reader = new YamlFileReader(messagesFile);
    assertThat(reader.getString(MessageKey.TWO_FACTOR_CREATE.getKey()), equalTo("Old 2fa create text"));
    // from pre-5.5 key
    assertThat(reader.getString(MessageKey.WRONG_PASSWORD.getKey()), equalTo("test2 - wrong password"));
    // from messages_en.yml
    assertThat(reader.getString(MessageKey.SECOND.getKey()), equalTo("second"));
}
Also used : YamlFileReader(ch.jalu.configme.resource.YamlFileReader) PropertyReader(ch.jalu.configme.resource.PropertyReader) File(java.io.File) Test(org.junit.Test)

Example 7 with PropertyReader

use of ch.jalu.configme.resource.PropertyReader in project AuthMeReloaded by AuthMe.

the class SettingsTest method mockPropertyResourceAndReader.

private static PropertyResource mockPropertyResourceAndReader() {
    PropertyReader reader = mock(PropertyReader.class, RETURNS_DEEP_STUBS);
    given(reader.getList(anyString())).willReturn(Collections.emptyList());
    PropertyResource resource = mock(PropertyResource.class);
    given(resource.createReader()).willReturn(reader);
    return resource;
}
Also used : PropertyResource(ch.jalu.configme.resource.PropertyResource) PropertyReader(ch.jalu.configme.resource.PropertyReader)

Example 8 with PropertyReader

use of ch.jalu.configme.resource.PropertyReader in project AuthMeReloaded by AuthMe.

the class MessageUpdater method migrateAndSave.

/**
 * Performs the migration.
 *
 * @param userFile the file to verify and migrate
 * @param jarMessageSource jar message source to get texts from if missing
 * @return true if the file has been migrated and saved, false if it is up-to-date
 */
private boolean migrateAndSave(File userFile, JarMessageSource jarMessageSource) {
    // YamlConfiguration escapes all special characters when saving, making the file hard to use, so use ConfigMe
    MessageKeyConfigurationData configurationData = createConfigurationData();
    PropertyResource userResource = new MigraterYamlFileResource(userFile);
    PropertyReader reader = userResource.createReader();
    configurationData.initializeValues(reader);
    // Step 1: Migrate any old keys in the file to the new paths
    boolean movedOldKeys = migrateOldKeys(reader, configurationData);
    // Step 2: Perform newer migrations
    boolean movedNewerKeys = migrateKeys(reader, configurationData);
    // Step 3: Take any missing messages from the message files shipped in the AuthMe JAR
    boolean addedMissingKeys = addMissingKeys(jarMessageSource, configurationData);
    if (movedOldKeys || movedNewerKeys || addedMissingKeys) {
        backupMessagesFile(userFile);
        userResource.exportProperties(configurationData);
        logger.debug("Successfully saved {0}", userFile);
        return true;
    }
    return false;
}
Also used : PropertyResource(ch.jalu.configme.resource.PropertyResource) PropertyReader(ch.jalu.configme.resource.PropertyReader)

Example 9 with PropertyReader

use of ch.jalu.configme.resource.PropertyReader in project AuthMeReloaded by AuthMe.

the class MessagesFileWriter method performWrite.

private void performWrite() {
    // Initialize ConfigMe classes
    MessageKeyConfigurationData configurationData = MessageUpdater.createConfigurationData();
    YamlFileResource resource = new MigraterYamlFileResource(file);
    PropertyReader reader = resource.createReader();
    configurationData.initializeValues(reader);
    // Store initial comments so we can add them back later
    List<String> initialComments = getInitialUserComments(configurationData);
    // Create property resource with new defaults, save with ConfigMe for proper sections & comments
    addMissingMessagesWithCommentMarker(reader, configurationData);
    resource.exportProperties(configurationData);
    // Go through the newly saved file and replace texts with comment marker to actual YAML comments
    // and add initial comments back to the file
    rewriteToFileWithComments(initialComments);
}
Also used : YamlFileResource(ch.jalu.configme.resource.YamlFileResource) MigraterYamlFileResource(fr.xephi.authme.message.updater.MigraterYamlFileResource) PropertyReader(ch.jalu.configme.resource.PropertyReader) MessageKeyConfigurationData(fr.xephi.authme.message.updater.MessageKeyConfigurationData) MigraterYamlFileResource(fr.xephi.authme.message.updater.MigraterYamlFileResource)

Example 10 with PropertyReader

use of ch.jalu.configme.resource.PropertyReader in project AuthMeReloaded by AuthMe.

the class TranslationsGatherer method processMessagesFile.

private void processMessagesFile(String code, File file) {
    PropertyReader reader = new YamlFileReader(file);
    int availableMessages = 0;
    for (MessageKey key : MessageKey.values()) {
        if (reader.contains(key.getKey())) {
            ++availableMessages;
        }
    }
    translationInfo.add(new TranslationInfo(code, (double) availableMessages / MessageKey.values().length));
}
Also used : YamlFileReader(ch.jalu.configme.resource.YamlFileReader) MessageKey(fr.xephi.authme.message.MessageKey) PropertyReader(ch.jalu.configme.resource.PropertyReader)

Aggregations

PropertyReader (ch.jalu.configme.resource.PropertyReader)13 Test (org.junit.Test)9 YamlFileReader (ch.jalu.configme.resource.YamlFileReader)7 File (java.io.File)7 PropertyResource (ch.jalu.configme.resource.PropertyResource)3 ArrayList (java.util.ArrayList)2 ConfigurationData (ch.jalu.configme.configurationdata.ConfigurationData)1 StringProperty (ch.jalu.configme.properties.StringProperty)1 YamlFileResource (ch.jalu.configme.resource.YamlFileResource)1 Injector (ch.jalu.injector.Injector)1 InjectorBuilder (ch.jalu.injector.InjectorBuilder)1 AuthMeApi (fr.xephi.authme.api.v3.AuthMeApi)1 CommandHandler (fr.xephi.authme.command.CommandHandler)1 DataSource (fr.xephi.authme.datasource.DataSource)1 BlockListener (fr.xephi.authme.listener.BlockListener)1 MessageKey (fr.xephi.authme.message.MessageKey)1 MessageKeyConfigurationData (fr.xephi.authme.message.updater.MessageKeyConfigurationData)1 MigraterYamlFileResource (fr.xephi.authme.message.updater.MigraterYamlFileResource)1 PermissionsManager (fr.xephi.authme.permission.PermissionsManager)1 Management (fr.xephi.authme.process.Management)1