Search in sources :

Example 6 with YAML

use of at.pcgamingfreaks.yaml.YAML in project Bukkit_Bungee_PluginLib by GeorgH93.

the class Configuration method loadConfig.

private void loadConfig() {
    try {
        if (!configFile.exists() || configFile.length() == 0) {
            logger.info("No config found. Create new one ...");
            if (!configBaseDir.exists() && !configBaseDir.mkdir()) {
                logger.warning("Couldn't create directory. " + configBaseDir.toString());
            }
            try (InputStream is = getClass().getResourceAsStream("/" + inJarPrefix + configPath);
                OutputStream os = new FileOutputStream(configFile)) {
                ByteStreams.copy(is, os);
                os.flush();
            } catch (NullPointerException | IOException e) {
                logger.warning(ConsoleColor.RED + "Failed to extract configuration!" + ConsoleColor.RESET);
                return;
            }
            logger.info("Configuration extracted successfully!");
            config = new YAML(configFile);
            if (newConfigCreated()) {
                try {
                    saveConfig();
                } catch (FileNotFoundException e) {
                    logger.warning(ConsoleColor.RED + "Failed to save the config cause the file does not exist! Has it been deleted?" + ConsoleColor.RESET);
                }
            }
        } else {
            config = new YAML(configFile);
            updateConfig();
        }
    } catch (IOException | YAMLInvalidContentException | NoSuchElementException e) {
        logger.warning(ConsoleColor.RED + "Failed to read the config file! Please check it!" + ConsoleColor.RESET);
        e.printStackTrace();
    }
}
Also used : YAMLInvalidContentException(at.pcgamingfreaks.yaml.YAMLInvalidContentException) YAML(at.pcgamingfreaks.yaml.YAML) NoSuchElementException(java.util.NoSuchElementException)

Example 7 with YAML

use of at.pcgamingfreaks.yaml.YAML in project Bukkit_Bungee_PluginLib by GeorgH93.

the class Language method updateLang.

private boolean updateLang() {
    if (getVersion() < expectedVersion) {
        if (getVersion() < upgradeThreshold || updateMode == LanguageUpdateMethod.UPGRADE) {
            logger.info("Language version: " + getVersion() + " => Language outdated! Upgrading ...");
            try {
                int oldVersion = getVersion();
                File oldLang = new File(langFile + ".old_v" + oldVersion);
                if (oldLang.exists() && !oldLang.delete()) {
                    logger.warning("Failed to delete old language file backup!");
                    return false;
                }
                if (!langFile.renameTo(oldLang)) {
                    logger.warning("Failed to rename old language file! Could not do upgrade!");
                    return false;
                }
                YAML oldYAML = lang;
                loadLang();
                if (isLoaded()) {
                    doUpgrade(new Language(logger, baseDir, oldVersion, -1, path + ".old_v" + oldVersion, prefix, inJarPrefix, oldYAML));
                }
            } catch (Exception e) {
                e.printStackTrace();
                lang = null;
                return false;
            }
        } else {
            logger.info("Language version: " + getVersion() + " => Language outdated! Updating ...");
            if (updateMode == LanguageUpdateMethod.OVERWRITE) {
                extractLangFile();
                loadLang();
                logger.info("Language file has been updated.");
            } else {
                doUpdate();
                lang.set("Version", expectedVersion);
                try {
                    save();
                    logger.info("Language file has been updated.");
                } catch (Exception e) {
                    e.printStackTrace();
                    return false;
                }
            }
        }
        return true;
    }
    if (expectedVersion < getVersion()) {
        logger.warning("Language file version newer than expected!");
    }
    return false;
}
Also used : YAML(at.pcgamingfreaks.yaml.YAML) YAMLNotInitializedException(at.pcgamingfreaks.yaml.YAMLNotInitializedException)

Example 8 with YAML

use of at.pcgamingfreaks.yaml.YAML in project Bukkit_Bungee_PluginLib by GeorgH93.

the class ConfigurationTest method testBasicConfigurationFile.

