From cb7e5157236c11d274a25fb434bb1b7f9dc28138 Mon Sep 17 00:00:00 2001 From: Surapaneni Venkata Kiran Date: Wed, 12 Dec 2018 14:19:45 -0500 Subject: [PATCH] BAEL-2421 * Changes for http://jira.baeldung.com/browse/BAEL-2421 * fixed formatting * Merged the test and main classes as per the suggestion. * Merged the source code with tests. --- .../baeldung/ConvertStringToListUnitTest.java | 135 ++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 java-strings/src/test/java/com/baeldung/ConvertStringToListUnitTest.java diff --git a/java-strings/src/test/java/com/baeldung/ConvertStringToListUnitTest.java b/java-strings/src/test/java/com/baeldung/ConvertStringToListUnitTest.java new file mode 100644 index 0000000000..47357a99cc --- /dev/null +++ b/java-strings/src/test/java/com/baeldung/ConvertStringToListUnitTest.java @@ -0,0 +1,135 @@ +package com.baeldung; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.apache.commons.lang3.StringUtils; +import org.junit.Test; + +import com.google.common.base.Function; +import com.google.common.base.Splitter; +import com.google.common.collect.Lists; + +public class ConvertStringToListUnitTest { + + private final String countries = "Russia,Germany,England,France,Italy"; + private final String ranks = "1,2,3,4,5, 6,7"; + private final String emptyStrings = ",,,,,"; + private final List expectedCountriesList = Arrays.asList("Russia", "Germany", "England", "France", "Italy"); + private final List expectedRanksList = Arrays.asList(1, 2, 3, 4, 5, 6, 7); + private final List expectedEmptyStringsList = Arrays.asList("", "", "", "", "", ""); + + @Test + public void givenString_thenGetListOfStringByJava() { + List convertedCountriesList = Arrays.asList(countries.split(",", -1)); + + assertEquals(expectedCountriesList, convertedCountriesList); + } + + @Test + public void givenString_thenGetListOfStringByApache() { + List convertedCountriesList = Arrays.asList(StringUtils.splitPreserveAllTokens(countries, ",")); + + assertEquals(expectedCountriesList, convertedCountriesList); + } + + @Test + public void givenString_thenGetListOfStringByGuava() { + List convertedCountriesList = Splitter.on(",") + .trimResults() + .splitToList(countries); + + assertEquals(expectedCountriesList, convertedCountriesList); + } + + @Test + public void givenString_thenGetListOfStringByJava8() { + List convertedCountriesList = Stream.of(countries.split(",", -1)) + .collect(Collectors.toList()); + + assertEquals(expectedCountriesList, convertedCountriesList); + } + + @Test + public void givenString_thenGetListOfIntegerByJava() { + String[] convertedRankArray = ranks.split(","); + List convertedRankList = new ArrayList(); + for (String number : convertedRankArray) { + convertedRankList.add(Integer.parseInt(number.trim())); + } + + assertEquals(expectedRanksList, convertedRankList); + } + + @Test + public void givenString_thenGetListOfIntegerByGuava() { + List convertedRankList = Lists.transform(Splitter.on(",") + .trimResults() + .splitToList(ranks), new Function() { + @Override + public Integer apply(String input) { + return Integer.parseInt(input.trim()); + } + }); + + assertEquals(expectedRanksList, convertedRankList); + } + + @Test + public void givenString_thenGetListOfIntegerByJava8() { + List convertedRankList = Stream.of(ranks.split(",")) + .map(String::trim) + .map(Integer::parseInt) + .collect(Collectors.toList()); + + assertEquals(expectedRanksList, convertedRankList); + } + + @Test + public void givenString_thenGetListOfIntegerByApache() { + String[] convertedRankArray = StringUtils.split(ranks, ","); + List convertedRankList = new ArrayList(); + for (String number : convertedRankArray) { + convertedRankList.add(Integer.parseInt(number.trim())); + } + + assertEquals(expectedRanksList, convertedRankList); + } + + @Test + public void givenEmptyStrings_thenGetListOfStringByJava() { + List convertedEmptyStringsList = Arrays.asList(emptyStrings.split(",", -1)); + + assertEquals(expectedEmptyStringsList, convertedEmptyStringsList); + } + + @Test + public void givenEmptyStrings_thenGetListOfStringByApache() { + List convertedEmptyStringsList = Arrays.asList(StringUtils.splitPreserveAllTokens(emptyStrings, ",")); + + assertEquals(expectedEmptyStringsList, convertedEmptyStringsList); + } + + @Test + public void givenEmptyStrings_thenGetListOfStringByGuava() { + List convertedEmptyStringsList = Splitter.on(",") + .trimResults() + .splitToList(emptyStrings); + + assertEquals(expectedEmptyStringsList, convertedEmptyStringsList); + } + + @Test + public void givenEmptyStrings_thenGetListOfStringByJava8() { + List convertedEmptyStringsList = Stream.of(emptyStrings.split(",", -1)) + .collect(Collectors.toList()); + + assertEquals(expectedEmptyStringsList, convertedEmptyStringsList); + } + +}