1 16 17 package org.springframework.util; 18 19 import java.util.Arrays ; 20 import java.util.Properties ; 21 22 import junit.framework.TestCase; 23 24 28 public class StringUtilsTests extends TestCase { 29 30 public void testHasTextBlank() throws Exception { 31 String blank = " "; 32 assertEquals(false, StringUtils.hasText(blank)); 33 } 34 35 public void testHasTextNullEmpty() throws Exception { 36 assertEquals(false, StringUtils.hasText(null)); 37 assertEquals(false, StringUtils.hasText("")); 38 } 39 40 public void testHasTextValid() throws Exception { 41 assertEquals(true, StringUtils.hasText("t")); 42 } 43 44 public void testTrimLeadingWhitespace() throws Exception { 45 assertEquals("", StringUtils.trimLeadingWhitespace("")); 46 assertEquals("", StringUtils.trimLeadingWhitespace(" ")); 47 assertEquals("", StringUtils.trimLeadingWhitespace("\t")); 48 assertEquals("a", StringUtils.trimLeadingWhitespace(" a")); 49 assertEquals("a ", StringUtils.trimLeadingWhitespace("a ")); 50 assertEquals("a ", StringUtils.trimLeadingWhitespace(" a ")); 51 } 52 53 public void testTrimTrailingWhitespace() throws Exception { 54 assertEquals("", StringUtils.trimTrailingWhitespace("")); 55 assertEquals("", StringUtils.trimTrailingWhitespace(" ")); 56 assertEquals("", StringUtils.trimTrailingWhitespace("\t")); 57 assertEquals("a", StringUtils.trimTrailingWhitespace("a ")); 58 assertEquals(" a", StringUtils.trimTrailingWhitespace(" a")); 59 assertEquals(" a", StringUtils.trimTrailingWhitespace(" a ")); 60 } 61 62 public void testCountOccurrencesOf() { 63 assertTrue("nullx2 = 0", 64 StringUtils.countOccurrencesOf(null, null) == 0); 65 assertTrue("null string = 0", 66 StringUtils.countOccurrencesOf("s", null) == 0); 67 assertTrue("null substring = 0", 68 StringUtils.countOccurrencesOf(null, "s") == 0); 69 String s = "erowoiueoiur"; 70 assertTrue("not found = 0", 71 StringUtils.countOccurrencesOf(s, "WERWER") == 0); 72 assertTrue("not found char = 0", 73 StringUtils.countOccurrencesOf(s, "x") == 0); 74 assertTrue("not found ws = 0", 75 StringUtils.countOccurrencesOf(s, " ") == 0); 76 assertTrue("not found empty string = 0", 77 StringUtils.countOccurrencesOf(s, "") == 0); 78 assertTrue("found char=2", StringUtils.countOccurrencesOf(s, "e") == 2); 79 assertTrue("found substring=2", 80 StringUtils.countOccurrencesOf(s, "oi") == 2); 81 assertTrue("found substring=2", 82 StringUtils.countOccurrencesOf(s, "oiu") == 2); 83 assertTrue("found substring=3", 84 StringUtils.countOccurrencesOf(s, "oiur") == 1); 85 assertTrue("test last", StringUtils.countOccurrencesOf(s, "r") == 2); 86 } 87 88 public void testReplace() throws Exception { 89 String inString = "a6AazAaa77abaa"; 90 String oldPattern = "aa"; 91 String newPattern = "foo"; 92 93 String s = StringUtils.replace(inString, oldPattern, newPattern); 95 assertTrue("Replace 1 worked", s.equals("a6AazAfoo77abfoo")); 96 97 s = StringUtils.replace(inString, "qwoeiruqopwieurpoqwieur", newPattern); 99 assertTrue("Replace non matched is equal", s.equals(inString)); 100 101 s = StringUtils.replace(inString, oldPattern, null); 103 assertTrue("Replace non matched is equal", s.equals(inString)); 104 105 s = StringUtils.replace(inString, null, newPattern); 107 assertTrue("Replace non matched is equal", s.equals(inString)); 108 } 109 110 public void testDelete() throws Exception { 111 String inString = "The quick brown fox jumped over the lazy dog"; 112 113 String noThe = StringUtils.delete(inString, "the"); 114 assertTrue("Result has no the [" + noThe + "]", 115 noThe.equals("The quick brown fox jumped over lazy dog")); 116 117 String nohe = StringUtils.delete(inString, "he"); 118 assertTrue("Result has no he [" + nohe + "]", 119 nohe.equals("T quick brown fox jumped over t lazy dog")); 120 121 String nosp = StringUtils.delete(inString, " "); 122 assertTrue("Result has no spaces", 123 nosp.equals("Thequickbrownfoxjumpedoverthelazydog")); 124 125 String killEnd = StringUtils.delete(inString, "dog"); 126 assertTrue("Result has no dog", 127 killEnd.equals("The quick brown fox jumped over the lazy ")); 128 129 String mismatch = StringUtils.delete(inString, "dxxcxcxog"); 130 assertTrue("Result is unchanged", mismatch.equals(inString)); 131 } 132 133 public void testDeleteAny() throws Exception { 134 String inString = "Able was I ere I saw Elba"; 135 136 String res = StringUtils.deleteAny(inString, "I"); 137 assertTrue("Result has no Is [" + res + "]", 138 res.equals("Able was ere saw Elba")); 139 140 res = StringUtils.deleteAny(inString, "AeEba!"); 141 assertTrue("Result has no Is [" + res + "]", 142 res.equals("l ws I r I sw l")); 143 144 String mismatch = StringUtils.deleteAny(inString, "#@$#$^"); 145 assertTrue("Result is unchanged", mismatch.equals(inString)); 146 147 String whitespace = 148 "This is\n\n\n \t a messagy string with whitespace\n"; 149 assertTrue("Has CR", whitespace.indexOf("\n") != -1); 150 assertTrue("Has tab", whitespace.indexOf("\t") != -1); 151 assertTrue("Has sp", whitespace.indexOf(" ") != -1); 152 String cleaned = StringUtils.deleteAny(whitespace, "\n\t "); 153 assertTrue("Has no CR", cleaned.indexOf("\n") == -1); 154 assertTrue("Has no tab", cleaned.indexOf("\t") == -1); 155 assertTrue("Has no sp", cleaned.indexOf(" ") == -1); 156 assertTrue("Still has chars", cleaned.length() > 10); 157 } 158 159 160 public void testUnqualify() throws Exception { 161 String qualified = "i.am.not.unqualified"; 162 assertEquals("unqualified", StringUtils.unqualify(qualified)); 163 } 164 165 public void testUncapitalize() throws Exception { 166 String capitalized = "I am capitalized"; 167 assertEquals("i am capitalized", StringUtils.uncapitalize(capitalized)); 168 } 169 170 public void testCapitalize() throws Exception { 171 String capitalized = "i am not capitalized"; 172 assertEquals("I am not capitalized", StringUtils.capitalize(capitalized)); 173 } 174 175 public void testPathEquals() { 176 assertTrue("Must be true for the same strings", 177 StringUtils.pathEquals("/dummy1/dummy2/dummy3", 178 "/dummy1/dummy2/dummy3")); 179 assertTrue("Must be true for the same win strings", 180 StringUtils.pathEquals("C:\\dummy1\\dummy2\\dummy3", 181 "C:\\dummy1\\dummy2\\dummy3")); 182 assertTrue("Must be true for one top path on 1", 183 StringUtils.pathEquals("/dummy1/bin/../dummy2/dummy3", 184 "/dummy1/dummy2/dummy3")); 185 assertTrue("Must be true for one win top path on 2", 186 StringUtils.pathEquals("C:\\dummy1\\dummy2\\dummy3", 187 "C:\\dummy1\\bin\\..\\dummy2\\dummy3")); 188 assertTrue("Must be true for two top paths on 1", 189 StringUtils.pathEquals("/dummy1/bin/../dummy2/bin/../dummy3", 190 "/dummy1/dummy2/dummy3")); 191 assertTrue("Must be true for two win top paths on 2", 192 StringUtils.pathEquals("C:\\dummy1\\dummy2\\dummy3", 193 "C:\\dummy1\\bin\\..\\dummy2\\bin\\..\\dummy3")); 194 assertTrue("Must be true for double top paths on 1", 195 StringUtils.pathEquals("/dummy1/bin/tmp/../../dummy2/dummy3", 196 "/dummy1/dummy2/dummy3")); 197 assertTrue("Must be true for double top paths on 2 with similarity", 198 StringUtils.pathEquals("/dummy1/dummy2/dummy3", 199 "/dummy1/dum/dum/../../dummy2/dummy3")); 200 assertTrue("Must be true for current paths", 201 StringUtils.pathEquals("./dummy1/dummy2/dummy3", 202 "dummy1/dum/./dum/../../dummy2/dummy3")); 203 assertFalse("Must be false for relative/absolute paths", 204 StringUtils.pathEquals("./dummy1/dummy2/dummy3", 205 "/dummy1/dum/./dum/../../dummy2/dummy3")); 206 assertFalse("Must be false for different strings", 207 StringUtils.pathEquals("/dummy1/dummy2/dummy3", 208 "/dummy1/dummy4/dummy3")); 209 assertFalse("Must be false for one false path on 1", 210 StringUtils.pathEquals("/dummy1/bin/tmp/../dummy2/dummy3", 211 "/dummy1/dummy2/dummy3")); 212 assertFalse("Must be false for one false win top path on 2", 213 StringUtils.pathEquals("C:\\dummy1\\dummy2\\dummy3", 214 "C:\\dummy1\\bin\\tmp\\..\\dummy2\\dummy3")); 215 assertFalse("Must be false for top path on 1 + difference", 216 StringUtils.pathEquals("/dummy1/bin/../dummy2/dummy3", 217 "/dummy1/dummy2/dummy4")); 218 } 219 220 221 public void testSplitArrayElementsIntoProperties() { 222 String [] input = new String [] {"key1=value1 ", "key2 =\"value2\""}; 223 Properties result = StringUtils.splitArrayElementsIntoProperties(input, "="); 224 assertEquals("value1", result.getProperty("key1")); 225 assertEquals("\"value2\"", result.getProperty("key2")); 226 } 227 228 public void testSplitArrayElementsIntoPropertiesAndDeletedChars() { 229 String [] input = new String [] {"key1=value1 ", "key2 =\"value2\""}; 230 Properties result = StringUtils.splitArrayElementsIntoProperties(input, "=", "\""); 231 assertEquals("value1", result.getProperty("key1")); 232 assertEquals("value2", result.getProperty("key2")); 233 } 234 235 public void testTokenizeToStringArray() { 236 String [] sa = StringUtils.tokenizeToStringArray("a,b , ,c", ","); 237 assertEquals(3, sa.length); 238 assertTrue("components are correct", 239 sa[0].equals("a") && sa[1].equals("b") && sa[2].equals("c")); 240 } 241 242 public void testTokenizeToStringArrayWithNotIgnoreEmptyTokens() { 243 String [] sa = StringUtils.tokenizeToStringArray("a,b , ,c", ",", true, false); 244 assertEquals(4, sa.length); 245 assertTrue("components are correct", 246 sa[0].equals("a") && sa[1].equals("b") && sa[2].equals("") && sa[3].equals("c")); 247 } 248 249 public void testTokenizeToStringArrayWithNotTrimTokens() { 250 String [] sa = StringUtils.tokenizeToStringArray("a,b ,c", ",", false, true); 251 assertEquals(3, sa.length); 252 assertTrue("components are correct", 253 sa[0].equals("a") && sa[1].equals("b ") && sa[2].equals("c")); 254 } 255 256 public void testCommaDelimitedListToStringArrayWithNullProducesEmptyArray() { 257 String [] sa = StringUtils.commaDelimitedListToStringArray(null); 258 assertTrue("String array isn't null with null input", sa != null); 259 assertTrue("String array length == 0 with null input", sa.length == 0); 260 } 261 262 public void testCommaDelimitedListToStringArrayWithEmptyStringProducesEmptyArray() { 263 String [] sa = StringUtils.commaDelimitedListToStringArray(""); 264 assertTrue("String array isn't null with null input", sa != null); 265 assertTrue("String array length == 0 with null input", sa.length == 0); 266 } 267 268 private void testStringArrayReverseTransformationMatches(String [] sa) { 269 String [] reverse = 270 StringUtils.commaDelimitedListToStringArray(StringUtils.arrayToCommaDelimitedString(sa)); 271 assertEquals("Reverse transformation is equal", 272 Arrays.asList(sa), 273 Arrays.asList(reverse)); 274 } 275 276 public void testCommaDelimitedListToStringArrayMatchWords() { 277 String [] sa = new String [] {"foo", "bar", "big"}; 279 doTestCommaDelimitedListToStringArrayLegalMatch(sa); 280 testStringArrayReverseTransformationMatches(sa); 281 282 sa = new String [] {"a", "b", "c"}; 283 doTestCommaDelimitedListToStringArrayLegalMatch(sa); 284 testStringArrayReverseTransformationMatches(sa); 285 286 sa = new String [] {"AA", "AA", "AA", "AA", "AA"}; 288 doTestCommaDelimitedListToStringArrayLegalMatch(sa); 289 testStringArrayReverseTransformationMatches(sa); 290 } 291 292 public void testCommaDelimitedListToStringArraySingleString() { 293 String s = "woeirqupoiewuropqiewuorpqiwueopriquwopeiurqopwieur"; 295 String [] sa = StringUtils.commaDelimitedListToStringArray(s); 296 assertTrue("Found one String with no delimiters", sa.length == 1); 297 assertTrue("Single array entry matches input String with no delimiters", 298 sa[0].equals(s)); 299 } 300 301 public void testCommaDelimitedListToStringArrayWithOtherPunctuation() { 302 String [] sa = new String [] {"xcvwert4456346&*.", "///", ".!", ".", ";"}; 304 doTestCommaDelimitedListToStringArrayLegalMatch(sa); 305 } 306 307 310 public void testCommaDelimitedListToStringArrayEmptyStrings() { 311 String [] sa = StringUtils.commaDelimitedListToStringArray("a,,b"); 313 assertEquals("a,,b produces array length 3", 3, sa.length); 314 assertTrue("components are correct", 315 sa[0].equals("a") && sa[1].equals("") && sa[2].equals("b")); 316 317 sa = new String [] {"", "", "a", ""}; 318 doTestCommaDelimitedListToStringArrayLegalMatch(sa); 319 } 320 321 private void doTestCommaDelimitedListToStringArrayLegalMatch(String [] components) { 322 StringBuffer sbuf = new StringBuffer (); 323 for (int i = 0; i < components.length; i++) { 324 if (i != 0) { 325 sbuf.append(","); 326 } 327 sbuf.append(components[i]); 328 } 329 330 String [] sa = StringUtils.commaDelimitedListToStringArray(sbuf.toString()); 331 assertTrue("String array isn't null with legal match", sa != null); 332 assertEquals("String array length is correct with legal match", components.length, sa.length); 333 assertTrue("Output equals input", Arrays.equals(sa, components)); 334 } 335 336 } 337 | Popular Tags |