From fa5cac4449e87b40ab889a22e24a56e4e0fd4636 Mon Sep 17 00:00:00 2001 From: Stephen Colebourne Date: Sat, 31 Jan 2004 13:00:07 +0000 Subject: [PATCH] Add CharUtils classes git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137767 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/commons/lang/CharUtils.java | 556 ++++++++++++++++++ .../apache/commons/lang/CharUtilsTest.java | 396 +++++++++++++ .../apache/commons/lang/LangTestSuite.java | 5 +- 3 files changed, 955 insertions(+), 2 deletions(-) create mode 100644 src/java/org/apache/commons/lang/CharUtils.java create mode 100644 src/test/org/apache/commons/lang/CharUtilsTest.java diff --git a/src/java/org/apache/commons/lang/CharUtils.java b/src/java/org/apache/commons/lang/CharUtils.java new file mode 100644 index 000000000..df0476eab --- /dev/null +++ b/src/java/org/apache/commons/lang/CharUtils.java @@ -0,0 +1,556 @@ +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2004 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowledgement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgement may appear in the software itself, + * if and wherever such third-party acknowledgements normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ +package org.apache.commons.lang; + +/** + *

Operations on char primitives and Char objects.

+ * + *

This class tries to handle null input gracefully. + * An exception will not be thrown for a null input. + * Each method documents its behaviour in more detail.

+ * + * @author Stephen Colebourne + * @since 2.1 + * @version $Id: CharUtils.java,v 1.1 2004/01/31 13:00:07 scolebourne Exp $ + */ +public class CharUtils { + + private static final String CHAR_STRING = + "\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007" + + "\b\t\n\u000b\f\r\u000e\u000f" + + "\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017" + + "\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f" + + "\u0020\u0021\"\u0023\u0024\u0025\u0026\u0027" + + "\u0028\u0029\u002a\u002b\u002c\u002d\u002e\u002f" + + "\u0030\u0031\u0032\u0033\u0034\u0035\u0036\u0037" + + "\u0038\u0039\u003a\u003b\u003c\u003d\u003e\u003f" + + "\u0040\u0041\u0042\u0043\u0044\u0045\u0046\u0047" + + "\u0048\u0049\u004a\u004b\u004c\u004d\u004e\u004f" + + "\u0050\u0051\u0052\u0053\u0054\u0055\u0056\u0057" + + "\u0058\u0059\u005a\u005b\\\u005d\u005e\u005f" + + "\u0060\u0061\u0062\u0063\u0064\u0065\u0066\u0067" + + "\u0068\u0069\u006a\u006b\u006c\u006d\u006e\u006f" + + "\u0070\u0071\u0072\u0073\u0074\u0075\u0076\u0077" + + "\u0078\u0079\u007a\u007b\u007c\u007d\u007e\u007f"; + + private static final String[] CHAR_STRING_ARRAY = new String[128]; + private static final Character[] CHAR_ARRAY = new Character[128]; + + static { + for (int i = 127; i >= 0; i--) { + CHAR_STRING_ARRAY[i] = CHAR_STRING.substring(i, i + 1); + CHAR_ARRAY[i] = new Character((char) i); + } + } + + /** + *

CharUtils instances should NOT be constructed in standard programming. + * Instead, the class should be used as CharUtils.toString('c');.

+ * + *

This constructor is public to permit tools that require a JavaBean instance + * to operate.

+ */ + public CharUtils() { + } + + //----------------------------------------------------------------------- + /** + *

Converts the character to a Character.

+ * + *

For ASCII 7 bit characters, this uses a cache that will return the + * same Character object each time.

+ * + *
+     *   CharUtils.toCharacterObject(' ')  = ' '
+     *   CharUtils.toCharacterObject('A')  = 'A'
+     * 
+ * + * @param ch the character to convert + * @return a Character of the specified character + */ + public static Character toCharacterObject(char ch) { + if (ch < 128) { + return CHAR_ARRAY[ch]; + } else { + return new Character(ch); + } + } + + //----------------------------------------------------------------------- + /** + *

Converts the Character to a char throwing an exception for null.

+ * + *
+     *   CharUtils.toCharacter(null, 'X') = IllegalArgumentException
+     *   CharUtils.toCharacter(' ', 'X')  = ' '
+     *   CharUtils.toCharacter('A', 'X')  = 'A'
+     * 
+ * + * @param ch the character to convert + * @return the char value of the Character or the default if null + * @throws IllegalArgumentException if the Character is null + */ + public static char toCharacter(Character ch) { + if (ch == null) { + throw new IllegalArgumentException("The Character must not be null"); + } + return ch.charValue(); + } + + /** + *

Converts the Character to a char handling null.

+ * + *
+     *   CharUtils.toCharacter(null, 'X') = 'X'
+     *   CharUtils.toCharacter(' ', 'X')  = ' '
+     *   CharUtils.toCharacter('A', 'X')  = 'A'
+     * 
+ * + * @param ch the character to convert + * @param defaultValue the value to use if the Character is null + * @return the char value of the Character or the default if null + */ + public static char toCharacter(Character ch, char defaultValue) { + if (ch == null) { + return defaultValue; + } + return ch.charValue(); + } + + //----------------------------------------------------------------------- + /** + *

Converts the String to a char using the first character throwing + * an exception on empty Strings.

+ * + *
+     *   CharUtils.toCharacter(null, 'X') = IllegalArgumentException
+     *   CharUtils.toCharacter("", 'X')   = IllegalArgumentException
+     *   CharUtils.toCharacter("A", 'X')  = 'A'
+     *   CharUtils.toCharacter("BA", 'X') = 'B'
+     * 
+ * + * @param ch the character to convert + * @return the char value of the Character or the default if null + * @throws IllegalArgumentException if the String is empty + */ + public static char toCharacter(String str) { + if (str == null || str.length() == 0) { + throw new IllegalArgumentException("The String must not be empty"); + } + return str.charAt(0); + } + + /** + *

Converts the String to a char using the first character defaulting + * the value on empty Strings.

+ * + *
+     *   CharUtils.toCharacter(null, 'X') = 'X'
+     *   CharUtils.toCharacter("", 'X')   = 'X'
+     *   CharUtils.toCharacter("A", 'X')  = 'A'
+     *   CharUtils.toCharacter("BA", 'X') = 'B'
+     * 
+ * + * @param ch the character to convert + * @param defaultValue the value to use if the Character is null + * @return the char value of the Character or the default if null + */ + public static char toCharacter(String str, char defaultValue) { + if (str == null || str.length() == 0) { + return defaultValue; + } + return str.charAt(0); + } + + //----------------------------------------------------------------------- + /** + *

Converts the character to the Integer it represents, throwing an + * exception if the character is not numeric.

+ * + *

This method coverts the char '1' to the int 1 and so on.

+ * + *
+     *   CharUtils.toInteger('3')  = 3
+     *   CharUtils.toInteger('A')  = IllegalArgumentException
+     * 
+ * + * @param ch the character to convert + * @return the int value of the character + * @throws IllegalArgumentException if the character is not ASCII numeric + */ + public static int toInteger(char ch) { + if (isASCIINumeric(ch) == false) { + throw new IllegalArgumentException("The character " + ch + " is not in the range '0' - '9'"); + } + return (ch - 48); + } + + /** + *

Converts the character to the Integer it represents, throwing an + * exception if the character is not numeric.

+ * + *

This method coverts the char '1' to the int 1 and so on.

+ * + *
+     *   CharUtils.toInteger('3', -1)  = 3
+     *   CharUtils.toInteger('A', -1)  = -1
+     * 
+ * + * @param ch the character to convert + * @param defaultValue the default value to use if the character is not numeric + * @return the int value of the character + */ + public static int toInteger(char ch, int defaultValue) { + if (isASCIINumeric(ch) == false) { + return defaultValue; + } + return (ch - 48); + } + + /** + *

Converts the character to the Integer it represents, throwing an + * exception if the character is not numeric.

+ * + *

This method coverts the char '1' to the int 1 and so on.

+ * + *
+     *   CharUtils.toInteger(null) = IllegalArgumentException
+     *   CharUtils.toInteger('3')  = 3
+     *   CharUtils.toInteger('A')  = IllegalArgumentException
+     * 
+ * + * @param ch the character to convert, not null + * @return the int value of the character + * @throws IllegalArgumentException if the Character is not ASCII numeric or is null + */ + public static int toInteger(Character ch) { + if (ch == null) { + throw new IllegalArgumentException("The character must not be null"); + } + return toInteger(ch.charValue()); + } + + /** + *

Converts the character to the Integer it represents, throwing an + * exception if the character is not numeric.

+ * + *

This method coverts the char '1' to the int 1 and so on.

+ * + *
+     *   CharUtils.toInteger(null, -1) = -1
+     *   CharUtils.toInteger('3', -1)  = 3
+     *   CharUtils.toInteger('A', -1)  = -1
+     * 
+ * + * @param ch the character to convert + * @param defaultValue the default value to use if the character is not numeric + * @return the int value of the character + */ + public static int toInteger(Character ch, int defaultValue) { + if (ch == null) { + return defaultValue; + } + return toInteger(ch.charValue(), defaultValue); + } + + //----------------------------------------------------------------------- + /** + *

Converts the character to a String that contains the one character.

+ * + *

For ASCII 7 bit characters, this uses a cache that will return the + * same String object each time.

+ * + *
+     *   CharUtils.toString(' ')  = " "
+     *   CharUtils.toString('A')  = "A"
+     * 
+ * + * @param ch the character to convert + * @return a String containing the one specified character + */ + public static String toString(char ch) { + if (ch < 128) { + return CHAR_STRING_ARRAY[ch]; + } else { + return new String(new char[] {ch}); + } + } + + /** + *

Converts the character to a String that contains the one character.

+ * + *

For ASCII 7 bit characters, this uses a cache that will return the + * same String object each time.

+ * + *

If null is passed in, null will be returned.

+ * + *
+     *   CharUtils.toString(null) = null
+     *   CharUtils.toString(' ')  = " "
+     *   CharUtils.toString('A')  = "A"
+     * 
+ * + * @param ch the character to convert + * @return a String containing the one specified character + */ + public static String toString(Character ch) { + if (ch == null) { + return null; + } else { + return toString(ch.charValue()); + } + } + + //-------------------------------------------------------------------------- + /** + *

Converts the string to the unicode format '\u0020'.

+ * + *

This format is the Java source code format.

+ * + *
+     *   CharUtils.toUnicode(' ') = "\u0020"
+     *   CharUtils.toUnicode('A') = "\u0041"
+     * 
+ * + * @param ch the character to convert + * @return the escaped unicode string + */ + public static String unicodeEscaped(char ch) { + if (ch < 0x10) { + return "\\u000" + Integer.toHexString(ch); + } else if (ch < 0x100) { + return "\\u00" + Integer.toHexString(ch); + } else if (ch < 0x1000) { + return "\\u0" + Integer.toHexString(ch); + } + return "\\u" + Integer.toHexString(ch); + } + + /** + *

Converts the string to the unicode format '\u0020'.

+ * + *

This format is the Java source code format.

+ * + *

If null is passed in, null will be returned.

+ * + *
+     *   CharUtils.toUnicode(null) = null
+     *   CharUtils.toUnicode(' ')  = "\u0020"
+     *   CharUtils.toUnicode('A')  = "\u0041"
+     * 
+ * + * @param ch the character to convert, may be null + * @return the escaped unicode string, null if null input + */ + public static String unicodeEscaped(Character ch) { + if (ch == null) { + return null; + } + return unicodeEscaped(ch.charValue()); + } + + //-------------------------------------------------------------------------- + /** + *

Checks whether the character is ASCII 7 bit.

+ * + *
+     *   CharUtils.isASCII('a')  = true
+     *   CharUtils.isASCII('A')  = true
+     *   CharUtils.isASCII('3')  = true
+     *   CharUtils.isASCII('-')  = true
+     *   CharUtils.isASCII('\n') = true
+     *   CharUtils.isASCII('©') = false
+     * 
+ * + * @param ch the character to check + * @return true if less than 128 + */ + public static boolean isASCII(char ch) { + return (ch < 128); + } + + /** + *

Checks whether the character is ASCII 7 bit printable.

+ * + *
+     *   CharUtils.isASCIIPrintable('a')  = true
+     *   CharUtils.isASCIIPrintable('A')  = true
+     *   CharUtils.isASCIIPrintable('3')  = true
+     *   CharUtils.isASCIIPrintable('-')  = true
+     *   CharUtils.isASCIIPrintable('\n') = false
+     *   CharUtils.isASCIIPrintable('©') = false
+     * 
+ * + * @param ch the character to check + * @return true if between 32 and 126 inclusive + */ + public static boolean isASCIIPrintable(char ch) { + return (ch >= 32 && ch < 127); + } + + /** + *

Checks whether the character is ASCII 7 bit control.

+ * + *
+     *   CharUtils.isASCIIControl('a')  = false
+     *   CharUtils.isASCIIControl('A')  = false
+     *   CharUtils.isASCIIControl('3')  = false
+     *   CharUtils.isASCIIControl('-')  = false
+     *   CharUtils.isASCIIControl('\n') = true
+     *   CharUtils.isASCIIControl('©') = false
+     * 
+ * + * @param ch the character to check + * @return true if less than 32 or equals 127 + */ + public static boolean isASCIIControl(char ch) { + return (ch < 32 || ch == 127); + } + + /** + *

Checks whether the character is ASCII 7 bit alphabetic.

+ * + *
+     *   CharUtils.isASCIIAlpha('a')  = true
+     *   CharUtils.isASCIIAlpha('A')  = true
+     *   CharUtils.isASCIIAlpha('3')  = false
+     *   CharUtils.isASCIIAlpha('-')  = false
+     *   CharUtils.isASCIIAlpha('\n') = false
+     *   CharUtils.isASCIIAlpha('©') = false
+     * 
+ * + * @param ch the character to check + * @return true if between 65 and 90 or 97 and 122 inclusive + */ + public static boolean isASCIIAlpha(char ch) { + return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'); + } + + /** + *

Checks whether the character is ASCII 7 bit alphabetic upper case.

+ * + *
+     *   CharUtils.isASCIIAlphaUpper('a')  = false
+     *   CharUtils.isASCIIAlphaUpper('A')  = true
+     *   CharUtils.isASCIIAlphaUpper('3')  = false
+     *   CharUtils.isASCIIAlphaUpper('-')  = false
+     *   CharUtils.isASCIIAlphaUpper('\n') = false
+     *   CharUtils.isASCIIAlphaUpper('©') = false
+     * 
+ * + * @param ch the character to check + * @return true if between 65 and 90 inclusive + */ + public static boolean isASCIIAlphaUpper(char ch) { + return (ch >= 'A' && ch <= 'Z'); + } + + /** + *

Checks whether the character is ASCII 7 bit alphabetic lower case.

+ * + *
+     *   CharUtils.isASCIIAlphaLower('a')  = true
+     *   CharUtils.isASCIIAlphaLower('A')  = false
+     *   CharUtils.isASCIIAlphaLower('3')  = false
+     *   CharUtils.isASCIIAlphaLower('-')  = false
+     *   CharUtils.isASCIIAlphaLower('\n') = false
+     *   CharUtils.isASCIIAlphaLower('©') = false
+     * 
+ * + * @param ch the character to check + * @return true if between 97 and 122 inclusive + */ + public static boolean isASCIIAlphaLower(char ch) { + return (ch >= 'a' && ch <= 'z'); + } + + /** + *

Checks whether the character is ASCII 7 bit numeric.

+ * + *
+     *   CharUtils.isASCIINumeric('a')  = false
+     *   CharUtils.isASCIINumeric('A')  = false
+     *   CharUtils.isASCIINumeric('3')  = true
+     *   CharUtils.isASCIINumeric('-')  = false
+     *   CharUtils.isASCIINumeric('\n') = false
+     *   CharUtils.isASCIINumeric('©') = false
+     * 
+ * + * @param ch the character to check + * @return true if between 48 and 57 inclusive + */ + public static boolean isASCIINumeric(char ch) { + return (ch >= '0' && ch <= '9'); + } + + /** + *

Checks whether the character is ASCII 7 bit numeric.

+ * + *
+     *   CharUtils.isASCIIAlphanumeric('a')  = true
+     *   CharUtils.isASCIIAlphanumeric('A')  = true
+     *   CharUtils.isASCIIAlphanumeric('3')  = true
+     *   CharUtils.isASCIIAlphanumeric('-')  = false
+     *   CharUtils.isASCIIAlphanumeric('\n') = false
+     *   CharUtils.isASCIIAlphanumeric('©') = false
+     * 
+ * + * @param ch the character to check + * @return true if between 48 and 57 or 65 and 90 or 97 and 122 inclusive + */ + public static boolean isASCIIAlphanumeric(char ch) { + return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9'); + } + +} diff --git a/src/test/org/apache/commons/lang/CharUtilsTest.java b/src/test/org/apache/commons/lang/CharUtilsTest.java new file mode 100644 index 000000000..59f575a3f --- /dev/null +++ b/src/test/org/apache/commons/lang/CharUtilsTest.java @@ -0,0 +1,396 @@ +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2004 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowledgement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgement may appear in the software itself, + * if and wherever such third-party acknowledgements normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ +package org.apache.commons.lang; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Modifier; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import junit.textui.TestRunner; + +/** + * Unit tests {@link org.apache.commons.lang.CharUtils}. + * + * @author Stephen Colebourne + * @version $Id: CharUtilsTest.java,v 1.1 2004/01/31 13:00:07 scolebourne Exp $ + */ +public class CharUtilsTest extends TestCase { + + private static final Character CHARACTER_A = new Character('A'); + private static final Character CHARACTER_B = new Character('B'); + private static final char CHAR_COPY = '\u00a9'; + + public CharUtilsTest(String name) { + super(name); + } + + public static void main(String[] args) { + TestRunner.run(suite()); + } + + public static Test suite() { + TestSuite suite = new TestSuite(CharUtilsTest.class); + suite.setName("CharUtils Tests"); + return suite; + } + + protected void setUp() throws Exception { + super.setUp(); + } + + protected void tearDown() throws Exception { + super.tearDown(); + } + + //----------------------------------------------------------------------- + public void testConstructor() { + assertNotNull(new CharUtils()); + Constructor[] cons = CharUtils.class.getDeclaredConstructors(); + assertEquals(1, cons.length); + assertEquals(true, Modifier.isPublic(cons[0].getModifiers())); + assertEquals(true, Modifier.isPublic(BooleanUtils.class.getModifiers())); + assertEquals(false, Modifier.isFinal(BooleanUtils.class.getModifiers())); + } + + //----------------------------------------------------------------------- + public void testToCharacterObject_char() { + assertEquals(new Character('a'), CharUtils.toCharacterObject('a')); + assertSame(CharUtils.toCharacterObject('a'), CharUtils.toCharacterObject('a')); + + for (int i = 0; i < 128; i++) { + Character ch = CharUtils.toCharacterObject((char) i); + Character ch2 = CharUtils.toCharacterObject((char) i); + assertSame(ch, ch2); + assertEquals(i, ch.charValue()); + } + for (int i = 128; i < 196; i++) { + Character ch = CharUtils.toCharacterObject((char) i); + Character ch2 = CharUtils.toCharacterObject((char) i); + assertEquals(ch, ch2); + assertTrue(ch != ch2); + assertEquals(i, ch.charValue()); + assertEquals(i, ch2.charValue()); + } + } + + //----------------------------------------------------------------------- + public void testToCharacter_Character() { + assertEquals('A', CharUtils.toCharacter(CHARACTER_A)); + assertEquals('B', CharUtils.toCharacter(CHARACTER_B)); + try { + CharUtils.toCharacter((Character) null); + } catch (IllegalArgumentException ex) {} + } + + public void testToCharacter_Character_char() { + assertEquals('A', CharUtils.toCharacter(CHARACTER_A, 'X')); + assertEquals('B', CharUtils.toCharacter(CHARACTER_B, 'X')); + assertEquals('X', CharUtils.toCharacter((Character) null, 'X')); + } + + //----------------------------------------------------------------------- + public void testToCharacter_String() { + assertEquals('A', CharUtils.toCharacter("A")); + assertEquals('B', CharUtils.toCharacter("BA")); + try { + CharUtils.toCharacter((String) null); + } catch (IllegalArgumentException ex) {} + try { + CharUtils.toCharacter(""); + } catch (IllegalArgumentException ex) {} + } + + public void testToCharacter_String_char() { + assertEquals('A', CharUtils.toCharacter("A", 'X')); + assertEquals('B', CharUtils.toCharacter("BA", 'X')); + assertEquals('X', CharUtils.toCharacter("", 'X')); + assertEquals('X', CharUtils.toCharacter((String) null, 'X')); + } + + //----------------------------------------------------------------------- + public void testToInteger_char() { + assertEquals(0, CharUtils.toInteger('0')); + assertEquals(1, CharUtils.toInteger('1')); + assertEquals(2, CharUtils.toInteger('2')); + assertEquals(3, CharUtils.toInteger('3')); + assertEquals(4, CharUtils.toInteger('4')); + assertEquals(5, CharUtils.toInteger('5')); + assertEquals(6, CharUtils.toInteger('6')); + assertEquals(7, CharUtils.toInteger('7')); + assertEquals(8, CharUtils.toInteger('8')); + assertEquals(9, CharUtils.toInteger('9')); + try { + CharUtils.toInteger('a'); + } catch (IllegalArgumentException ex) {} + } + + public void testToInteger_char_int() { + assertEquals(0, CharUtils.toInteger('0', -1)); + assertEquals(3, CharUtils.toInteger('3', -1)); + assertEquals(-1, CharUtils.toInteger('a', -1)); + } + + //----------------------------------------------------------------------- + public void testToInteger_Character() { + assertEquals(0, CharUtils.toInteger(new Character('0'))); + assertEquals(3, CharUtils.toInteger(new Character('3'))); + try { + CharUtils.toInteger(null); + } catch (IllegalArgumentException ex) {} + try { + CharUtils.toInteger(CHARACTER_A); + } catch (IllegalArgumentException ex) {} + } + + public void testToInteger_Character_int() { + assertEquals(0, CharUtils.toInteger(new Character('0'), -1)); + assertEquals(3, CharUtils.toInteger(new Character('3'), -1)); + assertEquals(-1, CharUtils.toInteger(new Character('A'), -1)); + assertEquals(-1, CharUtils.toInteger(null, -1)); + } + + //----------------------------------------------------------------------- + public void testToString_char() { + assertEquals("a", CharUtils.toString('a')); + assertSame(CharUtils.toString('a'), CharUtils.toString('a')); + + for (int i = 0; i < 128; i++) { + String str = CharUtils.toString((char) i); + String str2 = CharUtils.toString((char) i); + assertSame(str, str2); + assertEquals(1, str.length()); + assertEquals(i, str.charAt(0)); + } + for (int i = 128; i < 196; i++) { + String str = CharUtils.toString((char) i); + String str2 = CharUtils.toString((char) i); + assertEquals(str, str2); + assertTrue(str != str2); + assertEquals(1, str.length()); + assertEquals(i, str.charAt(0)); + assertEquals(1, str2.length()); + assertEquals(i, str2.charAt(0)); + } + } + + public void testToString_Character() { + assertEquals(null, CharUtils.toString(null)); + assertEquals("A", CharUtils.toString(CHARACTER_A)); + assertSame(CharUtils.toString(CHARACTER_A), CharUtils.toString(CHARACTER_A)); + } + + //----------------------------------------------------------------------- + public void testToUnicodeEscaped_char() { + assertEquals("\\u0041", CharUtils.unicodeEscaped('A')); + + for (int i = 0; i < 196; i++) { + String str = CharUtils.unicodeEscaped((char) i); + assertEquals(6, str.length()); + int val = Integer.parseInt(str.substring(2), 16); + assertEquals(i, val); + } + } + + public void testToUnicodeEscaped_Character() { + assertEquals(null, CharUtils.unicodeEscaped(null)); + assertEquals("\\u0041", CharUtils.unicodeEscaped(CHARACTER_A)); + } + + //----------------------------------------------------------------------- + public void testIsASCII_char() { + assertEquals(true, CharUtils.isASCII('a')); + assertEquals(true, CharUtils.isASCII('A')); + assertEquals(true, CharUtils.isASCII('3')); + assertEquals(true, CharUtils.isASCII('-')); + assertEquals(true, CharUtils.isASCII('\n')); + assertEquals(false, CharUtils.isASCII(CHAR_COPY)); + + for (int i = 0; i < 128; i++) { + if (i < 128) { + assertEquals(true, CharUtils.isASCII((char) i)); + } else { + assertEquals(false, CharUtils.isASCII((char) i)); + } + } + } + + //----------------------------------------------------------------------- + public void testIsASCIIPrintable_char() { + assertEquals(true, CharUtils.isASCIIPrintable('a')); + assertEquals(true, CharUtils.isASCIIPrintable('A')); + assertEquals(true, CharUtils.isASCIIPrintable('3')); + assertEquals(true, CharUtils.isASCIIPrintable('-')); + assertEquals(false, CharUtils.isASCIIPrintable('\n')); + assertEquals(false, CharUtils.isASCII(CHAR_COPY)); + + for (int i = 0; i < 196; i++) { + if (i >= 32 && i <= 126) { + assertEquals(true, CharUtils.isASCIIPrintable((char) i)); + } else { + assertEquals(false, CharUtils.isASCIIPrintable((char) i)); + } + } + } + + //----------------------------------------------------------------------- + public void testIsASCIIControl_char() { + assertEquals(false, CharUtils.isASCIIControl('a')); + assertEquals(false, CharUtils.isASCIIControl('A')); + assertEquals(false, CharUtils.isASCIIControl('3')); + assertEquals(false, CharUtils.isASCIIControl('-')); + assertEquals(true, CharUtils.isASCIIControl('\n')); + assertEquals(false, CharUtils.isASCIIControl(CHAR_COPY)); + + for (int i = 0; i < 196; i++) { + if (i < 32 || i == 127) { + assertEquals(true, CharUtils.isASCIIControl((char) i)); + } else { + assertEquals(false, CharUtils.isASCIIControl((char) i)); + } + } + } + + //----------------------------------------------------------------------- + public void testIsASCIIAlpha_char() { + assertEquals(true, CharUtils.isASCIIAlpha('a')); + assertEquals(true, CharUtils.isASCIIAlpha('A')); + assertEquals(false, CharUtils.isASCIIAlpha('3')); + assertEquals(false, CharUtils.isASCIIAlpha('-')); + assertEquals(false, CharUtils.isASCIIAlpha('\n')); + assertEquals(false, CharUtils.isASCIIAlpha(CHAR_COPY)); + + for (int i = 0; i < 196; i++) { + if ((i >= 'A' && i <= 'Z') || (i >= 'a' && i <= 'z')) { + assertEquals(true, CharUtils.isASCIIAlpha((char) i)); + } else { + assertEquals(false, CharUtils.isASCIIAlpha((char) i)); + } + } + } + + //----------------------------------------------------------------------- + public void testIsASCIIAlphaUpper_char() { + assertEquals(false, CharUtils.isASCIIAlphaUpper('a')); + assertEquals(true, CharUtils.isASCIIAlphaUpper('A')); + assertEquals(false, CharUtils.isASCIIAlphaUpper('3')); + assertEquals(false, CharUtils.isASCIIAlphaUpper('-')); + assertEquals(false, CharUtils.isASCIIAlphaUpper('\n')); + assertEquals(false, CharUtils.isASCIIAlphaUpper(CHAR_COPY)); + + for (int i = 0; i < 196; i++) { + if (i >= 'A' && i <= 'Z') { + assertEquals(true, CharUtils.isASCIIAlphaUpper((char) i)); + } else { + assertEquals(false, CharUtils.isASCIIAlphaUpper((char) i)); + } + } + } + + //----------------------------------------------------------------------- + public void testIsASCIIAlphaLower_char() { + assertEquals(true, CharUtils.isASCIIAlphaLower('a')); + assertEquals(false, CharUtils.isASCIIAlphaLower('A')); + assertEquals(false, CharUtils.isASCIIAlphaLower('3')); + assertEquals(false, CharUtils.isASCIIAlphaLower('-')); + assertEquals(false, CharUtils.isASCIIAlphaLower('\n')); + assertEquals(false, CharUtils.isASCIIAlphaLower(CHAR_COPY)); + + for (int i = 0; i < 196; i++) { + if (i >= 'a' && i <= 'z') { + assertEquals(true, CharUtils.isASCIIAlphaLower((char) i)); + } else { + assertEquals(false, CharUtils.isASCIIAlphaLower((char) i)); + } + } + } + + //----------------------------------------------------------------------- + public void testIsASCIINumeric_char() { + assertEquals(false, CharUtils.isASCIINumeric('a')); + assertEquals(false, CharUtils.isASCIINumeric('A')); + assertEquals(true, CharUtils.isASCIINumeric('3')); + assertEquals(false, CharUtils.isASCIINumeric('-')); + assertEquals(false, CharUtils.isASCIINumeric('\n')); + assertEquals(false, CharUtils.isASCIINumeric(CHAR_COPY)); + + for (int i = 0; i < 196; i++) { + if (i >= '0' && i <= '9') { + assertEquals(true, CharUtils.isASCIINumeric((char) i)); + } else { + assertEquals(false, CharUtils.isASCIINumeric((char) i)); + } + } + } + + //----------------------------------------------------------------------- + public void testIsASCIIAlphanumeric_char() { + assertEquals(true, CharUtils.isASCIIAlphanumeric('a')); + assertEquals(true, CharUtils.isASCIIAlphanumeric('A')); + assertEquals(true, CharUtils.isASCIIAlphanumeric('3')); + assertEquals(false, CharUtils.isASCIIAlphanumeric('-')); + assertEquals(false, CharUtils.isASCIIAlphanumeric('\n')); + assertEquals(false, CharUtils.isASCIIAlphanumeric(CHAR_COPY)); + + for (int i = 0; i < 196; i++) { + if ((i >= 'A' && i <= 'Z') || (i >= 'a' && i <= 'z') || (i >= '0' && i <= '9')) { + assertEquals(true, CharUtils.isASCIIAlphanumeric((char) i)); + } else { + assertEquals(false, CharUtils.isASCIIAlphanumeric((char) i)); + } + } + } + +} diff --git a/src/test/org/apache/commons/lang/LangTestSuite.java b/src/test/org/apache/commons/lang/LangTestSuite.java index e60c82f09..9ab1efc7e 100644 --- a/src/test/org/apache/commons/lang/LangTestSuite.java +++ b/src/test/org/apache/commons/lang/LangTestSuite.java @@ -1,7 +1,7 @@ /* ==================================================================== * The Apache Software License, Version 1.1 * - * Copyright (c) 2002-2003 The Apache Software Foundation. All rights + * Copyright (c) 2002-2004 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -64,7 +64,7 @@ * @author Stephen Colebourne * @author Ringo De Smet * @author Matthew Hawthorne - * @version $Id: LangTestSuite.java,v 1.23 2003/08/19 02:32:16 bayard Exp $ + * @version $Id: LangTestSuite.java,v 1.24 2004/01/31 13:00:07 scolebourne Exp $ */ public class LangTestSuite extends TestCase { @@ -94,6 +94,7 @@ public static Test suite() { suite.addTest(CharRangeTest.suite()); suite.addTest(CharSetTest.suite()); suite.addTest(CharSetUtilsTest.suite()); + suite.addTest(CharUtilsTest.suite()); suite.addTest(ClassUtilsTest.suite()); suite.addTest(EntitiesTest.suite()); suite.addTest(IllegalClassExceptionTest.suite());