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 339fb7ec44
commit f318cd05d9
4 changed files with 38 additions and 1 deletions

View File

@ -5,6 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-string-operations-3</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-string-operations-3</name>
<packaging>jar</packaging>
@ -44,7 +45,22 @@
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<assertj.version>3.6.1</assertj.version>
<maven-artifact.version>3.6.3</maven-artifact.version>
<gradle-core.version>6.1.1</gradle-core.version>

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");
}
}

View File

@ -111,7 +111,6 @@
<module>core-java-string-conversions-2</module>
<module>core-java-string-operations</module>
<module>core-java-string-operations-2</module>
<module>core-java-string-operations-3</module>
<module>core-java-sun</module>
<module>core-java-regex</module>
<module>pre-jpms</module>

View File

@ -1293,6 +1293,7 @@
<module>core-java-modules/core-java-datetime-string</module>
<module>core-java-modules/core-java-jpms</module>
<module>core-java-modules/core-java-os</module>
<module>core-java-modules/core-java-string-operations-3</module>
<module>core-java-modules/core-java-time-measurements</module>
<module>core-java-modules/multimodulemavenproject</module>
</modules>
@ -1338,6 +1339,7 @@
<module>core-java-modules/core-java-datetime-string</module>
<module>core-java-modules/core-java-jpms</module>
<module>core-java-modules/core-java-os</module>
<module>core-java-modules/core-java-string-operations-3</module>
<module>core-java-modules/core-java-time-measurements</module>
<module>core-java-modules/multimodulemavenproject</module>
<module>core-java-modules/core-java-strings</module>