|
1 | 1 | package com.robertsmieja.example.apache.commons.lang3; |
2 | 2 |
|
3 | 3 | import org.apache.commons.lang3.StringUtils; |
4 | | -import org.apache.commons.lang3.tuple.ImmutablePair; |
5 | | -import org.apache.commons.lang3.tuple.Pair; |
6 | 4 | import org.junit.Test; |
7 | 5 |
|
8 | | -import java.util.Arrays; |
9 | | -import java.util.List; |
10 | 6 | import java.util.function.BiFunction; |
11 | 7 | import java.util.function.Function; |
12 | | -import java.util.stream.Collectors; |
13 | 8 |
|
14 | 9 | import static org.junit.Assert.assertArrayEquals; |
15 | 10 | import static org.junit.Assert.assertEquals; |
16 | 11 |
|
17 | 12 | /** |
18 | 13 | * This class contains example usages of StringUtils |
19 | | - * Many of these methods are null-safe |
| 14 | + * |
| 15 | + * All of these methods are null-safe |
20 | 16 | */ |
21 | 17 | public class StringUtilsExamples { |
22 | 18 |
|
23 | 19 | @Test |
24 | | - public void isBlankExamples(){ |
| 20 | + public void isBlankExamples() { |
25 | 21 | assertFunction(StringUtils::isBlank, "", true); |
26 | 22 | assertFunction(StringUtils::isBlank, null, true); |
27 | 23 | assertFunction(StringUtils::isBlank, " ", true); |
28 | 24 | assertFunction(StringUtils::isBlank, " This isn't blank ", false); |
29 | 25 | } |
30 | 26 |
|
31 | 27 | @Test |
32 | | - public void isEmptyExamples(){ |
| 28 | + public void isEmptyExamples() { |
33 | 29 | assertFunction(StringUtils::isEmpty, "", true); |
34 | 30 | assertFunction(StringUtils::isEmpty, null, true); |
35 | 31 | assertFunction(StringUtils::isEmpty, " ", false); |
@@ -57,42 +53,37 @@ public void uncapitalizationExamples() { |
57 | 53 | } |
58 | 54 |
|
59 | 55 | @Test |
60 | | - public void splitExamples(){ |
| 56 | + public void splitExamples() { |
61 | 57 | assertFunction(StringUtils::split, "Here are a bunch of words.", new String[]{"Here", "are", "a", "bunch", "of", "words."}); |
62 | | - assertFunction(StringUtils::split, "C, S, V, example",",", new String[]{"C", " S", " V", " example"}); |
63 | | - assertFunction(StringUtils::split, (String)null, null); |
| 58 | + assertFunction(StringUtils::split, "C, S, V, example", ",", new String[]{"C", " S", " V", " example"}); |
| 59 | + |
| 60 | + //Expanded lambda expression is needed because the arguments (null, null) match multiple method signatures |
| 61 | + assertFunction((String str, String seperatorChar) -> StringUtils.split(str, seperatorChar), null, null, null); |
64 | 62 | } |
65 | 63 |
|
66 | 64 | @Test |
67 | | - public void differenceExamples(){ |
| 65 | + public void differenceExamples() { |
68 | 66 | assertFunction(StringUtils::difference, "Similar string", "Similar string kindof", " kindof"); |
69 | 67 | assertFunction(StringUtils::difference, "same string", "same string", ""); |
70 | 68 | assertFunction(StringUtils::difference, "different string", "foo bar", "foo bar"); |
71 | 69 | assertFunction(StringUtils::difference, null, null, null); |
72 | 70 | } |
73 | 71 |
|
74 | 72 |
|
| 73 | + /* Helpers */ |
| 74 | + private <T, R> void assertFunction(Function<T, R> function, T input, R output) { |
| 75 | + assertEquals(output, function.apply(input)); |
| 76 | + } |
75 | 77 |
|
| 78 | + private <T, R> void assertFunction(Function<T, R[]> function, T input, R[] output) { |
| 79 | + assertArrayEquals(output, function.apply(input)); |
| 80 | + } |
76 | 81 |
|
77 | | - |
78 | | - |
79 | | - |
80 | | - |
81 | | - |
82 | | - |
83 | | - /* Helpers */ |
84 | | - private <T, R> void assertFunction(Function<T, R> function, T input, R output){ |
85 | | - if (output instanceof Object[]){ |
86 | | - assertArrayEquals((R[])output, (R[]) function.apply(input)); |
87 | | - } else { |
88 | | - assertEquals(output, function.apply(input)); |
89 | | - } |
| 82 | + private <T, O, R> void assertFunction(BiFunction<T, O, R> function, T input, O otherInput, R output) { |
| 83 | + assertEquals(output, function.apply(input, otherInput)); |
90 | 84 | } |
91 | | - private <T, O, R> void assertFunction(BiFunction<T, O, R> function, T input, O otherInput, R output){ |
92 | | - if (output instanceof Object[]){ |
93 | | - assertArrayEquals((R[])output, (R[]) function.apply(input, otherInput)); |
94 | | - } else { |
95 | | - assertEquals(output, function.apply(input,otherInput)); |
96 | | - } |
| 85 | + |
| 86 | + private <T, O, R> void assertFunction(BiFunction<T, O, R[]> function, T input, O otherInput, R[] output) { |
| 87 | + assertArrayEquals(output, function.apply(input, otherInput)); |
97 | 88 | } |
98 | 89 | } |
0 commit comments