baeldung-articles : BAEL-6402 (#16185)

* baeldung-articles : BAEL-6402

Normalizing the EOL Character in Java

* Add dep to pom.xml file
This commit is contained in:
DiegoMarti2 2024-03-21 00:02:21 +02:00 committed by GitHub
parent e8f490718a
commit 30b95b0161
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 40 additions and 0 deletions

View File

@ -19,6 +19,12 @@
<artifactId>commons-lang3</artifactId>
<version>${apache.commons.lang3.version}</version>
</dependency>
<dependency>
<groupId>org.apache.storm</groupId>
<artifactId>storm-core</artifactId>
<version>1.2.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1,34 @@
package com.baeldung.EOLNormalizer;
import org.apache.storm.shade.org.apache.commons.lang.StringUtils;
import org.junit.Test;
import java.util.Arrays;
import java.util.stream.Collectors;
import static org.junit.Assert.assertEquals;
public class EOLNormalizerUnitTest {
String originalText = "This is a text\rwith different\r\nEOL characters\n";
String expectedText = "This is a text" + System.getProperty("line.separator") + "with different" + System.getProperty("line.separator") +
"EOL characters" + System.getProperty("line.separator");
@Test
public void givenText_whenUsingStringReplace_thenEOLNormalized() {
String normalizedText = originalText.replaceAll("\\r\\n|\\r|\\n", System.getProperty("line.separator"));
assertEquals(expectedText, normalizedText);
}
@Test
public void givenText_whenUsingStringUtils_thenEOLNormalized() {
String normalizedText = StringUtils.replaceEach(originalText, new String[]{"\r\n", "\r", "\n"}, new String[]{System.getProperty("line.separator"), System.getProperty("line.separator"), System.getProperty("line.separator")});
assertEquals(expectedText, normalizedText);
}
@Test
public void givenText_whenUsingStreamAPI_thenEOLNormalized() {
String normalizedText = Arrays.stream(originalText.split("\\r\\n|\\r|\\n"))
.collect(Collectors.joining(System.getProperty("line.separator"))).trim();
assertEquals(expectedText.trim(), normalizedText);
}
}