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 ByteConverter class.
31   */
32  public class ByteConverterTest extends AbstractNumberConverterTest<Byte> {
33  
34      private Converter<Byte> converter;
35  
36      @Override
37      protected Class<Byte> getExpectedType() {
38          return Byte.class;
39      }
40  
41      @Override
42      protected ByteConverter makeConverter() {
43          return new ByteConverter();
44      }
45  
46      @Override
47      protected ByteConverter makeConverter(final Byte defaultValue) {
48          return new ByteConverter(defaultValue);
49      }
50  
51      @BeforeEach
52      public void setUp() throws Exception {
53          converter = makeConverter();
54          numbers[0] = Byte.valueOf("-12");
55          numbers[1] = Byte.valueOf("13");
56          numbers[2] = Byte.valueOf("-22");
57          numbers[3] = Byte.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<Byte> converter = makeConverter();
71          final Class<Byte> clazz = Byte.class;
72  
73          final Long min = Long.valueOf(Byte.MIN_VALUE);
74          final Long max = Long.valueOf(Byte.MAX_VALUE);
75          final Long minMinusOne = Long.valueOf(min.longValue() - 1);
76          final Long maxPlusOne = Long.valueOf(max.longValue() + 1);
77  
78          // Minimum
79          assertEquals(Byte.valueOf(Byte.MIN_VALUE), converter.convert(clazz, min), "Minimum");
80  
81          // Maximum
82          assertEquals(Byte.valueOf(Byte.MAX_VALUE), converter.convert(clazz, max), "Maximum");
83  
84          // Too Small
85          assertThrows(ConversionException.class, () -> converter.convert(clazz, minMinusOne), "Less than minimum, expected ConversionException");
86  
87          // Too Large
88          assertThrows(ConversionException.class, () -> converter.convert(clazz, maxPlusOne), "More than maximum, expected ConversionException");
89      }
90  
91      @Test
92      public void testSimpleConversion() throws Exception {
93          final String[] message = { "from String", "from String", "from String", "from String", "from String", "from String", "from String", "from Byte",
94                  "from Short", "from Integer", "from Long", "from Float", "from Double" };
95  
96          final Object[] input = { String.valueOf(Byte.MIN_VALUE), "-17", "-1", "0", "1", "17", String.valueOf(Byte.MAX_VALUE), Byte.valueOf((byte) 7),
97                  Short.valueOf((short) 8), Integer.valueOf(9), Long.valueOf(10), Float.valueOf((float) 11.1), Double.valueOf(12.2) };
98  
99          final Byte[] expected = { Byte.valueOf(Byte.MIN_VALUE), Byte.valueOf((byte) -17), Byte.valueOf((byte) -1), Byte.valueOf((byte) 0),
100                 Byte.valueOf((byte) 1), Byte.valueOf((byte) 17), Byte.valueOf(Byte.MAX_VALUE), Byte.valueOf((byte) 7), Byte.valueOf((byte) 8),
101                 Byte.valueOf((byte) 9), Byte.valueOf((byte) 10), Byte.valueOf((byte) 11), Byte.valueOf((byte) 12) };
102 
103         for (int i = 0; i < expected.length; i++) {
104             assertEquals(expected[i], converter.convert(Byte.class, input[i]), message[i] + " to Byte");
105             assertEquals(expected[i], converter.convert(Byte.TYPE, input[i]), message[i] + " to byte");
106             assertEquals(expected[i], converter.convert(null, input[i]), message[i] + " to null type");
107         }
108     }
109 
110 }