1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.beanutils.converters;
19
20 import junit.framework.TestSuite;
21
22 import org.apache.commons.beanutils.Converter;
23
24
25
26
27
28
29
30
31 public class ShortConverterTestCase extends NumberConverterTestBase {
32
33 private Converter converter = null;
34
35
36
37 public ShortConverterTestCase(final String name) {
38 super(name);
39 }
40
41
42
43 @Override
44 public void setUp() throws Exception {
45 converter = makeConverter();
46 numbers[0] = new Short("-12");
47 numbers[1] = new Short("13");
48 numbers[2] = new Short("-22");
49 numbers[3] = new Short("23");
50 }
51
52 public static TestSuite suite() {
53 return new TestSuite(ShortConverterTestCase.class);
54 }
55
56 @Override
57 public void tearDown() throws Exception {
58 converter = null;
59 }
60
61
62
63 @Override
64 protected NumberConverter makeConverter() {
65 return new ShortConverter();
66 }
67
68 @Override
69 protected NumberConverter makeConverter(final Object defaultValue) {
70 return new ShortConverter(defaultValue);
71 }
72
73 @Override
74 protected Class<?> getExpectedType() {
75 return Short.class;
76 }
77
78
79
80 public void testSimpleConversion() throws Exception {
81 final String[] message= {
82 "from String",
83 "from String",
84 "from String",
85 "from String",
86 "from String",
87 "from String",
88 "from String",
89 "from Byte",
90 "from Short",
91 "from Integer",
92 "from Long",
93 "from Float",
94 "from Double"
95 };
96
97 final Object[] input = {
98 String.valueOf(Short.MIN_VALUE),
99 "-17",
100 "-1",
101 "0",
102 "1",
103 "17",
104 String.valueOf(Short.MAX_VALUE),
105 new Byte((byte)7),
106 new Short((short)8),
107 new Integer(9),
108 new Long(10),
109 new Float(11.1),
110 new Double(12.2)
111 };
112
113 final Short[] expected = {
114 new Short(Short.MIN_VALUE),
115 new Short((short)-17),
116 new Short((short)-1),
117 new Short((short)0),
118 new Short((short)1),
119 new Short((short)17),
120 new Short(Short.MAX_VALUE),
121 new Short((short)7),
122 new Short((short)8),
123 new Short((short)9),
124 new Short((short)10),
125 new Short((short)11),
126 new Short((short)12)
127 };
128
129 for(int i=0;i<expected.length;i++) {
130 assertEquals(message[i] + " to Short",expected[i],converter.convert(Short.class,input[i]));
131 assertEquals(message[i] + " to short",expected[i],converter.convert(Short.TYPE,input[i]));
132 assertEquals(message[i] + " to null type",expected[i],converter.convert(null,input[i]));
133 }
134 }
135
136
137
138
139 public void testInvalidAmount() {
140 final Converter converter = makeConverter();
141 final Class<?> clazz = Short.class;
142
143 final Long min = new Long(Short.MIN_VALUE);
144 final Long max = new Long(Short.MAX_VALUE);
145 final Long minMinusOne = new Long(min.longValue() - 1);
146 final Long maxPlusOne = new Long(max.longValue() + 1);
147
148
149 assertEquals("Minimum", new Short(Short.MIN_VALUE), converter.convert(clazz, min));
150
151
152 assertEquals("Maximum", new Short(Short.MAX_VALUE), converter.convert(clazz, max));
153
154
155 try {
156 assertEquals("Minimum - 1", null, converter.convert(clazz, minMinusOne));
157 fail("Less than minimum, expected ConversionException");
158 } catch (final Exception e) {
159
160 }
161
162
163 try {
164 assertEquals("Maximum + 1", null, converter.convert(clazz, maxPlusOne));
165 fail("More than maximum, expected ConversionException");
166 } catch (final Exception e) {
167
168 }
169 }
170 }
171