KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > util > StringUtilTest


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.util;
4
5 import junit.framework.TestCase;
6
7 public class StringUtilTest extends TestCase {
8
9     public void testSplit() {
10         String JavaDoc src = "1,22,3,44,5";
11         String JavaDoc[] r;
12
13         r = StringUtil.split(src, ",");
14         assertEquals(5, r.length);
15         assertEquals("1", r[0]);
16         assertEquals("22", r[1]);
17         assertEquals("3", r[2]);
18         assertEquals("44", r[3]);
19         assertEquals("5", r[4]);
20
21         src = "1,22,,,5";
22         r = StringUtil.split(src, ",");
23         assertEquals(5, r.length);
24         assertEquals("1", r[0]);
25         assertEquals("22", r[1]);
26         assertEquals("", r[2]);
27         assertEquals("", r[3]);
28         assertEquals("5", r[4]);
29
30
31         src = "173";
32         r = StringUtil.split(src, ",");
33         assertEquals(1, r.length);
34         assertEquals("173", r[0]);
35
36         src = ",";
37         r = StringUtil.split(src, ",");
38         assertEquals(2, r.length);
39         assertEquals("", r[0]);
40         assertEquals("", r[1]);
41     }
42
43
44     public void testSplit2() {
45         String JavaDoc src = "1,22,3,44,5";
46         String JavaDoc[] r;
47
48         r = StringUtil.splitc(src, "a,.q");
49         assertEquals(5, r.length);
50         assertEquals("1", r[0]);
51         assertEquals("22", r[1]);
52         assertEquals("3", r[2]);
53         assertEquals("44", r[3]);
54         assertEquals("5", r[4]);
55
56         src = "1,22,,,5";
57         r = StringUtil.splitc(src, ",");
58         assertEquals(3, r.length);
59         assertEquals("1", r[0]);
60         assertEquals("22", r[1]);
61         assertEquals("5", r[2]);
62
63         r = StringUtil.splitc(src, ',');
64         assertEquals(3, r.length);
65         assertEquals("1", r[0]);
66         assertEquals("22", r[1]);
67         assertEquals("5", r[2]);
68
69
70
71         src = "173";
72         r = StringUtil.splitc(src, ",");
73         assertEquals(1, r.length);
74         assertEquals("173", r[0]);
75
76         src = "173";
77         r = StringUtil.splitc(src, ',');
78         assertEquals(1, r.length);
79         assertEquals("173", r[0]);
80
81
82         src = ",";
83         r = StringUtil.splitc(src, ",");
84         assertEquals(2, r.length);
85         assertEquals("", r[0]);
86         assertEquals("", r[1]);
87
88         src = ",";
89         r = StringUtil.splitc(src, ',');
90         assertEquals(2, r.length);
91         assertEquals("", r[0]);
92         assertEquals("", r[1]);
93
94         
95         src = "1, 22 , 5";
96         r = StringUtil.splitc(src, ", ");
97         assertEquals(3, r.length);
98         assertEquals("1", r[0]);
99         assertEquals("22", r[1]);
100         assertEquals("5", r[2]);
101
102         src = " , 22 , 5";
103         r = StringUtil.splitc(src, ", ");
104         assertEquals(3, r.length);
105         assertEquals("", r[0]);
106         assertEquals("22", r[1]);
107         assertEquals("5", r[2]);
108     }
109
110
111     public void testReplace() {
112         String JavaDoc src = "12345";
113
114         assertEquals("12qwe45", StringUtil.replace(src, "3", "qwe"));
115         assertEquals("1234qwe", StringUtil.replace(src, "5", "qwe"));
116         assertEquals("qwe2345", StringUtil.replace(src, "1", "qwe"));
117         assertEquals(src, StringUtil.replace(src, "0", "qwe"));
118         assertEquals(src, StringUtil.replace(src, "0", null));
119
120         src = "100010001";
121         assertEquals("dd000dd000dd", StringUtil.replace(src, "1", "dd"));
122         assertEquals(src, StringUtil.replace(src, "2", "dd"));
123
124         src = "qweqwe";
125         assertEquals("QWEQWE", StringUtil.replace(src, "qwe", "QWE"));
126
127         src = "11221144";
128         src = StringUtil.replaceFirst(src, "11", "55");
129         assertEquals("55221144", src);
130         src = StringUtil.replaceFirst(src, "11", "55");
131         assertEquals("55225544", src);
132         src = StringUtil.replaceFirst(src, "11", "55");
133         assertEquals("55225544", src);
134
135         src = StringUtil.replaceLast(src, "55", "11");
136         assertEquals("55221144", src);
137         src = StringUtil.replaceLast(src, "55", "11");
138         assertEquals("11221144", src);
139         src = StringUtil.replaceLast(src, "55", "11");
140         assertEquals("11221144", src);
141     }
142
143
144     public void testIndexOf() {
145         String JavaDoc src = "1234567890qWeRtY";
146
147         assertEquals(0, StringUtil.indexOfIgnoreCase(src, "123"));
148         assertEquals(0, StringUtil.lastIndexOfIgnoreCase(src, "123"));
149         assertEquals(0, src.lastIndexOf("123"));
150         assertTrue(StringUtil.startsWithIgnoreCase(src, "123"));
151         assertTrue(StringUtil.endsWithIgnoreCase(src, "y"));
152         assertTrue(StringUtil.endsWithIgnoreCase(src, "qwerty"));
153         assertFalse(StringUtil.endsWithIgnoreCase(src, "qwert"));
154         assertFalse(StringUtil.endsWithIgnoreCase(src, "q"));
155         
156         assertEquals(0, StringUtil.indexOfIgnoreCase(src, "1234567890QwErTy"));
157         assertEquals(0, StringUtil.lastIndexOfIgnoreCase(src, "1234567890QwErTy"));
158         assertTrue(StringUtil.startsWithIgnoreCase(src, "1234567890QwErTy"));
159         assertTrue(StringUtil.endsWithIgnoreCase(src, "1234567890QwErTy"));
160
161         assertEquals(1, StringUtil.indexOfIgnoreCase(src, "2345"));
162         assertEquals(1, StringUtil.lastIndexOfIgnoreCase(src, "2345"));
163         assertFalse(StringUtil.startsWithIgnoreCase(src, "2345"));
164
165         assertEquals(10, StringUtil.indexOfIgnoreCase(src, "qwe"));
166         assertEquals(10, StringUtil.lastIndexOfIgnoreCase(src, "qwe"));
167         assertTrue(StringUtil.startsWithIgnoreCase(src, "qwe", 10));
168
169         assertEquals(10, StringUtil.indexOfIgnoreCase(src, "qwerty"));
170         assertEquals(10, StringUtil.lastIndexOfIgnoreCase(src, "qwerty"));
171         assertTrue(StringUtil.startsWithIgnoreCase(src, "qwerty", 10));
172
173         assertEquals(10, StringUtil.indexOfIgnoreCase(src, "QWERTY"));
174         assertEquals(10, StringUtil.lastIndexOfIgnoreCase(src, "QWERTY"));
175         assertTrue(StringUtil.startsWithIgnoreCase(src, "QWERTY", 10));
176
177         assertEquals(-1, StringUtil.indexOfIgnoreCase(src, "qwertyu"));
178         assertEquals(-1, StringUtil.lastIndexOfIgnoreCase(src, "qwertyu"));
179         assertFalse(StringUtil.startsWithIgnoreCase(src, "qwertyu", 10));
180
181         assertEquals(10, StringUtil.indexOfIgnoreCase(src, "qwerty", 9));
182         assertEquals(-1, StringUtil.lastIndexOfIgnoreCase(src, "qwerty", 9));
183         assertEquals(-1, src.lastIndexOf("qWeRtY", 9));
184
185         assertEquals(-1, StringUtil.indexOfIgnoreCase(src, "qwerty", 11));
186         assertEquals(10, StringUtil.lastIndexOfIgnoreCase(src, "qwerty", 11));
187         assertEquals(10, src.lastIndexOf("qWeRtY", 11));
188         assertEquals(10, StringUtil.lastIndexOfIgnoreCase(src, "qwerty", 10));
189         assertEquals(10, src.lastIndexOf("qWeRtY", 10));
190         assertFalse(StringUtil.startsWithIgnoreCase(src, "qwerty", 11));
191
192         src = "AAA111aaa";
193
194         assertEquals(0, StringUtil.indexOfIgnoreCase(src, "aaa", 0));
195         assertEquals(0, StringUtil.lastIndexOfIgnoreCase(src, "aaa", 0));
196         assertEquals(0, src.lastIndexOf("AAA", 0));
197         assertTrue(StringUtil.startsWithIgnoreCase(src, "aaa", 0));
198         assertTrue(StringUtil.startsWithIgnoreCase(src, "aa", 1));
199         assertTrue(StringUtil.startsWithIgnoreCase(src, "a", 2));
200         assertTrue(StringUtil.startsWithIgnoreCase(src, "A", 6));
201         assertTrue(StringUtil.startsWithIgnoreCase(src, "AA", 6));
202         assertTrue(StringUtil.startsWithIgnoreCase(src, "AAA", 6));
203         assertFalse(StringUtil.startsWithIgnoreCase(src, "AAAA", 6));
204         
205         assertEquals(6, StringUtil.indexOfIgnoreCase(src, "aaa", 1));
206         assertEquals(6, StringUtil.lastIndexOfIgnoreCase(src, "aaa", 7));
207         assertEquals(6, src.lastIndexOf("aaa", 7));
208
209         src = "123";
210
211         assertEquals(-1, StringUtil.indexOfIgnoreCase(src, "1234"));
212         assertEquals(-1, StringUtil.lastIndexOfIgnoreCase(src, "1234"));
213
214         assertEquals(0, StringUtil.indexOfIgnoreCase(src, ""));
215         assertEquals(3, StringUtil.lastIndexOfIgnoreCase(src, ""));
216         assertEquals(3, src.lastIndexOf(""));
217     }
218     
219     public void testRemove() {
220         String JavaDoc s = "qwertyq";
221         assertEquals("qwertyq", StringUtil.remove(s, "W"));
222         assertEquals("qertyq", StringUtil.remove(s, "w"));
223         assertEquals("", StringUtil.remove(s, "qwertyq"));
224         assertEquals("qwertyq", StringUtil.remove(s, ""));
225         assertEquals("werty", StringUtil.remove(s, "q"));
226         assertEquals("qq", StringUtil.remove(s, "werty"));
227         
228         assertEquals("werty", StringUtil.removeChars(s, "q"));
229         assertEquals("werty", StringUtil.remove(s, 'q'));
230         assertEquals("qeryq", StringUtil.removeChars(s, "wt"));
231         assertEquals("qeryq", StringUtil.removeChars(s, new char[] {'w', 't'}));
232         assertEquals("", StringUtil.removeChars(s, "qwerty".toCharArray()));
233         assertEquals("", StringUtil.removeChars(s, "qwerty"));
234     }
235     
236     public void testArrays() {
237         String JavaDoc s = "qwertyuiop";
238         
239         assertEquals("qWERtyuIOp", StringUtil.replace(s, new String JavaDoc[] {"wer", "io"}, new String JavaDoc[] {"WER", "IO"}));
240         assertEquals("qwertyuiop", StringUtil.replace(s, new String JavaDoc[] {"wer1", "io1"}, new String JavaDoc[] {"WER", "IO"}));
241         
242         assertEquals("qWERtyuIOP", StringUtil.replace(s, new String JavaDoc[] {"wer", "iop"}, new String JavaDoc[] {"WER", "IOP"}));
243         assertEquals("qWERtyuIOP", StringUtil.replaceIgnoreCase(s, new String JavaDoc[] {"WER", "IOP"}, new String JavaDoc[] {"WER", "IOP"}));
244         
245         assertEquals("qwertyuiop", StringUtil.replace(s, new String JavaDoc[] {}, new String JavaDoc[] {}));
246         
247         assertEquals("qWERtyuiop", StringUtil.replace(s, new String JavaDoc[] {"wer", "we"}, new String JavaDoc[] {"WER", "11"}));
248         
249         assertTrue(StringUtil.equals(new String JavaDoc[] {"wer", "io"}, new String JavaDoc[] {"wer", "io"}));
250         assertFalse(StringUtil.equals(new String JavaDoc[] {"wer", "io"}, new String JavaDoc[] {"WER", "IO"}));
251         assertTrue(StringUtil.equalsIgnoreCase(new String JavaDoc[] {"wer", "io"}, new String JavaDoc[] {"WER", "IO"}));
252         
253         assertEquals(1, StringUtil.indexOf(s, new String JavaDoc[] {"wer", "io"})[1]);
254         assertEquals(7, StringUtil.indexOfIgnoreCase(s, new String JavaDoc[] {"wer", "IO"}, 2)[1]);
255         assertEquals(7, StringUtil.lastIndexOf(s, new String JavaDoc[] {"wer", "io"})[1]);
256         assertEquals(1, StringUtil.lastIndexOfIgnoreCase(s, new String JavaDoc[] {"WER", "io"}, 5)[1]);
257     }
258
259     public void testRanges() {
260         String JavaDoc s = "qwertyiop";
261
262         assertEquals(1, StringUtil.indexOf(s, "wer", 0, 5));
263         assertEquals(1, StringUtil.indexOf(s, 'w', 0, 5));
264         assertEquals(1, StringUtil.indexOf(s, "wer", 1, 5));
265         assertEquals(1, StringUtil.indexOf(s, 'w', 1, 5));
266         assertEquals(-1, StringUtil.indexOf(s, "wer", 2, 5));
267         assertEquals(-1, StringUtil.indexOf(s, 'w', 2, 5));
268         assertEquals(1, s.indexOf("wer"));
269         assertEquals(1, s.indexOf("wer", 1));
270         assertEquals(-1, s.indexOf("wer", 2));
271
272         assertEquals(1, StringUtil.indexOf(s, "wer", 1, 4));
273         assertEquals(1, StringUtil.indexOf(s, 'w', 1, 4));
274         assertEquals(-1, StringUtil.indexOf(s, "wer", 1, 3));
275         assertEquals(1, StringUtil.indexOf(s, 'w', 1, 3));
276         assertEquals(-1, StringUtil.indexOf(s, "wer", 0, 3));
277         assertEquals(1, StringUtil.indexOf(s, 'w', 0, 3));
278         assertEquals('r', s.charAt(3));
279
280
281         assertEquals(6, StringUtil.indexOf(s, "iop", 0, s.length()));
282         assertEquals(6, StringUtil.indexOf(s, 'i', 0, s.length()));
283         assertEquals(6, StringUtil.indexOf(s, "iop", 6, s.length()));
284         assertEquals(6, StringUtil.indexOf(s, 'i', 6, s.length()));
285         assertEquals(-1, StringUtil.indexOf(s, "iop", 0, s.length() - 1));
286         assertEquals(6, StringUtil.indexOf(s, 'i', 0, s.length() - 1));
287         assertEquals(-1, StringUtil.indexOf(s, "iop", 7, s.length()));
288         assertEquals(-1, StringUtil.indexOf(s, 'i', 7, s.length()));
289         assertEquals(6, s.indexOf("iop", 0));
290         assertEquals(6, s.indexOf("iop", 6));
291         assertEquals(-1, s.indexOf("iop", 7));
292
293
294
295         assertEquals(1, StringUtil.indexOfIgnoreCase(s, "wEr", 0, 5));
296         assertEquals(1, StringUtil.indexOfIgnoreCase(s, "wEr", 1, 5));
297         assertEquals(-1, StringUtil.indexOfIgnoreCase(s, "wEr", 2, 5));
298
299         assertEquals(1, StringUtil.indexOfIgnoreCase(s, "wEr", 1, 4));
300         assertEquals(-1, StringUtil.indexOfIgnoreCase(s, "wEr", 0, 3));
301
302         assertEquals(6, StringUtil.indexOfIgnoreCase(s, "iOp", 0, s.length()));
303         assertEquals(6, StringUtil.indexOfIgnoreCase(s, "iOp", 6, s.length()));
304         assertEquals(-1, StringUtil.indexOfIgnoreCase(s, "iOp", 0, s.length() - 1));
305         assertEquals(-1, StringUtil.indexOfIgnoreCase(s, "iOp", 7, s.length()));
306
307
308
309         assertEquals(1, StringUtil.lastIndexOfIgnoreCase(s, "wEr", 5, 0));
310         assertEquals(1, StringUtil.lastIndexOfIgnoreCase(s, "wEr", 5, 1));
311         assertEquals(-1, StringUtil.lastIndexOfIgnoreCase(s, "wEr", 5, 2));
312         assertEquals(1, StringUtil.lastIndexOf(s, "wer", 5, 0));
313         assertEquals(1, StringUtil.lastIndexOf(s, 'w', 5, 0));
314         assertEquals(1, StringUtil.lastIndexOf(s, "wer", 5, 1));
315         assertEquals(1, StringUtil.lastIndexOf(s, 'w', 5, 1));
316         assertEquals(-1, StringUtil.lastIndexOf(s, "wer", 5, 2));
317         assertEquals(-1, StringUtil.lastIndexOf(s, 'w', 5, 2));
318         assertEquals('w', s.charAt(1));
319         assertEquals(1, s.lastIndexOf("wer", 4));
320         assertEquals(1, s.lastIndexOf("wer", 3));
321         assertEquals(1, s.lastIndexOf("wer", 2));
322         assertEquals(1, s.lastIndexOf("wer", 1));
323         assertEquals(-1, s.lastIndexOf("wer", 0));
324         assertEquals(1, StringUtil.lastIndexOfIgnoreCase(s, "wEr", 4));
325         assertEquals(1, StringUtil.lastIndexOfIgnoreCase(s, "wEr", 3));
326         assertEquals(1, StringUtil.lastIndexOfIgnoreCase(s, "wEr", 2));
327         assertEquals(1, StringUtil.lastIndexOfIgnoreCase(s, "wEr", 1));
328         assertEquals(-1, StringUtil.lastIndexOfIgnoreCase(s, "wEr", 0));
329         assertEquals(1, StringUtil.lastIndexOf(s, "wer", 4, 0));
330         assertEquals(1, StringUtil.lastIndexOf(s, 'w', 4, 0));
331         assertEquals(1, StringUtil.lastIndexOf(s, "wer", 3, 0));
332         assertEquals(1, StringUtil.lastIndexOf(s, 'w', 3, 0));
333         assertEquals(1, StringUtil.lastIndexOf(s, "wer", 2, 0));
334         assertEquals(1, StringUtil.lastIndexOf(s, 'w', 2, 0));
335         assertEquals(1, StringUtil.lastIndexOf(s, "wer", 1, 0));
336         assertEquals(1, StringUtil.lastIndexOf(s, 'w', 1, 0));
337         assertEquals(-1, StringUtil.lastIndexOf(s, "wer", 0, 0));
338         assertEquals(-1, StringUtil.lastIndexOf(s, 'w', 0, 0));
339
340         assertEquals(1, StringUtil.lastIndexOfIgnoreCase(s, "wEr", 4, 1));
341         assertEquals(1, StringUtil.lastIndexOfIgnoreCase(s, "wEr", 3, 1));
342         assertEquals(-1, StringUtil.lastIndexOfIgnoreCase(s, "wEr", 3, 2));
343         assertEquals(1, StringUtil.lastIndexOf(s, "wer", 4, 1));
344         assertEquals(1, StringUtil.lastIndexOf(s, 'w', 4, 1));
345         assertEquals(1, StringUtil.lastIndexOf(s, "wer", 3, 1));
346         assertEquals(1, StringUtil.lastIndexOf(s, 'w', 3, 1));
347         assertEquals(-1, StringUtil.lastIndexOf(s, "wer", 3, 2));
348         assertEquals(-1, StringUtil.lastIndexOf(s, 'w', 3, 2));
349
350         assertEquals(6, StringUtil.lastIndexOfIgnoreCase(s, "iOp", s.length(), 0));
351         assertEquals(6, StringUtil.lastIndexOf(s, "iop", s.length(), 0));
352         assertEquals(6, StringUtil.lastIndexOf(s, 'i', s.length(), 0));
353         assertEquals(6, s.lastIndexOf("iop", s.length()));
354         assertEquals(6, StringUtil.lastIndexOfIgnoreCase(s, "iOp", s.length(), 6));
355         assertEquals(6, StringUtil.lastIndexOf(s, "iop", s.length(), 6));
356         assertEquals(6, StringUtil.lastIndexOf(s, 'i', s.length(), 6));
357         assertEquals(6, StringUtil.lastIndexOfIgnoreCase(s, "iOp", s.length() - 1, 0));
358         assertEquals(6, StringUtil.lastIndexOf(s, "iop", s.length() - 1, 0));
359         assertEquals(6, StringUtil.lastIndexOf(s, 'i', s.length() - 1, 0));
360         assertEquals(6, s.lastIndexOf("iop", s.length() - 1));
361         assertEquals(-1, StringUtil.lastIndexOfIgnoreCase(s, "iOp", s.length(), 7));
362         assertEquals(-1, StringUtil.lastIndexOf(s, "iop", s.length(), 7));
363         assertEquals(-1, StringUtil.lastIndexOf(s, 'i', s.length(), 7));
364
365
366         assertEquals(0, StringUtil.indexOf(s, "", 0, 5));
367         assertEquals(1, StringUtil.indexOf(s, "", 1, 5));
368         assertEquals(0, s.indexOf("", 0));
369         assertEquals(1, s.indexOf("", 1));
370
371         assertEquals(4, StringUtil.lastIndexOf(s, "", 4, 0));
372     }
373
374
375     public void testEscapeQuotes() {
376         String JavaDoc s = "ccc'aaa";
377         assertEquals("ccc\\'aaa", StringUtil.replace(s, "'", "\\'"));
378
379         s = "ccc\"aaa";
380         assertEquals("ccc\\\"aaa", StringUtil.replace(s, "\"", "\\\""));
381     }
382
383
384     public void testTrim() {
385         assertEquals("123", StringUtil.trimNonEmpty(" 123 "));
386         assertEquals("123", StringUtil.trimNonEmpty("123"));
387         assertNull(StringUtil.trimNonEmpty(""));
388         assertNull(StringUtil.trimNonEmpty(" "));
389     }
390
391
392     void checkInts(int x, int y, int z, int w, int[] arr) {
393         assertNotNull(arr);
394         assertEquals(x, arr[0]);
395         assertEquals(y, arr[1]);
396         assertEquals(z, arr[2]);
397         assertEquals(w, arr[3]);
398     }
399
400     public void testRegion() {
401         String JavaDoc string = "qwertyuiop";
402         int[] res;
403         res = StringUtil.indexOfRegion(string, "q", "e");
404         checkInts(0, 1, 2, 3, res);
405         assertEquals("w", string.substring(res[1], res[2]));
406         res = StringUtil.indexOfRegion(string, "qw", "e");
407         checkInts(0, 2, 2, 3, res);
408         assertEquals("", string.substring(res[1], res[2]));
409         res = StringUtil.indexOfRegion(string, "qwe", "e");
410         assertNull(res);
411         res = StringUtil.indexOfRegion(string, "qwe", "ui");
412         checkInts(0, 3, 6, 8, res);
413         assertEquals("rty", string.substring(res[1], res[2]));
414
415         string = "aa\\${fo${o\\}foo}dd";
416         res = StringUtil.indexOfRegion(string, "${", "}", '\\');
417         checkInts(7, 9, 15, 16, res);
418         assertEquals("o\\}foo", string.substring(res[1], res[2]));
419
420     }
421
422
423 }
424
Popular Tags