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 org.apache.commons.beanutils2.Converter;
23  import org.junit.jupiter.api.AfterEach;
24  import org.junit.jupiter.api.BeforeEach;
25  import org.junit.jupiter.api.Test;
26  
27  /**
28   * Test Case for the DoubleConverter class.
29   */
30  public class DoubleConverterTest extends AbstractNumberConverterTest<Double> {
31  
32      private Converter<Double> converter;
33  
34      @Override
35      protected Class<Double> getExpectedType() {
36          return Double.class;
37      }
38  
39      @Override
40      protected DoubleConverter makeConverter() {
41          return new DoubleConverter();
42      }
43  
44      @Override
45      protected DoubleConverter makeConverter(final Double defaultValue) {
46          return new DoubleConverter(defaultValue);
47      }
48  
49      @BeforeEach
50      public void setUp() throws Exception {
51          converter = makeConverter();
52          numbers[0] = Double.valueOf("-12");
53          numbers[1] = Double.valueOf("13");
54          numbers[2] = Double.valueOf("-22");
55          numbers[3] = Double.valueOf("23");
56      }
57  
58      @AfterEach
59      public void tearDown() throws Exception {
60          converter = null;
61      }
62  
63      @Test
64      public void testSimpleConversion() {
65          final String[] message = { "from String", "from String", "from String", "from String", "from String", "from String", "from String", "from Byte",
66                  "from Short", "from Integer", "from Long", "from Float", "from Double" };
67  
68          final Object[] input = { String.valueOf(Double.MIN_VALUE), "-17.2", "-1.1", "0.0", "1.1", "17.2", String.valueOf(Double.MAX_VALUE),
69                  Byte.valueOf((byte) 7), Short.valueOf((short) 8), Integer.valueOf(9), Long.valueOf(10), Float.valueOf((float) 11.1), Double.valueOf(12.2) };
70  
71          final Double[] expected = { Double.valueOf(Double.MIN_VALUE), Double.valueOf(-17.2), Double.valueOf(-1.1), Double.valueOf(0.0), Double.valueOf(1.1),
72                  Double.valueOf(17.2), Double.valueOf(Double.MAX_VALUE), Double.valueOf(7), Double.valueOf(8), Double.valueOf(9), Double.valueOf(10),
73                  Double.valueOf(11.1), Double.valueOf(12.2) };
74  
75          for (int i = 0; i < expected.length; i++) {
76              assertEquals(expected[i].doubleValue(), converter.convert(Double.class, input[i]).doubleValue(), 0.00001D, message[i] + " to Double");
77              assertEquals(expected[i].doubleValue(), converter.convert(Double.TYPE, input[i]).doubleValue(), 0.00001D, message[i] + " to double");
78              assertEquals(expected[i].doubleValue(), converter.convert((Class<Double>) null, input[i]).doubleValue(), 0.00001D, message[i] + " to null type");
79          }
80      }
81  
82  }