Fix exception on loading empty file in YamlConfiguration

This commit is contained in:
Christian Koop 2022-06-26 01:34:54 +02:00
parent 2262652577
commit 2683bc12c0
No known key found for this signature in database
GPG Key ID: 89A8181384E010A3
2 changed files with 15 additions and 2 deletions

View File

@ -183,9 +183,12 @@ public class YamlConfiguration implements IConfiguration, HeaderCommentable, Nod
@Override
public void load(Reader reader) {
Object yamlData = this.yaml.load(reader);
if (yamlData == null) {
yamlData = Collections.emptyMap();
}
if (!(yamlData instanceof Map)) {
throw new IllegalStateException("The YAML file does not have the expected tree structure");
throw new IllegalStateException("The YAML file does not have the expected tree structure: " + yamlData.getClass().getName());
}
synchronized (this.values) {

View File

@ -103,6 +103,16 @@ class YamlConfigurationTest {
assertEquals(3, ((List<?>) cfg.get("primitives.set")).size());
}
@Test
void testYamlParserWithEmptyFile() {
final YamlConfiguration cfg = new YamlConfiguration();
cfg.load(new StringReader(""));
assertTrue(cfg.getKeys("").isEmpty());
cfg.load(new StringReader("\n"));
assertTrue(cfg.getKeys("").isEmpty());
}
@Test
void testYamlParserWithDuplicateKeys() {
assertThrowsExactly(DuplicateKeyException.class,
@ -524,7 +534,7 @@ class YamlConfigurationTest {
IllegalStateException exception = assertThrowsExactly(IllegalStateException.class,
() -> cfg.load(new StringReader("Hello world")));
assertEquals("The YAML file does not have the expected tree structure", exception.getMessage());
assertEquals("The YAML file does not have the expected tree structure: java.lang.String", exception.getMessage());
}
@Test