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:
018: package org.apache.commons.beanutils.converters;
019:
020: import java.math.BigDecimal;
021:
022: import junit.framework.TestSuite;
023:
024: import org.apache.commons.beanutils.Converter;
025:
026: /**
027: * Test Case for the DoubleConverter class.
028: *
029: * @author Rodney Waldhoff
030: * @version $Revision: 471348 $ $Date: 2006-11-05 02:59:26 +0000 (Sun, 05 Nov 2006) $
031: */
032:
033: public class BigDecimalConverterTestCase extends
034: NumberConverterTestBase {
035:
036: private Converter converter = null;
037:
038: // ------------------------------------------------------------------------
039:
040: public BigDecimalConverterTestCase(String name) {
041: super (name);
042: }
043:
044: // ------------------------------------------------------------------------
045:
046: public void setUp() throws Exception {
047: converter = makeConverter();
048: numbers[0] = new BigDecimal("-12");
049: numbers[1] = new BigDecimal("13");
050: numbers[2] = new BigDecimal("-22");
051: numbers[3] = new BigDecimal("23");
052: }
053:
054: public static TestSuite suite() {
055: return new TestSuite(BigDecimalConverterTestCase.class);
056: }
057:
058: public void tearDown() throws Exception {
059: converter = null;
060: }
061:
062: // ------------------------------------------------------------------------
063:
064: protected NumberConverter makeConverter() {
065: return new BigDecimalConverter();
066: }
067:
068: protected NumberConverter makeConverter(Object defaultValue) {
069: return new BigDecimalConverter(defaultValue);
070: }
071:
072: protected Class getExpectedType() {
073: return BigDecimal.class;
074: }
075:
076: // ------------------------------------------------------------------------
077:
078: public void testSimpleConversion() throws Exception {
079: String[] message = { "from String", "from String",
080: "from String", "from String", "from String",
081: "from Byte", "from Short", "from Integer", "from Long",
082: "from Float", "from Double" };
083:
084: Object[] input = { "-17.2", "-1.1", "0.0", "1.1", "17.2",
085: new Byte((byte) 7), new Short((short) 8),
086: new Integer(9), new Long(10), new Float("11.1"),
087: new Double("12.2") };
088:
089: BigDecimal[] expected = { new BigDecimal("-17.2"),
090: new BigDecimal("-1.1"), new BigDecimal("0.0"),
091: new BigDecimal("1.1"), new BigDecimal("17.2"),
092: new BigDecimal("7"), new BigDecimal("8"),
093: new BigDecimal("9"), new BigDecimal("10"),
094: new BigDecimal("11.1"), new BigDecimal("12.2") };
095:
096: for (int i = 0; i < expected.length; i++) {
097: assertEquals(message[i] + " to BigDecimal", expected[i],
098: converter.convert(BigDecimal.class, input[i]));
099: assertEquals(message[i] + " to null type", expected[i],
100: converter.convert(null, input[i]));
101: }
102: }
103:
104: }
|