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 FloatConverterTestCase extends NumberConverterTestBase {
32
33 private Converter converter = null;
34
35
36
37 public FloatConverterTestCase(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 Float("-12");
47 numbers[1] = new Float("13");
48 numbers[2] = new Float("-22");
49 numbers[3] = new Float("23");
50 }
51
52 public static TestSuite suite() {
53 return new TestSuite(FloatConverterTestCase.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 FloatConverter();
66 }
67
68 @Override
69 protected NumberConverter makeConverter(final Object defaultValue) {
70 return new FloatConverter(defaultValue);
71 }
72
73 @Override
74 protected Class<?> getExpectedType() {
75 return Float.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(Float.MIN_VALUE),
99 "-17.2",
100 "-1.1",
101 "0.0",
102 "1.1",
103 "17.2",
104 String.valueOf(Float.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 Float[] expected = {
114 new Float(Float.MIN_VALUE),
115 new Float(-17.2),
116 new Float(-1.1),
117 new Float(0.0),
118 new Float(1.1),
119 new Float(17.2),
120 new Float(Float.MAX_VALUE),
121 new Float(7),
122 new Float(8),
123 new Float(9),
124 new Float(10),
125 new Float(11.1),
126 new Float(12.2)
127 };
128
129 for(int i=0;i<expected.length;i++) {
130 assertEquals(
131 message[i] + " to Float",
132 expected[i].floatValue(),
133 (converter.convert(Float.class,input[i])).floatValue(),
134 0.00001);
135 assertEquals(
136 message[i] + " to float",
137 expected[i].floatValue(),
138 (converter.convert(Float.TYPE,input[i])).floatValue(),
139 0.00001);
140 assertEquals(
141 message[i] + " to null type",
142 expected[i].floatValue(),
143 ((Float)(converter.convert(null,input[i]))).floatValue(),
144 0.00001);
145 }
146 }
147
148
149
150
151
152 public void testInvalidAmount() {
153 final Converter converter = makeConverter();
154 final Class<?> clazz = Float.class;
155
156 final Double max = new Double(Float.MAX_VALUE);
157 final Double tooBig = new Double(Double.MAX_VALUE);
158
159
160 assertEquals("Maximum", new Float(Float.MAX_VALUE), converter.convert(clazz, max));
161
162
163 try {
164 assertEquals("Too Big", null, converter.convert(clazz, tooBig));
165 fail("More than maximum, expected ConversionException");
166 } catch (final Exception e) {
167
168 }
169 }
170 }
171