@Test
public void testBasicConfigurationFile() throws IOException, YAMLInvalidContentException, YAMLKeyNotFoundException, NoSuchFieldException, IllegalAccessException {
    setConfigFile();
    Configuration configuration = new Configuration(mockedLogger, new File(System.getProperty("user.dir")), 5);
    assertNotNull("The configuration file must not be null", configuration);
    Field configField = TestUtils.setAccessible(Configuration.class, configuration, "config", null);
    assertFalse("The configuration file should not be loaded", configuration.isLoaded());
    TestUtils.setUnaccessible(configField, configuration, false);
    assertTrue("The configuration file should be loaded", configuration.isLoaded());
    YAML config = configuration.getConfig();
    assertEquals("The test string value should be found in the test config file", "Another Test", config.getString("TestConfig.Value2"));
    assertEquals("The test int value should be found in the test config file", 5, config.getInt("Version"));
    assertEquals("The test boolean value should be found in the test config file", false, config.getBoolean("TestBoolean"));
    assertEquals("The test double value should be found in the test config file", 2.6, config.getDouble("TestDouble"), 0.1);
    assertEquals("The returned string value should match", config.getString("TestConfig.Value2"), configuration.getString("TestConfig.Value2"));
    assertEquals("The returned integer value should match", config.getInt("Version"), configuration.getInt("Version"));
    assertEquals("The returned boolean value should match", config.getBoolean("TestBoolean"), configuration.getBool("TestBoolean"));
    assertEquals("The returned double value should match", config.getDouble("TestDouble"), configuration.getDouble("TestDouble"), 0.1);
    assertEquals("The returned string value should match", "Not there", configuration.getString("Nothing", "Not there"));
    assertEquals("The returned integer value should match", -1, configuration.getInt("Nothing", -1));
    assertEquals("The returned boolean value should match", true, configuration.getBool("Nothing", true));
    assertEquals("The returned double value should match", 1.0, configuration.getDouble("Nothing", 1.0), 0.1);
    config.set("TestConfig.Value3", "String");
    config.set("VersionX", 3);
    config.set("TestBoolean2", true);
    config.set("TestDouble2", 3.1415);
    configuration.set("TestConfig.Value2", config.getString("TestConfig.Value3"));
    configuration.set("Version", config.getInt("VersionX"));
    configuration.set("TestBoolean", config.getBoolean("TestBoolean2"));
    configuration.set("TestDouble", config.getDouble("TestDouble2"));
    assertEquals("The returned string value should match after setting the new value", config.getString("TestConfig.Value3"), configuration.getString("TestConfig.Value2"));
    assertEquals("The returned integer value should match after setting the new value", config.getInt("VersionX"), configuration.getInt("Version"));
    assertEquals("The returned boolean value should match after setting the new value", config.getBoolean("TestBoolean2"), configuration.getBool("TestBoolean"));
    assertEquals("The returned double value should match after setting the new value", config.getDouble("TestDouble2"), configuration.getDouble("TestDouble"), 0.1);
    configuration.reload();
    YAML copiedConfig = new YAML(new File(System.getProperty("user.dir"), "config.yml"));
    assertEquals("The returned string value should match after reload", copiedConfig.getString("TestConfig.Value2"), configuration.getString("TestConfig.Value2"));
    assertEquals("The returned integer value should match after reload", copiedConfig.getInt("Version"), configuration.getInt("Version"));
    assertEquals("The returned boolean value should match after reload", copiedConfig.getBoolean("TestBoolean"), configuration.getBool("TestBoolean"));
    assertEquals("The returned double value should match after reload", copiedConfig.getDouble("TestDouble"), configuration.getDouble("TestDouble"), 0.1);
    assertEquals("The language element should be found in the configuration", config.getString("Language"), configuration.getLanguage());
    assertEquals("The language update method should be found in the configuration", LanguageUpdateMethod.valueOf(config.getString("LanguageUpdateMode").toUpperCase()), configuration.getLanguageUpdateMode());
}
Also used : Field(java.lang.reflect.Field) YAML(at.pcgamingfreaks.yaml.YAML) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 9 with YAML

use of at.pcgamingfreaks.yaml.YAML in project Bukkit_Bungee_PluginLib by GeorgH93.

the class LanguageTest method testSave.

@Test
public void testSave() throws FileNotFoundException, NoSuchFieldException, IllegalAccessException, YAMLNotInitializedException {
    File userDir = new File(System.getProperty("user.dir"));
    Language language = PowerMockito.spy(new Language(mockedLogger, userDir, 1));
    YAML mockedYAML = mock(YAML.class);
    doThrow(new YAMLNotInitializedException()).when(mockedYAML).save(any(File.class));
    Field lang = TestUtils.setAccessible(Language.class, language, "lang", mockedYAML);
    language.save();
    TestUtils.setUnaccessible(lang, language, false);
}
Also used : Field(java.lang.reflect.Field) File(java.io.File) YAML(at.pcgamingfreaks.yaml.YAML) YAMLNotInitializedException(at.pcgamingfreaks.yaml.YAMLNotInitializedException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

YAML (at.pcgamingfreaks.yaml.YAML)9 YAMLNotInitializedException (at.pcgamingfreaks.yaml.YAMLNotInitializedException)5 Test (org.junit.Test)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 Field (java.lang.reflect.Field)4 YAMLInvalidContentException (at.pcgamingfreaks.yaml.YAMLInvalidContentException)2 NoSuchElementException (java.util.NoSuchElementException)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 YAMLKeyNotFoundException (at.pcgamingfreaks.yaml.YAMLKeyNotFoundException)1 File (java.io.File)1 Logger (java.util.logging.Logger)1 Mockito.doAnswer (org.mockito.Mockito.doAnswer)1 Answer (org.mockito.stubbing.Answer)1