[BAEL-3750] Breaking YAML Strings Over Multiple Lines (#8664)
* [BAEL-3750] Breaking YAML Strings Over Multiple Lines * Added yamo module to appropriate profiles * Article improvements * Fix * [BAEL-3750] Moved multi line yaml files from the yaml module to the libraries-data-io folder * [BAEL-3750] deleted the yaml module Co-authored-by: cristirosu <cristi.rosu4@gmail.com>
This commit is contained in:
parent
1c5597e481
commit
5860e8baa6
|
@ -0,0 +1,90 @@
|
||||||
|
package com.baeldung.libraries.snakeyaml;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.yaml.snakeyaml.Yaml;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
public class MultiLineStringsUnitTest {
|
||||||
|
|
||||||
|
private Yaml yaml;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup() {
|
||||||
|
yaml = new Yaml();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenLiteral_ThenLineBreaksArePresent() {
|
||||||
|
String key = parseYamlKey("literal.yaml", "key");
|
||||||
|
assertEquals("Line1\nLine2\nLine3", key);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenLiteral_ThenEndingBreaksAreReducedToOne() {
|
||||||
|
String key = parseYamlKey("literal2.yaml", "key");
|
||||||
|
assertEquals("\n\nLine1\n\nLine2\n\nLine3\n", key);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenFolded_ThenLineBreaksAreReplaced() {
|
||||||
|
String key = parseYamlKey("folded.yaml", "key");
|
||||||
|
assertEquals("Line1 Line2 Line3", key);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenFolded_ThenEmptyLinesAreReducedToOne() {
|
||||||
|
String key = parseYamlKey("folded2.yaml", "key");
|
||||||
|
assertEquals("Line1 Line2\n\nLine3\n", key);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenLiteralKeep_ThenLastEmptyLinesArePresent() {
|
||||||
|
String key = parseYamlKey("literal_keep.yaml", "key");
|
||||||
|
assertEquals("Line1\nLine2\nLine3\n\n", key);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenLiteralStrip_ThenLastEmptyLinesAreRemoved() {
|
||||||
|
String key = parseYamlKey("literal_strip.yaml", "key");
|
||||||
|
assertEquals("Line1\nLine2\nLine3", key);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenFoldedKeep_ThenLastEmptyLinesArePresent() {
|
||||||
|
String key = parseYamlKey("folded_keep.yaml", "key");
|
||||||
|
assertEquals("Line1 Line2 Line3\n\n\n", key);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenFoldedStrip_ThenLastEmptyLinesAreRemoved() {
|
||||||
|
String key = parseYamlKey("folded_strip.yaml", "key");
|
||||||
|
assertEquals("Line1 Line2 Line3", key);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenDoubleQuotes_ThenExplicitBreaksArePreserved() {
|
||||||
|
String key = parseYamlKey("plain_double_quotes.yaml", "key");
|
||||||
|
assertEquals("Line1\nLine2\nLine3", key);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSingleQuotes_ThenExplicitBreaksAreIgnored() {
|
||||||
|
String key = parseYamlKey("plain_single_quotes.yaml", "key");
|
||||||
|
assertEquals("Line1\\nLine2\nLine3", key);
|
||||||
|
}
|
||||||
|
|
||||||
|
String parseYamlKey(String fileName, String key) {
|
||||||
|
InputStream inputStream = this.getClass()
|
||||||
|
.getClassLoader()
|
||||||
|
.getResourceAsStream("yaml" + File.separator + "multiline" + File.separator + fileName);
|
||||||
|
Map<String, String> parsed = yaml.load(inputStream);
|
||||||
|
return parsed.get(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,4 @@
|
||||||
|
key: >
|
||||||
|
Line1
|
||||||
|
Line2
|
||||||
|
Line3
|
|
@ -0,0 +1,8 @@
|
||||||
|
key: >
|
||||||
|
Line1
|
||||||
|
Line2
|
||||||
|
|
||||||
|
|
||||||
|
Line3
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
key: >+
|
||||||
|
Line1
|
||||||
|
Line2
|
||||||
|
Line3
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
key: >-
|
||||||
|
Line1
|
||||||
|
Line2
|
||||||
|
Line3
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
key: |
|
||||||
|
Line1
|
||||||
|
Line2
|
||||||
|
Line3
|
|
@ -0,0 +1,10 @@
|
||||||
|
key: |
|
||||||
|
|
||||||
|
|
||||||
|
Line1
|
||||||
|
|
||||||
|
Line2
|
||||||
|
|
||||||
|
Line3
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
key: |+
|
||||||
|
Line1
|
||||||
|
Line2
|
||||||
|
Line3
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
key: |-
|
||||||
|
Line1
|
||||||
|
Line2
|
||||||
|
Line3
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
key: "Line1\nLine2\nLine3"
|
|
@ -0,0 +1,3 @@
|
||||||
|
key: 'Line1\nLine2
|
||||||
|
|
||||||
|
Line3'
|
Loading…
Reference in New Issue