[BAEL-3750] Breaking YAML Strings Over Multiple Lines

This commit is contained in:
crist 2020-02-04 18:20:00 +02:00
parent 39ea8b9a91
commit eb4e462b36
13 changed files with 180 additions and 0 deletions

7
yaml/README.md Normal file
View File

@ -0,0 +1,7 @@
## YAML
This module contains articles about YAML
### Relevant articles

29
yaml/pom.xml Normal file
View File

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>yaml</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.21</version>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
<snakeyaml.version>1.21</snakeyaml.version>
</properties>
</project>

View File

@ -0,0 +1,90 @@
package com.baeldung.multiline;
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");
assertEquals("Line1\nLine2\nLine3", key);
}
@Test
public void whenLiteral_ThenEndingBreaksAreReducedToOne() {
String key = parseYamlKey("literal2.yaml");
assertEquals("\n\nLine1\n\nLine2\n\nLine3\n", key);
}
@Test
public void whenFolded_ThenLineBreaksAreReplaced() {
String key = parseYamlKey("folded.yaml");
assertEquals("Line1 Line2 Line3", key);
}
@Test
public void whenFolded_ThenEmptyLinesAreReducedToOne() {
String key = parseYamlKey("folded2.yaml");
assertEquals("Line1\nLine2\n\nLine3\n", key);
}
@Test
public void whenLiteralKeep_ThenLastEmptyLinesArePresent() {
String key = parseYamlKey("literal_keep.yaml");
assertEquals("Line1\nLine2\nLine3\n\n", key);
}
@Test
public void whenLiteralStrip_ThenLastEmptyLinesAreRemoved() {
String key = parseYamlKey("literal_strip.yaml");
assertEquals("Line1\nLine2\nLine3", key);
}
@Test
public void whenFoldedKeep_ThenLastEmptyLinesArePresent() {
String key = parseYamlKey("folded_keep.yaml");
assertEquals("Line1 Line2 Line3\n\n\n", key);
}
@Test
public void whenFoldedStrip_ThenLastEmptyLinesAreRemoved() {
String key = parseYamlKey("folded_strip.yaml");
assertEquals("Line1 Line2 Line3", key);
}
@Test
public void whenDoubleQuotes_ThenExplicitBreaksArePreserved() {
String key = parseYamlKey("plain_double_quotes.yaml");
assertEquals("Line1\nLine2\nLine3", key);
}
@Test
public void whenSingleQuotes_ThenExplicitBreaksAreIgnored() {
String key = parseYamlKey("plain_single_quotes.yaml");
assertEquals("Line1\\nLine2\nLine3", key);
}
private String parseYamlKey(String fileName) {
InputStream inputStream = this.getClass()
.getClassLoader()
.getResourceAsStream("multi-line" + File.separator + fileName);
Map<String, String> parsed = yaml.load(inputStream);
return parsed.get("key");
}
}

View File

@ -0,0 +1,4 @@
key: >
Line1
Line2
Line3

View File

@ -0,0 +1,9 @@
key: >
Line1
Line2
Line3

View File

@ -0,0 +1,6 @@
key: >+
Line1
Line2
Line3

View File

@ -0,0 +1,7 @@
key: >-
Line1
Line2
Line3

View File

@ -0,0 +1,4 @@
key: |
Line1
Line2
Line3

View File

@ -0,0 +1,10 @@
key: |
Line1
Line2
Line3

View File

@ -0,0 +1,5 @@
key: |+
Line1
Line2
Line3

View File

@ -0,0 +1,5 @@
key: |-
Line1
Line2
Line3

View File

@ -0,0 +1 @@
key: "Line1\nLine2\nLine3"

View File

@ -0,0 +1,3 @@
key: 'Line1\nLine2
Line3'