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:
parent
e8f490718a
commit
30b95b0161
@ -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>
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user