KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > util > TestStringUtil


1 package jodd.util;
2
3 import junit.framework.TestCase;
4
5 public class TestStringUtil extends TestCase {
6
7     public void testSplit() {
8         String src = "1,22,3,44,5";
9         String[] r = null;
10
11         r = StringUtil.split(src, ",");
12         assertEquals(5, r.length);
13         assertEquals("1", r[0]);
14         assertEquals("22", r[1]);
15         assertEquals("3", r[2]);
16         assertEquals("44", r[3]);
17         assertEquals("5", r[4]);
18
19         src = "1,22,,,5";
20         r = StringUtil.split(src, ",");
21         assertEquals(5, r.length);
22         assertEquals("1", r[0]);
23         assertEquals("22", r[1]);
24         assertEquals("", r[2]);
25         assertEquals("", r[3]);
26         assertEquals("5", r[4]);
27
28         r = StringUtil.split(null, ",");
29         assertNull(r);
30
31         src = "173";
32         r = StringUtil.split(src, ",");
33         assertEquals(1, r.length);
34         assertEquals("173", r[0]);
35
36         r = StringUtil.split(src, null);
37         assertEquals(1, r.length);
38         assertEquals("173", r[0]);
39
40         src = "1,2";
41         r = StringUtil.split(src, null);
42         assertEquals(1, r.length);
43         assertEquals("1,2", r[0]);
44
45         src = ",";
46         r = StringUtil.split(src, ",");
47         assertEquals(2, r.length);
48         assertEquals("", r[0]);
49         assertEquals("", r[1]);
50     }
51
52
53     public void testSplit2() {
54         String src = "1,22,3,44,5";
55         String[] r = null;
56
57         r = StringUtil.splitc(src, "a,.q");
58         assertEquals(5, r.length);
59         assertEquals("1", r[0]);
60         assertEquals("22", r[1]);
61         assertEquals("3", r[2]);
62         assertEquals("44", r[3]);
63         assertEquals("5", r[4]);
64
65         src = "1,22,,,5";
66         r = StringUtil.splitc(src, ",");
67         assertEquals(3, r.length);
68         assertEquals("1", r[0]);
69         assertEquals("22", r[1]);
70         assertEquals("5", r[2]);
71
72         r = StringUtil.splitc(null, ",");
73         assertNull(r);
74
75         src = "173";
76         r = StringUtil.splitc(src, ",");
77         assertEquals(1, r.length);
78         assertEquals("173", r[0]);
79
80         r = StringUtil.splitc(src, (String)null);
81         assertEquals(1, r.length);
82         assertEquals("173", r[0]);
83
84         src = "1,2";
85         r = StringUtil.splitc(src, null);
86         assertEquals(1, r.length);
87         assertEquals("1,2", r[0]);
88
89         src = ",";
90         r = StringUtil.splitc(src, ",");
91         assertEquals(2, r.length);
92         assertEquals("", r[0]);
93         assertEquals("", r[1]);
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 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         assertNull(StringUtil.replace(null, "1", "2"));
125
126         src = "11221144";
127         src = StringUtil.replaceFirst(src, "11", "55");
128         assertEquals("55221144", src);
129         src = StringUtil.replaceFirst(src, "11", "55");
130         assertEquals("55225544", src);
131         src = StringUtil.replaceFirst(src, "11", "55");
132         assertEquals("55225544", src);
133
134         src = StringUtil.replaceLast(src, "55", "11");
135         assertEquals("55221144", src);
136         src = StringUtil.replaceLast(src, "55", "11");
137         assertEquals("11221144", src);
138         src = StringUtil.replaceLast(src, "55", "11");
139         assertEquals("11221144", src);
140
141     }
142
143
144     public void testIndexOf() {
145         String src = "1234567890qWeRtY";
146
147         assertEquals(0, StringUtil.indexOfIgnoreCase(src, "123"));
148         assertEquals(0, StringUtil.lastIndexOfIgnoreCase(src, "123"));
149         assertTrue(StringUtil.startsWithIgnoreCase(src, "123"));
150         assertTrue(StringUtil.endsWithIgnoreCase(src, "y"));
151         assertTrue(StringUtil.endsWithIgnoreCase(src, "qwerty"));
152         assertFalse(StringUtil.endsWithIgnoreCase(src, "qwert"));
153         assertFalse(StringUtil.endsWithIgnoreCase(src, "q"));
154         
155         assertEquals(0, StringUtil.indexOfIgnoreCase(src, "1234567890QwErTy"));
156         assertEquals(0, StringUtil.lastIndexOfIgnoreCase(src, "1234567890QwErTy"));
157         assertTrue(StringUtil.startsWithIgnoreCase(src, "1234567890QwErTy"));
158         assertTrue(StringUtil.endsWithIgnoreCase(src, "1234567890QwErTy"));
159
160         assertEquals(1, StringUtil.indexOfIgnoreCase(src, "2345"));
161         assertEquals(1, StringUtil.lastIndexOfIgnoreCase(src, "2345"));
162         assertFalse(StringUtil.startsWithIgnoreCase(src, "2345"));
163
164         assertEquals(10, StringUtil.indexOfIgnoreCase(src, "qwe"));
165         assertEquals(10, StringUtil.lastIndexOfIgnoreCase(src, "qwe"));
166         assertTrue(StringUtil.startsWithIgnoreCase(src, "qwe", 10));
167
168         assertEquals(10, StringUtil.indexOfIgnoreCase(src, "qwerty"));
169         assertEquals(10, StringUtil.lastIndexOfIgnoreCase(src, "qwerty"));
170         assertTrue(StringUtil.startsWithIgnoreCase(src, "qwerty", 10));
171
172         assertEquals(10, StringUtil.indexOfIgnoreCase(src, "QWERTY"));
173         assertEquals(10, StringUtil.lastIndexOfIgnoreCase(src, "QWERTY"));
174         assertTrue(StringUtil.startsWithIgnoreCase(src, "QWERTY", 10));
175
176         assertEquals(-1, StringUtil.indexOfIgnoreCase(src, "qwertyu"));
177         assertEquals(-1, StringUtil.lastIndexOfIgnoreCase(src, "qwertyu"));
178         assertFalse(StringUtil.startsWithIgnoreCase(src, "qwertyu", 10));
179
180         assertEquals(10, StringUtil.indexOfIgnoreCase(src, "qwerty", 9));
181         assertEquals(10, StringUtil.lastIndexOfIgnoreCase(src, "qwerty", 9));
182
183         assertEquals(-1, StringUtil.indexOfIgnoreCase(src, "qwerty", 11));
184         assertEquals(-1, StringUtil.lastIndexOfIgnoreCase(src, "qwerty", 11));
185         assertFalse(StringUtil.startsWithIgnoreCase(src, "qwerty", 11));
186
187         src = "AAA111aaa";
188
189         assertEquals(0, StringUtil.indexOfIgnoreCase(src, "aaa", 0));
190         assertEquals(6, StringUtil.lastIndexOfIgnoreCase(src, "aaa", 0));
191         assertTrue(StringUtil.startsWithIgnoreCase(src, "aaa", 0));
192         assertTrue(StringUtil.startsWithIgnoreCase(src, "aa", 1));
193         assertTrue(StringUtil.startsWithIgnoreCase(src, "a", 2));
194         assertTrue(StringUtil.startsWithIgnoreCase(src, "A", 6));
195         assertTrue(StringUtil.startsWithIgnoreCase(src, "AA", 6));
196         assertTrue(StringUtil.startsWithIgnoreCase(src, "AAA", 6));
197         assertFalse(StringUtil.startsWithIgnoreCase(src, "AAAA", 6));
198         
199         assertEquals(6, StringUtil.indexOfIgnoreCase(src, "aaa", 1));
200         assertEquals(6, StringUtil.lastIndexOfIgnoreCase(src, "aaa", 1));
201
202         src = "123";
203
204         assertEquals(-1, StringUtil.indexOfIgnoreCase(src, "1234"));
205         assertEquals(-1, StringUtil.lastIndexOfIgnoreCase(src, "1234"));
206
207         assertEquals(0, StringUtil.indexOfIgnoreCase(src, ""));
208         assertEquals(3, StringUtil.lastIndexOfIgnoreCase(src, ""));
209     }
210     
211     
212     public void testMatch() {
213         assertTrue(StringUtil.match("CfgOptions.class", "C*class"));
214         assertFalse(StringUtil.match("CfgOptions.class", "C*clas"));
215         assertFalse(StringUtil.match("C*r*class", "CfgOptions.class"));
216         
217         // wildcard on the start and end of the expression
218
assertTrue(StringUtil.match("CfgOptions.class", "*g*class"));
219         assertTrue(StringUtil.match("CfgOptions.class", "*C*g*class"));
220         assertTrue(StringUtil.match("CfgOptions.class", "C*g*cl*"));
221         assertTrue(StringUtil.match("CfgOptions.class", "*C*g*cl*"));
222         
223         // multiple wildcards
224
assertTrue(StringUtil.match("CfgOptions.class", "*g*c**ss"));
225         assertTrue(StringUtil.match("CfgOptions.class", "*g*c**s")); // THIS IS TRUE!!!
226
assertTrue(StringUtil.match("CfgOptions.class", "*gOpti******ons.c**ss"));
227         assertTrue(StringUtil.match("CfgOptions.class", "***gOpti*ons.c**ss"));
228         assertTrue(StringUtil.match("CfgOptions.class", "***gOptions.c**"));
229
230         // wildcard '?':
231
assertTrue(StringUtil.match("CfgOptions.class", "*gOpti*c?ass"));
232         assertFalse(StringUtil.match("CfgOptions.class", "*gOpti*c?ss"));
233
234         // wildcard on the start and end of the expression:
235
assertTrue(StringUtil.match("CfgOptions.class", "*gOpti*c?as?"));
236         assertFalse(StringUtil.match("CfgOptions.class", "*gOpti*c?a?"));
237         assertTrue(StringUtil.match("CfgOptions.class", "?fgOpti*c?ass"));
238         assertFalse(StringUtil.match("CfgOptions.class", "?gOpti*c?as?"));
239         assertTrue(StringUtil.match("CfgOptions.class", "??gOpti*c?ass"));
240         
241         // multiple wildcards
242
assertTrue(StringUtil.match("CfgOptions.class", "C????ti*c?ass"));
243         assertFalse(StringUtil.match("CfgOptions.class", "C???ti*c?ass"));
244
245         // wildcards '*' and '?' mixed together:
246
assertTrue(StringUtil.match("CfgOptions.class", "C??*ti*c?ass"));
247         assertTrue(StringUtil.match("CfgOptions.class", "C*ti*c?as?*"));
248         assertTrue(StringUtil.match("CfgOptions.class", "C*ti*c?a*?"));
249         assertTrue(StringUtil.match("CfgOptions.class", "C*ti*?a*"));
250         assertTrue(StringUtil.match("CfgOptions.class", "C*ti*c?a?*"));
251         assertFalse(StringUtil.match("CfgOptions.class", "C*ti*c?*la?*"));
252
253         // escaped wildcards:
254
assertFalse(StringUtil.match("CfgOptions.class", "*gOpti\\*c?ass"));
255         assertTrue(StringUtil.match("CfgOpti*class", "*gOpti\\*class"));
256         assertTrue(StringUtil.match("CfgOpti*class", "*gOpti\\*c?ass"));
257         assertFalse(StringUtil.match("CfgOpti*class", "*gOpti\\\\*c?ass"));
258         assertTrue(StringUtil.match("CfgOpti\\*class", "*gOpti\\\\*c?ass"));
259         assertTrue(StringUtil.match("CfgOpti?class", "*gOpti\\?c*ass"));
260         assertFalse(StringUtil.match("CfgOpti\\?class", "*gOpti\\?c*ass"));
261         assertTrue(StringUtil.match("CfgOptions.class", "CfgOpti\\ons.class"));
262         assertTrue(StringUtil.match("What's this?", "What*\\?"));
263     }
264     
265 }
266
Popular Tags