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);
}
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]);
}
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");
}
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();
}
}
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;
}
}
Aggregations