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.CharRange}.
030: *
031: * @author Stephen Colebourne
032: * @version $Id: CharRangeTest.java 437554 2006-08-28 06:21:41Z bayard $
033: */
034: public class CharRangeTest extends TestCase {
035:
036: public CharRangeTest(String name) {
037: super (name);
038: }
039:
040: public static void main(String[] args) {
041: TestRunner.run(suite());
042: }
043:
044: public static Test suite() {
045: TestSuite suite = new TestSuite(CharRangeTest.class);
046: suite.setName("CharRange Tests");
047: return suite;
048: }
049:
050: protected void setUp() throws Exception {
051: super .setUp();
052: }
053:
054: protected void tearDown() throws Exception {
055: super .tearDown();
056: }
057:
058: //-----------------------------------------------------------------------
059: public void testClass() {
060: assertEquals(true, Modifier.isPublic(CharRange.class
061: .getModifiers()));
062: assertEquals(true, Modifier.isFinal(CharRange.class
063: .getModifiers()));
064: }
065:
066: //-----------------------------------------------------------------------
067: public void testConstructorAccessors_Char() {
068: CharRange rangea = new CharRange('a');
069: assertEquals('a', rangea.getStart());
070: assertEquals('a', rangea.getEnd());
071: assertEquals(false, rangea.isNegated());
072: assertEquals("a", rangea.toString());
073: }
074:
075: public void testConstructorAccessors_CharBoolean_Normal() {
076: CharRange rangea = new CharRange('a');
077: assertEquals('a', rangea.getStart());
078: assertEquals('a', rangea.getEnd());
079: assertEquals(false, rangea.isNegated());
080: assertEquals("a", rangea.toString());
081: }
082:
083: public void testConstructorAccessors_CharBoolean_Negated() {
084: CharRange rangea = new CharRange('a', true);
085: assertEquals('a', rangea.getStart());
086: assertEquals('a', rangea.getEnd());
087: assertEquals(true, rangea.isNegated());
088: assertEquals("^a", rangea.toString());
089: }
090:
091: public void testConstructorAccessors_CharChar_Same() {
092: CharRange rangea = new CharRange('a', 'a');
093: assertEquals('a', rangea.getStart());
094: assertEquals('a', rangea.getEnd());
095: assertEquals(false, rangea.isNegated());
096: assertEquals("a", rangea.toString());
097: }
098:
099: public void testConstructorAccessors_CharChar_Normal() {
100: CharRange rangea = new CharRange('a', 'e');
101: assertEquals('a', rangea.getStart());
102: assertEquals('e', rangea.getEnd());
103: assertEquals(false, rangea.isNegated());
104: assertEquals("a-e", rangea.toString());
105: }
106:
107: public void testConstructorAccessors_CharChar_Reversed() {
108: CharRange rangea = new CharRange('e', 'a');
109: assertEquals('a', rangea.getStart());
110: assertEquals('e', rangea.getEnd());
111: assertEquals(false, rangea.isNegated());
112: assertEquals("a-e", rangea.toString());
113: }
114:
115: public void testConstructorAccessors_CharCharBoolean_Same() {
116: CharRange rangea = new CharRange('a', 'a', false);
117: assertEquals('a', rangea.getStart());
118: assertEquals('a', rangea.getEnd());
119: assertEquals(false, rangea.isNegated());
120: assertEquals("a", rangea.toString());
121: }
122:
123: public void testConstructorAccessors_CharCharBoolean_Normal() {
124: CharRange rangea = new CharRange('a', 'e', false);
125: assertEquals('a', rangea.getStart());
126: assertEquals('e', rangea.getEnd());
127: assertEquals(false, rangea.isNegated());
128: assertEquals("a-e", rangea.toString());
129: }
130:
131: public void testConstructorAccessors_CharCharBoolean_Reversed() {
132: CharRange rangea = new CharRange('e', 'a', false);
133: assertEquals('a', rangea.getStart());
134: assertEquals('e', rangea.getEnd());
135: assertEquals(false, rangea.isNegated());
136: assertEquals("a-e", rangea.toString());
137: }
138:
139: public void testConstructorAccessors_CharCharBoolean_SameNegated() {
140: CharRange rangea = new CharRange('a', 'a', true);
141: assertEquals('a', rangea.getStart());
142: assertEquals('a', rangea.getEnd());
143: assertEquals(true, rangea.isNegated());
144: assertEquals("^a", rangea.toString());
145: }
146:
147: public void testConstructorAccessors_CharCharBoolean_NormalNegated() {
148: CharRange rangea = new CharRange('a', 'e', true);
149: assertEquals('a', rangea.getStart());
150: assertEquals('e', rangea.getEnd());
151: assertEquals(true, rangea.isNegated());
152: assertEquals("^a-e", rangea.toString());
153: }
154:
155: public void testConstructorAccessors_CharCharBoolean_ReversedNegated() {
156: CharRange rangea = new CharRange('e', 'a', true);
157: assertEquals('a', rangea.getStart());
158: assertEquals('e', rangea.getEnd());
159: assertEquals(true, rangea.isNegated());
160: assertEquals("^a-e", rangea.toString());
161: }
162:
163: //-----------------------------------------------------------------------
164: public void testEquals_Object() {
165: CharRange rangea = new CharRange('a');
166: CharRange rangeae = new CharRange('a', 'e');
167: CharRange rangenotbf = new CharRange('b', 'f', false);
168:
169: assertEquals(false, rangea.equals(null));
170:
171: assertEquals(true, rangea.equals(rangea));
172: assertEquals(true, rangea.equals(new CharRange('a')));
173: assertEquals(true, rangeae.equals(rangeae));
174: assertEquals(true, rangeae.equals(new CharRange('a', 'e')));
175: assertEquals(true, rangenotbf.equals(rangenotbf));
176: assertEquals(true, rangenotbf.equals(new CharRange('b', 'f',
177: false)));
178:
179: assertEquals(false, rangea.equals(rangeae));
180: assertEquals(false, rangea.equals(rangenotbf));
181: assertEquals(false, rangeae.equals(rangea));
182: assertEquals(false, rangeae.equals(rangenotbf));
183: assertEquals(false, rangenotbf.equals(rangea));
184: assertEquals(false, rangenotbf.equals(rangeae));
185: }
186:
187: public void testHashCode() {
188: CharRange rangea = new CharRange('a');
189: CharRange rangeae = new CharRange('a', 'e');
190: CharRange rangenotbf = new CharRange('b', 'f', false);
191:
192: assertEquals(true, rangea.hashCode() == rangea.hashCode());
193: assertEquals(true, rangea.hashCode() == new CharRange('a')
194: .hashCode());
195: assertEquals(true, rangeae.hashCode() == rangeae.hashCode());
196: assertEquals(true,
197: rangeae.hashCode() == new CharRange('a', 'e')
198: .hashCode());
199: assertEquals(true, rangenotbf.hashCode() == rangenotbf
200: .hashCode());
201: assertEquals(true, rangenotbf.hashCode() == new CharRange('b',
202: 'f', false).hashCode());
203:
204: assertEquals(false, rangea.hashCode() == rangeae.hashCode());
205: assertEquals(false, rangea.hashCode() == rangenotbf.hashCode());
206: assertEquals(false, rangeae.hashCode() == rangea.hashCode());
207: assertEquals(false, rangeae.hashCode() == rangenotbf.hashCode());
208: assertEquals(false, rangenotbf.hashCode() == rangea.hashCode());
209: assertEquals(false, rangenotbf.hashCode() == rangeae.hashCode());
210: }
211:
212: //-----------------------------------------------------------------------
213: public void testContains_Char() {
214: CharRange range = new CharRange('c');
215: assertEquals(false, range.contains('b'));
216: assertEquals(true, range.contains('c'));
217: assertEquals(false, range.contains('d'));
218: assertEquals(false, range.contains('e'));
219:
220: range = new CharRange('c', 'd');
221: assertEquals(false, range.contains('b'));
222: assertEquals(true, range.contains('c'));
223: assertEquals(true, range.contains('d'));
224: assertEquals(false, range.contains('e'));
225:
226: range = new CharRange('d', 'c');
227: assertEquals(false, range.contains('b'));
228: assertEquals(true, range.contains('c'));
229: assertEquals(true, range.contains('d'));
230: assertEquals(false, range.contains('e'));
231:
232: range = new CharRange('c', 'd', false);
233: assertEquals(false, range.contains('b'));
234: assertEquals(true, range.contains('c'));
235: assertEquals(true, range.contains('d'));
236: assertEquals(false, range.contains('e'));
237:
238: range = new CharRange('c', 'd', true);
239: assertEquals(true, range.contains('b'));
240: assertEquals(false, range.contains('c'));
241: assertEquals(false, range.contains('d'));
242: assertEquals(true, range.contains('e'));
243: assertEquals(true, range.contains((char) 0));
244: assertEquals(true, range.contains(Character.MAX_VALUE));
245: }
246:
247: //-----------------------------------------------------------------------
248: public void testContains_Charrange() {
249: CharRange a = new CharRange('a');
250: CharRange b = new CharRange('b');
251: CharRange c = new CharRange('c');
252: CharRange c2 = new CharRange('c');
253: CharRange d = new CharRange('d');
254: CharRange e = new CharRange('e');
255: CharRange cd = new CharRange('c', 'd');
256: CharRange bd = new CharRange('b', 'd');
257: CharRange bc = new CharRange('b', 'c');
258: CharRange ab = new CharRange('a', 'b');
259: CharRange de = new CharRange('d', 'e');
260: CharRange ef = new CharRange('e', 'f');
261: CharRange ae = new CharRange('a', 'e');
262:
263: // normal/normal
264: assertEquals(false, c.contains(b));
265: assertEquals(true, c.contains(c));
266: assertEquals(true, c.contains(c2));
267: assertEquals(false, c.contains(d));
268:
269: assertEquals(false, c.contains(cd));
270: assertEquals(false, c.contains(bd));
271: assertEquals(false, c.contains(bc));
272: assertEquals(false, c.contains(ab));
273: assertEquals(false, c.contains(de));
274:
275: assertEquals(true, cd.contains(c));
276: assertEquals(true, bd.contains(c));
277: assertEquals(true, bc.contains(c));
278: assertEquals(false, ab.contains(c));
279: assertEquals(false, de.contains(c));
280:
281: assertEquals(true, ae.contains(b));
282: assertEquals(true, ae.contains(ab));
283: assertEquals(true, ae.contains(bc));
284: assertEquals(true, ae.contains(cd));
285: assertEquals(true, ae.contains(de));
286:
287: CharRange notb = new CharRange('b', 'b', true);
288: CharRange notc = new CharRange('c', 'c', true);
289: CharRange notd = new CharRange('d', 'd', true);
290: CharRange notab = new CharRange('a', 'b', true);
291: CharRange notbc = new CharRange('b', 'c', true);
292: CharRange notbd = new CharRange('b', 'd', true);
293: CharRange notcd = new CharRange('c', 'd', true);
294: CharRange notde = new CharRange('d', 'e', true);
295: CharRange notae = new CharRange('a', 'e', true);
296: CharRange all = new CharRange((char) 0, Character.MAX_VALUE);
297: CharRange allbutfirst = new CharRange((char) 1,
298: Character.MAX_VALUE);
299:
300: // normal/negated
301: assertEquals(false, c.contains(notc));
302: assertEquals(false, c.contains(notbd));
303: assertEquals(true, all.contains(notc));
304: assertEquals(true, all.contains(notbd));
305: assertEquals(false, allbutfirst.contains(notc));
306: assertEquals(false, allbutfirst.contains(notbd));
307:
308: // negated/normal
309: assertEquals(true, notc.contains(a));
310: assertEquals(true, notc.contains(b));
311: assertEquals(false, notc.contains(c));
312: assertEquals(true, notc.contains(d));
313: assertEquals(true, notc.contains(e));
314:
315: assertEquals(true, notc.contains(ab));
316: assertEquals(false, notc.contains(bc));
317: assertEquals(false, notc.contains(bd));
318: assertEquals(false, notc.contains(cd));
319: assertEquals(true, notc.contains(de));
320: assertEquals(false, notc.contains(ae));
321: assertEquals(false, notc.contains(all));
322: assertEquals(false, notc.contains(allbutfirst));
323:
324: assertEquals(true, notbd.contains(a));
325: assertEquals(false, notbd.contains(b));
326: assertEquals(false, notbd.contains(c));
327: assertEquals(false, notbd.contains(d));
328: assertEquals(true, notbd.contains(e));
329:
330: assertEquals(true, notcd.contains(ab));
331: assertEquals(false, notcd.contains(bc));
332: assertEquals(false, notcd.contains(bd));
333: assertEquals(false, notcd.contains(cd));
334: assertEquals(false, notcd.contains(de));
335: assertEquals(false, notcd.contains(ae));
336: assertEquals(true, notcd.contains(ef));
337: assertEquals(false, notcd.contains(all));
338: assertEquals(false, notcd.contains(allbutfirst));
339:
340: // negated/negated
341: assertEquals(false, notc.contains(notb));
342: assertEquals(true, notc.contains(notc));
343: assertEquals(false, notc.contains(notd));
344:
345: assertEquals(false, notc.contains(notab));
346: assertEquals(true, notc.contains(notbc));
347: assertEquals(true, notc.contains(notbd));
348: assertEquals(true, notc.contains(notcd));
349: assertEquals(false, notc.contains(notde));
350:
351: assertEquals(false, notbd.contains(notb));
352: assertEquals(false, notbd.contains(notc));
353: assertEquals(false, notbd.contains(notd));
354:
355: assertEquals(false, notbd.contains(notab));
356: assertEquals(false, notbd.contains(notbc));
357: assertEquals(true, notbd.contains(notbd));
358: assertEquals(false, notbd.contains(notcd));
359: assertEquals(false, notbd.contains(notde));
360: assertEquals(true, notbd.contains(notae));
361: }
362:
363: public void testContainsNullArg() {
364: CharRange range = new CharRange('a');
365: try {
366: boolean contains = range.contains(null);
367: } catch (IllegalArgumentException e) {
368: assertEquals("The Range must not be null", e.getMessage());
369: }
370: }
371:
372: //-----------------------------------------------------------------------
373: public void testSerialization() {
374: CharRange range = new CharRange('a');
375: assertEquals(range, SerializationUtils.clone(range));
376: range = new CharRange('a', 'e');
377: assertEquals(range, SerializationUtils.clone(range));
378: range = new CharRange('a', 'e', true);
379: assertEquals(range, SerializationUtils.clone(range));
380: }
381:
382: }
|