From ddfaf83f0b4673727a6344b0f27778888df912f6 Mon Sep 17 00:00:00 2001 From: Federico Herrera Date: Fri, 17 Nov 2023 22:18:03 -0300 Subject: [PATCH 1/5] Add property type and extend test, fixes#380 --- .../configme/properties/types/InlineArrayPropertyType.java | 5 +++++ .../properties/types/InlineArrayPropertyTypeTest.java | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/src/main/java/ch/jalu/configme/properties/types/InlineArrayPropertyType.java b/src/main/java/ch/jalu/configme/properties/types/InlineArrayPropertyType.java index ad7dfde0..dfc6ee55 100644 --- a/src/main/java/ch/jalu/configme/properties/types/InlineArrayPropertyType.java +++ b/src/main/java/ch/jalu/configme/properties/types/InlineArrayPropertyType.java @@ -5,6 +5,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.math.BigDecimal; import java.util.Arrays; import java.util.Objects; import java.util.function.Function; @@ -60,6 +61,10 @@ public class InlineArrayPropertyType implements PropertyType { public static final InlineArrayPropertyType STRING = new InlineArrayPropertyType<>(StringType.STRING, "\n", false, String[]::new); + /** Big Decimal values, comma-separated. */ + public static final InlineArrayPropertyType BIG_DECIMAL = + new InlineArrayPropertyType<>(NumberType.BIG_DECIMAL, ",", true, BigDecimal[]::new); + private final PropertyType entryType; private final String separator; diff --git a/src/test/java/ch/jalu/configme/properties/types/InlineArrayPropertyTypeTest.java b/src/test/java/ch/jalu/configme/properties/types/InlineArrayPropertyTypeTest.java index bb1b5cbd..b156a073 100644 --- a/src/test/java/ch/jalu/configme/properties/types/InlineArrayPropertyTypeTest.java +++ b/src/test/java/ch/jalu/configme/properties/types/InlineArrayPropertyTypeTest.java @@ -10,6 +10,7 @@ import java.lang.reflect.Field; import java.lang.reflect.Modifier; +import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; @@ -170,6 +171,9 @@ private static TestData getTestData(InlineArrayPropertyType converter) { String someString = "An even longer String\twith a tab"; testData.setInputWithErrors(someString + "\n\n", someString, "", ""); + } else if (converter == InlineArrayPropertyType.BIG_DECIMAL) { + testData.setInputAndExpected("3, 4.5, -445.68234", "3, 4.5, -445.68234", BigDecimal.valueOf(3), BigDecimal.valueOf(4.5), BigDecimal.valueOf(-445.68234)); + testData.setInputWithErrors("3, a, 4.5, -2, -b", BigDecimal.valueOf(3), BigDecimal.valueOf(4.5), BigDecimal.valueOf(-2)); } else { throw new IllegalStateException("Unhandled converter '" + converter + "'"); } From 4acbcfc91b61f2a29c95cb3e955b2e7979db477a Mon Sep 17 00:00:00 2001 From: Federico Herrera Date: Mon, 20 Nov 2023 15:32:54 -0300 Subject: [PATCH 2/5] Add empty constructor for MapProperty --- .../jalu/configme/properties/MapProperty.java | 12 +++ .../YamlFileResourceRootMapPropertyTest.java | 78 +++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 src/test/java/ch/jalu/configme/resource/YamlFileResourceRootMapPropertyTest.java diff --git a/src/main/java/ch/jalu/configme/properties/MapProperty.java b/src/main/java/ch/jalu/configme/properties/MapProperty.java index b5c17749..e9460d56 100644 --- a/src/main/java/ch/jalu/configme/properties/MapProperty.java +++ b/src/main/java/ch/jalu/configme/properties/MapProperty.java @@ -21,6 +21,18 @@ public class MapProperty extends BaseProperty> { private final PropertyType valueType; + /** + * Constructor. Build a {@link MapProperty} with empty default values. + * + * @param path the path of the property + * @param valueType the property type of the values + */ + public MapProperty(@NotNull String path, @NotNull PropertyType valueType) { + super(path, Collections.emptyMap()); + Objects.requireNonNull(valueType, "valueType"); + this.valueType = valueType; + } + /** * Constructor. * diff --git a/src/test/java/ch/jalu/configme/resource/YamlFileResourceRootMapPropertyTest.java b/src/test/java/ch/jalu/configme/resource/YamlFileResourceRootMapPropertyTest.java new file mode 100644 index 00000000..09ddd985 --- /dev/null +++ b/src/test/java/ch/jalu/configme/resource/YamlFileResourceRootMapPropertyTest.java @@ -0,0 +1,78 @@ +package ch.jalu.configme.resource; + +import ch.jalu.configme.SettingsHolder; +import ch.jalu.configme.TestUtils; +import ch.jalu.configme.configurationdata.ConfigurationData; +import ch.jalu.configme.configurationdata.ConfigurationDataBuilder; +import ch.jalu.configme.properties.MapProperty; +import ch.jalu.configme.properties.Property; +import ch.jalu.configme.properties.types.BeanPropertyType; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static ch.jalu.configme.properties.PropertyInitializer.newListProperty; +import static java.util.Collections.singletonList; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; + +class YamlFileResourceRootMapPropertyTest { + + @TempDir + public Path temporaryFolder; + + @Test + void shouldWriteAndReadFile() { + Path yamlFile = TestUtils.createTemporaryFile(temporaryFolder); + + YamlFileResource resource = new YamlFileResource(yamlFile); + ConfigurationData configurationData = ConfigurationDataBuilder.createConfiguration(SampleConfig.class); + + configurationData.setValue(SampleConfig.SHAPE, singletonList("foo")); + + Map mapProperty = new HashMap<>(); + mapProperty.put("props", new InnerProperties()); + mapProperty.get("props").setProps(Arrays.asList("bar", "baz")); + + configurationData.setValue(SampleConfig.MAP_PROPERTY, mapProperty); + + resource.exportProperties(configurationData); + + List actualShapeProperty = configurationData.getValue(SampleConfig.SHAPE); + Map actualMapProperty = configurationData.getValue(SampleConfig.MAP_PROPERTY); + + assertThat(actualShapeProperty, equalTo(singletonList("foo"))); + assertThat(actualMapProperty, equalTo(mapProperty)); + } + + public static final class SampleConfig implements SettingsHolder { + + public static final Property> SHAPE = newListProperty("shape"); + + public static final Property> MAP_PROPERTY = new MapProperty<>( + "map", + BeanPropertyType.of(InnerProperties.class) + ); + + private SampleConfig() { + } + } + + public static class InnerProperties { + private List props = new ArrayList<>(); + + public List getProps() { + return props; + } + + public void setProps(List props) { + this.props = props; + } + } +} From f2e8877a580d52bfb5de603c2f391f89bbe3930d Mon Sep 17 00:00:00 2001 From: Federico Herrera Date: Tue, 21 Nov 2023 12:54:29 -0300 Subject: [PATCH 3/5] Add unit test for constructor, reformat test according to feedback --- .../jalu/configme/properties/MapProperty.java | 2 +- .../configme/properties/MapPropertyTest.java | 16 ++++++++++ .../YamlFileResourceRootMapPropertyTest.java | 30 +++++++++---------- 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/src/main/java/ch/jalu/configme/properties/MapProperty.java b/src/main/java/ch/jalu/configme/properties/MapProperty.java index e9460d56..cbf44f7c 100644 --- a/src/main/java/ch/jalu/configme/properties/MapProperty.java +++ b/src/main/java/ch/jalu/configme/properties/MapProperty.java @@ -22,7 +22,7 @@ public class MapProperty extends BaseProperty> { private final PropertyType valueType; /** - * Constructor. Build a {@link MapProperty} with empty default values. + * Constructor. Builds a {@link MapProperty} with an empty map as default value. * * @param path the path of the property * @param valueType the property type of the values diff --git a/src/test/java/ch/jalu/configme/properties/MapPropertyTest.java b/src/test/java/ch/jalu/configme/properties/MapPropertyTest.java index 806fde84..a243c274 100644 --- a/src/test/java/ch/jalu/configme/properties/MapPropertyTest.java +++ b/src/test/java/ch/jalu/configme/properties/MapPropertyTest.java @@ -3,6 +3,7 @@ import ch.jalu.configme.TestUtils; import ch.jalu.configme.properties.convertresult.ConvertErrorRecorder; import ch.jalu.configme.properties.convertresult.PropertyValue; +import ch.jalu.configme.properties.types.NumberType; import ch.jalu.configme.properties.types.PropertyType; import ch.jalu.configme.properties.types.StringType; import ch.jalu.configme.resource.PropertyReader; @@ -27,6 +28,7 @@ import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.instanceOf; +import static org.hamcrest.collection.IsMapWithSize.anEmptyMap; import static org.mockito.BDDMockito.given; /** @@ -111,6 +113,20 @@ void shouldKeepOrderInExportValue() { assertThat(((Map) exportValue).keySet(), contains("first", "second", "third", "fourth")); } + @Test + void shouldUseEmptyMapAsDefaultValue() { + // given + MapProperty property = new MapProperty<>("test", NumberType.INTEGER); + + //when + Map actualDefaultValue = property.getDefaultValue(); + String actualPath = property.getPath(); + + // then + assertThat(actualDefaultValue, anEmptyMap()); + assertThat(actualPath, equalTo("test")); + } + private static Map createSampleMap() { Map map = new HashMap<>(); map.put("test", "keks"); diff --git a/src/test/java/ch/jalu/configme/resource/YamlFileResourceRootMapPropertyTest.java b/src/test/java/ch/jalu/configme/resource/YamlFileResourceRootMapPropertyTest.java index 09ddd985..7567e9b9 100644 --- a/src/test/java/ch/jalu/configme/resource/YamlFileResourceRootMapPropertyTest.java +++ b/src/test/java/ch/jalu/configme/resource/YamlFileResourceRootMapPropertyTest.java @@ -6,6 +6,7 @@ import ch.jalu.configme.configurationdata.ConfigurationDataBuilder; import ch.jalu.configme.properties.MapProperty; import ch.jalu.configme.properties.Property; +import ch.jalu.configme.properties.convertresult.PropertyValue; import ch.jalu.configme.properties.types.BeanPropertyType; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; @@ -17,10 +18,9 @@ import java.util.List; import java.util.Map; -import static ch.jalu.configme.properties.PropertyInitializer.newListProperty; -import static java.util.Collections.singletonList; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.collection.IsIterableContainingInOrder.contains; class YamlFileResourceRootMapPropertyTest { @@ -29,34 +29,32 @@ class YamlFileResourceRootMapPropertyTest { @Test void shouldWriteAndReadFile() { - Path yamlFile = TestUtils.createTemporaryFile(temporaryFolder); - - YamlFileResource resource = new YamlFileResource(yamlFile); + //given + Path tempFolder = TestUtils.createTemporaryFile(temporaryFolder); + YamlFileResource resource = new YamlFileResource(tempFolder); ConfigurationData configurationData = ConfigurationDataBuilder.createConfiguration(SampleConfig.class); - configurationData.setValue(SampleConfig.SHAPE, singletonList("foo")); - Map mapProperty = new HashMap<>(); mapProperty.put("props", new InnerProperties()); - mapProperty.get("props").setProps(Arrays.asList("bar", "baz")); + mapProperty.get("props").setProps(Arrays.asList("foo", "bar")); configurationData.setValue(SampleConfig.MAP_PROPERTY, mapProperty); - resource.exportProperties(configurationData); - List actualShapeProperty = configurationData.getValue(SampleConfig.SHAPE); - Map actualMapProperty = configurationData.getValue(SampleConfig.MAP_PROPERTY); + //when + PropertyValue> actualPropertyValue = + SampleConfig.MAP_PROPERTY.determineValue(resource.createReader()); - assertThat(actualShapeProperty, equalTo(singletonList("foo"))); - assertThat(actualMapProperty, equalTo(mapProperty)); + //then + assertThat(actualPropertyValue.isValidInResource(), equalTo(true)); + assertThat(actualPropertyValue.getValue().keySet(), contains("props")); + assertThat(actualPropertyValue.getValue().get("props").getProps(), contains("foo", "bar")); } public static final class SampleConfig implements SettingsHolder { - public static final Property> SHAPE = newListProperty("shape"); - public static final Property> MAP_PROPERTY = new MapProperty<>( - "map", + "", BeanPropertyType.of(InnerProperties.class) ); From 22a0cbda67f1034d3a585b3e497220e6a299907d Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 30 Nov 2023 13:08:14 +0100 Subject: [PATCH 4/5] Bump actions/setup-java from 3 to 4 (#401) Bumps [actions/setup-java](https://github.com/actions/setup-java) from 3 to 4. - [Release notes](https://github.com/actions/setup-java/releases) - [Commits](https://github.com/actions/setup-java/compare/v3...v4) --- updated-dependencies: - dependency-name: actions/setup-java dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/maven_jdk11.yml | 2 +- .github/workflows/maven_jdk8.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/maven_jdk11.yml b/.github/workflows/maven_jdk11.yml index 55d5e0b5..3825a980 100644 --- a/.github/workflows/maven_jdk11.yml +++ b/.github/workflows/maven_jdk11.yml @@ -10,7 +10,7 @@ jobs: steps: - uses: actions/checkout@v4 - name: Set up JDK 11 - uses: actions/setup-java@v3 + uses: actions/setup-java@v4 with: java-version: '11' distribution: 'adopt' diff --git a/.github/workflows/maven_jdk8.yml b/.github/workflows/maven_jdk8.yml index a881d443..da46343f 100644 --- a/.github/workflows/maven_jdk8.yml +++ b/.github/workflows/maven_jdk8.yml @@ -13,7 +13,7 @@ jobs: - uses: actions/checkout@v4 - name: Set up JDK 8 - uses: actions/setup-java@v3 + uses: actions/setup-java@v4 with: java-version: '8' distribution: 'adopt' From 518e002426afcf133a419d4d5e09ae8c840ad043 Mon Sep 17 00:00:00 2001 From: gamerover98 <9408687+gamerover98@users.noreply.github.com> Date: Thu, 18 Jan 2024 22:01:26 +0100 Subject: [PATCH 5/5] #405 Export multi-line strings as YAML blocks (#407) * Add multi-line string support in configuration files * Add test for writing multiple lines * Move tests / remove CR and trimming logic for multi-line string exports --- .../yaml/SnakeYamlNodeBuilderImpl.java | 5 +- .../YamlFileResourceMultilineStringTest.java | 202 ++++++++++++++++++ .../yaml/SnakeYamlNodeBuilderImplTest.java | 25 +++ src/test/resources/multiple_lines.yml | 5 + 4 files changed, 236 insertions(+), 1 deletion(-) create mode 100644 src/test/java/ch/jalu/configme/resource/YamlFileResourceMultilineStringTest.java create mode 100644 src/test/resources/multiple_lines.yml diff --git a/src/main/java/ch/jalu/configme/resource/yaml/SnakeYamlNodeBuilderImpl.java b/src/main/java/ch/jalu/configme/resource/yaml/SnakeYamlNodeBuilderImpl.java index 3c8b97f9..2f9368bf 100644 --- a/src/main/java/ch/jalu/configme/resource/yaml/SnakeYamlNodeBuilderImpl.java +++ b/src/main/java/ch/jalu/configme/resource/yaml/SnakeYamlNodeBuilderImpl.java @@ -98,7 +98,10 @@ public void transferComments(@NotNull Node valueNode, @NotNull Node keyNode) { } protected @NotNull Node createStringNode(@NotNull String value) { - return new ScalarNode(Tag.STR, value, null, null, DumperOptions.ScalarStyle.PLAIN); + DumperOptions.ScalarStyle scalarStyle = value.contains("\n") + ? DumperOptions.ScalarStyle.LITERAL // Used for strings that span multiple lines + : DumperOptions.ScalarStyle.PLAIN; // Used for single line string + return new ScalarNode(Tag.STR, value, null, null, scalarStyle); } protected @NotNull Node createNumberNode(@NotNull Number value) { diff --git a/src/test/java/ch/jalu/configme/resource/YamlFileResourceMultilineStringTest.java b/src/test/java/ch/jalu/configme/resource/YamlFileResourceMultilineStringTest.java new file mode 100644 index 00000000..45feb930 --- /dev/null +++ b/src/test/java/ch/jalu/configme/resource/YamlFileResourceMultilineStringTest.java @@ -0,0 +1,202 @@ +package ch.jalu.configme.resource; + +import ch.jalu.configme.TestUtils; +import ch.jalu.configme.configurationdata.ConfigurationData; +import ch.jalu.configme.configurationdata.ConfigurationDataBuilder; +import ch.jalu.configme.properties.Property; +import ch.jalu.configme.properties.PropertyInitializer; +import ch.jalu.configme.properties.StringProperty; +import ch.jalu.configme.properties.convertresult.PropertyValue; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; + +/** + * Tests that multi-line string values are properly read and exported. + * + * @see Issue #405 + */ +class YamlFileResourceMultilineStringTest { + + @TempDir + public Path temporaryFolder; + + @Test + void shouldReadAndExportMultipleLines() throws IOException { + // given + Path configFile = TestUtils.copyFileFromResources("/multiple_lines.yml", temporaryFolder); + PropertyResource resource = new YamlFileResource(configFile); + + Property linesProperty = PropertyInitializer.newProperty("lines", ""); + ConfigurationData configurationData = ConfigurationDataBuilder.createConfiguration(Collections.singletonList(linesProperty)); + configurationData.initializeValues(resource.createReader()); + + // when (read) + String linesValue = configurationData.getValue(linesProperty); + + // then + assertThat(linesValue, equalTo("First row\n\nSecond row\nThird row\n")); + + // when (write) + resource.exportProperties(configurationData); + + // then + String lines = linesProperty.determineValue(resource.createReader()).getValue(); + assertThat(lines, equalTo("First row\n\nSecond row\nThird row\n")); + + byte[] fileBytes = Files.readAllBytes(configFile); + String fileContent = new String(fileBytes, StandardCharsets.UTF_8); + assertThat(fileContent, + equalTo( + "lines: |" + + "\n First row" + + "\n" + + "\n Second row" + + "\n Third row" + + "\n")); + } + + @Test + void shouldWriteMultipleLines() throws IOException { + // given + Path configFile = TestUtils.copyFileFromResources("/empty_file.yml", temporaryFolder); + PropertyResource resource = new YamlFileResource(configFile); + + Property l1Property = PropertyInitializer.newProperty("l1", ""); + Property l2Property = PropertyInitializer.newProperty("l2", ""); + Property l3Property = PropertyInitializer.newProperty("l3", ""); + Property l4Property = PropertyInitializer.newProperty("l4", ""); + + ConfigurationData configurationData = ConfigurationDataBuilder.createConfiguration( + Arrays.asList(l1Property, l2Property, l3Property, l4Property)); + configurationData.initializeValues(resource.createReader()); + + // set multiple-line strings + configurationData.setValue(l1Property, "First row\nSecond row"); + configurationData.setValue(l2Property, "First row\r\nSecond row"); + configurationData.setValue(l3Property, "First text Second text"); + configurationData.setValue(l4Property, "[{\r\n\"enabled\" : true \r\n}]"); + + // when + resource.exportProperties(configurationData); + + // then + PropertyReader propertyReader = resource.createReader(); + String l1 = l1Property.determineValue(propertyReader).getValue(); + assertThat(l1, equalTo("First row\nSecond row")); + + String l2 = l2Property.determineValue(propertyReader).getValue(); + assertThat(l2, equalTo("First row\r\nSecond row")); + + String l3 = l3Property.determineValue(propertyReader).getValue(); + assertThat(l3, equalTo("First text Second text")); + + String l4 = l4Property.determineValue(propertyReader).getValue(); + assertThat(l4, equalTo("[{\r\n\"enabled\" : true \r\n}]")); + + byte[] fileBytes = Files.readAllBytes(configFile); + String fileContent = new String(fileBytes, StandardCharsets.UTF_8); + + assertThat(fileContent, + equalTo( + "l1: |-" + + "\n First row" + + "\n Second row" + + "\nl2: \"First row\\r\\nSecond row\"" + + "\nl3: First text Second text" + + "\nl4: \"[{\\r\\n\\\"enabled\\\" : true \\r\\n}]\"" + + "\n")); + } + + @ParameterizedTest + @MethodSource("getStringsToExport") + void shouldExportIdenticalValue(String value) { + // given + Property property = new StringProperty("test.string", "#Default"); + ConfigurationData configurationData = ConfigurationDataBuilder.createConfiguration(Collections.singletonList(property)); + configurationData.setValue(property, value); + + Path file = temporaryFolder.resolve("test.yml"); + YamlFileResource resource = new YamlFileResource(file); + + // when + resource.exportProperties(configurationData); + configurationData.setValue(property, "#Overwritten"); + configurationData.initializeValues(resource.createReader()); + + // then + assertThat(configurationData.getValue(property), equalTo(value)); + } + + /** + * Multi-line string properties with CRLF are parsed as LF. + */ + @Test + void shouldReadStringWithCarriageReturnsAndDiscardThem() throws IOException { + // given + String contents = "shopping:" + + "\r\n items: |" + + "\r\n Potatoes" + + "\r\n Sausages" + + "\r\n Onions"; + Path file = temporaryFolder.resolve("test.yml"); + Files.write(file, contents.getBytes()); + YamlFileReader reader = new YamlFileReader(file); + + Property property = new StringProperty("shopping.items", "undefined"); + + // when + PropertyValue result = property.determineValue(reader); + + // then + assertThat(result.getValue(), equalTo("Potatoes\nSausages\nOnions")); + assertThat(result.isValidInResource(), equalTo(true)); + } + + /* + * Some of these texts aren't exported as scalar blocks despite having a newline in them; SnakeYAML checks the + * contents and decides whether the literal scalar style can be applied. The purpose of these test cases is to + * ensure that the strings are exported and re-read to the IDENTICAL value (including any and all whitespace), + * and more distantly, to ensure that no values break the export (though that is the responsibility of SnakeYAML). + */ + private static List getStringsToExport() { + List cases = new ArrayList<>(); + + // Whitespace cases + cases.add(" Text with\ninitial whitespace"); + cases.add("Ending spaces\nin this text "); + cases.add("Text with\nending whitespace\n\n"); + cases.add("\nStart with a new line\nEnd with no new line"); + cases.add("\tFirst line\nhas a tab"); + cases.add("Indent the end\nwith a tab\t"); + + // Carriage returns + cases.add("\rCR lines\rCR lines"); + cases.add("More CR lines\r"); + cases.add("Here are\r\nWindows lines"); + cases.add("Another\r\nWin line text\r\n"); + + // Other characters (typically chars that have a special meaning in YAML) + cases.add("Second line\n# looks like a comment"); + cases.add("test: true\nhmm"); + cases.add("??? hi\n--- hello"); + cases.add("!!int 3\n!!string"); + cases.add("|MD table|Col_2|\n|---|---|\n|row1|rowA|\n|row2|rowB|"); + cases.add("\\ testing\n\\|some combinations\\n\n: test"); + + return cases; + } +} diff --git a/src/test/java/ch/jalu/configme/resource/yaml/SnakeYamlNodeBuilderImplTest.java b/src/test/java/ch/jalu/configme/resource/yaml/SnakeYamlNodeBuilderImplTest.java index 8a67f37d..c637328b 100644 --- a/src/test/java/ch/jalu/configme/resource/yaml/SnakeYamlNodeBuilderImplTest.java +++ b/src/test/java/ch/jalu/configme/resource/yaml/SnakeYamlNodeBuilderImplTest.java @@ -70,6 +70,7 @@ void shouldCreateNodeForString() { ScalarNode scalarNode = (ScalarNode) node; assertThat(scalarNode.getTag(), equalTo(Tag.STR)); assertThat(scalarNode.getValue(), equalTo(value)); + assertThat(scalarNode.getScalarStyle(), equalTo(DumperOptions.ScalarStyle.PLAIN)); assertThat(scalarNode.getInLineComments(), nullValue()); assertThat(scalarNode.getEndComments(), nullValue()); @@ -79,6 +80,29 @@ void shouldCreateNodeForString() { assertThat(scalarNode.getBlockComments().get(2), isBlockComment(" Title text")); } + @Test + void shouldCreateStringNodeWithLiteralStyle() { + // given + String value = "Multi-line text\nMulti-line text\nMulti-line text"; + ConfigurationData configurationData = mock(ConfigurationData.class); + String path = "disclaimer.text"; + given(configurationData.getCommentsForSection(path)).willReturn(Collections.emptyList()); + + // when + Node node = nodeBuilder.createYamlNode(value, path, configurationData, 0); + + // then + assertThat(node, instanceOf(ScalarNode.class)); + ScalarNode scalarNode = (ScalarNode) node; + assertThat(scalarNode.getTag(), equalTo(Tag.STR)); + assertThat(scalarNode.getValue(), equalTo(value)); + assertThat(scalarNode.getScalarStyle(), equalTo(DumperOptions.ScalarStyle.LITERAL)); + + assertThat(scalarNode.getInLineComments(), nullValue()); + assertThat(scalarNode.getEndComments(), nullValue()); + assertThat(scalarNode.getBlockComments(), empty()); + } + @Test void shouldCreateNodeForEnum() { // given @@ -95,6 +119,7 @@ void shouldCreateNodeForEnum() { ScalarNode scalarNode = (ScalarNode) node; assertThat(scalarNode.getTag(), equalTo(Tag.STR)); assertThat(scalarNode.getValue(), equalTo("DAYS")); + assertThat(scalarNode.getScalarStyle(), equalTo(DumperOptions.ScalarStyle.PLAIN)); assertThat(scalarNode.getInLineComments(), nullValue()); assertThat(scalarNode.getEndComments(), nullValue()); diff --git a/src/test/resources/multiple_lines.yml b/src/test/resources/multiple_lines.yml new file mode 100644 index 00000000..91a55214 --- /dev/null +++ b/src/test/resources/multiple_lines.yml @@ -0,0 +1,5 @@ +lines: | + First row + + Second row + Third row