|
| 1 | +package ch.jalu.configme.resource; |
| 2 | + |
| 3 | +import ch.jalu.configme.SettingsHolder; |
| 4 | +import ch.jalu.configme.TestUtils; |
| 5 | +import ch.jalu.configme.configurationdata.CommentsConfiguration; |
| 6 | +import ch.jalu.configme.configurationdata.ConfigurationData; |
| 7 | +import ch.jalu.configme.configurationdata.ConfigurationDataBuilder; |
| 8 | +import ch.jalu.configme.properties.Property; |
| 9 | +import ch.jalu.configme.properties.StringProperty; |
| 10 | +import org.jetbrains.annotations.NotNull; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | +import org.junit.jupiter.api.io.TempDir; |
| 13 | + |
| 14 | +import java.io.IOException; |
| 15 | +import java.nio.file.Files; |
| 16 | +import java.nio.file.Path; |
| 17 | +import java.util.*; |
| 18 | + |
| 19 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 20 | +import static org.hamcrest.Matchers.contains; |
| 21 | + |
| 22 | +public class YamlFileResourceFooterCommentTest { |
| 23 | + |
| 24 | + @TempDir |
| 25 | + public Path tempFolder; |
| 26 | + |
| 27 | + @Test |
| 28 | + void shouldIncludeFooterCommentInYamlWithMultipleProperties() throws IOException { |
| 29 | + // given |
| 30 | + Path file = TestUtils.createTemporaryFile(tempFolder); |
| 31 | + |
| 32 | + YamlFileResource yamlResource = new YamlFileResource(file); |
| 33 | + ConfigurationData configurationData = |
| 34 | + ConfigurationDataBuilder.createConfiguration(RootPropertyHolderWithFooter.class); |
| 35 | + configurationData.setValue(RootPropertyHolderWithFooter.PROPERTY_NAME, "ExampleProperty"); |
| 36 | + configurationData.setValue(RootPropertyHolderWithFooter.PROPERTY_NAME2, "ExampleProperty2"); |
| 37 | + |
| 38 | + // when |
| 39 | + yamlResource.exportProperties(configurationData); |
| 40 | + |
| 41 | + // then |
| 42 | + List<String> lines = Files.readAllLines(file); |
| 43 | + assertThat(lines, contains( |
| 44 | + "# Header comment line 1", |
| 45 | + "# Header comment line 2", |
| 46 | + "PropertyName: ExampleProperty", |
| 47 | + "PropertyName2: ExampleProperty2", |
| 48 | + "# Footer comment line 1", |
| 49 | + "# Footer comment line 2" |
| 50 | + )); |
| 51 | + } |
| 52 | + |
| 53 | + |
| 54 | + @Test |
| 55 | + void shouldIncludeFooterCommentForSingleRootProperty() throws IOException { |
| 56 | + // given |
| 57 | + Path file = TestUtils.createTemporaryFile(tempFolder); |
| 58 | + |
| 59 | + YamlFileResource yamlResource = new YamlFileResource(file); |
| 60 | + ConfigurationData configurationData = |
| 61 | + ConfigurationDataBuilder.createConfiguration(RootPropertyHolderSingleRootProperty.class); |
| 62 | + configurationData.setValue(RootPropertyHolderSingleRootProperty.ROOT_PROPERTY, "RootValue"); |
| 63 | + |
| 64 | + // when |
| 65 | + yamlResource.exportProperties(configurationData); |
| 66 | + |
| 67 | + // then |
| 68 | + List<String> lines = Files.readAllLines(file); |
| 69 | + assertThat(lines, contains( |
| 70 | + "# Header comment line 1", |
| 71 | + "# Header comment line 2", |
| 72 | + "RootValue", |
| 73 | + "# Footer comment line 1", |
| 74 | + "# Footer comment line 2" |
| 75 | + )); |
| 76 | + } |
| 77 | + |
| 78 | + public static final class RootPropertyHolderSingleRootProperty implements SettingsHolder { |
| 79 | + |
| 80 | + public static final Property<String> ROOT_PROPERTY = |
| 81 | + new StringProperty("", "DefaultValue"); |
| 82 | + |
| 83 | + private RootPropertyHolderSingleRootProperty() { |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public void registerComments(@NotNull CommentsConfiguration conf) { |
| 88 | + conf.setComment("", "Header comment line 1", "Header comment line 2"); |
| 89 | + conf.setFooterComments("Footer comment line 1", "Footer comment line 2"); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + public static final class RootPropertyHolderWithFooter implements SettingsHolder { |
| 94 | + |
| 95 | + public static final Property<String> PROPERTY_NAME = |
| 96 | + new StringProperty("PropertyName", "DefaultPropertyValue"); |
| 97 | + |
| 98 | + public static final Property<String> PROPERTY_NAME2 = |
| 99 | + new StringProperty("PropertyName2", "DefaultPropertyValue2"); |
| 100 | + |
| 101 | + private RootPropertyHolderWithFooter() { |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void registerComments(@NotNull CommentsConfiguration conf) { |
| 106 | + conf.setComment("", "Header comment line 1", "Header comment line 2"); |
| 107 | + conf.setFooterComments("Footer comment line 1", "Footer comment line 2"); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | +} |
0 commit comments