⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 convertutilstestcase.java

📁 apache beanutils开源项目源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * Copyright 2001-2004 The Apache Software Foundation. *  * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *  *      http://www.apache.org/licenses/LICENSE-2.0 *  * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.commons.beanutils;import java.lang.reflect.InvocationTargetException;import java.sql.Date;import java.sql.Time;import java.sql.Timestamp;import java.util.HashMap;import junit.framework.TestCase;import junit.framework.Test;import junit.framework.TestSuite;import org.apache.commons.beanutils.converters.BooleanConverter;/** * <p> *  Test Case for the ConvertUtils class. * </p> * * @author Craig R. McClanahan * @version $Revision: 1.10 $ $Date: 2004/02/28 13:18:36 $ */public class ConvertUtilsTestCase extends TestCase {    // ---------------------------------------------------- Instance Variables    // ---------------------------------------------------------- Constructors    /**     * Construct a new instance of this test case.     *     * @param name Name of the test case     */    public ConvertUtilsTestCase(String name) {        super(name);    }    // -------------------------------------------------- Overall Test Methods    /**     * Set up instance variables required by this test case.     */    public void setUp() {        ConvertUtils.deregister();    }    /**     * Return the tests included in this test suite.     */    public static Test suite() {        return (new TestSuite(ConvertUtilsTestCase.class));    }    /**     * Tear down instance variables required by this test case.     */    public void tearDown() {        ;    // No action required    }    // ------------------------------------------------ Individual Test Methods    /**     * Negative String to primitive integer array tests.     */    public void testNegativeIntegerArray() {        Object value = null;        int intArray[] = new int[0];        value = ConvertUtils.convert((String) null, intArray.getClass());        checkIntegerArray(value, intArray);        value = ConvertUtils.convert("a", intArray.getClass());        checkIntegerArray(value, intArray);        value = ConvertUtils.convert("{ a }", intArray.getClass());        checkIntegerArray(value, intArray);        value = ConvertUtils.convert("1a3", intArray.getClass());        checkIntegerArray(value, intArray);        value = ConvertUtils.convert("{ 1a3 }", intArray.getClass());        checkIntegerArray(value, intArray);        value = ConvertUtils.convert("0,1a3", intArray.getClass());        checkIntegerArray(value, intArray);        value = ConvertUtils.convert("{ 0, 1a3 }", intArray.getClass());        checkIntegerArray(value, intArray);    }    /**     * Negative scalar conversion tests.  These rely on the standard     * default value conversions in ConvertUtils.     */    public void testNegativeScalar() {        Object value = null;        value = ConvertUtils.convert("foo", Boolean.TYPE);        assertTrue(value instanceof Boolean);        assertEquals(((Boolean) value).booleanValue(), false);        value = ConvertUtils.convert("foo", Boolean.class);        assertTrue(value instanceof Boolean);        assertEquals(((Boolean) value).booleanValue(), false);        value = ConvertUtils.convert("foo", Byte.TYPE);        assertTrue(value instanceof Byte);        assertEquals(((Byte) value).byteValue(), (byte) 0);        value = ConvertUtils.convert("foo", Byte.class);        assertTrue(value instanceof Byte);        assertEquals(((Byte) value).byteValue(), (byte) 0);        try {            value = ConvertUtils.convert                ("org.apache.commons.beanutils.Undefined", Class.class);            fail("Should have thrown conversion exception");        } catch (ConversionException e) {            ; // Expected result        }        value = ConvertUtils.convert("foo", Double.TYPE);        assertTrue(value instanceof Double);        assertEquals(((Double) value).doubleValue(), (double) 0.0,                     (double) 0.005);        value = ConvertUtils.convert("foo", Double.class);        assertTrue(value instanceof Double);        assertEquals(((Double) value).doubleValue(), (double) 0.0,                     (double) 0.005);        value = ConvertUtils.convert("foo", Float.TYPE);        assertTrue(value instanceof Float);        assertEquals(((Float) value).floatValue(), (float) 0.0,                     (float) 0.005);        value = ConvertUtils.convert("foo", Float.class);        assertTrue(value instanceof Float);        assertEquals(((Float) value).floatValue(), (float) 0.0,                     (float) 0.005);        value = ConvertUtils.convert("foo", Integer.TYPE);        assertTrue(value instanceof Integer);        assertEquals(((Integer) value).intValue(), (int) 0);        value = ConvertUtils.convert("foo", Integer.class);        assertTrue(value instanceof Integer);        assertEquals(((Integer) value).intValue(), (int) 0);        value = ConvertUtils.convert("foo", Byte.TYPE);        assertTrue(value instanceof Byte);        assertEquals(((Byte) value).byteValue(), (byte) 0);        value = ConvertUtils.convert("foo", Long.class);        assertTrue(value instanceof Long);        assertEquals(((Long) value).longValue(), (long) 0);        value = ConvertUtils.convert("foo", Short.TYPE);        assertTrue(value instanceof Short);        assertEquals(((Short) value).shortValue(), (short) 0);        value = ConvertUtils.convert("foo", Short.class);        assertTrue(value instanceof Short);        assertEquals(((Short) value).shortValue(), (short) 0);    }    /**     * Negative String to String array tests.     */    public void testNegativeStringArray() {        Object value = null;        String stringArray[] = new String[0];        value = ConvertUtils.convert((String) null, stringArray.getClass());        checkStringArray(value, stringArray);    }    /**     * Test conversion of object to string for arrays.     */    public void testObjectToStringArray() {        int intArray0[] = new int[0];        int intArray1[] = { 123 };        int intArray2[] = { 123, 456 };        String stringArray0[] = new String[0];        String stringArray1[] = { "abc" };        String stringArray2[] = { "abc", "def" };        assertEquals("intArray0", null,                     ConvertUtils.convert(intArray0));        assertEquals("intArray1", "123",                     ConvertUtils.convert(intArray1));        assertEquals("intArray2", "123",                     ConvertUtils.convert(intArray2));        assertEquals("stringArray0", null,                     ConvertUtils.convert(stringArray0));        assertEquals("stringArray1", "abc",                     ConvertUtils.convert(stringArray1));        assertEquals("stringArray2", "abc",                     ConvertUtils.convert(stringArray2));    }    /**     * Test conversion of object to string for scalars.     */    public void testObjectToStringScalar() {        assertEquals("Boolean->String", "false",                     ConvertUtils.convert(Boolean.FALSE));        assertEquals("Boolean->String", "true",                     ConvertUtils.convert(Boolean.TRUE));        assertEquals("Byte->String", "123",                     ConvertUtils.convert(new Byte((byte) 123)));        assertEquals("Character->String", "a",                     ConvertUtils.convert(new Character('a')));        assertEquals("Double->String", "123.0",                     ConvertUtils.convert(new Double((double) 123.0)));        assertEquals("Float->String", "123.0",                     ConvertUtils.convert(new Float((float) 123.0)));        assertEquals("Integer->String", "123",                     ConvertUtils.convert(new Integer((int) 123)));        assertEquals("Long->String", "123",                     ConvertUtils.convert(new Long((long) 123)));        assertEquals("Short->String", "123",                     ConvertUtils.convert(new Short((short) 123)));        assertEquals("String->String", "abc",                     ConvertUtils.convert("abc"));        assertEquals("String->String null", null,                     ConvertUtils.convert(null));    }    /**     * Positive array conversion tests.     */    public void testPositiveArray() {        String values1[] = { "10", "20", "30" };        Object value = ConvertUtils.convert(values1, Integer.TYPE);        int shape[] = new int[0];        assertEquals(shape.getClass(), value.getClass());        int results1[] = (int[]) value;        assertEquals(results1[0], 10);        assertEquals(results1[1], 20);        assertEquals(results1[2], 30);        String values2[] = { "100", "200", "300" };        value = ConvertUtils.convert(values2, shape.getClass());        assertEquals(shape.getClass(), value.getClass());        int results2[] = (int[]) value;        assertEquals(results2[0], 100);        assertEquals(results2[1], 200);        assertEquals(results2[2], 300);    }    /**     * Positive String to primitive integer array tests.     */    public void testPositiveIntegerArray() {        Object value = null;        int intArray[] = new int[0];        int intArray1[] = new int[] { 0 };        int intArray2[] = new int[] { 0, 10 };        value = ConvertUtils.convert("{  }", intArray.getClass());        checkIntegerArray(value, intArray);        value = ConvertUtils.convert("0", intArray.getClass());        checkIntegerArray(value, intArray1);        value = ConvertUtils.convert(" 0 ", intArray.getClass());        checkIntegerArray(value, intArray1);        value = ConvertUtils.convert("{ 0 }", intArray.getClass());        checkIntegerArray(value, intArray1);        value = ConvertUtils.convert("0,10", intArray.getClass());        checkIntegerArray(value, intArray2);        value = ConvertUtils.convert("0 10", intArray.getClass());        checkIntegerArray(value, intArray2);        value = ConvertUtils.convert("{0,10}", intArray.getClass());        checkIntegerArray(value, intArray2);        value = ConvertUtils.convert("{0 10}", intArray.getClass());        checkIntegerArray(value, intArray2);        value = ConvertUtils.convert("{ 0, 10 }", intArray.getClass());        checkIntegerArray(value, intArray2);        value = ConvertUtils.convert("{ 0 10 }", intArray.getClass());        checkIntegerArray(value, intArray2);    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -