KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2004-2005 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.Constructor JavaDoc;
19 import java.lang.reflect.Modifier JavaDoc;
20
21 import junit.framework.Test;
22 import junit.framework.TestCase;
23 import junit.framework.TestSuite;
24 import junit.textui.TestRunner;
25
26 /**
27  * Unit tests {@link org.apache.commons.lang.CharUtils}.
28  *
29  * @author Stephen Colebourne
30  * @version $Id: CharUtilsTest.java 161244 2005-04-14 06:16:36Z ggregory $
31  */

32 public class CharUtilsTest extends TestCase {
33
34     private static final Character JavaDoc CHARACTER_A = new Character JavaDoc('A');
35     private static final Character JavaDoc CHARACTER_B = new Character JavaDoc('B');
36     private static final char CHAR_COPY = '\u00a9';
37     
38     public CharUtilsTest(String JavaDoc name) {
39         super(name);
40     }
41
42     public static void main(String JavaDoc[] args) {
43         TestRunner.run(suite());
44     }
45
46     public static Test suite() {
47         TestSuite suite = new TestSuite(CharUtilsTest.class);
48         suite.setName("CharUtils Tests");
49         return suite;
50     }
51
52     protected void setUp() throws Exception JavaDoc {
53         super.setUp();
54     }
55
56     protected void tearDown() throws Exception JavaDoc {
57         super.tearDown();
58     }
59
60     //-----------------------------------------------------------------------
61
public void testConstructor() {
62         assertNotNull(new CharUtils());
63         Constructor JavaDoc[] cons = CharUtils.class.getDeclaredConstructors();
64         assertEquals(1, cons.length);
65         assertEquals(true, Modifier.isPublic(cons[0].getModifiers()));
66         assertEquals(true, Modifier.isPublic(BooleanUtils.class.getModifiers()));
67         assertEquals(false, Modifier.isFinal(BooleanUtils.class.getModifiers()));
68     }
69     
70     //-----------------------------------------------------------------------
71
public void testToCharacterObject_char() {
72         assertEquals(new Character JavaDoc('a'), CharUtils.toCharacterObject('a'));
73         assertSame(CharUtils.toCharacterObject('a'), CharUtils.toCharacterObject('a'));
74        
75         for (int i = 0; i < 128; i++) {
76             Character JavaDoc ch = CharUtils.toCharacterObject((char) i);
77             Character JavaDoc ch2 = CharUtils.toCharacterObject((char) i);
78             assertSame(ch, ch2);
79             assertEquals(i, ch.charValue());
80         }
81         for (int i = 128; i < 196; i++) {
82             Character JavaDoc ch = CharUtils.toCharacterObject((char) i);
83             Character JavaDoc ch2 = CharUtils.toCharacterObject((char) i);
84             assertEquals(ch, ch2);
85             assertTrue(ch != ch2);
86             assertEquals(i, ch.charValue());
87             assertEquals(i, ch2.charValue());
88         }
89     }
90     
91     public void testToCharacterObject_String() {
92         assertEquals(null, CharUtils.toCharacterObject(null));
93         assertEquals(null, CharUtils.toCharacterObject(""));
94         assertEquals(new Character JavaDoc('a'), CharUtils.toCharacterObject("a"));
95         assertEquals(new Character JavaDoc('a'), CharUtils.toCharacterObject("abc"));
96         assertSame(CharUtils.toCharacterObject("a"), CharUtils.toCharacterObject("a"));
97         assertSame(CharUtils.toCharacterObject("a"), CharUtils.toCharacterObject('a'));
98     }
99     
100     //-----------------------------------------------------------------------
101
public void testToChar_Character() {
102         assertEquals('A', CharUtils.toChar(CHARACTER_A));
103         assertEquals('B', CharUtils.toChar(CHARACTER_B));
104         try {
105             CharUtils.toChar((Character JavaDoc) null);
106         } catch (IllegalArgumentException JavaDoc ex) {}
107     }
108     
109     public void testToChar_Character_char() {
110         assertEquals('A', CharUtils.toChar(CHARACTER_A, 'X'));
111         assertEquals('B', CharUtils.toChar(CHARACTER_B, 'X'));
112         assertEquals('X', CharUtils.toChar((Character JavaDoc) null, 'X'));
113     }
114     
115     //-----------------------------------------------------------------------
116
public void testToChar_String() {
117         assertEquals('A', CharUtils.toChar("A"));
118         assertEquals('B', CharUtils.toChar("BA"));
119         try {
120             CharUtils.toChar((String JavaDoc) null);
121         } catch (IllegalArgumentException JavaDoc ex) {}
122         try {
123             CharUtils.toChar("");
124         } catch (IllegalArgumentException JavaDoc ex) {}
125     }
126     
127     public void testToChar_String_char() {
128         assertEquals('A', CharUtils.toChar("A", 'X'));
129         assertEquals('B', CharUtils.toChar("BA", 'X'));
130         assertEquals('X', CharUtils.toChar("", 'X'));
131         assertEquals('X', CharUtils.toChar((String JavaDoc) null, 'X'));
132     }
133     
134     //-----------------------------------------------------------------------
135
public void testToIntValue_char() {
136         assertEquals(0, CharUtils.toIntValue('0'));
137         assertEquals(1, CharUtils.toIntValue('1'));
138         assertEquals(2, CharUtils.toIntValue('2'));
139         assertEquals(3, CharUtils.toIntValue('3'));
140         assertEquals(4, CharUtils.toIntValue('4'));
141         assertEquals(5, CharUtils.toIntValue('5'));
142         assertEquals(6, CharUtils.toIntValue('6'));
143         assertEquals(7, CharUtils.toIntValue('7'));
144         assertEquals(8, CharUtils.toIntValue('8'));
145         assertEquals(9, CharUtils.toIntValue('9'));
146         try {
147             CharUtils.toIntValue('a');
148         } catch (IllegalArgumentException JavaDoc ex) {}
149     }
150     
151     public void testToIntValue_char_int() {
152         assertEquals(0, CharUtils.toIntValue('0', -1));
153         assertEquals(3, CharUtils.toIntValue('3', -1));
154         assertEquals(-1, CharUtils.toIntValue('a', -1));
155     }
156     
157     //-----------------------------------------------------------------------
158
public void testToIntValue_Character() {
159         assertEquals(0, CharUtils.toIntValue(new Character JavaDoc('0')));
160         assertEquals(3, CharUtils.toIntValue(new Character JavaDoc('3')));
161         try {
162             CharUtils.toIntValue(null);
163         } catch (IllegalArgumentException JavaDoc ex) {}
164         try {
165             CharUtils.toIntValue(CHARACTER_A);
166         } catch (IllegalArgumentException JavaDoc ex) {}
167     }
168     
169     public void testToIntValue_Character_int() {
170         assertEquals(0, CharUtils.toIntValue(new Character JavaDoc('0'), -1));
171         assertEquals(3, CharUtils.toIntValue(new Character JavaDoc('3'), -1));
172         assertEquals(-1, CharUtils.toIntValue(new Character JavaDoc('A'), -1));
173         assertEquals(-1, CharUtils.toIntValue(null, -1));
174     }
175     
176     //-----------------------------------------------------------------------
177
public void testToString_char() {
178         assertEquals("a", CharUtils.toString('a'));
179         assertSame(CharUtils.toString('a'), CharUtils.toString('a'));
180        
181         for (int i = 0; i < 128; i++) {
182             String JavaDoc str = CharUtils.toString((char) i);
183             String JavaDoc str2 = CharUtils.toString((char) i);
184             assertSame(str, str2);
185             assertEquals(1, str.length());
186             assertEquals(i, str.charAt(0));
187         }
188         for (int i = 128; i < 196; i++) {
189             String JavaDoc str = CharUtils.toString((char) i);
190             String JavaDoc str2 = CharUtils.toString((char) i);
191             assertEquals(str, str2);
192             assertTrue(str != str2);
193             assertEquals(1, str.length());
194             assertEquals(i, str.charAt(0));
195             assertEquals(1, str2.length());
196             assertEquals(i, str2.charAt(0));
197         }
198     }
199     
200     public void testToString_Character() {
201         assertEquals(null, CharUtils.toString(null));
202         assertEquals("A", CharUtils.toString(CHARACTER_A));
203         assertSame(CharUtils.toString(CHARACTER_A), CharUtils.toString(CHARACTER_A));
204     }
205     
206     //-----------------------------------------------------------------------
207
public void testToUnicodeEscaped_char() {
208         assertEquals("\\u0041", CharUtils.unicodeEscaped('A'));
209        
210         for (int i = 0; i < 196; i++) {
211             String JavaDoc str = CharUtils.unicodeEscaped((char) i);
212             assertEquals(6, str.length());
213             int val = Integer.parseInt(str.substring(2), 16);
214             assertEquals(i, val);
215         }
216     }
217     
218     public void testToUnicodeEscaped_Character() {
219         assertEquals(null, CharUtils.unicodeEscaped(null));
220         assertEquals("\\u0041", CharUtils.unicodeEscaped(CHARACTER_A));
221     }
222     
223     //-----------------------------------------------------------------------
224
public void testIsAscii_char() {
225         assertEquals(true, CharUtils.isAscii('a'));
226         assertEquals(true, CharUtils.isAscii('A'));
227         assertEquals(true, CharUtils.isAscii('3'));
228         assertEquals(true, CharUtils.isAscii('-'));
229         assertEquals(true, CharUtils.isAscii('\n'));
230         assertEquals(false, CharUtils.isAscii(CHAR_COPY));
231        
232         for (int i = 0; i < 128; i++) {
233             if (i < 128) {
234                 assertEquals(true, CharUtils.isAscii((char) i));
235             } else {
236                 assertEquals(false, CharUtils.isAscii((char) i));
237             }
238         }
239     }
240     
241     //-----------------------------------------------------------------------
242
public void testIsAsciiPrintable_char() {
243         assertEquals(true, CharUtils.isAsciiPrintable('a'));
244         assertEquals(true, CharUtils.isAsciiPrintable('A'));
245         assertEquals(true, CharUtils.isAsciiPrintable('3'));
246         assertEquals(true, CharUtils.isAsciiPrintable('-'));
247         assertEquals(false, CharUtils.isAsciiPrintable('\n'));
248         assertEquals(false, CharUtils.isAscii(CHAR_COPY));
249        
250         for (int i = 0; i < 196; i++) {
251             if (i >= 32 && i <= 126) {
252                 assertEquals(true, CharUtils.isAsciiPrintable((char) i));
253             } else {
254                 assertEquals(false, CharUtils.isAsciiPrintable((char) i));
255             }
256         }
257     }
258     
259     //-----------------------------------------------------------------------
260
public void testIsAsciiControl_char() {
261         assertEquals(false, CharUtils.isAsciiControl('a'));
262         assertEquals(false, CharUtils.isAsciiControl('A'));
263         assertEquals(false, CharUtils.isAsciiControl('3'));
264         assertEquals(false, CharUtils.isAsciiControl('-'));
265         assertEquals(true, CharUtils.isAsciiControl('\n'));
266         assertEquals(false, CharUtils.isAsciiControl(CHAR_COPY));
267        
268         for (int i = 0; i < 196; i++) {
269             if (i < 32 || i == 127) {
270                 assertEquals(true, CharUtils.isAsciiControl((char) i));
271             } else {
272                 assertEquals(false, CharUtils.isAsciiControl((char) i));
273             }
274         }
275     }
276     
277     //-----------------------------------------------------------------------
278
public void testIsAsciiAlpha_char() {
279         assertEquals(true, CharUtils.isAsciiAlpha('a'));
280         assertEquals(true, CharUtils.isAsciiAlpha('A'));
281         assertEquals(false, CharUtils.isAsciiAlpha('3'));
282         assertEquals(false, CharUtils.isAsciiAlpha('-'));
283         assertEquals(false, CharUtils.isAsciiAlpha('\n'));
284         assertEquals(false, CharUtils.isAsciiAlpha(CHAR_COPY));
285        
286         for (int i = 0; i < 196; i++) {
287             if ((i >= 'A' && i <= 'Z') || (i >= 'a' && i <= 'z')) {
288                 assertEquals(true, CharUtils.isAsciiAlpha((char) i));
289             } else {
290                 assertEquals(false, CharUtils.isAsciiAlpha((char) i));
291             }
292         }
293     }
294     
295     //-----------------------------------------------------------------------
296
public void testIsAsciiAlphaUpper_char() {
297         assertEquals(false, CharUtils.isAsciiAlphaUpper('a'));
298         assertEquals(true, CharUtils.isAsciiAlphaUpper('A'));
299         assertEquals(false, CharUtils.isAsciiAlphaUpper('3'));
300         assertEquals(false, CharUtils.isAsciiAlphaUpper('-'));
301         assertEquals(false, CharUtils.isAsciiAlphaUpper('\n'));
302         assertEquals(false, CharUtils.isAsciiAlphaUpper(CHAR_COPY));
303        
304         for (int i = 0; i < 196; i++) {
305             if (i >= 'A' && i <= 'Z') {
306                 assertEquals(true, CharUtils.isAsciiAlphaUpper((char) i));
307             } else {
308                 assertEquals(false, CharUtils.isAsciiAlphaUpper((char) i));
309             }
310         }
311     }
312     
313     //-----------------------------------------------------------------------
314
public void testIsAsciiAlphaLower_char() {
315         assertEquals(true, CharUtils.isAsciiAlphaLower('a'));
316         assertEquals(false, CharUtils.isAsciiAlphaLower('A'));
317         assertEquals(false, CharUtils.isAsciiAlphaLower('3'));
318         assertEquals(false, CharUtils.isAsciiAlphaLower('-'));
319         assertEquals(false, CharUtils.isAsciiAlphaLower('\n'));
320         assertEquals(false, CharUtils.isAsciiAlphaLower(CHAR_COPY));
321        
322         for (int i = 0; i < 196; i++) {
323             if (i >= 'a' && i <= 'z') {
324                 assertEquals(true, CharUtils.isAsciiAlphaLower((char) i));
325             } else {
326                 assertEquals(false, CharUtils.isAsciiAlphaLower((char) i));
327             }
328         }
329     }
330     
331     //-----------------------------------------------------------------------
332
public void testIsAsciiNumeric_char() {
333         assertEquals(false, CharUtils.isAsciiNumeric('a'));
334         assertEquals(false, CharUtils.isAsciiNumeric('A'));
335         assertEquals(true, CharUtils.isAsciiNumeric('3'));
336         assertEquals(false, CharUtils.isAsciiNumeric('-'));
337         assertEquals(false, CharUtils.isAsciiNumeric('\n'));
338         assertEquals(false, CharUtils.isAsciiNumeric(CHAR_COPY));
339        
340         for (int i = 0; i < 196; i++) {
341             if (i >= '0' && i <= '9') {
342                 assertEquals(true, CharUtils.isAsciiNumeric((char) i));
343             } else {
344                 assertEquals(false, CharUtils.isAsciiNumeric((char) i));
345             }
346         }
347     }
348     
349     //-----------------------------------------------------------------------
350
public void testIsAsciiAlphanumeric_char() {
351         assertEquals(true, CharUtils.isAsciiAlphanumeric('a'));
352         assertEquals(true, CharUtils.isAsciiAlphanumeric('A'));
353         assertEquals(true, CharUtils.isAsciiAlphanumeric('3'));
354         assertEquals(false, CharUtils.isAsciiAlphanumeric('-'));
355         assertEquals(false, CharUtils.isAsciiAlphanumeric('\n'));
356         assertEquals(false, CharUtils.isAsciiAlphanumeric(CHAR_COPY));
357        
358         for (int i = 0; i < 196; i++) {
359             if ((i >= 'A' && i <= 'Z') || (i >= 'a' && i <= 'z') || (i >= '0' && i <= '9')) {
360                 assertEquals(true, CharUtils.isAsciiAlphanumeric((char) i));
361             } else {
362                 assertEquals(false, CharUtils.isAsciiAlphanumeric((char) i));
363             }
364         }
365     }
366     
367 }
368
Popular Tags