View Javadoc
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  import org.apache.commons.beanutils.Converter;
24  
25  
26  /**
27   * Test Case for the DoubleConverter class.
28   *
29   * @version $Id$
30   */
31  
32  public class BigDecimalConverterTestCase extends NumberConverterTestBase {
33  
34      private Converter converter = null;
35  
36      // ------------------------------------------------------------------------
37  
38      public BigDecimalConverterTestCase(final String name) {
39          super(name);
40      }
41  
42      // ------------------------------------------------------------------------
43  
44      @Override
45      public void setUp() throws Exception {
46          converter = makeConverter();
47          numbers[0] = new BigDecimal("-12");
48          numbers[1] = new BigDecimal("13");
49          numbers[2] = new BigDecimal("-22");
50          numbers[3] = new BigDecimal("23");
51      }
52  
53      public static TestSuite suite() {
54          return new TestSuite(BigDecimalConverterTestCase.class);
55      }
56  
57      @Override
58      public void tearDown() throws Exception {
59          converter = null;
60      }
61  
62      // ------------------------------------------------------------------------
63  
64      @Override
65      protected NumberConverter makeConverter() {
66          return new BigDecimalConverter();
67      }
68  
69      @Override
70      protected NumberConverter makeConverter(final Object defaultValue) {
71          return new BigDecimalConverter(defaultValue);
72      }
73  
74      @Override
75      protected Class<?> getExpectedType() {
76          return BigDecimal.class;
77      }
78  
79      // ------------------------------------------------------------------------
80  
81      public void testSimpleConversion() throws Exception {
82          final String[] message= {
83              "from String",
84              "from String",
85              "from String",
86              "from String",
87              "from String",
88              "from Byte",
89              "from Short",
90              "from Integer",
91              "from Long",
92              "from Float",
93              "from Double",
94              "from BigDecimal",
95              "from BigDecimal extension"
96          };
97  
98          final Object[] input = {
99              "-17.2",
100             "-1.1",
101             "0.0",
102             "1.1",
103             "17.2",
104             new Byte((byte)7),
105             new Short((short)8),
106             new Integer(9),
107             new Long(10),
108             new Float("11.1"),
109             new Double("12.2"),
110             new BigDecimal("3200.11"),
111             new ExtendingBigDecimal("3200.11")
112         };
113 
114         final BigDecimal[] expected = {
115             new BigDecimal("-17.2"),
116             new BigDecimal("-1.1"),
117             new BigDecimal("0.0"),
118             new BigDecimal("1.1"),
119             new BigDecimal("17.2"),
120             new BigDecimal("7"),
121             new BigDecimal("8"),
122             new BigDecimal("9"),
123             new BigDecimal("10"),
124             new BigDecimal("11.1"),
125             new BigDecimal("12.2"),
126             new BigDecimal("3200.11"),
127             new BigDecimal("3200.11")
128         };
129 
130         for(int i=0;i<expected.length;i++) {
131             assertEquals(
132                 message[i] + " to BigDecimal",
133                 expected[i],
134                 converter.convert(BigDecimal.class,input[i]));
135             assertEquals(
136                 message[i] + " to null type",
137                 expected[i],
138                 converter.convert(null,input[i]));
139         }
140     }
141 
142     /**
143      * A class derived from {@code BigDecimal} used for testing whether
144      * derived number classes are handled correctly.
145      */
146     private class ExtendingBigDecimal extends BigDecimal {
147         private ExtendingBigDecimal(final String val) {
148             super(val);
149         }
150     }
151 }
152