|
|
|
@ -16,7 +16,6 @@ |
|
|
|
|
|
|
|
|
|
|
|
package org.springframework.util; |
|
|
|
package org.springframework.util; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.Arrays; |
|
|
|
|
|
|
|
import java.util.Collections; |
|
|
|
import java.util.Collections; |
|
|
|
import java.util.Locale; |
|
|
|
import java.util.Locale; |
|
|
|
import java.util.Properties; |
|
|
|
import java.util.Properties; |
|
|
|
@ -515,120 +514,106 @@ class StringUtilsTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void tokenizeToStringArray() { |
|
|
|
void tokenizeToStringArray() { |
|
|
|
String[] sa = StringUtils.tokenizeToStringArray("a,b , ,c", ","); |
|
|
|
String[] array = StringUtils.tokenizeToStringArray("a,b , ,c", ","); |
|
|
|
assertThat(sa).hasSize(3); |
|
|
|
assertThat(array).containsExactly("a", "b", "c"); |
|
|
|
assertThat(sa[0].equals("a") && sa[1].equals("b") && sa[2].equals("c")).as("components are correct").isTrue(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void tokenizeToStringArrayWithNotIgnoreEmptyTokens() { |
|
|
|
void tokenizeToStringArrayWithNotIgnoreEmptyTokens() { |
|
|
|
String[] sa = StringUtils.tokenizeToStringArray("a,b , ,c", ",", true, false); |
|
|
|
String[] array = StringUtils.tokenizeToStringArray("a,b , ,c", ",", true, false); |
|
|
|
assertThat(sa).hasSize(4); |
|
|
|
assertThat(array).containsExactly("a", "b", "", "c"); |
|
|
|
assertThat(sa[0].equals("a") && sa[1].equals("b") && sa[2].isEmpty() && sa[3].equals("c")).as("components are correct").isTrue(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void tokenizeToStringArrayWithNotTrimTokens() { |
|
|
|
void tokenizeToStringArrayWithNotTrimTokens() { |
|
|
|
String[] sa = StringUtils.tokenizeToStringArray("a,b ,c", ",", false, true); |
|
|
|
String[] array = StringUtils.tokenizeToStringArray("a,b ,c", ",", false, true); |
|
|
|
assertThat(sa).hasSize(3); |
|
|
|
assertThat(array).containsExactly("a", "b ", "c"); |
|
|
|
assertThat(sa[0].equals("a") && sa[1].equals("b ") && sa[2].equals("c")).as("components are correct").isTrue(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void commaDelimitedListToStringArrayWithNullProducesEmptyArray() { |
|
|
|
void commaDelimitedListToStringArrayWithNullProducesEmptyArray() { |
|
|
|
String[] sa = StringUtils.commaDelimitedListToStringArray(null); |
|
|
|
String[] array = StringUtils.commaDelimitedListToStringArray(null); |
|
|
|
assertThat(sa).as("String array isn't null with null input").isNotNull(); |
|
|
|
assertThat(array).isEmpty(); |
|
|
|
assertThat(sa.length).as("String array length == 0 with null input").isEqualTo(0); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void commaDelimitedListToStringArrayWithEmptyStringProducesEmptyArray() { |
|
|
|
void commaDelimitedListToStringArrayWithEmptyStringProducesEmptyArray() { |
|
|
|
String[] sa = StringUtils.commaDelimitedListToStringArray(""); |
|
|
|
String[] array = StringUtils.commaDelimitedListToStringArray(""); |
|
|
|
assertThat(sa).as("String array isn't null with null input").isNotNull(); |
|
|
|
assertThat(array).isEmpty(); |
|
|
|
assertThat(sa.length).as("String array length == 0 with null input").isEqualTo(0); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void delimitedListToStringArrayWithComma() { |
|
|
|
void delimitedListToStringArrayWithComma() { |
|
|
|
String[] sa = StringUtils.delimitedListToStringArray("a,b", ","); |
|
|
|
String[] array = StringUtils.delimitedListToStringArray("a,b", ","); |
|
|
|
assertThat(sa).hasSize(2); |
|
|
|
assertThat(array).containsExactly("a", "b"); |
|
|
|
assertThat(sa[0]).isEqualTo("a"); |
|
|
|
|
|
|
|
assertThat(sa[1]).isEqualTo("b"); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void delimitedListToStringArrayWithSemicolon() { |
|
|
|
void delimitedListToStringArrayWithSemicolon() { |
|
|
|
String[] sa = StringUtils.delimitedListToStringArray("a;b", ";"); |
|
|
|
String[] array = StringUtils.delimitedListToStringArray("a;b", ";"); |
|
|
|
assertThat(sa).hasSize(2); |
|
|
|
assertThat(array).containsExactly("a", "b"); |
|
|
|
assertThat(sa[0]).isEqualTo("a"); |
|
|
|
|
|
|
|
assertThat(sa[1]).isEqualTo("b"); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void delimitedListToStringArrayWithEmptyDelimiter() { |
|
|
|
void delimitedListToStringArrayWithEmptyDelimiter() { |
|
|
|
String[] sa = StringUtils.delimitedListToStringArray("a,b", ""); |
|
|
|
String[] array = StringUtils.delimitedListToStringArray("a,b", ""); |
|
|
|
assertThat(sa).hasSize(3); |
|
|
|
assertThat(array).containsExactly("a", ",", "b"); |
|
|
|
assertThat(sa[0]).isEqualTo("a"); |
|
|
|
|
|
|
|
assertThat(sa[1]).isEqualTo(","); |
|
|
|
|
|
|
|
assertThat(sa[2]).isEqualTo("b"); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void delimitedListToStringArrayWithNullDelimiter() { |
|
|
|
void delimitedListToStringArrayWithNullDelimiter() { |
|
|
|
String[] sa = StringUtils.delimitedListToStringArray("a,b", null); |
|
|
|
String[] array = StringUtils.delimitedListToStringArray("a,b", null); |
|
|
|
assertThat(sa).hasSize(1); |
|
|
|
assertThat(array).containsExactly("a,b"); |
|
|
|
assertThat(sa[0]).isEqualTo("a,b"); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void delimitedListToStringArrayWithCharacterToDelete() { |
|
|
|
void delimitedListToStringArrayWithCharacterToDelete() { |
|
|
|
String[] sa = StringUtils.delimitedListToStringArray("a,b,c", ",", "a"); |
|
|
|
String[] array = StringUtils.delimitedListToStringArray("a,b,c", ",", "a"); |
|
|
|
assertThat(sa).containsExactly("", "b", "c"); |
|
|
|
assertThat(array).containsExactly("", "b", "c"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void delimitedListToStringArrayWithCharacterToDeleteEqualsToDelimiter() { |
|
|
|
void delimitedListToStringArrayWithCharacterToDeleteEqualsToDelimiter() { |
|
|
|
String[] sa = StringUtils.delimitedListToStringArray("a,b,c", ",", ","); |
|
|
|
String[] array = StringUtils.delimitedListToStringArray("a,b,c", ",", ","); |
|
|
|
assertThat(sa).containsExactly("a", "b", "c"); |
|
|
|
assertThat(array).containsExactly("a", "b", "c"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void commaDelimitedListToStringArrayMatchWords() { |
|
|
|
void commaDelimitedListToStringArrayMatchWords() { |
|
|
|
// Could read these from files
|
|
|
|
// Could read these from files
|
|
|
|
String[] sa = new String[] {"foo", "bar", "big"}; |
|
|
|
String[] array = new String[] {"foo", "bar", "big"}; |
|
|
|
doTestCommaDelimitedListToStringArrayLegalMatch(sa); |
|
|
|
doTestCommaDelimitedListToStringArrayLegalMatch(array); |
|
|
|
doTestStringArrayReverseTransformationMatches(sa); |
|
|
|
doTestStringArrayReverseTransformationMatches(array); |
|
|
|
|
|
|
|
|
|
|
|
sa = new String[] {"a", "b", "c"}; |
|
|
|
array = new String[] {"a", "b", "c"}; |
|
|
|
doTestCommaDelimitedListToStringArrayLegalMatch(sa); |
|
|
|
doTestCommaDelimitedListToStringArrayLegalMatch(array); |
|
|
|
doTestStringArrayReverseTransformationMatches(sa); |
|
|
|
doTestStringArrayReverseTransformationMatches(array); |
|
|
|
|
|
|
|
|
|
|
|
// Test same words
|
|
|
|
// Test same words
|
|
|
|
sa = new String[] {"AA", "AA", "AA", "AA", "AA"}; |
|
|
|
array = new String[] {"AA", "AA", "AA", "AA", "AA"}; |
|
|
|
doTestCommaDelimitedListToStringArrayLegalMatch(sa); |
|
|
|
doTestCommaDelimitedListToStringArrayLegalMatch(array); |
|
|
|
doTestStringArrayReverseTransformationMatches(sa); |
|
|
|
doTestStringArrayReverseTransformationMatches(array); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void doTestStringArrayReverseTransformationMatches(String[] sa) { |
|
|
|
private void doTestStringArrayReverseTransformationMatches(String[] array) { |
|
|
|
String[] reverse = |
|
|
|
String[] reverse = |
|
|
|
StringUtils.commaDelimitedListToStringArray(StringUtils.arrayToCommaDelimitedString(sa)); |
|
|
|
StringUtils.commaDelimitedListToStringArray(StringUtils.arrayToCommaDelimitedString(array)); |
|
|
|
assertThat(Arrays.asList(reverse)).as("Reverse transformation is equal").isEqualTo(Arrays.asList(sa)); |
|
|
|
assertThat(reverse).as("Reverse transformation is equal").isEqualTo(array); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void commaDelimitedListToStringArraySingleString() { |
|
|
|
void commaDelimitedListToStringArraySingleString() { |
|
|
|
// Could read these from files
|
|
|
|
// Could read these from files
|
|
|
|
String s = "woeirqupoiewuropqiewuorpqiwueopriquwopeiurqopwieur"; |
|
|
|
String s = "woeirqupoiewuropqiewuorpqiwueopriquwopeiurqopwieur"; |
|
|
|
String[] sa = StringUtils.commaDelimitedListToStringArray(s); |
|
|
|
String[] array = StringUtils.commaDelimitedListToStringArray(s); |
|
|
|
assertThat(sa.length).as("Found one String with no delimiters").isEqualTo(1); |
|
|
|
assertThat(array).as("Single array entry matches input String with no delimiters").containsExactly(s); |
|
|
|
assertThat(sa[0]).as("Single array entry matches input String with no delimiters").isEqualTo(s); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void commaDelimitedListToStringArrayWithOtherPunctuation() { |
|
|
|
void commaDelimitedListToStringArrayWithOtherPunctuation() { |
|
|
|
// Could read these from files
|
|
|
|
// Could read these from files
|
|
|
|
String[] sa = new String[] {"xcvwert4456346&*.", "///", ".!", ".", ";"}; |
|
|
|
String[] array = new String[] {"xcvwert4456346&*.", "///", ".!", ".", ";"}; |
|
|
|
doTestCommaDelimitedListToStringArrayLegalMatch(sa); |
|
|
|
doTestCommaDelimitedListToStringArrayLegalMatch(array); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
@ -637,20 +622,17 @@ class StringUtilsTests { |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void commaDelimitedListToStringArrayEmptyStrings() { |
|
|
|
void commaDelimitedListToStringArrayEmptyStrings() { |
|
|
|
// Could read these from files
|
|
|
|
// Could read these from files
|
|
|
|
String[] sa = StringUtils.commaDelimitedListToStringArray("a,,b"); |
|
|
|
String[] array = StringUtils.commaDelimitedListToStringArray("a,,b"); |
|
|
|
assertThat(sa.length).as("a,,b produces array length 3").isEqualTo(3); |
|
|
|
assertThat(array).containsExactly("a", "", "b"); |
|
|
|
assertThat(sa[0].equals("a") && sa[1].isEmpty() && sa[2].equals("b")).as("components are correct").isTrue(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sa = new String[] {"", "", "a", ""}; |
|
|
|
array = new String[] {"", "", "a", ""}; |
|
|
|
doTestCommaDelimitedListToStringArrayLegalMatch(sa); |
|
|
|
doTestCommaDelimitedListToStringArrayLegalMatch(array); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void doTestCommaDelimitedListToStringArrayLegalMatch(String[] components) { |
|
|
|
private void doTestCommaDelimitedListToStringArrayLegalMatch(String[] components) { |
|
|
|
String sb = String.join(String.valueOf(','), components); |
|
|
|
String sb = String.join(String.valueOf(','), components); |
|
|
|
String[] sa = StringUtils.commaDelimitedListToStringArray(sb); |
|
|
|
String[] array = StringUtils.commaDelimitedListToStringArray(sb); |
|
|
|
assertThat(sa).as("String array isn't null with legal match").isNotNull(); |
|
|
|
assertThat(array).as("Output equals input").isEqualTo(components); |
|
|
|
assertThat(sa.length).as("String array length is correct with legal match").isEqualTo(components.length); |
|
|
|
|
|
|
|
assertThat(Arrays.equals(sa, components)).as("Output equals input").isTrue(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|