KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > CharSetTest


1 /*
2  * Copyright 2003,2004 The Apache Software Foundation.
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 package org.apache.commons.lang;
17
18 import java.lang.reflect.Modifier JavaDoc;
19
20 import junit.framework.Test;
21 import junit.framework.TestCase;
22 import junit.framework.TestSuite;
23 import junit.textui.TestRunner;
24
25 /**
26  * Unit tests {@link org.apache.commons.lang.CharSet}.
27  *
28  * @author Stephen Colebourne
29  * @author Phil Steitz
30  * @version $Id: CharSetTest.java 155423 2005-02-26 13:08:30Z dirkv $
31  */

32 public class CharSetTest extends TestCase {
33     
34     public CharSetTest(String JavaDoc name) {
35         super(name);
36     }
37
38     public static void main(String JavaDoc[] args) {
39         TestRunner.run(suite());
40     }
41
42     public static Test suite() {
43         TestSuite suite = new TestSuite(CharSetTest.class);
44         suite.setName("CharSet Tests");
45         return suite;
46     }
47
48     protected void setUp() throws Exception JavaDoc {
49         super.setUp();
50     }
51
52     protected void tearDown() throws Exception JavaDoc {
53         super.tearDown();
54     }
55
56     //-----------------------------------------------------------------------
57
public void testClass() {
58         assertEquals(true, Modifier.isPublic(CharSet.class.getModifiers()));
59         assertEquals(false, Modifier.isFinal(CharSet.class.getModifiers()));
60     }
61     
62     //-----------------------------------------------------------------------
63
public void testGetInstance() {
64         assertSame(CharSet.EMPTY, CharSet.getInstance(null));
65         assertSame(CharSet.EMPTY, CharSet.getInstance(""));
66         assertSame(CharSet.ASCII_ALPHA, CharSet.getInstance("a-zA-Z"));
67         assertSame(CharSet.ASCII_ALPHA, CharSet.getInstance("A-Za-z"));
68         assertSame(CharSet.ASCII_ALPHA_LOWER, CharSet.getInstance("a-z"));
69         assertSame(CharSet.ASCII_ALPHA_UPPER, CharSet.getInstance("A-Z"));
70         assertSame(CharSet.ASCII_NUMERIC, CharSet.getInstance("0-9"));
71     }
72             
73     //-----------------------------------------------------------------------
74
public void testConstructor_String_simple() {
75         CharSet set;
76         CharRange[] array;
77         
78         set = CharSet.getInstance((String JavaDoc) null);
79         array = set.getCharRanges();
80         assertEquals("[]", set.toString());
81         assertEquals(0, array.length);
82         
83         set = CharSet.getInstance("");
84         array = set.getCharRanges();
85         assertEquals("[]", set.toString());
86         assertEquals(0, array.length);
87         
88         set = CharSet.getInstance("a");
89         array = set.getCharRanges();
90         assertEquals("[a]", set.toString());
91         assertEquals(1, array.length);
92         assertEquals("a", array[0].toString());
93         
94         set = CharSet.getInstance("^a");
95         array = set.getCharRanges();
96         assertEquals("[^a]", set.toString());
97         assertEquals(1, array.length);
98         assertEquals("^a", array[0].toString());
99         
100         set = CharSet.getInstance("a-e");
101         array = set.getCharRanges();
102         assertEquals("[a-e]", set.toString());
103         assertEquals(1, array.length);
104         assertEquals("a-e", array[0].toString());
105         
106         set = CharSet.getInstance("^a-e");
107         array = set.getCharRanges();
108         assertEquals("[^a-e]", set.toString());
109         assertEquals(1, array.length);
110         assertEquals("^a-e", array[0].toString());
111     }
112     
113     public void testConstructor_String_combo() {
114         CharSet set;
115         CharRange[] array;
116         
117         set = CharSet.getInstance("abc");
118         array = set.getCharRanges();
119         assertEquals(3, array.length);
120         assertEquals(true, ArrayUtils.contains(array, new CharRange('a')));
121         assertEquals(true, ArrayUtils.contains(array, new CharRange('b')));
122         assertEquals(true, ArrayUtils.contains(array, new CharRange('c')));
123         
124         set = CharSet.getInstance("a-ce-f");
125         array = set.getCharRanges();
126         assertEquals(2, array.length);
127         assertEquals(true, ArrayUtils.contains(array, new CharRange('a', 'c')));
128         assertEquals(true, ArrayUtils.contains(array, new CharRange('e', 'f')));
129         
130         set = CharSet.getInstance("ae-f");
131         array = set.getCharRanges();
132         assertEquals(2, array.length);
133         assertEquals(true, ArrayUtils.contains(array, new CharRange('a')));
134         assertEquals(true, ArrayUtils.contains(array, new CharRange('e', 'f')));
135         
136         set = CharSet.getInstance("e-fa");
137         array = set.getCharRanges();
138         assertEquals(2, array.length);
139         assertEquals(true, ArrayUtils.contains(array, new CharRange('a')));
140         assertEquals(true, ArrayUtils.contains(array, new CharRange('e', 'f')));
141         
142         set = CharSet.getInstance("ae-fm-pz");
143         array = set.getCharRanges();
144         assertEquals(4, array.length);
145         assertEquals(true, ArrayUtils.contains(array, new CharRange('a')));
146         assertEquals(true, ArrayUtils.contains(array, new CharRange('e', 'f')));
147         assertEquals(true, ArrayUtils.contains(array, new CharRange('m', 'p')));
148         assertEquals(true, ArrayUtils.contains(array, new CharRange('z')));
149     }
150     
151     public void testConstructor_String_comboNegated() {
152         CharSet set;
153         CharRange[] array;
154         
155         set = CharSet.getInstance("^abc");
156         array = set.getCharRanges();
157         assertEquals(3, array.length);
158         assertEquals(true, ArrayUtils.contains(array, new CharRange('a', 'a', true)));
159         assertEquals(true, ArrayUtils.contains(array, new CharRange('b')));
160         assertEquals(true, ArrayUtils.contains(array, new CharRange('c')));
161         
162         set = CharSet.getInstance("b^ac");
163         array = set.getCharRanges();
164         assertEquals(3, array.length);
165         assertEquals(true, ArrayUtils.contains(array, new CharRange('b')));
166         assertEquals(true, ArrayUtils.contains(array, new CharRange('a', 'a', true)));
167         assertEquals(true, ArrayUtils.contains(array, new CharRange('c')));
168         
169         set = CharSet.getInstance("db^ac");
170         array = set.getCharRanges();
171         assertEquals(4, array.length);
172         assertEquals(true, ArrayUtils.contains(array, new CharRange('d')));
173         assertEquals(true, ArrayUtils.contains(array, new CharRange('b')));
174         assertEquals(true, ArrayUtils.contains(array, new CharRange('a', 'a', true)));
175         assertEquals(true, ArrayUtils.contains(array, new CharRange('c')));
176         
177         set = CharSet.getInstance("^b^a");
178         array = set.getCharRanges();
179         assertEquals(2, array.length);
180         assertEquals(true, ArrayUtils.contains(array, new CharRange('b', 'b', true)));
181         assertEquals(true, ArrayUtils.contains(array, new CharRange('a', 'a', true)));
182         
183         set = CharSet.getInstance("b^a-c^z");
184         array = set.getCharRanges();
185         assertEquals(3, array.length);
186         assertEquals(true, ArrayUtils.contains(array, new CharRange('a', 'c', true)));
187         assertEquals(true, ArrayUtils.contains(array, new CharRange('z', 'z', true)));
188         assertEquals(true, ArrayUtils.contains(array, new CharRange('b')));
189     }
190         
191     public void testConstructor_String_oddDash() {
192         CharSet set;
193         CharRange[] array;
194         
195         set = CharSet.getInstance("-");
196         array = set.getCharRanges();
197         assertEquals(1, array.length);
198         assertEquals(true, ArrayUtils.contains(array, new CharRange('-')));
199         
200         set = CharSet.getInstance("--");
201         array = set.getCharRanges();
202         assertEquals(1, array.length);
203         assertEquals(true, ArrayUtils.contains(array, new CharRange('-')));
204         
205         set = CharSet.getInstance("---");
206         array = set.getCharRanges();
207         assertEquals(1, array.length);
208         assertEquals(true, ArrayUtils.contains(array, new CharRange('-')));
209         
210         set = CharSet.getInstance("----");
211         array = set.getCharRanges();
212         assertEquals(1, array.length);
213         assertEquals(true, ArrayUtils.contains(array, new CharRange('-')));
214         
215         set = CharSet.getInstance("-a");
216         array = set.getCharRanges();
217         assertEquals(2, array.length);
218         assertEquals(true, ArrayUtils.contains(array, new CharRange('-')));
219         assertEquals(true, ArrayUtils.contains(array, new CharRange('a')));
220         
221         set = CharSet.getInstance("a-");
222         array = set.getCharRanges();
223         assertEquals(2, array.length);
224         assertEquals(true, ArrayUtils.contains(array, new CharRange('a')));
225         assertEquals(true, ArrayUtils.contains(array, new CharRange('-')));
226         
227         set = CharSet.getInstance("a--");
228         array = set.getCharRanges();
229         assertEquals(1, array.length);
230         assertEquals(true, ArrayUtils.contains(array, new CharRange('a', '-')));
231         
232         set = CharSet.getInstance("--a");
233         array = set.getCharRanges();
234         assertEquals(1, array.length);
235         assertEquals(true, ArrayUtils.contains(array, new CharRange('-', 'a')));
236     }
237     
238     public void testConstructor_String_oddNegate() {
239         CharSet set;
240         CharRange[] array;
241         set = CharSet.getInstance("^");
242         array = set.getCharRanges();
243         assertEquals(1, array.length);
244         assertEquals(true, ArrayUtils.contains(array, new CharRange('^'))); // "^"
245

246         set = CharSet.getInstance("^^");
247         array = set.getCharRanges();
248         assertEquals(1, array.length);
249         assertEquals(true, ArrayUtils.contains(array, new CharRange('^', '^', true))); // "^^"
250

251         set = CharSet.getInstance("^^^");
252         array = set.getCharRanges();
253         assertEquals(2, array.length);
254         assertEquals(true, ArrayUtils.contains(array, new CharRange('^', '^', true))); // "^^"
255
assertEquals(true, ArrayUtils.contains(array, new CharRange('^', '^'))); // "^"
256

257         set = CharSet.getInstance("^^^^");
258         array = set.getCharRanges();
259         assertEquals(1, array.length);
260         assertEquals(true, ArrayUtils.contains(array, new CharRange('^', '^', true))); // "^^" x2
261

262         set = CharSet.getInstance("a^");
263         array = set.getCharRanges();
264         assertEquals(2, array.length);
265         assertEquals(true, ArrayUtils.contains(array, new CharRange('a'))); // "a"
266
assertEquals(true, ArrayUtils.contains(array, new CharRange('^'))); // "^"
267

268         set = CharSet.getInstance("^a-");
269         array = set.getCharRanges();
270         assertEquals(2, array.length);
271         assertEquals(true, ArrayUtils.contains(array, new CharRange('a', 'a', true))); // "^a"
272
assertEquals(true, ArrayUtils.contains(array, new CharRange('-'))); // "-"
273

274         set = CharSet.getInstance("^^-c");
275         array = set.getCharRanges();
276         assertEquals(1, array.length);
277         assertEquals(true, ArrayUtils.contains(array, new CharRange('^', 'c', true))); // "^^-c"
278

279         set = CharSet.getInstance("^c-^");
280         array = set.getCharRanges();
281         assertEquals(1, array.length);
282         assertEquals(true, ArrayUtils.contains(array, new CharRange('c', '^', true))); // "^c-^"
283

284         set = CharSet.getInstance("^c-^d");
285         array = set.getCharRanges();
286         assertEquals(2, array.length);
287         assertEquals(true, ArrayUtils.contains(array, new CharRange('c', '^', true))); // "^c-^"
288
assertEquals(true, ArrayUtils.contains(array, new CharRange('d'))); // "d"
289

290         set = CharSet.getInstance("^^-");
291         array = set.getCharRanges();
292         assertEquals(2, array.length);
293         assertEquals(true, ArrayUtils.contains(array, new CharRange('^', '^', true))); // "^^"
294
assertEquals(true, ArrayUtils.contains(array, new CharRange('-'))); // "-"
295
}
296     
297     public void testConstructor_String_oddCombinations() {
298         CharSet set;
299         CharRange[] array = null;
300         
301         set = CharSet.getInstance("a-^c");
302         array = set.getCharRanges();
303         assertEquals(true, ArrayUtils.contains(array, new CharRange('a', '^'))); // "a-^"
304
assertEquals(true, ArrayUtils.contains(array, new CharRange('c'))); // "c"
305
assertEquals(false, set.contains('b'));
306         assertEquals(true, set.contains('^'));
307         assertEquals(true, set.contains('_')); // between ^ and a
308
assertEquals(true, set.contains('c'));
309         
310         set = CharSet.getInstance("^a-^c");
311         array = set.getCharRanges();
312         assertEquals(true, ArrayUtils.contains(array, new CharRange('a', '^', true))); // "^a-^"
313
assertEquals(true, ArrayUtils.contains(array, new CharRange('c'))); // "c"
314
assertEquals(true, set.contains('b'));
315         assertEquals(false, set.contains('^'));
316         assertEquals(false, set.contains('_')); // between ^ and a
317

318         set = CharSet.getInstance("a- ^-- "); //contains everything
319
array = set.getCharRanges();
320         assertEquals(true, ArrayUtils.contains(array, new CharRange('a', ' '))); // "a- "
321
assertEquals(true, ArrayUtils.contains(array, new CharRange('-', ' ', true))); // "^-- "
322
assertEquals(true, set.contains('#'));
323         assertEquals(true, set.contains('^'));
324         assertEquals(true, set.contains('a'));
325         assertEquals(true, set.contains('*'));
326         assertEquals(true, set.contains('A'));
327         
328         set = CharSet.getInstance("^-b");
329         array = set.getCharRanges();
330         assertEquals(true, ArrayUtils.contains(array, new CharRange('^','b'))); // "^-b"
331
assertEquals(true, set.contains('b'));
332         assertEquals(true, set.contains('_')); // between ^ and a
333
assertEquals(false, set.contains('A'));
334         assertEquals(true, set.contains('^'));
335         
336         set = CharSet.getInstance("b-^");
337         array = set.getCharRanges();
338         assertEquals(true, ArrayUtils.contains(array, new CharRange('^','b'))); // "b-^"
339
assertEquals(true, set.contains('b'));
340         assertEquals(true, set.contains('^'));
341         assertEquals(true, set.contains('a')); // between ^ and b
342
assertEquals(false, set.contains('c'));
343     }
344         
345     //-----------------------------------------------------------------------
346
public void testEquals_Object() {
347         CharSet abc = CharSet.getInstance("abc");
348         CharSet abc2 = CharSet.getInstance("abc");
349         CharSet atoc = CharSet.getInstance("a-c");
350         CharSet atoc2 = CharSet.getInstance("a-c");
351         CharSet notatoc = CharSet.getInstance("^a-c");
352         CharSet notatoc2 = CharSet.getInstance("^a-c");
353         
354         assertEquals(false, abc.equals(null));
355         
356         assertEquals(true, abc.equals(abc));
357         assertEquals(true, abc.equals(abc2));
358         assertEquals(false, abc.equals(atoc));
359         assertEquals(false, abc.equals(notatoc));
360         
361         assertEquals(false, atoc.equals(abc));
362         assertEquals(true, atoc.equals(atoc));
363         assertEquals(true, atoc.equals(atoc2));
364         assertEquals(false, atoc.equals(notatoc));
365         
366         assertEquals(false, notatoc.equals(abc));
367         assertEquals(false, notatoc.equals(atoc));
368         assertEquals(true, notatoc.equals(notatoc));
369         assertEquals(true, notatoc.equals(notatoc2));
370     }
371             
372     public void testHashCode() {
373         CharSet abc = CharSet.getInstance("abc");
374         CharSet abc2 = CharSet.getInstance("abc");
375         CharSet atoc = CharSet.getInstance("a-c");
376         CharSet atoc2 = CharSet.getInstance("a-c");
377         CharSet notatoc = CharSet.getInstance("^a-c");
378         CharSet notatoc2 = CharSet.getInstance("^a-c");
379         
380         assertEquals(abc.hashCode(), abc.hashCode());
381         assertEquals(abc.hashCode(), abc2.hashCode());
382         assertEquals(atoc.hashCode(), atoc.hashCode());
383         assertEquals(atoc.hashCode(), atoc2.hashCode());
384         assertEquals(notatoc.hashCode(), notatoc.hashCode());
385         assertEquals(notatoc.hashCode(), notatoc2.hashCode());
386     }
387     
388     //-----------------------------------------------------------------------
389
public void testContains_Char() {
390         CharSet btod = CharSet.getInstance("b-d");
391         CharSet dtob = CharSet.getInstance("d-b");
392         CharSet bcd = CharSet.getInstance("bcd");
393         CharSet bd = CharSet.getInstance("bd");
394         CharSet notbtod = CharSet.getInstance("^b-d");
395         
396         assertEquals(false, btod.contains('a'));
397         assertEquals(true, btod.contains('b'));
398         assertEquals(true, btod.contains('c'));
399         assertEquals(true, btod.contains('d'));
400         assertEquals(false, btod.contains('e'));
401         
402         assertEquals(false, bcd.contains('a'));
403         assertEquals(true, bcd.contains('b'));
404         assertEquals(true, bcd.contains('c'));
405         assertEquals(true, bcd.contains('d'));
406         assertEquals(false, bcd.contains('e'));
407         
408         assertEquals(false, bd.contains('a'));
409         assertEquals(true, bd.contains('b'));
410         assertEquals(false, bd.contains('c'));
411         assertEquals(true, bd.contains('d'));
412         assertEquals(false, bd.contains('e'));
413         
414         assertEquals(true, notbtod.contains('a'));
415         assertEquals(false, notbtod.contains('b'));
416         assertEquals(false, notbtod.contains('c'));
417         assertEquals(false, notbtod.contains('d'));
418         assertEquals(true, notbtod.contains('e'));
419         
420         assertEquals(false, dtob.contains('a'));
421         assertEquals(true, dtob.contains('b'));
422         assertEquals(true, dtob.contains('c'));
423         assertEquals(true, dtob.contains('d'));
424         assertEquals(false, dtob.contains('e'));
425       
426         CharRange[] array = dtob.getCharRanges();
427         assertEquals("[b-d]", dtob.toString());
428         assertEquals(1, array.length);
429     }
430     
431     //-----------------------------------------------------------------------
432
public void testSerialization() {
433         CharSet set = CharSet.getInstance("a");
434         assertEquals(set, SerializationUtils.clone(set));
435         set = CharSet.getInstance("a-e");
436         assertEquals(set, SerializationUtils.clone(set));
437         set = CharSet.getInstance("be-f^a-z");
438         assertEquals(set, SerializationUtils.clone(set));
439     }
440     
441     //-----------------------------------------------------------------------
442
public void testStatics() {
443         CharRange[] array;
444         
445         array = CharSet.EMPTY.getCharRanges();
446         assertEquals(0, array.length);
447         
448         array = CharSet.ASCII_ALPHA.getCharRanges();
449         assertEquals(2, array.length);
450         assertEquals(true, ArrayUtils.contains(array, new CharRange('a', 'z')));
451         assertEquals(true, ArrayUtils.contains(array, new CharRange('A', 'Z')));
452         
453         array = CharSet.ASCII_ALPHA_LOWER.getCharRanges();
454         assertEquals(1, array.length);
455         assertEquals(true, ArrayUtils.contains(array, new CharRange('a', 'z')));
456         
457         array = CharSet.ASCII_ALPHA_UPPER.getCharRanges();
458         assertEquals(1, array.length);
459         assertEquals(true, ArrayUtils.contains(array, new CharRange('A', 'Z')));
460         
461         array = CharSet.ASCII_NUMERIC.getCharRanges();
462         assertEquals(1, array.length);
463         assertEquals(true, ArrayUtils.contains(array, new CharRange('0', '9')));
464     }
465     
466 }
467
Popular Tags