1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */ 
17  
18  package org.apache.commons.beanutils.converters;
19  
20  import java.math.BigDecimal;
21  
22  import junit.framework.TestSuite;
23  
24  import org.apache.commons.beanutils.Converter;
25  
26  
27  /***
28   * Test Case for the DoubleConverter class.
29   *
30   * @author Rodney Waldhoff
31   * @version $Revision: 471348 $ $Date: 2006-11-05 02:59:26 +0000 (Sun, 05 Nov 2006) $
32   */
33  
34  public class BigDecimalConverterTestCase extends NumberConverterTestBase {
35  
36      private Converter converter = null;
37  
38      // ------------------------------------------------------------------------
39  
40      public BigDecimalConverterTestCase(String name) {
41          super(name);
42      }
43      
44      // ------------------------------------------------------------------------
45  
46      public void setUp() throws Exception {
47          converter = makeConverter();
48          numbers[0] = new BigDecimal("-12");
49          numbers[1] = new BigDecimal("13");
50          numbers[2] = new BigDecimal("-22");
51          numbers[3] = new BigDecimal("23");
52      }
53  
54      public static TestSuite suite() {
55          return new TestSuite(BigDecimalConverterTestCase.class);        
56      }
57  
58      public void tearDown() throws Exception {
59          converter = null;
60      }
61  
62      // ------------------------------------------------------------------------
63      
64      protected NumberConverter makeConverter() {
65          return new BigDecimalConverter();
66      }
67      
68      protected NumberConverter makeConverter(Object defaultValue) {
69          return new BigDecimalConverter(defaultValue);
70      }
71      
72      protected Class getExpectedType() {
73          return BigDecimal.class;
74      }
75  
76      // ------------------------------------------------------------------------
77  
78      public void testSimpleConversion() throws Exception {
79          String[] message= { 
80              "from String",
81              "from String",
82              "from String",
83              "from String",
84              "from String",
85              "from Byte",
86              "from Short",
87              "from Integer",
88              "from Long",
89              "from Float",
90              "from Double"
91          };
92          
93          Object[] input = { 
94              "-17.2",
95              "-1.1",
96              "0.0",
97              "1.1",
98              "17.2",
99              new Byte((byte)7),
100             new Short((short)8),
101             new Integer(9),
102             new Long(10),
103             new Float("11.1"),
104             new Double("12.2")
105         };
106         
107         BigDecimal[] expected = { 
108             new BigDecimal("-17.2"),
109             new BigDecimal("-1.1"),
110             new BigDecimal("0.0"),
111             new BigDecimal("1.1"),
112             new BigDecimal("17.2"),
113             new BigDecimal("7"),
114             new BigDecimal("8"),
115             new BigDecimal("9"),
116             new BigDecimal("10"),
117             new BigDecimal("11.1"),
118             new BigDecimal("12.2")
119         };
120         
121         for(int i=0;i<expected.length;i++) {
122             assertEquals(
123                 message[i] + " to BigDecimal",
124                 expected[i],
125                 converter.convert(BigDecimal.class,input[i]));
126             assertEquals(
127                 message[i] + " to null type",
128                 expected[i],
129                 converter.convert(null,input[i]));
130         }
131     }
132     
133 }
134