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    *      https://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.beanutils2.converters;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  
22  import java.math.BigDecimal;
23  
24  import org.apache.commons.beanutils2.Converter;
25  import org.junit.jupiter.api.AfterEach;
26  import org.junit.jupiter.api.BeforeEach;
27  import org.junit.jupiter.api.Test;
28  
29  /**
30   * Test Case for the DoubleConverter class.
31   */
32  public class BigDecimalConverterTest extends AbstractNumberConverterTest<BigDecimal> {
33  
34      /**
35       * A class derived from {@code BigDecimal} used for testing whether derived number classes are handled correctly.
36       */
37      private static class ExtendingBigDecimal extends BigDecimal {
38          private static final long serialVersionUID = 1L;
39  
40          private ExtendingBigDecimal(final String val) {
41              super(val);
42          }
43      }
44  
45      private Converter<BigDecimal> converter;
46  
47      @Override
48      protected Class<BigDecimal> getExpectedType() {
49          return BigDecimal.class;
50      }
51  
52      @Override
53      protected BigDecimalConverter makeConverter() {
54          return new BigDecimalConverter();
55      }
56  
57      @Override
58      protected BigDecimalConverter makeConverter(final BigDecimal defaultValue) {
59          return new BigDecimalConverter(defaultValue);
60      }
61  
62      @BeforeEach
63      public void setUp() throws Exception {
64          converter = makeConverter();
65          numbers[0] = new BigDecimal("-12");
66          numbers[1] = new BigDecimal("13");
67          numbers[2] = new BigDecimal("-22");
68          numbers[3] = new BigDecimal("23");
69      }
70  
71      @AfterEach
72      public void tearDown() throws Exception {
73          converter = null;
74      }
75  
76      @Test
77      public void testSimpleConversion() throws Exception {
78          final String[] message = { "from String", "from String", "from String", "from String", "from String", "from Byte", "from Short", "from Integer",
79                  "from Long", "from Float", "from Double", "from BigDecimal", "from BigDecimal extension" };
80  
81          final Object[] input = { "-17.2", "-1.1", "0.0", "1.1", "17.2", Byte.valueOf((byte) 7), Short.valueOf((short) 8), Integer.valueOf(9), Long.valueOf(10),
82                  Float.valueOf("11.1"), Double.valueOf("12.2"), new BigDecimal("3200.11"), new ExtendingBigDecimal("3200.11") };
83  
84          final BigDecimal[] expected = { new BigDecimal("-17.2"), new BigDecimal("-1.1"), new BigDecimal("0.0"), new BigDecimal("1.1"), new BigDecimal("17.2"),
85                  new BigDecimal("7"), new BigDecimal("8"), new BigDecimal("9"), new BigDecimal("10"), new BigDecimal("11.1"), new BigDecimal("12.2"),
86                  new BigDecimal("3200.11"), new BigDecimal("3200.11") };
87  
88          for (int i = 0; i < expected.length; i++) {
89              assertEquals(expected[i], converter.convert(BigDecimal.class, input[i]), message[i] + " to BigDecimal");
90              assertEquals(expected[i], converter.convert(null, input[i]), message[i] + " to null type");
91          }
92      }
93  }