From 2683bc12c079a69eb5b7924500edea1c23676d43 Mon Sep 17 00:00:00 2001 From: Christian Koop Date: Sun, 26 Jun 2022 01:34:54 +0200 Subject: [PATCH] Fix exception on loading empty file in YamlConfiguration --- .../core/configuration/yaml/YamlConfiguration.java | 5 ++++- .../configuration/yaml/YamlConfigurationTest.java | 12 +++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/Core/src/main/java/com/songoda/core/configuration/yaml/YamlConfiguration.java b/Core/src/main/java/com/songoda/core/configuration/yaml/YamlConfiguration.java index 5bbeed17..04d42701 100644 --- a/Core/src/main/java/com/songoda/core/configuration/yaml/YamlConfiguration.java +++ b/Core/src/main/java/com/songoda/core/configuration/yaml/YamlConfiguration.java @@ -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) { diff --git a/Core/src/test/java/com/songoda/core/configuration/yaml/YamlConfigurationTest.java b/Core/src/test/java/com/songoda/core/configuration/yaml/YamlConfigurationTest.java index a4071a09..e0ccccf1 100644 --- a/Core/src/test/java/com/songoda/core/configuration/yaml/YamlConfigurationTest.java +++ b/Core/src/test/java/com/songoda/core/configuration/yaml/YamlConfigurationTest.java @@ -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