From 30b95b0161079e3eb6cb937dddf94ec1f9cd3adb Mon Sep 17 00:00:00 2001 From: DiegoMarti2 <150871541+DiegoMarti2@users.noreply.github.com> Date: Thu, 21 Mar 2024 00:02:21 +0200 Subject: [PATCH] baeldung-articles : BAEL-6402 (#16185) * baeldung-articles : BAEL-6402 Normalizing the EOL Character in Java * Add dep to pom.xml file --- .../core-java-string-operations-8/pom.xml | 6 ++++ .../EOLNormalizer/EOLNormalizerUnitTest.java | 34 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 core-java-modules/core-java-string-operations-8/src/test/java/com/baeldung/EOLNormalizer/EOLNormalizerUnitTest.java diff --git a/core-java-modules/core-java-string-operations-8/pom.xml b/core-java-modules/core-java-string-operations-8/pom.xml index de93718ae6..0b6cdc6b2f 100644 --- a/core-java-modules/core-java-string-operations-8/pom.xml +++ b/core-java-modules/core-java-string-operations-8/pom.xml @@ -19,6 +19,12 @@ commons-lang3 ${apache.commons.lang3.version} + + org.apache.storm + storm-core + 1.2.2 + test + diff --git a/core-java-modules/core-java-string-operations-8/src/test/java/com/baeldung/EOLNormalizer/EOLNormalizerUnitTest.java b/core-java-modules/core-java-string-operations-8/src/test/java/com/baeldung/EOLNormalizer/EOLNormalizerUnitTest.java new file mode 100644 index 0000000000..caeea60da1 --- /dev/null +++ b/core-java-modules/core-java-string-operations-8/src/test/java/com/baeldung/EOLNormalizer/EOLNormalizerUnitTest.java @@ -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); + } +}