001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.lang;
018:
019: import java.lang.reflect.Constructor;
020: import java.lang.reflect.Modifier;
021:
022: import junit.framework.Test;
023: import junit.framework.TestCase;
024: import junit.framework.TestSuite;
025: import junit.textui.TestRunner;
026:
027: /**
028: * Unit tests {@link org.apache.commons.lang.CharUtils}.
029: *
030: * @author Stephen Colebourne
031: * @version $Id: CharUtilsTest.java 437554 2006-08-28 06:21:41Z bayard $
032: */
033: public class CharUtilsTest extends TestCase {
034:
035: private static final Character CHARACTER_A = new Character('A');
036: private static final Character CHARACTER_B = new Character('B');
037: private static final char CHAR_COPY = '\u00a9';
038:
039: public CharUtilsTest(String name) {
040: super (name);
041: }
042:
043: public static void main(String[] args) {
044: TestRunner.run(suite());
045: }
046:
047: public static Test suite() {
048: TestSuite suite = new TestSuite(CharUtilsTest.class);
049: suite.setName("CharUtils Tests");
050: return suite;
051: }
052:
053: protected void setUp() throws Exception {
054: super .setUp();
055: }
056:
057: protected void tearDown() throws Exception {
058: super .tearDown();
059: }
060:
061: //-----------------------------------------------------------------------
062: public void testConstructor() {
063: assertNotNull(new CharUtils());
064: Constructor[] cons = CharUtils.class.getDeclaredConstructors();
065: assertEquals(1, cons.length);
066: assertEquals(true, Modifier.isPublic(cons[0].getModifiers()));
067: assertEquals(true, Modifier.isPublic(BooleanUtils.class
068: .getModifiers()));
069: assertEquals(false, Modifier.isFinal(BooleanUtils.class
070: .getModifiers()));
071: }
072:
073: //-----------------------------------------------------------------------
074: public void testToCharacterObject_char() {
075: assertEquals(new Character('a'), CharUtils
076: .toCharacterObject('a'));
077: assertSame(CharUtils.toCharacterObject('a'), CharUtils
078: .toCharacterObject('a'));
079:
080: for (int i = 0; i < 128; i++) {
081: Character ch = CharUtils.toCharacterObject((char) i);
082: Character ch2 = CharUtils.toCharacterObject((char) i);
083: assertSame(ch, ch2);
084: assertEquals(i, ch.charValue());
085: }
086: for (int i = 128; i < 196; i++) {
087: Character ch = CharUtils.toCharacterObject((char) i);
088: Character ch2 = CharUtils.toCharacterObject((char) i);
089: assertEquals(ch, ch2);
090: assertTrue(ch != ch2);
091: assertEquals(i, ch.charValue());
092: assertEquals(i, ch2.charValue());
093: }
094: }
095:
096: public void testToCharacterObject_String() {
097: assertEquals(null, CharUtils.toCharacterObject(null));
098: assertEquals(null, CharUtils.toCharacterObject(""));
099: assertEquals(new Character('a'), CharUtils
100: .toCharacterObject("a"));
101: assertEquals(new Character('a'), CharUtils
102: .toCharacterObject("abc"));
103: assertSame(CharUtils.toCharacterObject("a"), CharUtils
104: .toCharacterObject("a"));
105: assertSame(CharUtils.toCharacterObject("a"), CharUtils
106: .toCharacterObject('a'));
107: }
108:
109: //-----------------------------------------------------------------------
110: public void testToChar_Character() {
111: assertEquals('A', CharUtils.toChar(CHARACTER_A));
112: assertEquals('B', CharUtils.toChar(CHARACTER_B));
113: try {
114: CharUtils.toChar((Character) null);
115: } catch (IllegalArgumentException ex) {
116: }
117: }
118:
119: public void testToChar_Character_char() {
120: assertEquals('A', CharUtils.toChar(CHARACTER_A, 'X'));
121: assertEquals('B', CharUtils.toChar(CHARACTER_B, 'X'));
122: assertEquals('X', CharUtils.toChar((Character) null, 'X'));
123: }
124:
125: //-----------------------------------------------------------------------
126: public void testToChar_String() {
127: assertEquals('A', CharUtils.toChar("A"));
128: assertEquals('B', CharUtils.toChar("BA"));
129: try {
130: CharUtils.toChar((String) null);
131: } catch (IllegalArgumentException ex) {
132: }
133: try {
134: CharUtils.toChar("");
135: } catch (IllegalArgumentException ex) {
136: }
137: }
138:
139: public void testToChar_String_char() {
140: assertEquals('A', CharUtils.toChar("A", 'X'));
141: assertEquals('B', CharUtils.toChar("BA", 'X'));
142: assertEquals('X', CharUtils.toChar("", 'X'));
143: assertEquals('X', CharUtils.toChar((String) null, 'X'));
144: }
145:
146: //-----------------------------------------------------------------------
147: public void testToIntValue_char() {
148: assertEquals(0, CharUtils.toIntValue('0'));
149: assertEquals(1, CharUtils.toIntValue('1'));
150: assertEquals(2, CharUtils.toIntValue('2'));
151: assertEquals(3, CharUtils.toIntValue('3'));
152: assertEquals(4, CharUtils.toIntValue('4'));
153: assertEquals(5, CharUtils.toIntValue('5'));
154: assertEquals(6, CharUtils.toIntValue('6'));
155: assertEquals(7, CharUtils.toIntValue('7'));
156: assertEquals(8, CharUtils.toIntValue('8'));
157: assertEquals(9, CharUtils.toIntValue('9'));
158: try {
159: CharUtils.toIntValue('a');
160: } catch (IllegalArgumentException ex) {
161: }
162: }
163:
164: public void testToIntValue_char_int() {
165: assertEquals(0, CharUtils.toIntValue('0', -1));
166: assertEquals(3, CharUtils.toIntValue('3', -1));
167: assertEquals(-1, CharUtils.toIntValue('a', -1));
168: }
169:
170: //-----------------------------------------------------------------------
171: public void testToIntValue_Character() {
172: assertEquals(0, CharUtils.toIntValue(new Character('0')));
173: assertEquals(3, CharUtils.toIntValue(new Character('3')));
174: try {
175: CharUtils.toIntValue(null);
176: } catch (IllegalArgumentException ex) {
177: }
178: try {
179: CharUtils.toIntValue(CHARACTER_A);
180: } catch (IllegalArgumentException ex) {
181: }
182: }
183:
184: public void testToIntValue_Character_int() {
185: assertEquals(0, CharUtils.toIntValue(new Character('0'), -1));
186: assertEquals(3, CharUtils.toIntValue(new Character('3'), -1));
187: assertEquals(-1, CharUtils.toIntValue(new Character('A'), -1));
188: assertEquals(-1, CharUtils.toIntValue(null, -1));
189: }
190:
191: //-----------------------------------------------------------------------
192: public void testToString_char() {
193: assertEquals("a", CharUtils.toString('a'));
194: assertSame(CharUtils.toString('a'), CharUtils.toString('a'));
195:
196: for (int i = 0; i < 128; i++) {
197: String str = CharUtils.toString((char) i);
198: String str2 = CharUtils.toString((char) i);
199: assertSame(str, str2);
200: assertEquals(1, str.length());
201: assertEquals(i, str.charAt(0));
202: }
203: for (int i = 128; i < 196; i++) {
204: String str = CharUtils.toString((char) i);
205: String str2 = CharUtils.toString((char) i);
206: assertEquals(str, str2);
207: assertTrue(str != str2);
208: assertEquals(1, str.length());
209: assertEquals(i, str.charAt(0));
210: assertEquals(1, str2.length());
211: assertEquals(i, str2.charAt(0));
212: }
213: }
214:
215: public void testToString_Character() {
216: assertEquals(null, CharUtils.toString(null));
217: assertEquals("A", CharUtils.toString(CHARACTER_A));
218: assertSame(CharUtils.toString(CHARACTER_A), CharUtils
219: .toString(CHARACTER_A));
220: }
221:
222: //-----------------------------------------------------------------------
223: public void testToUnicodeEscaped_char() {
224: assertEquals("\\u0041", CharUtils.unicodeEscaped('A'));
225:
226: for (int i = 0; i < 196; i++) {
227: String str = CharUtils.unicodeEscaped((char) i);
228: assertEquals(6, str.length());
229: int val = Integer.parseInt(str.substring(2), 16);
230: assertEquals(i, val);
231: }
232: assertEquals("\\u0999", CharUtils.unicodeEscaped((char) 0x999));
233: assertEquals("\\u1001", CharUtils.unicodeEscaped((char) 0x1001));
234: }
235:
236: public void testToUnicodeEscaped_Character() {
237: assertEquals(null, CharUtils.unicodeEscaped(null));
238: assertEquals("\\u0041", CharUtils.unicodeEscaped(CHARACTER_A));
239: }
240:
241: //-----------------------------------------------------------------------
242: public void testIsAscii_char() {
243: assertEquals(true, CharUtils.isAscii('a'));
244: assertEquals(true, CharUtils.isAscii('A'));
245: assertEquals(true, CharUtils.isAscii('3'));
246: assertEquals(true, CharUtils.isAscii('-'));
247: assertEquals(true, CharUtils.isAscii('\n'));
248: assertEquals(false, CharUtils.isAscii(CHAR_COPY));
249:
250: for (int i = 0; i < 128; i++) {
251: if (i < 128) {
252: assertEquals(true, CharUtils.isAscii((char) i));
253: } else {
254: assertEquals(false, CharUtils.isAscii((char) i));
255: }
256: }
257: }
258:
259: //-----------------------------------------------------------------------
260: public void testIsAsciiPrintable_char() {
261: assertEquals(true, CharUtils.isAsciiPrintable('a'));
262: assertEquals(true, CharUtils.isAsciiPrintable('A'));
263: assertEquals(true, CharUtils.isAsciiPrintable('3'));
264: assertEquals(true, CharUtils.isAsciiPrintable('-'));
265: assertEquals(false, CharUtils.isAsciiPrintable('\n'));
266: assertEquals(false, CharUtils.isAscii(CHAR_COPY));
267:
268: for (int i = 0; i < 196; i++) {
269: if (i >= 32 && i <= 126) {
270: assertEquals(true, CharUtils.isAsciiPrintable((char) i));
271: } else {
272: assertEquals(false, CharUtils
273: .isAsciiPrintable((char) i));
274: }
275: }
276: }
277:
278: //-----------------------------------------------------------------------
279: public void testIsAsciiControl_char() {
280: assertEquals(false, CharUtils.isAsciiControl('a'));
281: assertEquals(false, CharUtils.isAsciiControl('A'));
282: assertEquals(false, CharUtils.isAsciiControl('3'));
283: assertEquals(false, CharUtils.isAsciiControl('-'));
284: assertEquals(true, CharUtils.isAsciiControl('\n'));
285: assertEquals(false, CharUtils.isAsciiControl(CHAR_COPY));
286:
287: for (int i = 0; i < 196; i++) {
288: if (i < 32 || i == 127) {
289: assertEquals(true, CharUtils.isAsciiControl((char) i));
290: } else {
291: assertEquals(false, CharUtils.isAsciiControl((char) i));
292: }
293: }
294: }
295:
296: //-----------------------------------------------------------------------
297: public void testIsAsciiAlpha_char() {
298: assertEquals(true, CharUtils.isAsciiAlpha('a'));
299: assertEquals(true, CharUtils.isAsciiAlpha('A'));
300: assertEquals(false, CharUtils.isAsciiAlpha('3'));
301: assertEquals(false, CharUtils.isAsciiAlpha('-'));
302: assertEquals(false, CharUtils.isAsciiAlpha('\n'));
303: assertEquals(false, CharUtils.isAsciiAlpha(CHAR_COPY));
304:
305: for (int i = 0; i < 196; i++) {
306: if ((i >= 'A' && i <= 'Z') || (i >= 'a' && i <= 'z')) {
307: assertEquals(true, CharUtils.isAsciiAlpha((char) i));
308: } else {
309: assertEquals(false, CharUtils.isAsciiAlpha((char) i));
310: }
311: }
312: }
313:
314: //-----------------------------------------------------------------------
315: public void testIsAsciiAlphaUpper_char() {
316: assertEquals(false, CharUtils.isAsciiAlphaUpper('a'));
317: assertEquals(true, CharUtils.isAsciiAlphaUpper('A'));
318: assertEquals(false, CharUtils.isAsciiAlphaUpper('3'));
319: assertEquals(false, CharUtils.isAsciiAlphaUpper('-'));
320: assertEquals(false, CharUtils.isAsciiAlphaUpper('\n'));
321: assertEquals(false, CharUtils.isAsciiAlphaUpper(CHAR_COPY));
322:
323: for (int i = 0; i < 196; i++) {
324: if (i >= 'A' && i <= 'Z') {
325: assertEquals(true, CharUtils
326: .isAsciiAlphaUpper((char) i));
327: } else {
328: assertEquals(false, CharUtils
329: .isAsciiAlphaUpper((char) i));
330: }
331: }
332: }
333:
334: //-----------------------------------------------------------------------
335: public void testIsAsciiAlphaLower_char() {
336: assertEquals(true, CharUtils.isAsciiAlphaLower('a'));
337: assertEquals(false, CharUtils.isAsciiAlphaLower('A'));
338: assertEquals(false, CharUtils.isAsciiAlphaLower('3'));
339: assertEquals(false, CharUtils.isAsciiAlphaLower('-'));
340: assertEquals(false, CharUtils.isAsciiAlphaLower('\n'));
341: assertEquals(false, CharUtils.isAsciiAlphaLower(CHAR_COPY));
342:
343: for (int i = 0; i < 196; i++) {
344: if (i >= 'a' && i <= 'z') {
345: assertEquals(true, CharUtils
346: .isAsciiAlphaLower((char) i));
347: } else {
348: assertEquals(false, CharUtils
349: .isAsciiAlphaLower((char) i));
350: }
351: }
352: }
353:
354: //-----------------------------------------------------------------------
355: public void testIsAsciiNumeric_char() {
356: assertEquals(false, CharUtils.isAsciiNumeric('a'));
357: assertEquals(false, CharUtils.isAsciiNumeric('A'));
358: assertEquals(true, CharUtils.isAsciiNumeric('3'));
359: assertEquals(false, CharUtils.isAsciiNumeric('-'));
360: assertEquals(false, CharUtils.isAsciiNumeric('\n'));
361: assertEquals(false, CharUtils.isAsciiNumeric(CHAR_COPY));
362:
363: for (int i = 0; i < 196; i++) {
364: if (i >= '0' && i <= '9') {
365: assertEquals(true, CharUtils.isAsciiNumeric((char) i));
366: } else {
367: assertEquals(false, CharUtils.isAsciiNumeric((char) i));
368: }
369: }
370: }
371:
372: //-----------------------------------------------------------------------
373: public void testIsAsciiAlphanumeric_char() {
374: assertEquals(true, CharUtils.isAsciiAlphanumeric('a'));
375: assertEquals(true, CharUtils.isAsciiAlphanumeric('A'));
376: assertEquals(true, CharUtils.isAsciiAlphanumeric('3'));
377: assertEquals(false, CharUtils.isAsciiAlphanumeric('-'));
378: assertEquals(false, CharUtils.isAsciiAlphanumeric('\n'));
379: assertEquals(false, CharUtils.isAsciiAlphanumeric(CHAR_COPY));
380:
381: for (int i = 0; i < 196; i++) {
382: if ((i >= 'A' && i <= 'Z') || (i >= 'a' && i <= 'z')
383: || (i >= '0' && i <= '9')) {
384: assertEquals(true, CharUtils
385: .isAsciiAlphanumeric((char) i));
386: } else {
387: assertEquals(false, CharUtils
388: .isAsciiAlphanumeric((char) i));
389: }
390: }
391: }
392:
393: }
|