001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.commons.lang;
020:
021: import java.lang.reflect.Modifier;
022:
023: import junit.framework.Test;
024: import junit.framework.TestCase;
025: import junit.framework.TestSuite;
026: import junit.textui.TestRunner;
027:
028: /**
029: * Unit tests {@link org.apache.commons.lang.CharSet}.
030: *
031: * @author Stephen Colebourne
032: * @author Phil Steitz
033: * @version $Id: CharSetTest.java 437554 2006-08-28 06:21:41Z bayard $
034: */
035: public class CharSetTest extends TestCase {
036:
037: public CharSetTest(String name) {
038: super (name);
039: }
040:
041: public static void main(String[] args) {
042: TestRunner.run(suite());
043: }
044:
045: public static Test suite() {
046: TestSuite suite = new TestSuite(CharSetTest.class);
047: suite.setName("CharSet Tests");
048: return suite;
049: }
050:
051: protected void setUp() throws Exception {
052: super .setUp();
053: }
054:
055: protected void tearDown() throws Exception {
056: super .tearDown();
057: }
058:
059: //-----------------------------------------------------------------------
060: public void testClass() {
061: assertEquals(true, Modifier.isPublic(CharSet.class
062: .getModifiers()));
063: assertEquals(false, Modifier.isFinal(CharSet.class
064: .getModifiers()));
065: }
066:
067: //-----------------------------------------------------------------------
068: public void testGetInstance() {
069: assertSame(CharSet.EMPTY, CharSet.getInstance(null));
070: assertSame(CharSet.EMPTY, CharSet.getInstance(""));
071: assertSame(CharSet.ASCII_ALPHA, CharSet.getInstance("a-zA-Z"));
072: assertSame(CharSet.ASCII_ALPHA, CharSet.getInstance("A-Za-z"));
073: assertSame(CharSet.ASCII_ALPHA_LOWER, CharSet
074: .getInstance("a-z"));
075: assertSame(CharSet.ASCII_ALPHA_UPPER, CharSet
076: .getInstance("A-Z"));
077: assertSame(CharSet.ASCII_NUMERIC, CharSet.getInstance("0-9"));
078: }
079:
080: //-----------------------------------------------------------------------
081: public void testConstructor_String_simple() {
082: CharSet set;
083: CharRange[] array;
084:
085: set = CharSet.getInstance((String) null);
086: array = set.getCharRanges();
087: assertEquals("[]", set.toString());
088: assertEquals(0, array.length);
089:
090: set = CharSet.getInstance("");
091: array = set.getCharRanges();
092: assertEquals("[]", set.toString());
093: assertEquals(0, array.length);
094:
095: set = CharSet.getInstance("a");
096: array = set.getCharRanges();
097: assertEquals("[a]", set.toString());
098: assertEquals(1, array.length);
099: assertEquals("a", array[0].toString());
100:
101: set = CharSet.getInstance("^a");
102: array = set.getCharRanges();
103: assertEquals("[^a]", set.toString());
104: assertEquals(1, array.length);
105: assertEquals("^a", array[0].toString());
106:
107: set = CharSet.getInstance("a-e");
108: array = set.getCharRanges();
109: assertEquals("[a-e]", set.toString());
110: assertEquals(1, array.length);
111: assertEquals("a-e", array[0].toString());
112:
113: set = CharSet.getInstance("^a-e");
114: array = set.getCharRanges();
115: assertEquals("[^a-e]", set.toString());
116: assertEquals(1, array.length);
117: assertEquals("^a-e", array[0].toString());
118: }
119:
120: public void testConstructor_String_combo() {
121: CharSet set;
122: CharRange[] array;
123:
124: set = CharSet.getInstance("abc");
125: array = set.getCharRanges();
126: assertEquals(3, array.length);
127: assertEquals(true, ArrayUtils.contains(array,
128: new CharRange('a')));
129: assertEquals(true, ArrayUtils.contains(array,
130: new CharRange('b')));
131: assertEquals(true, ArrayUtils.contains(array,
132: new CharRange('c')));
133:
134: set = CharSet.getInstance("a-ce-f");
135: array = set.getCharRanges();
136: assertEquals(2, array.length);
137: assertEquals(true, ArrayUtils.contains(array, new CharRange(
138: 'a', 'c')));
139: assertEquals(true, ArrayUtils.contains(array, new CharRange(
140: 'e', 'f')));
141:
142: set = CharSet.getInstance("ae-f");
143: array = set.getCharRanges();
144: assertEquals(2, array.length);
145: assertEquals(true, ArrayUtils.contains(array,
146: new CharRange('a')));
147: assertEquals(true, ArrayUtils.contains(array, new CharRange(
148: 'e', 'f')));
149:
150: set = CharSet.getInstance("e-fa");
151: array = set.getCharRanges();
152: assertEquals(2, array.length);
153: assertEquals(true, ArrayUtils.contains(array,
154: new CharRange('a')));
155: assertEquals(true, ArrayUtils.contains(array, new CharRange(
156: 'e', 'f')));
157:
158: set = CharSet.getInstance("ae-fm-pz");
159: array = set.getCharRanges();
160: assertEquals(4, array.length);
161: assertEquals(true, ArrayUtils.contains(array,
162: new CharRange('a')));
163: assertEquals(true, ArrayUtils.contains(array, new CharRange(
164: 'e', 'f')));
165: assertEquals(true, ArrayUtils.contains(array, new CharRange(
166: 'm', 'p')));
167: assertEquals(true, ArrayUtils.contains(array,
168: new CharRange('z')));
169: }
170:
171: public void testConstructor_String_comboNegated() {
172: CharSet set;
173: CharRange[] array;
174:
175: set = CharSet.getInstance("^abc");
176: array = set.getCharRanges();
177: assertEquals(3, array.length);
178: assertEquals(true, ArrayUtils.contains(array, new CharRange(
179: 'a', 'a', true)));
180: assertEquals(true, ArrayUtils.contains(array,
181: new CharRange('b')));
182: assertEquals(true, ArrayUtils.contains(array,
183: new CharRange('c')));
184:
185: set = CharSet.getInstance("b^ac");
186: array = set.getCharRanges();
187: assertEquals(3, array.length);
188: assertEquals(true, ArrayUtils.contains(array,
189: new CharRange('b')));
190: assertEquals(true, ArrayUtils.contains(array, new CharRange(
191: 'a', 'a', true)));
192: assertEquals(true, ArrayUtils.contains(array,
193: new CharRange('c')));
194:
195: set = CharSet.getInstance("db^ac");
196: array = set.getCharRanges();
197: assertEquals(4, array.length);
198: assertEquals(true, ArrayUtils.contains(array,
199: new CharRange('d')));
200: assertEquals(true, ArrayUtils.contains(array,
201: new CharRange('b')));
202: assertEquals(true, ArrayUtils.contains(array, new CharRange(
203: 'a', 'a', true)));
204: assertEquals(true, ArrayUtils.contains(array,
205: new CharRange('c')));
206:
207: set = CharSet.getInstance("^b^a");
208: array = set.getCharRanges();
209: assertEquals(2, array.length);
210: assertEquals(true, ArrayUtils.contains(array, new CharRange(
211: 'b', 'b', true)));
212: assertEquals(true, ArrayUtils.contains(array, new CharRange(
213: 'a', 'a', true)));
214:
215: set = CharSet.getInstance("b^a-c^z");
216: array = set.getCharRanges();
217: assertEquals(3, array.length);
218: assertEquals(true, ArrayUtils.contains(array, new CharRange(
219: 'a', 'c', true)));
220: assertEquals(true, ArrayUtils.contains(array, new CharRange(
221: 'z', 'z', true)));
222: assertEquals(true, ArrayUtils.contains(array,
223: new CharRange('b')));
224: }
225:
226: public void testConstructor_String_oddDash() {
227: CharSet set;
228: CharRange[] array;
229:
230: set = CharSet.getInstance("-");
231: array = set.getCharRanges();
232: assertEquals(1, array.length);
233: assertEquals(true, ArrayUtils.contains(array,
234: new CharRange('-')));
235:
236: set = CharSet.getInstance("--");
237: array = set.getCharRanges();
238: assertEquals(1, array.length);
239: assertEquals(true, ArrayUtils.contains(array,
240: new CharRange('-')));
241:
242: set = CharSet.getInstance("---");
243: array = set.getCharRanges();
244: assertEquals(1, array.length);
245: assertEquals(true, ArrayUtils.contains(array,
246: new CharRange('-')));
247:
248: set = CharSet.getInstance("----");
249: array = set.getCharRanges();
250: assertEquals(1, array.length);
251: assertEquals(true, ArrayUtils.contains(array,
252: new CharRange('-')));
253:
254: set = CharSet.getInstance("-a");
255: array = set.getCharRanges();
256: assertEquals(2, array.length);
257: assertEquals(true, ArrayUtils.contains(array,
258: new CharRange('-')));
259: assertEquals(true, ArrayUtils.contains(array,
260: new CharRange('a')));
261:
262: set = CharSet.getInstance("a-");
263: array = set.getCharRanges();
264: assertEquals(2, array.length);
265: assertEquals(true, ArrayUtils.contains(array,
266: new CharRange('a')));
267: assertEquals(true, ArrayUtils.contains(array,
268: new CharRange('-')));
269:
270: set = CharSet.getInstance("a--");
271: array = set.getCharRanges();
272: assertEquals(1, array.length);
273: assertEquals(true, ArrayUtils.contains(array, new CharRange(
274: 'a', '-')));
275:
276: set = CharSet.getInstance("--a");
277: array = set.getCharRanges();
278: assertEquals(1, array.length);
279: assertEquals(true, ArrayUtils.contains(array, new CharRange(
280: '-', 'a')));
281: }
282:
283: public void testConstructor_String_oddNegate() {
284: CharSet set;
285: CharRange[] array;
286: set = CharSet.getInstance("^");
287: array = set.getCharRanges();
288: assertEquals(1, array.length);
289: assertEquals(true, ArrayUtils.contains(array,
290: new CharRange('^'))); // "^"
291:
292: set = CharSet.getInstance("^^");
293: array = set.getCharRanges();
294: assertEquals(1, array.length);
295: assertEquals(true, ArrayUtils.contains(array, new CharRange(
296: '^', '^', true))); // "^^"
297:
298: set = CharSet.getInstance("^^^");
299: array = set.getCharRanges();
300: assertEquals(2, array.length);
301: assertEquals(true, ArrayUtils.contains(array, new CharRange(
302: '^', '^', true))); // "^^"
303: assertEquals(true, ArrayUtils.contains(array, new CharRange(
304: '^', '^'))); // "^"
305:
306: set = CharSet.getInstance("^^^^");
307: array = set.getCharRanges();
308: assertEquals(1, array.length);
309: assertEquals(true, ArrayUtils.contains(array, new CharRange(
310: '^', '^', true))); // "^^" x2
311:
312: set = CharSet.getInstance("a^");
313: array = set.getCharRanges();
314: assertEquals(2, array.length);
315: assertEquals(true, ArrayUtils.contains(array,
316: new CharRange('a'))); // "a"
317: assertEquals(true, ArrayUtils.contains(array,
318: new CharRange('^'))); // "^"
319:
320: set = CharSet.getInstance("^a-");
321: array = set.getCharRanges();
322: assertEquals(2, array.length);
323: assertEquals(true, ArrayUtils.contains(array, new CharRange(
324: 'a', 'a', true))); // "^a"
325: assertEquals(true, ArrayUtils.contains(array,
326: new CharRange('-'))); // "-"
327:
328: set = CharSet.getInstance("^^-c");
329: array = set.getCharRanges();
330: assertEquals(1, array.length);
331: assertEquals(true, ArrayUtils.contains(array, new CharRange(
332: '^', 'c', true))); // "^^-c"
333:
334: set = CharSet.getInstance("^c-^");
335: array = set.getCharRanges();
336: assertEquals(1, array.length);
337: assertEquals(true, ArrayUtils.contains(array, new CharRange(
338: 'c', '^', true))); // "^c-^"
339:
340: set = CharSet.getInstance("^c-^d");
341: array = set.getCharRanges();
342: assertEquals(2, array.length);
343: assertEquals(true, ArrayUtils.contains(array, new CharRange(
344: 'c', '^', true))); // "^c-^"
345: assertEquals(true, ArrayUtils.contains(array,
346: new CharRange('d'))); // "d"
347:
348: set = CharSet.getInstance("^^-");
349: array = set.getCharRanges();
350: assertEquals(2, array.length);
351: assertEquals(true, ArrayUtils.contains(array, new CharRange(
352: '^', '^', true))); // "^^"
353: assertEquals(true, ArrayUtils.contains(array,
354: new CharRange('-'))); // "-"
355: }
356:
357: public void testConstructor_String_oddCombinations() {
358: CharSet set;
359: CharRange[] array = null;
360:
361: set = CharSet.getInstance("a-^c");
362: array = set.getCharRanges();
363: assertEquals(true, ArrayUtils.contains(array, new CharRange(
364: 'a', '^'))); // "a-^"
365: assertEquals(true, ArrayUtils.contains(array,
366: new CharRange('c'))); // "c"
367: assertEquals(false, set.contains('b'));
368: assertEquals(true, set.contains('^'));
369: assertEquals(true, set.contains('_')); // between ^ and a
370: assertEquals(true, set.contains('c'));
371:
372: set = CharSet.getInstance("^a-^c");
373: array = set.getCharRanges();
374: assertEquals(true, ArrayUtils.contains(array, new CharRange(
375: 'a', '^', true))); // "^a-^"
376: assertEquals(true, ArrayUtils.contains(array,
377: new CharRange('c'))); // "c"
378: assertEquals(true, set.contains('b'));
379: assertEquals(false, set.contains('^'));
380: assertEquals(false, set.contains('_')); // between ^ and a
381:
382: set = CharSet.getInstance("a- ^-- "); //contains everything
383: array = set.getCharRanges();
384: assertEquals(true, ArrayUtils.contains(array, new CharRange(
385: 'a', ' '))); // "a- "
386: assertEquals(true, ArrayUtils.contains(array, new CharRange(
387: '-', ' ', true))); // "^-- "
388: assertEquals(true, set.contains('#'));
389: assertEquals(true, set.contains('^'));
390: assertEquals(true, set.contains('a'));
391: assertEquals(true, set.contains('*'));
392: assertEquals(true, set.contains('A'));
393:
394: set = CharSet.getInstance("^-b");
395: array = set.getCharRanges();
396: assertEquals(true, ArrayUtils.contains(array, new CharRange(
397: '^', 'b'))); // "^-b"
398: assertEquals(true, set.contains('b'));
399: assertEquals(true, set.contains('_')); // between ^ and a
400: assertEquals(false, set.contains('A'));
401: assertEquals(true, set.contains('^'));
402:
403: set = CharSet.getInstance("b-^");
404: array = set.getCharRanges();
405: assertEquals(true, ArrayUtils.contains(array, new CharRange(
406: '^', 'b'))); // "b-^"
407: assertEquals(true, set.contains('b'));
408: assertEquals(true, set.contains('^'));
409: assertEquals(true, set.contains('a')); // between ^ and b
410: assertEquals(false, set.contains('c'));
411: }
412:
413: //-----------------------------------------------------------------------
414: public void testEquals_Object() {
415: CharSet abc = CharSet.getInstance("abc");
416: CharSet abc2 = CharSet.getInstance("abc");
417: CharSet atoc = CharSet.getInstance("a-c");
418: CharSet atoc2 = CharSet.getInstance("a-c");
419: CharSet notatoc = CharSet.getInstance("^a-c");
420: CharSet notatoc2 = CharSet.getInstance("^a-c");
421:
422: assertEquals(false, abc.equals(null));
423:
424: assertEquals(true, abc.equals(abc));
425: assertEquals(true, abc.equals(abc2));
426: assertEquals(false, abc.equals(atoc));
427: assertEquals(false, abc.equals(notatoc));
428:
429: assertEquals(false, atoc.equals(abc));
430: assertEquals(true, atoc.equals(atoc));
431: assertEquals(true, atoc.equals(atoc2));
432: assertEquals(false, atoc.equals(notatoc));
433:
434: assertEquals(false, notatoc.equals(abc));
435: assertEquals(false, notatoc.equals(atoc));
436: assertEquals(true, notatoc.equals(notatoc));
437: assertEquals(true, notatoc.equals(notatoc2));
438: }
439:
440: public void testHashCode() {
441: CharSet abc = CharSet.getInstance("abc");
442: CharSet abc2 = CharSet.getInstance("abc");
443: CharSet atoc = CharSet.getInstance("a-c");
444: CharSet atoc2 = CharSet.getInstance("a-c");
445: CharSet notatoc = CharSet.getInstance("^a-c");
446: CharSet notatoc2 = CharSet.getInstance("^a-c");
447:
448: assertEquals(abc.hashCode(), abc.hashCode());
449: assertEquals(abc.hashCode(), abc2.hashCode());
450: assertEquals(atoc.hashCode(), atoc.hashCode());
451: assertEquals(atoc.hashCode(), atoc2.hashCode());
452: assertEquals(notatoc.hashCode(), notatoc.hashCode());
453: assertEquals(notatoc.hashCode(), notatoc2.hashCode());
454: }
455:
456: //-----------------------------------------------------------------------
457: public void testContains_Char() {
458: CharSet btod = CharSet.getInstance("b-d");
459: CharSet dtob = CharSet.getInstance("d-b");
460: CharSet bcd = CharSet.getInstance("bcd");
461: CharSet bd = CharSet.getInstance("bd");
462: CharSet notbtod = CharSet.getInstance("^b-d");
463:
464: assertEquals(false, btod.contains('a'));
465: assertEquals(true, btod.contains('b'));
466: assertEquals(true, btod.contains('c'));
467: assertEquals(true, btod.contains('d'));
468: assertEquals(false, btod.contains('e'));
469:
470: assertEquals(false, bcd.contains('a'));
471: assertEquals(true, bcd.contains('b'));
472: assertEquals(true, bcd.contains('c'));
473: assertEquals(true, bcd.contains('d'));
474: assertEquals(false, bcd.contains('e'));
475:
476: assertEquals(false, bd.contains('a'));
477: assertEquals(true, bd.contains('b'));
478: assertEquals(false, bd.contains('c'));
479: assertEquals(true, bd.contains('d'));
480: assertEquals(false, bd.contains('e'));
481:
482: assertEquals(true, notbtod.contains('a'));
483: assertEquals(false, notbtod.contains('b'));
484: assertEquals(false, notbtod.contains('c'));
485: assertEquals(false, notbtod.contains('d'));
486: assertEquals(true, notbtod.contains('e'));
487:
488: assertEquals(false, dtob.contains('a'));
489: assertEquals(true, dtob.contains('b'));
490: assertEquals(true, dtob.contains('c'));
491: assertEquals(true, dtob.contains('d'));
492: assertEquals(false, dtob.contains('e'));
493:
494: CharRange[] array = dtob.getCharRanges();
495: assertEquals("[b-d]", dtob.toString());
496: assertEquals(1, array.length);
497: }
498:
499: //-----------------------------------------------------------------------
500: public void testSerialization() {
501: CharSet set = CharSet.getInstance("a");
502: assertEquals(set, SerializationUtils.clone(set));
503: set = CharSet.getInstance("a-e");
504: assertEquals(set, SerializationUtils.clone(set));
505: set = CharSet.getInstance("be-f^a-z");
506: assertEquals(set, SerializationUtils.clone(set));
507: }
508:
509: //-----------------------------------------------------------------------
510: public void testStatics() {
511: CharRange[] array;
512:
513: array = CharSet.EMPTY.getCharRanges();
514: assertEquals(0, array.length);
515:
516: array = CharSet.ASCII_ALPHA.getCharRanges();
517: assertEquals(2, array.length);
518: assertEquals(true, ArrayUtils.contains(array, new CharRange(
519: 'a', 'z')));
520: assertEquals(true, ArrayUtils.contains(array, new CharRange(
521: 'A', 'Z')));
522:
523: array = CharSet.ASCII_ALPHA_LOWER.getCharRanges();
524: assertEquals(1, array.length);
525: assertEquals(true, ArrayUtils.contains(array, new CharRange(
526: 'a', 'z')));
527:
528: array = CharSet.ASCII_ALPHA_UPPER.getCharRanges();
529: assertEquals(1, array.length);
530: assertEquals(true, ArrayUtils.contains(array, new CharRange(
531: 'A', 'Z')));
532:
533: array = CharSet.ASCII_NUMERIC.getCharRanges();
534: assertEquals(1, array.length);
535: assertEquals(true, ArrayUtils.contains(array, new CharRange(
536: '0', '9')));
537: }
538:
539: }
|