KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > util > StringUtilsTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.util;
18
19 import java.util.Arrays JavaDoc;
20 import java.util.Properties JavaDoc;
21
22 import junit.framework.TestCase;
23
24 /**
25  * @author Rod Johnson
26  * @author Juergen Hoeller
27  */

28 public class StringUtilsTests extends TestCase {
29
30     public void testHasTextBlank() throws Exception JavaDoc {
31         String JavaDoc blank = " ";
32         assertEquals(false, StringUtils.hasText(blank));
33     }
34
35     public void testHasTextNullEmpty() throws Exception JavaDoc {
36         assertEquals(false, StringUtils.hasText(null));
37         assertEquals(false, StringUtils.hasText(""));
38     }
39
40     public void testHasTextValid() throws Exception JavaDoc {
41         assertEquals(true, StringUtils.hasText("t"));
42     }
43
44     public void testTrimLeadingWhitespace() throws Exception JavaDoc {
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 JavaDoc {
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 JavaDoc 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 JavaDoc {
89         String JavaDoc inString = "a6AazAaa77abaa";
90         String JavaDoc oldPattern = "aa";
91         String JavaDoc newPattern = "foo";
92
93         // Simple replace
94
String JavaDoc s = StringUtils.replace(inString, oldPattern, newPattern);
95         assertTrue("Replace 1 worked", s.equals("a6AazAfoo77abfoo"));
96
97         // Non match: no change
98
s = StringUtils.replace(inString, "qwoeiruqopwieurpoqwieur", newPattern);
99         assertTrue("Replace non matched is equal", s.equals(inString));
100
101         // Null new pattern: should ignore
102
s = StringUtils.replace(inString, oldPattern, null);
103         assertTrue("Replace non matched is equal", s.equals(inString));
104
105         // Null old pattern: should ignore
106
s = StringUtils.replace(inString, null, newPattern);
107         assertTrue("Replace non matched is equal", s.equals(inString));
108     }
109
110     public void testDelete() throws Exception JavaDoc {
111         String JavaDoc inString = "The quick brown fox jumped over the lazy dog";
112
113         String JavaDoc 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 JavaDoc 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 JavaDoc nosp = StringUtils.delete(inString, " ");
122         assertTrue("Result has no spaces",
123                 nosp.equals("Thequickbrownfoxjumpedoverthelazydog"));
124
125         String JavaDoc 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 JavaDoc mismatch = StringUtils.delete(inString, "dxxcxcxog");
130         assertTrue("Result is unchanged", mismatch.equals(inString));
131     }
132
133     public void testDeleteAny() throws Exception JavaDoc {
134         String JavaDoc inString = "Able was I ere I saw Elba";
135
136         String JavaDoc 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 JavaDoc mismatch = StringUtils.deleteAny(inString, "#@$#$^");
145         assertTrue("Result is unchanged", mismatch.equals(inString));
146
147         String JavaDoc 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 JavaDoc 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 JavaDoc {
161         String JavaDoc qualified = "i.am.not.unqualified";
162         assertEquals("unqualified", StringUtils.unqualify(qualified));
163     }
164
165     public void testUncapitalize() throws Exception JavaDoc {
166         String JavaDoc capitalized = "I am capitalized";
167         assertEquals("i am capitalized", StringUtils.uncapitalize(capitalized));
168     }
169
170     public void testCapitalize() throws Exception JavaDoc {
171         String JavaDoc 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 JavaDoc[] input = new String JavaDoc[] {"key1=value1 ", "key2 =\"value2\""};
223         Properties JavaDoc result = StringUtils.splitArrayElementsIntoProperties(input, "=");
224         assertEquals("value1", result.getProperty("key1"));
225         assertEquals("\"value2\"", result.getProperty("key2"));
226     }
227
228     public void testSplitArrayElementsIntoPropertiesAndDeletedChars() {
229         String JavaDoc[] input = new String JavaDoc[] {"key1=value1 ", "key2 =\"value2\""};
230         Properties JavaDoc result = StringUtils.splitArrayElementsIntoProperties(input, "=", "\"");
231         assertEquals("value1", result.getProperty("key1"));
232         assertEquals("value2", result.getProperty("key2"));
233     }
234
235     public void testTokenizeToStringArray() {
236         String JavaDoc[] 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 JavaDoc[] 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 JavaDoc[] 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 JavaDoc[] 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 JavaDoc[] 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 JavaDoc[] sa) {
269         String JavaDoc[] 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         // Could read these from files
278
String JavaDoc[] sa = new String JavaDoc[] {"foo", "bar", "big"};
279         doTestCommaDelimitedListToStringArrayLegalMatch(sa);
280         testStringArrayReverseTransformationMatches(sa);
281
282         sa = new String JavaDoc[] {"a", "b", "c"};
283         doTestCommaDelimitedListToStringArrayLegalMatch(sa);
284         testStringArrayReverseTransformationMatches(sa);
285
286         // Test same words
287
sa = new String JavaDoc[] {"AA", "AA", "AA", "AA", "AA"};
288         doTestCommaDelimitedListToStringArrayLegalMatch(sa);
289         testStringArrayReverseTransformationMatches(sa);
290     }
291
292     public void testCommaDelimitedListToStringArraySingleString() {
293         // Could read these from files
294
String JavaDoc s = "woeirqupoiewuropqiewuorpqiwueopriquwopeiurqopwieur";
295         String JavaDoc[] 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         // Could read these from files
303
String JavaDoc[] sa = new String JavaDoc[] {"xcvwert4456346&*.", "///", ".!", ".", ";"};
304         doTestCommaDelimitedListToStringArrayLegalMatch(sa);
305     }
306
307     /**
308      * We expect to see the empty Strings in the output.
309      */

310     public void testCommaDelimitedListToStringArrayEmptyStrings() {
311         // Could read these from files
312
String JavaDoc[] 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 JavaDoc[] {"", "", "a", ""};
318         doTestCommaDelimitedListToStringArrayLegalMatch(sa);
319     }
320
321     private void doTestCommaDelimitedListToStringArrayLegalMatch(String JavaDoc[] components) {
322         StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
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 JavaDoc[] 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