📄 localeconvertutilstestcase.java
字号:
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.locale;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;
import junit.framework.TestCase;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.commons.beanutils.ConversionException;
/**
* <p>
* Test Case for the LocaleConvertUtils class.
* See unimplemented functionality of the convert utils in the method begining with fixme
* </p>
*
* @author Michael Szlapa
* @author Paul Hamamnt & Rune Johannesen (pairing) - patches.
* @version $Revision: 628158 $ $Date: 2008-02-15 20:14:20 +0000 (Fri, 15 Feb 2008) $
*/
public class LocaleConvertUtilsTestCase extends TestCase {
// ---------------------------------------------------- Instance Variables
private char m_decimalSeparator;
// ---------------------------------------------------------- Constructors
/**
* Construct a new instance of this test case.
*
* @param name Name of the test case
*/
public LocaleConvertUtilsTestCase(String name) {
super(name);
}
// -------------------------------------------------- Overall Test Methods
/**
* Set up instance variables required by this test case.
*/
public void setUp() {
LocaleConvertUtils.deregister();
NumberFormat nf = DecimalFormat.getNumberInstance();
String result = nf.format(1.1);
// could be commas instead of stops in Europe.
m_decimalSeparator = result.charAt(1);
}
/**
* Return the tests included in this test suite.
*/
public static Test suite() {
return (new TestSuite(LocaleConvertUtilsTestCase.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 fixmetestNegativeIntegerArray() {
fail("Array conversions not implemented yet.");
Object value = null;
int intArray[] = new int[0];
value = LocaleConvertUtils.convert((String) null, intArray.getClass());
checkIntegerArray(value, intArray);
value = LocaleConvertUtils.convert("a", intArray.getClass());
checkIntegerArray(value, intArray);
value = LocaleConvertUtils.convert("{ a }", intArray.getClass());
checkIntegerArray(value, intArray);
value = LocaleConvertUtils.convert("1a3", intArray.getClass());
checkIntegerArray(value, intArray);
value = LocaleConvertUtils.convert("{ 1a3 }", intArray.getClass());
checkIntegerArray(value, intArray);
value = LocaleConvertUtils.convert("0,1a3", intArray.getClass());
checkIntegerArray(value, intArray);
value = LocaleConvertUtils.convert("{ 0, 1a3 }", intArray.getClass());
checkIntegerArray(value, intArray);
}
/**
* Negative scalar conversion tests. These rely on the standard
* default value conversions in LocaleConvertUtils.
*/
public void testNegativeScalar() {
Object value = null;
/* fixme Boolean converters not implemented at this point
value = LocaleConvertUtils.convert("foo", Boolean.TYPE);
...
value = LocaleConvertUtils.convert("foo", Boolean.class);
...
*/
try {
value = LocaleConvertUtils.convert("foo", Byte.TYPE);
fail("Should have thrown conversion exception (1)");
} catch (ConversionException e) {
// Expected result
}
try {
value = LocaleConvertUtils.convert("foo", Byte.class);
fail("Should have thrown conversion exception (2)");
} catch (ConversionException e) {
// Expected result
}
/* fixme - not implemented
try {
value = LocaleConvertUtils.convert("org.apache.commons.beanutils.Undefined", Class.class);
fail("Should have thrown conversion exception");
} catch (ConversionException e) {
; // Expected result
}
*/
try {
value = LocaleConvertUtils.convert("foo", Double.TYPE);
fail("Should have thrown conversion exception (3)");
} catch (ConversionException e) {
// Expected result
}
try {
value = LocaleConvertUtils.convert("foo", Double.class);
fail("Should have thrown conversion exception (4)");
} catch (ConversionException e) {
// Expected result
}
try {
value = LocaleConvertUtils.convert("foo", Float.TYPE);
fail("Should have thrown conversion exception (5)");
} catch (ConversionException e) {
// Expected result
}
try {
value = LocaleConvertUtils.convert("foo", Float.class);
fail("Should have thrown conversion exception (6)");
} catch (ConversionException e) {
// Expected result
}
try {
value = LocaleConvertUtils.convert("foo", Integer.TYPE);
fail("Should have thrown conversion exception (7)");
} catch (ConversionException e) {
// Expected result
}
try {
value = LocaleConvertUtils.convert("foo", Integer.class);
fail("Should have thrown conversion exception (8)");
} catch (ConversionException e) {
// Expected result
}
try {
value = LocaleConvertUtils.convert("foo", Byte.TYPE);
fail("Should have thrown conversion exception (9)");
} catch (ConversionException e) {
// Expected result
}
try {
value = LocaleConvertUtils.convert("foo", Long.class);
fail("Should have thrown conversion exception (10)");
} catch (ConversionException e) {
// Expected result
}
try {
value = LocaleConvertUtils.convert("foo", Short.TYPE);
fail("Should have thrown conversion exception (11)");
} catch (ConversionException e) {
// Expected result
}
try {
value = LocaleConvertUtils.convert("foo", Short.class);
fail("Should have thrown conversion exception (12)");
} catch (ConversionException e) {
// Expected result
}
}
/**
* Negative String to String array tests.
*/
public void fixmetestNegativeStringArray() {
fail("Array conversions not implemented yet.");
Object value = null;
String stringArray[] = new String[0];
value = LocaleConvertUtils.convert((String) null, stringArray.getClass());
checkStringArray(value, stringArray);
}
/**
* Test conversion of object to string for arrays - .
*/
public void fixmetestObjectToStringArray() {
fail("Array conversions not implemented yet.");
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,
LocaleConvertUtils.convert(intArray0));
assertEquals("intArray1", "123",
LocaleConvertUtils.convert(intArray1));
assertEquals("intArray2", "123",
LocaleConvertUtils.convert(intArray2));
assertEquals("stringArray0", null,
LocaleConvertUtils.convert(stringArray0));
assertEquals("stringArray1", "abc",
LocaleConvertUtils.convert(stringArray1));
assertEquals("stringArray2", "abc",
LocaleConvertUtils.convert(stringArray2));
}
/**
* Test conversion of object to string for scalars.
*/
public void testObjectToStringScalar() {
assertEquals("Boolean->String", "false",
LocaleConvertUtils.convert(Boolean.FALSE));
assertEquals("Boolean->String", "true",
LocaleConvertUtils.convert(Boolean.TRUE));
assertEquals("Byte->String", "123",
LocaleConvertUtils.convert(new Byte((byte) 123)));
assertEquals("Character->String", "a",
LocaleConvertUtils.convert(new Character('a')));
assertEquals("Double->String", "123" + m_decimalSeparator + "4",
LocaleConvertUtils.convert(new Double(123.4)));
assertEquals("Float->String", "123" + m_decimalSeparator + "4",
LocaleConvertUtils.convert(new Float((float) 123.4)));
assertEquals("Integer->String", "123",
LocaleConvertUtils.convert(new Integer(123)));
assertEquals("Long->String", "123",
LocaleConvertUtils.convert(new Long(123)));
assertEquals("Short->String", "123",
LocaleConvertUtils.convert(new Short((short) 123)));
assertEquals("String->String", "abc",
LocaleConvertUtils.convert("abc"));
assertEquals("String->String null", null,
LocaleConvertUtils.convert(null));
}
/**
* Positive array conversion tests.
*/
public void fixmetestPositiveArray() {
fail("Array conversions not implemented yet.");
String values1[] = {"10", "20", "30"};
Object value = LocaleConvertUtils.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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -