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  import static org.junit.jupiter.api.Assertions.assertThrows;
22  
23  import org.apache.commons.beanutils2.ConversionException;
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 FloatConverter class.
31   */
32  public class FloatConverterTest extends AbstractNumberConverterTest<Float> {
33  
34      private Converter<Float> converter;
35  
36      @Override
37      protected Class<Float> getExpectedType() {
38          return Float.class;
39      }
40  
41      @Override
42      protected FloatConverter makeConverter() {
43          return new FloatConverter();
44      }
45  
46      @Override
47      protected FloatConverter makeConverter(final Float defaultValue) {
48          return new FloatConverter(defaultValue);
49      }
50  
51      @BeforeEach
52      public void setUp() throws Exception {
53          converter = makeConverter();
54          numbers[0] = Float.valueOf("-12");
55          numbers[1] = Float.valueOf("13");
56          numbers[2] = Float.valueOf("-22");
57          numbers[3] = Float.valueOf("23");
58      }
59  
60      @AfterEach
61      public void tearDown() throws Exception {
62          converter = null;
63      }
64  
65      /**
66       * Test Invalid Amounts (too big/small)
67       */
68      @Test
69      public void testInvalidAmount() {
70          final Converter<Float> converter = makeConverter();
71          final Class<?> clazz = Float.class;
72  
73          final Double max = Double.valueOf(Float.MAX_VALUE);
74          final Double tooBig = Double.valueOf(Double.MAX_VALUE);
75  
76          // Maximum
77          assertEquals(Float.valueOf(Float.MAX_VALUE), converter.convert(clazz, max), "Maximum");
78  
79          // Too Large
80          assertThrows(ConversionException.class, () -> converter.convert(clazz, tooBig), "More than maximum, expected ConversionException");
81      }
82  
83      @Test
84      public void testSimpleConversion() {
85          final String[] message = { "from String", "from String", "from String", "from String", "from String", "from String", "from String", "from Byte",
86                  "from Short", "from Integer", "from Long", "from Float", "from Double" };
87  
88          final Object[] input = { String.valueOf(Float.MIN_VALUE), "-17.2", "-1.1", "0.0", "1.1", "17.2", String.valueOf(Float.MAX_VALUE),
89                  Byte.valueOf((byte) 7), Short.valueOf((short) 8), Integer.valueOf(9), Long.valueOf(10), Float.valueOf((float) 11.1), Double.valueOf(12.2), };
90  
91          final Float[] expected = { Float.valueOf(Float.MIN_VALUE), Float.valueOf((float) -17.2), Float.valueOf((float) -1.1), Float.valueOf((float) 0.0),
92                  Float.valueOf((float) 1.1), Float.valueOf((float) 17.2), Float.valueOf(Float.MAX_VALUE), Float.valueOf(7), Float.valueOf(8), Float.valueOf(9),
93                  Float.valueOf(10), Float.valueOf((float) 11.1), Float.valueOf((float) 12.2) };
94  
95          for (int i = 0; i < expected.length; i++) {
96              assertEquals(expected[i].floatValue(), converter.convert(Float.class, input[i]).floatValue(), 0.00001, message[i] + " to Float");
97              assertEquals(expected[i].floatValue(), converter.convert(Float.TYPE, input[i]).floatValue(), 0.00001, message[i] + " to float");
98              assertEquals(expected[i].floatValue(), converter.convert((Class<Float>) null, input[i]).floatValue(), 0.00001, message[i] + " to null type");
99          }
100     }
101 }