|
| 1 | +package com.baeldung; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.List; |
| 8 | +import java.util.stream.Collectors; |
| 9 | +import java.util.stream.Stream; |
| 10 | + |
| 11 | +import org.apache.commons.lang3.StringUtils; |
| 12 | +import org.junit.Test; |
| 13 | + |
| 14 | +import com.google.common.base.Function; |
| 15 | +import com.google.common.base.Splitter; |
| 16 | +import com.google.common.collect.Lists; |
| 17 | + |
| 18 | +public class ConvertStringToListUnitTest { |
| 19 | + |
| 20 | + private final String countries = "Russia,Germany,England,France,Italy"; |
| 21 | + private final String ranks = "1,2,3,4,5, 6,7"; |
| 22 | + private final String emptyStrings = ",,,,,"; |
| 23 | + private final List<String> expectedCountriesList = Arrays.asList("Russia", "Germany", "England", "France", "Italy"); |
| 24 | + private final List<Integer> expectedRanksList = Arrays.asList(1, 2, 3, 4, 5, 6, 7); |
| 25 | + private final List<String> expectedEmptyStringsList = Arrays.asList("", "", "", "", "", ""); |
| 26 | + |
| 27 | + @Test |
| 28 | + public void givenString_thenGetListOfStringByJava() { |
| 29 | + List<String> convertedCountriesList = Arrays.asList(countries.split(",", -1)); |
| 30 | + |
| 31 | + assertEquals(expectedCountriesList, convertedCountriesList); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void givenString_thenGetListOfStringByApache() { |
| 36 | + List<String> convertedCountriesList = Arrays.asList(StringUtils.splitPreserveAllTokens(countries, ",")); |
| 37 | + |
| 38 | + assertEquals(expectedCountriesList, convertedCountriesList); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void givenString_thenGetListOfStringByGuava() { |
| 43 | + List<String> convertedCountriesList = Splitter.on(",") |
| 44 | + .trimResults() |
| 45 | + .splitToList(countries); |
| 46 | + |
| 47 | + assertEquals(expectedCountriesList, convertedCountriesList); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void givenString_thenGetListOfStringByJava8() { |
| 52 | + List<String> convertedCountriesList = Stream.of(countries.split(",", -1)) |
| 53 | + .collect(Collectors.toList()); |
| 54 | + |
| 55 | + assertEquals(expectedCountriesList, convertedCountriesList); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void givenString_thenGetListOfIntegerByJava() { |
| 60 | + String[] convertedRankArray = ranks.split(","); |
| 61 | + List<Integer> convertedRankList = new ArrayList<Integer>(); |
| 62 | + for (String number : convertedRankArray) { |
| 63 | + convertedRankList.add(Integer.parseInt(number.trim())); |
| 64 | + } |
| 65 | + |
| 66 | + assertEquals(expectedRanksList, convertedRankList); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void givenString_thenGetListOfIntegerByGuava() { |
| 71 | + List<Integer> convertedRankList = Lists.transform(Splitter.on(",") |
| 72 | + .trimResults() |
| 73 | + .splitToList(ranks), new Function<String, Integer>() { |
| 74 | + @Override |
| 75 | + public Integer apply(String input) { |
| 76 | + return Integer.parseInt(input.trim()); |
| 77 | + } |
| 78 | + }); |
| 79 | + |
| 80 | + assertEquals(expectedRanksList, convertedRankList); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void givenString_thenGetListOfIntegerByJava8() { |
| 85 | + List<Integer> convertedRankList = Stream.of(ranks.split(",")) |
| 86 | + .map(String::trim) |
| 87 | + .map(Integer::parseInt) |
| 88 | + .collect(Collectors.toList()); |
| 89 | + |
| 90 | + assertEquals(expectedRanksList, convertedRankList); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void givenString_thenGetListOfIntegerByApache() { |
| 95 | + String[] convertedRankArray = StringUtils.split(ranks, ","); |
| 96 | + List<Integer> convertedRankList = new ArrayList<Integer>(); |
| 97 | + for (String number : convertedRankArray) { |
| 98 | + convertedRankList.add(Integer.parseInt(number.trim())); |
| 99 | + } |
| 100 | + |
| 101 | + assertEquals(expectedRanksList, convertedRankList); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void givenEmptyStrings_thenGetListOfStringByJava() { |
| 106 | + List<String> convertedEmptyStringsList = Arrays.asList(emptyStrings.split(",", -1)); |
| 107 | + |
| 108 | + assertEquals(expectedEmptyStringsList, convertedEmptyStringsList); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void givenEmptyStrings_thenGetListOfStringByApache() { |
| 113 | + List<String> convertedEmptyStringsList = Arrays.asList(StringUtils.splitPreserveAllTokens(emptyStrings, ",")); |
| 114 | + |
| 115 | + assertEquals(expectedEmptyStringsList, convertedEmptyStringsList); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void givenEmptyStrings_thenGetListOfStringByGuava() { |
| 120 | + List<String> convertedEmptyStringsList = Splitter.on(",") |
| 121 | + .trimResults() |
| 122 | + .splitToList(emptyStrings); |
| 123 | + |
| 124 | + assertEquals(expectedEmptyStringsList, convertedEmptyStringsList); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + public void givenEmptyStrings_thenGetListOfStringByJava8() { |
| 129 | + List<String> convertedEmptyStringsList = Stream.of(emptyStrings.split(",", -1)) |
| 130 | + .collect(Collectors.toList()); |
| 131 | + |
| 132 | + assertEquals(expectedEmptyStringsList, convertedEmptyStringsList); |
| 133 | + } |
| 134 | + |
| 135 | +} |
0 commit comments