1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
31
32 public class ShortConverterTest extends AbstractNumberConverterTest<Short> {
33
34 private ShortConverter converter;
35
36 @Override
37 protected Class<Short> getExpectedType() {
38 return Short.class;
39 }
40
41 @Override
42 protected ShortConverter makeConverter() {
43 return new ShortConverter();
44 }
45
46 @Override
47 protected ShortConverter makeConverter(final Short defaultValue) {
48 return new ShortConverter(defaultValue);
49 }
50
51 @BeforeEach
52 public void setUp() throws Exception {
53 converter = makeConverter();
54 numbers[0] = Short.valueOf("-12");
55 numbers[1] = Short.valueOf("13");
56 numbers[2] = Short.valueOf("-22");
57 numbers[3] = Short.valueOf("23");
58 }
59
60 @AfterEach
61 public void tearDown() throws Exception {
62 converter = null;
63 }
64
65
66
67
68 @Test
69 public void testInvalidAmount() {
70 final Converter<Short> converter = makeConverter();
71 final Class<?> clazz = Short.class;
72
73 final Long min = Long.valueOf(Short.MIN_VALUE);
74 final Long max = Long.valueOf(Short.MAX_VALUE);
75 final Long minMinusOne = Long.valueOf(min.longValue() - 1);
76 final Long maxPlusOne = Long.valueOf(max.longValue() + 1);
77
78
79 assertEquals(Short.valueOf(Short.MIN_VALUE), converter.convert(clazz, min), "Minimum");
80
81
82 assertEquals(Short.valueOf(Short.MAX_VALUE), converter.convert(clazz, max), "Maximum");
83
84
85 assertThrows(ConversionException.class, () -> converter.convert(clazz, minMinusOne), "Less than minimum, expected ConversionException");
86
87
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(Short.MIN_VALUE), "-17", "-1", "0", "1", "17", String.valueOf(Short.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 Short[] expected = { Short.valueOf(Short.MIN_VALUE), Short.valueOf((short) -17), Short.valueOf((short) -1), Short.valueOf((short) 0),
100 Short.valueOf((short) 1), Short.valueOf((short) 17), Short.valueOf(Short.MAX_VALUE), Short.valueOf((short) 7), Short.valueOf((short) 8),
101 Short.valueOf((short) 9), Short.valueOf((short) 10), Short.valueOf((short) 11), Short.valueOf((short) 12) };
102
103 for (int i = 0; i < expected.length; i++) {
104 assertEquals(expected[i], converter.convert(Short.class, input[i]), message[i] + " to Short");
105 assertEquals(expected[i], converter.convert(Short.TYPE, input[i]), message[i] + " to short");
106 assertEquals(expected[i], converter.convert(null, input[i]), message[i] + " to null type");
107 }
108 }
109 }