KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > Ostermiller > util > StringHelperTests


1 /*
2  * StringHelper regression test.
3  * Copyright (C) 2005 Stephen Ostermiller
4  * http://ostermiller.org/contact.pl?regarding=Java+Utilities
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * See COPYING.TXT for details.
17  */

18 package com.Ostermiller.util;
19
20 import java.util.*;
21 import java.io.*;
22
23 class StringHelperTests {
24
25     private static void equalOrDie(String JavaDoc testName, Object JavaDoc[] a, Object JavaDoc[] b) throws Exception JavaDoc {
26         if (!Arrays.equals(a,b)){
27             throw new Exception JavaDoc(testName + " failed, arrays are not equal");
28         }
29     }
30
31     private static void equalOrDie(String JavaDoc testName, String JavaDoc a, String JavaDoc b) throws Exception JavaDoc {
32         if (!a.equals(b)){
33             throw new Exception JavaDoc(testName + " failed, arrays are not equal");
34         }
35     }
36     private static void trueOrDie(String JavaDoc testName, boolean b) throws Exception JavaDoc {
37         if (!b){
38             throw new Exception JavaDoc(testName + " failed, condition not met");
39         }
40     }
41
42     public static void main(String JavaDoc[] args){
43         try {
44             equalOrDie(
45                 "Split Test 1",
46                 StringHelper.split("1-2-3", "-"),
47                 new String JavaDoc[]{"1","2","3"}
48             );
49             equalOrDie(
50                 "Split Test 2",
51                 StringHelper.split("-1--2-", "-"),
52                 new String JavaDoc[]{"","1","","2",""}
53             );
54             equalOrDie(
55                 "Split Test 3",
56                 StringHelper.split("123", ""),
57                 new String JavaDoc[]{"123"}
58             );
59             equalOrDie(
60                 "Split Test 4",
61                 StringHelper.split("1-2---3----4", "--"),
62                 new String JavaDoc[]{"1-2","-3","","4"}
63             );
64             equalOrDie(
65                 "Split Test 5",
66                 StringHelper.split("12345678", "--"),
67                 new String JavaDoc[]{"12345678"}
68             );
69             equalOrDie(
70                 "Prepad Test 1",
71                 StringHelper.prepad("a", 8),
72                 " a"
73             );
74             equalOrDie(
75                 "Prepad Test 2",
76                 StringHelper.prepad("aaaaa", 2),
77                 "aaaaa"
78             );
79             equalOrDie(
80                 "Prepad Test 3",
81                 StringHelper.prepad("a", 8, '-'),
82                 "-------a"
83             );
84             equalOrDie(
85                 "postpad Test 1",
86                 StringHelper.postpad("a", 8),
87                 "a "
88             );
89             equalOrDie(
90                 "postpad Test 2",
91                 StringHelper.postpad("aaaaa", 2),
92                 "aaaaa"
93             );
94             equalOrDie(
95                 "postpad Test 3",
96                 StringHelper.postpad("a", 8, '-'),
97                 "a-------"
98             );
99             equalOrDie(
100                 "midpad Test 1",
101                 StringHelper.midpad("a", 3),
102                 " a "
103             );
104             equalOrDie(
105                 "midpad Test 2",
106                 StringHelper.midpad("a", 4),
107                 " a "
108             );
109             equalOrDie(
110                 "midpad Test 3",
111                 StringHelper.midpad("a", 5, '-'),
112                 "--a--"
113             );
114             equalOrDie(
115                 "midpad Test 4",
116                 StringHelper.midpad("aaaaa", 2),
117                 "aaaaa"
118             );
119             equalOrDie(
120                 "replace Test 1",
121                 StringHelper.replace("1-2-3", "-", "|"),
122                 "1|2|3"
123             );
124             equalOrDie(
125                 "replace Test 2",
126                 StringHelper.replace("-1--2-", "-", "|"),
127                 "|1||2|"
128             );
129             equalOrDie(
130                 "replace Test 3",
131                 StringHelper.replace("123", "", "|"),
132                 "123"
133             );
134             equalOrDie(
135                 "replace Test 4",
136                 StringHelper.replace("1-2---3----4", "--", "|"),
137                 "1-2|-3||4"
138             );
139             equalOrDie(
140                 "replace Test 5",
141                 StringHelper.replace("1-2--3---4----", "--", "---"),
142                 "1-2---3----4------"
143             );
144             equalOrDie(
145                 "escapeHTML Test 1",
146                 StringHelper.escapeHTML("<>&\"'\0\1\2\n\r\f\thello"),
147                 "&lt;&gt;&amp;&quot;&#39;\n\r\f\thello"
148             );
149             equalOrDie(
150                 "escapeSQL Test 1",
151                 StringHelper.escapeSQL("\0\'\"\r\nhello"),
152                 "\\0\\'\\\"\r\nhello"
153             );
154             equalOrDie(
155                 "escapeJavaLiteral Test 1",
156                 StringHelper.escapeJavaLiteral("\0\'\"\r\nhello"),
157                 "\0\\'\\\"\\r\\nhello"
158             );
159             equalOrDie(
160                 "trim Test 1",
161                 StringHelper.trim("- -\r\nhello- -\r\nhello- -\r\n", "\r- \n"),
162                 "hello- -\r\nhello"
163             );
164             equalOrDie(
165                 "unescapeHTML Test 1",
166                 StringHelper.unescapeHTML("&gt;hello&euro;"),
167                 ">hello\u20AC"
168             );
169             trueOrDie(
170                 "containsAny Test 1",
171                 StringHelper.containsAny(
172                     "ontwothre",
173                     new String JavaDoc[]{
174                         "one",
175                         "two",
176                         "three"
177                     }
178                 )
179             );
180             trueOrDie(
181                 "containsAny Test 2",
182                 StringHelper.containsAny(
183                     "onetwthre",
184                     new String JavaDoc[]{
185                         "one",
186                         "two",
187                         "three"
188                     }
189                 )
190             );
191             trueOrDie(
192                 "containsAny Test 3",
193                 StringHelper.containsAny(
194                     "ontwthree",
195                     new String JavaDoc[]{
196                         "one",
197                         "two",
198                         "three"
199                     }
200                 )
201             );
202             trueOrDie(
203                 "containsAny Test 4",
204                 !StringHelper.containsAny(
205                     "ontwthre",
206                     new String JavaDoc[]{
207                         "one",
208                         "two",
209                         "three"
210                     }
211                 )
212             );
213             trueOrDie(
214                 "equalsAny Test 1",
215                 StringHelper.equalsAny(
216                     "two",
217                     new String JavaDoc[]{
218                         "one",
219                         "two",
220                         "three"
221                     }
222                 )
223             );
224             trueOrDie(
225                 "equalsAny Test 2",
226                 !StringHelper.equalsAny(
227                     "onetwothree",
228                     new String JavaDoc[]{
229                         "one",
230                         "two",
231                         "three"
232                     }
233                 )
234             );
235             trueOrDie(
236                 "startsWithAny Test 1",
237                 StringHelper.startsWithAny(
238                     "two",
239                     new String JavaDoc[]{
240                         "one",
241                         "two",
242                         "three"
243                     }
244                 )
245             );
246             trueOrDie(
247                 "startsWithAny Test 2",
248                 !StringHelper.startsWithAny(
249                     "ontwothree",
250                     new String JavaDoc[]{
251                         "one",
252                         "two",
253                         "three"
254                     }
255                 )
256             );
257             trueOrDie(
258                 "endsWithAny Test 1",
259                 StringHelper.endsWithAny(
260                     "two",
261                     new String JavaDoc[]{
262                         "one",
263                         "two",
264                         "three"
265                     }
266                 )
267             );
268             trueOrDie(
269                 "endsWithAny Test 2",
270                 !StringHelper.endsWithAny(
271                     "onetwothre",
272                     new String JavaDoc[]{
273                         "one",
274                         "two",
275                         "three"
276                     }
277                 )
278             );
279             trueOrDie(
280                 "containsAnyIgnoreCase Test 1",
281                 StringHelper.containsAnyIgnoreCase(
282                     "ontwothre",
283                     new String JavaDoc[]{
284                         "One",
285                         "Two",
286                         "Three"
287                     }
288                 )
289             );
290             trueOrDie(
291                 "containsAnyIgnoreCase Test 2",
292                 StringHelper.containsAnyIgnoreCase(
293                     "onetwthre",
294                     new String JavaDoc[]{
295                         "One",
296                         "Two",
297                         "Three"
298                     }
299                 )
300             );
301             trueOrDie(
302                 "containsAnyIgnoreCase Test 3",
303                 StringHelper.containsAnyIgnoreCase(
304                     "ontwthree",
305                     new String JavaDoc[]{
306                         "One",
307                         "Two",
308                         "Three"
309                     }
310                 )
311             );
312             trueOrDie(
313                 "containsAnyIgnoreCase Test 4",
314                 !StringHelper.containsAnyIgnoreCase(
315                     "ontwthre",
316                     new String JavaDoc[]{
317                         "One",
318                         "Two",
319                         "Three"
320                     }
321                 )
322             );
323             trueOrDie(
324                 "equalsAnyIgnoreCase Test 1",
325                 StringHelper.equalsAnyIgnoreCase(
326                     "Two",
327                     new String JavaDoc[]{
328                         "One",
329                         "Two",
330                         "Three"
331                     }
332                 )
333             );
334             trueOrDie(
335                 "equalsAnyIgnoreCase Test 2",
336                 !StringHelper.equalsAnyIgnoreCase(
337                     "onetwothree",
338                     new String JavaDoc[]{
339                         "One",
340                         "Two",
341                         "Three"
342                     }
343                 )
344             );
345             trueOrDie(
346                 "startsWithAnyIgnoreCase Test 1",
347                 StringHelper.startsWithAnyIgnoreCase(
348                     "Two",
349                     new String JavaDoc[]{
350                         "One",
351                         "Two",
352                         "Three"
353                     }
354                 )
355             );
356             trueOrDie(
357                 "startsWithAnyIgnoreCase Test 2",
358                 !StringHelper.startsWithAnyIgnoreCase(
359                     "ontwothree",
360                     new String JavaDoc[]{
361                         "One",
362                         "Two",
363                         "Three"
364                     }
365                 )
366             );
367             trueOrDie(
368                 "endsWithAnyIgnoreCase Test 1",
369                 StringHelper.endsWithAnyIgnoreCase(
370                     "Two",
371                     new String JavaDoc[]{
372                         "One",
373                         "Two",
374                         "Three"
375                     }
376                 )
377             );
378             trueOrDie(
379                 "endsWithAnyIgnoreCase Test 2",
380                 !StringHelper.endsWithAnyIgnoreCase(
381                     "onetwothre",
382                     new String JavaDoc[]{
383                         "One",
384                         "Two",
385                         "Three"
386                     }
387                 )
388             );
389             equalOrDie(
390                 "splitIncludeDelimiters Test 1",
391                 StringHelper.splitIncludeDelimiters("1-2-3", "-"),
392                 new String JavaDoc[]{"1","-","2","-","3"}
393             );
394             equalOrDie(
395                 "splitIncludeDelimiters Test 2",
396                 StringHelper.splitIncludeDelimiters("-1--2-", "-"),
397                 new String JavaDoc[]{"","-","1","-","","-","2","-",""}
398             );
399             equalOrDie(
400                 "splitIncludeDelimiters Test 3",
401                 StringHelper.splitIncludeDelimiters("123", ""),
402                 new String JavaDoc[]{"123"}
403             );
404             equalOrDie(
405                 "splitIncludeDelimiters Test 4",
406                 StringHelper.splitIncludeDelimiters("1-2--3---4----5", "--"),
407                 new String JavaDoc[]{"1-2","--","3","--","-4","--","","--","5"}
408             );
409             equalOrDie(
410                 "splitIncludeDelimiters Test 5",
411                 StringHelper.splitIncludeDelimiters("12345678", "--"),
412                 new String JavaDoc[]{"12345678"}
413             );
414             equalOrDie(
415                 "join Test 1",
416                 StringHelper.join(
417                     new String JavaDoc[]{
418                         null
419                     }
420                 ),
421                 ""
422             );
423             equalOrDie(
424                 "join Test 2",
425                 StringHelper.join(
426                     new String JavaDoc[]{
427                         ""
428                     }
429                 ),
430                 ""
431             );
432             equalOrDie(
433                 "join Test 3",
434                 StringHelper.join(
435                     new String JavaDoc[]{
436                         "",
437                         ""
438                     }
439                 ),
440                 ""
441             );
442             equalOrDie(
443                 "join Test 4",
444                 StringHelper.join(
445                     new String JavaDoc[]{
446                         "one"
447                     }
448                 ),
449                 "one"
450             );
451             equalOrDie(
452                 "join Test 5",
453                 StringHelper.join(
454                     new String JavaDoc[]{
455                         "one",
456                         "two",
457                     }
458                 ),
459                 "onetwo"
460             );
461             equalOrDie(
462                 "join Test 6",
463                 StringHelper.join(
464                     new String JavaDoc[]{
465                         "one",
466                         null,
467                         "two",
468                         "",
469                         "three"
470                     }
471                 ),
472                 "onetwothree"
473             );
474
475             equalOrDie(
476                 "join Test 7",
477                 StringHelper.join(
478                     new String JavaDoc[]{
479                         null
480                     },
481                     "|"
482                 ),
483                 ""
484             );
485             equalOrDie(
486                 "join Test 8",
487                 StringHelper.join(
488                     new String JavaDoc[]{
489                         ""
490                     },
491                     "|"
492                 ),
493                 ""
494             );
495             equalOrDie(
496                 "join Test 9",
497                 StringHelper.join(
498                     new String JavaDoc[]{
499                         "",
500                         ""
501                     },
502                     "|"
503                 ),
504                 "|"
505             );
506             equalOrDie(
507                 "join Test 10",
508                 StringHelper.join(
509                     new String JavaDoc[]{
510                         "one"
511                     },
512                     "|"
513                 ),
514                 "one"
515             );
516             equalOrDie(
517                 "join Test 11",
518                 StringHelper.join(
519                     new String JavaDoc[]{
520                         "one",
521                         "two",
522                     },
523                     "|"
524                 ),
525                 "one|two"
526             );
527             equalOrDie(
528                 "join Test 12",
529                 StringHelper.join(
530                     new String JavaDoc[]{
531                         "one",
532                         null,
533                         "two",
534                         "",
535                         "three"
536                     },
537                     "|"
538                 ),
539                 "one||two||three"
540             );
541
542         } catch (Exception JavaDoc x){
543             x.printStackTrace(System.err);
544             System.exit(1);
545         }
546         System.exit(0);
547     }
548 }
549
Popular Tags