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 ByteConverterTestCase extends NumberConverterTestBase {
32
33 private Converter converter = null;
34
35
36
37 public ByteConverterTestCase(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 Byte("-12");
47 numbers[1] = new Byte("13");
48 numbers[2] = new Byte("-22");
49 numbers[3] = new Byte("23");
50 }
51
52 public static TestSuite suite() {
53 return new TestSuite(ByteConverterTestCase.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 ByteConverter();
66 }
67
68 @Override
69 protected NumberConverter makeConverter(final Object defaultValue) {
70 return new ByteConverter(defaultValue);
71 }
72 @Override
73 protected Class<?> getExpectedType() {
74 return Byte.class;
75 }
76
77
78
79 public void testSimpleConversion() throws Exception {
80 final String[] message= {
81 "from String",
82 "from String",
83 "from String",
84 "from String",
85 "from String",
86 "from String",
87 "from String",
88 "from Byte",
89 "from Short",
90 "from Integer",
91 "from Long",
92 "from Float",
93 "from Double"
94 };
95
96 final Object[] input = {
97 String.valueOf(Byte.MIN_VALUE),
98 "-17",
99 "-1",
100 "0",
101 "1",
102 "17",
103 String.valueOf(Byte.MAX_VALUE),
104 new Byte((byte)7),
105 new Short((short)8),
106 new Integer(9),
107 new Long(10),
108 new Float(11.1),
109 new Double(12.2)
110 };
111
112 final Byte[] expected = {
113 new Byte(Byte.MIN_VALUE),
114 new Byte((byte)-17),
115 new Byte((byte)-1),
116 new Byte((byte)0),
117 new Byte((byte)1),
118 new Byte((byte)17),
119 new Byte(Byte.MAX_VALUE),
120 new Byte((byte)7),
121 new Byte((byte)8),
122 new Byte((byte)9),
123 new Byte((byte)10),
124 new Byte((byte)11),
125 new Byte((byte)12)
126 };
127
128 for(int i=0;i<expected.length;i++) {
129 assertEquals(message[i] + " to Byte",expected[i],converter.convert(Byte.class,input[i]));
130 assertEquals(message[i] + " to byte",expected[i],converter.convert(Byte.TYPE,input[i]));
131 assertEquals(message[i] + " to null type",expected[i],converter.convert(null,input[i]));
132 }
133 }
134
135
136
137
138 public void testInvalidAmount() {
139 final Converter converter = makeConverter();
140 final Class<?> clazz = Byte.class;
141
142 final Long min = new Long(Byte.MIN_VALUE);
143 final Long max = new Long(Byte.MAX_VALUE);
144 final Long minMinusOne = new Long(min.longValue() - 1);
145 final Long maxPlusOne = new Long(max.longValue() + 1);
146
147
148 assertEquals("Minimum", new Byte(Byte.MIN_VALUE), converter.convert(clazz, min));
149
150
151 assertEquals("Maximum", new Byte(Byte.MAX_VALUE), converter.convert(clazz, max));
152
153
154 try {
155 assertEquals("Minimum - 1", null, converter.convert(clazz, minMinusOne));
156 fail("Less than minimum, expected ConversionException");
157 } catch (final Exception e) {
158
159 }
160
161
162 try {
163 assertEquals("Maximum + 1", null, converter.convert(clazz, maxPlusOne));
164 fail("More than maximum, expected ConversionException");
165 } catch (final Exception e) {
166
167 }
168 }
169
170 }
171