From 78a5b013254893f94fa3934213dd8059dd00b612 Mon Sep 17 00:00:00 2001 From: ljacqu Date: Mon, 19 Apr 2021 15:21:15 +0200 Subject: [PATCH] #191 Add test for map property using bean type on root path --- .../beanmapper/BeanMapOnRootLevelTest.java | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 src/test/java/ch/jalu/configme/beanmapper/BeanMapOnRootLevelTest.java diff --git a/src/test/java/ch/jalu/configme/beanmapper/BeanMapOnRootLevelTest.java b/src/test/java/ch/jalu/configme/beanmapper/BeanMapOnRootLevelTest.java new file mode 100644 index 00000000..62a1fd7b --- /dev/null +++ b/src/test/java/ch/jalu/configme/beanmapper/BeanMapOnRootLevelTest.java @@ -0,0 +1,94 @@ +package ch.jalu.configme.beanmapper; + +import ch.jalu.configme.SettingsHolder; +import ch.jalu.configme.SettingsManager; +import ch.jalu.configme.SettingsManagerBuilder; +import ch.jalu.configme.TestUtils; +import ch.jalu.configme.properties.MapProperty; +import ch.jalu.configme.properties.types.BeanPropertyType; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import java.util.Map; + +import static ch.jalu.configme.properties.PropertyInitializer.mapProperty; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.equalTo; + +/** + * Tests that a map property can be used in the root with a bean type as values. + * + * @see Issue #191 + */ +class BeanMapOnRootLevelTest { + + @TempDir + Path tempDir; + + @Test + void shouldLoadMap() throws IOException { + // given + String yaml = "basic:\n" + + "\n name: \"hello\"\n" + + "\n lore:\n" + + "\n - \"world\"" + + "\n - \"moon\""; + Path tempFile = TestUtils.createTemporaryFile(tempDir); + Files.write(tempFile, yaml.getBytes()); + + // when + SettingsManager settingsManager = SettingsManagerBuilder.withYamlFile(tempFile) + .configurationData(TestSettingsHolder.class) + .create(); + + // then + Map result = settingsManager.getProperty(TestSettingsHolder.INFO); + assertThat(result.keySet(), contains("basic")); + assertThat(result.get("basic").getName(), equalTo("hello")); + assertThat(result.get("basic").getLore(), contains("world", "moon")); + } + + public static final class TestSettingsHolder implements SettingsHolder { + + public static final MapProperty INFO = mapProperty(BeanPropertyType.of(Info.class)) + .path("") + .defaultEntry("default", new Info("default", "def")) + .build(); + + private TestSettingsHolder() { + } + } + + public static final class Info { + + private String name; + private List lore; + + public Info(String name, String... lore) { + } + + public Info() { + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public List getLore() { + return lore; + } + + public void setLore(List lore) { + this.lore = lore; + } + } +}