mirror of
https://github.com/songoda/SongodaCore.git
synced 2024-11-30 05:53:35 +01:00
Fix exception on loading empty file in YamlConfiguration
This commit is contained in:
parent
2262652577
commit
2683bc12c0
@ -183,9 +183,12 @@ public class YamlConfiguration implements IConfiguration, HeaderCommentable, Nod
|
|||||||
@Override
|
@Override
|
||||||
public void load(Reader reader) {
|
public void load(Reader reader) {
|
||||||
Object yamlData = this.yaml.load(reader);
|
Object yamlData = this.yaml.load(reader);
|
||||||
|
if (yamlData == null) {
|
||||||
|
yamlData = Collections.emptyMap();
|
||||||
|
}
|
||||||
|
|
||||||
if (!(yamlData instanceof Map)) {
|
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) {
|
synchronized (this.values) {
|
||||||
|
@ -103,6 +103,16 @@ class YamlConfigurationTest {
|
|||||||
assertEquals(3, ((List<?>) cfg.get("primitives.set")).size());
|
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
|
@Test
|
||||||
void testYamlParserWithDuplicateKeys() {
|
void testYamlParserWithDuplicateKeys() {
|
||||||
assertThrowsExactly(DuplicateKeyException.class,
|
assertThrowsExactly(DuplicateKeyException.class,
|
||||||
@ -524,7 +534,7 @@ class YamlConfigurationTest {
|
|||||||
IllegalStateException exception = assertThrowsExactly(IllegalStateException.class,
|
IllegalStateException exception = assertThrowsExactly(IllegalStateException.class,
|
||||||
() -> cfg.load(new StringReader("Hello world")));
|
() -> 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
|
@Test
|
||||||
|
Loading…
Reference in New Issue
Block a user