Search in sources :

Example 1 with YAML

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

the class ConfigurationTest method testLoadConfig.

@Test
public void testLoadConfig() throws Exception {
    Configuration invalidConfiguration = spy(new Configuration(mockedLogger, new File(System.getProperty("user.dir")), 1, "NotFound.yml"));
    when(invalidConfiguration.newConfigCreated()).thenReturn(true);
    Field configBaseDir = Configuration.class.getDeclaredField("configBaseDir");
    Field configFile = Configuration.class.getDeclaredField("configFile");
    configBaseDir.setAccessible(true);
    configFile.setAccessible(true);
    File currentConfigBaseDir = (File) configBaseDir.get(invalidConfiguration);
    File currentConfigFile = (File) configFile.get(invalidConfiguration);
    File mockedFile = spy(currentConfigFile);
    when(mockedFile.exists()).thenReturn(false);
    when(mockedFile.mkdir()).thenReturn(false);
    configBaseDir.set(invalidConfiguration, mockedFile);
    configFile.set(invalidConfiguration, mockedFile);
    FileOutputStream mockedFileOutputStream = mock(FileOutputStream.class);
    whenNew(FileOutputStream.class).withAnyArguments().thenReturn(mockedFileOutputStream);
    PowerMockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
            return null;
        }
    }).when(mockedFileOutputStream).flush();
    mockStatic(ByteStreams.class);
    when(ByteStreams.copy(any(FileInputStream.class), any(FileOutputStream.class))).thenReturn((long) 0);
    invalidConfiguration.reload();
    when(mockedFile.mkdir()).thenReturn(true);
    whenNew(YAML.class).withAnyArguments().thenReturn(null);
    invalidConfiguration.reload();
    configBaseDir.set(invalidConfiguration, currentConfigBaseDir);
    configFile.set(invalidConfiguration, currentConfigFile);
    configBaseDir.setAccessible(false);
    configFile.setAccessible(false);
    assertEquals("The version should not be found", -1, invalidConfiguration.getVersion());
    mockStatic(LanguageUpdateMethod.class);
    given(LanguageUpdateMethod.valueOf(anyString())).willThrow(new IllegalArgumentException());
    YAML mockedYAML = mock(YAML.class);
    when(mockedYAML.getString(anyString(), anyString())).thenReturn("SOMETHING");
    Field configYAML = TestUtils.setAccessible(Configuration.class, invalidConfiguration, "config", mockedYAML);
    assertEquals("The language update mode should not be found", LanguageUpdateMethod.UPGRADE, invalidConfiguration.getLanguageUpdateMode());
    TestUtils.setUnaccessible(configYAML, invalidConfiguration, false);
}
Also used : Field(java.lang.reflect.Field) InvocationOnMock(org.mockito.invocation.InvocationOnMock) YAML(at.pcgamingfreaks.yaml.YAML) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with YAML

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

the class ConfigurationTest method testSaveError.

@Test
@SuppressWarnings("ResultOfMethodCallIgnored")
@PrepareForTest({ ByteStreams.class, Configuration.class })
public void testSaveError() throws Exception {
    final int[] loggerObject = { 0 };
    mockStatic(ByteStreams.class);
    when(ByteStreams.copy(any(FileInputStream.class), any(FileOutputStream.class))).thenReturn((long) 0);
    Logger mockedLogger = mock(Logger.class);
    YAML mockedYAML = mock(YAML.class);
    whenNew(YAML.class).withArguments(File.class).thenReturn(mockedYAML);
    Configuration configuration = spy(new Configuration(mockedLogger, new File(System.getProperty("user.dir")), 1, 10, "NOT_HERE.cfg"));
    new File("NOT_HERE.cfg").delete();
    doReturn(true).when(configuration).newConfigCreated();
    doThrow(new FileNotFoundException()).when(configuration).saveConfig();
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            loggerObject[0]++;
            return null;
        }
    }).when(mockedLogger).warning(anyString());
    configuration.reload();
    assertEquals("The error should be logged", 1, loggerObject[0]);
}
Also used : Logger(java.util.logging.Logger) YAML(at.pcgamingfreaks.yaml.YAML) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 3 with YAML

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

the class ConfigurationTest method testSaveConfigWithError.

@Test(expected = YAMLKeyNotFoundException.class)
public void testSaveConfigWithError() throws Exception {
    YAML mockedYAML = mock(YAML.class);
    doThrow(new YAMLNotInitializedException()).when(mockedYAML).save((File) anyObject());
    Configuration configuration = new Configuration(mockedLogger, new File(System.getProperty("user.dir")), 1);
    configuration.set("NotSavedValue", false);
    Field configurationYAML = Configuration.class.getDeclaredField("config");
    configurationYAML.setAccessible(true);
    YAML currentConfiguration = (YAML) configurationYAML.get(configuration);
    configurationYAML.set(configuration, mockedYAML);
    configuration.saveConfig();
    configurationYAML.set(configuration, currentConfiguration);
    configurationYAML.setAccessible(false);
    configuration.reload();
    configuration.getBool("NotSavedValue");
}
Also used : Field(java.lang.reflect.Field) YAML(at.pcgamingfreaks.yaml.YAML) YAMLNotInitializedException(at.pcgamingfreaks.yaml.YAMLNotInitializedException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with YAML

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

the class Language method loadLang.

private void loadLang() {
    langFile = new File(baseDir, prefix + language + ".yml");
    if (!langFile.exists() || langFile.length() == 0) {
        extractLangFile();
    }
    try {
        lang = new YAML(langFile);
        updateLang();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : YAML(at.pcgamingfreaks.yaml.YAML) YAMLNotInitializedException(at.pcgamingfreaks.yaml.YAMLNotInitializedException)

Example 5 with YAML

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

the class Configuration method upgradeConfig.

private void upgradeConfig() {
    try {
        int oldVersion = getVersion();
        File oldConfig = new File(configFile + ".old_v" + oldVersion);
        if (oldConfig.exists() && !oldConfig.delete()) {
            logger.warning("Failed to delete old config backup!");
        }
        if (!configFile.renameTo(oldConfig)) {
            logger.warning("Failed to rename old config! Could not do upgrade!");
            return;
        }
        YAML oldYAML = config;
        loadConfig();
        if (isLoaded()) {
            doUpgrade(new Configuration(logger, configBaseDir, oldVersion, -1, configPath + ".old_v" + oldVersion, inJarPrefix, oldYAML));
        }
    } catch (Exception e) {
        e.printStackTrace();
        config = null;
    }
}
Also used : YAML(at.pcgamingfreaks.yaml.YAML) YAMLNotInitializedException(at.pcgamingfreaks.yaml.YAMLNotInitializedException) YAMLInvalidContentException(at.pcgamingfreaks.yaml.YAMLInvalidContentException) YAMLKeyNotFoundException(at.pcgamingfreaks.yaml.YAMLKeyNotFoundException) NoSuchElementException(java.util.NoSuchElementException)

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