BAEL-5000: Add split String by newlines examples (#10881)

* BAEL-5000: Add split String by newlines examples

* BAEL-5000: Exclude core-java-string-operations-3 from persistence-modules

* BAEL-5000: Update tests

Co-authored-by: Krzysztof Woyke <krzysztof.woyke.sp@lhsystems.com>
This commit is contained in:
kwoyke
2021-06-12 14:23:37 +02:00
committed by GitHub
parent ea0af629e4
commit 35f88cf63a
4 changed files with 38 additions and 1 deletions

View File

@@ -1,9 +1,13 @@
package com.baeldung.splitstringbynewline;
import org.junit.Test;
import java.util.regex.Pattern;
import static org.assertj.core.api.Assertions.assertThat;
public class SplitStringByNewLineUnitTest {
@Test
public void givenString_whenSplitByNewLineUsingSystemLineSeparator_thenReturnsArray() {
assertThat("Line1\nLine2\nLine3".split(System.lineSeparator())).containsExactly("Line1", "Line2", "Line3");
@@ -26,4 +30,20 @@ public class SplitStringByNewLineUnitTest {
assertThat("Line1\r\nLine2\r\nLine3".split("\\R")).containsExactly("Line1", "Line2", "Line3");
}
@Test
public void givenString_whenSplitByNewLineUsingJava8PatternClass_thenReturnsStream() {
Pattern pattern = Pattern.compile("\\R");
assertThat(pattern.splitAsStream("Line1\nLine2\nLine3")).containsExactly("Line1", "Line2", "Line3");
assertThat(pattern.splitAsStream("Line1\rLine2\rLine3")).containsExactly("Line1", "Line2", "Line3");
assertThat(pattern.splitAsStream("Line1\r\nLine2\r\nLine3")).containsExactly("Line1", "Line2", "Line3");
}
@Test
public void givenString_whenSplitByNewLineUsingJava11Lines_thenReturnsStream() {
assertThat("Line1\nLine2\rLine3\r\nLine4".lines()).containsExactly("Line1", "Line2", "Line3", "Line4");
}
}