1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.dbunit.dataset.datatype;
23
24 import org.dbunit.database.ExtendedMockSingleRowResultSet;
25 import org.dbunit.dataset.ITable;
26
27 import java.math.BigDecimal;
28 import java.math.BigInteger;
29 import java.sql.Types;
30
31
32
33
34
35
36 public class NumberDataTypeTest extends AbstractDataTypeTest
37 {
38 private final static DataType[] TYPES = {DataType.NUMERIC, DataType.DECIMAL};
39
40 public NumberDataTypeTest(String name)
41 {
42 super(name);
43 }
44
45 public void testToString() throws Exception
46 {
47 String[] expected = {"NUMERIC", "DECIMAL"};
48
49 assertEquals("type count", expected.length, TYPES.length);
50 for (int i = 0; i < TYPES.length; i++)
51 {
52 assertEquals("name", expected[i], TYPES[i].toString());
53 }
54 }
55
56 public void testGetTypeClass() throws Exception
57 {
58 for (int i = 0; i < TYPES.length; i++)
59 {
60 assertEquals("class", BigDecimal.class, TYPES[i].getTypeClass());
61 }
62 }
63
64 public void testIsNumber() throws Exception
65 {
66 for (int i = 0; i < TYPES.length; i++)
67 {
68 assertEquals("is number", true, TYPES[i].isNumber());
69 }
70 }
71
72 public void testIsDateTime() throws Exception
73 {
74 for (int i = 0; i < TYPES.length; i++)
75 {
76 assertEquals("is date/time", false, TYPES[i].isDateTime());
77 }
78 }
79
80 public void testTypeCast() throws Exception
81 {
82 Object[] values = {
83 null,
84 new BigDecimal((double)1234),
85 "1234",
86 "12.34",
87 Boolean.TRUE,
88 Boolean.FALSE,
89 new Double(2.1),
90 new Float(3.1F),
91 new Integer(4),
92 new Long(5),
93 new Short((short)6),
94 new BigInteger("12345")
95 };
96 BigDecimal[] expected = {
97 null,
98 new BigDecimal((double)1234),
99 new BigDecimal((double)1234),
100 new BigDecimal("12.34"),
101 new BigDecimal("1"),
102 new BigDecimal("0"),
103 new BigDecimal("2.1"),
104 new BigDecimal("3.1"),
105 new BigDecimal("4"),
106 new BigDecimal("5"),
107 new BigDecimal("6"),
108 new BigDecimal("12345")
109 };
110
111 assertEquals("actual vs expected count", values.length, expected.length);
112
113 for (int i = 0; i < TYPES.length; i++)
114 {
115 for (int j = 0; j < values.length; j++)
116 {
117 assertEquals("typecast " + j, expected[j],
118 TYPES[i].typeCast(values[j]));
119 }
120 }
121 }
122
123 public void testTypeCastNone() throws Exception
124 {
125 for (int i = 0; i < TYPES.length; i++)
126 {
127 DataType type = TYPES[i];
128 assertEquals("typecast " + type, null, type.typeCast(ITable.NO_VALUE));
129 }
130 }
131
132 public void testTypeCastInvalid() throws Exception
133 {
134 Object[] values = {new Object(), "bla"};
135
136 for (int i = 0; i < TYPES.length; i++)
137 {
138 for (int j = 0; j < values.length; j++)
139 {
140 try
141 {
142 TYPES[i].typeCast(values[j]);
143 fail("Should throw TypeCastException");
144 }
145 catch (TypeCastException e)
146 {
147 }
148 }
149 }
150 }
151
152 public void testCompareEquals() throws Exception
153 {
154 Object[] values1 = {
155 null,
156 new BigDecimal((double)1234),
157 "1234",
158 "12.34",
159 Boolean.TRUE,
160 Boolean.FALSE,
161 new BigDecimal(123.4),
162 "123",
163 };
164 Object[] values2 = {
165 null,
166 new BigDecimal((double)1234),
167 new BigDecimal(1234),
168 new BigDecimal("12.34"),
169 new BigDecimal("1"),
170 new BigDecimal("0"),
171 new BigDecimal(123.4000),
172 new BigDecimal("123.0"),
173 };
174
175 assertEquals("values count", values1.length, values2.length);
176
177 for (int i = 0; i < TYPES.length; i++)
178 {
179 for (int j = 0; j < values1.length; j++)
180 {
181 assertEquals("compare1 " + j, 0, TYPES[i].compare(values1[j], values2[j]));
182 assertEquals("compare2 " + j, 0, TYPES[i].compare(values2[j], values1[j]));
183 }
184 }
185 }
186
187 public void testCompareInvalid() throws Exception
188 {
189 Object[] values1 = {
190 new Object(),
191 "bla",
192 new java.util.Date()
193 };
194 Object[] values2 = {
195 null,
196 null,
197 null
198 };
199
200 assertEquals("values count", values1.length, values2.length);
201
202 for (int i = 0; i < TYPES.length; i++)
203 {
204 for (int j = 0; j < values1.length; j++)
205 {
206 try
207 {
208 TYPES[i].compare(values1[j], values2[j]);
209 fail("Should throw TypeCastException");
210 }
211 catch (TypeCastException e)
212 {
213 }
214
215 try
216 {
217 TYPES[i].compare(values2[j], values1[j]);
218 fail("Should throw TypeCastException");
219 }
220 catch (TypeCastException e)
221 {
222 }
223 }
224 }
225 }
226
227 public void testCompareDifferent() throws Exception
228 {
229 Object[] less = {
230 null,
231 "-7500",
232 new BigDecimal("-0.01"),
233 new BigInteger("1234"),
234 };
235
236 Object[] greater = {
237 "0",
238 "5.555",
239 new BigDecimal("0.01"),
240 new BigDecimal("1234.5"),
241 };
242
243 assertEquals("values count", less.length, greater.length);
244
245 for (int i = 0; i < TYPES.length; i++)
246 {
247 for (int j = 0; j < less.length; j++)
248 {
249 assertTrue("less " + j, TYPES[i].compare(less[j], greater[j]) < 0);
250 assertTrue("greater " + j, TYPES[i].compare(greater[j], less[j]) > 0);
251 }
252 }
253 }
254
255 public void testSqlType() throws Exception
256 {
257 int[] sqlTypes = {Types.NUMERIC, Types.DECIMAL};
258
259 assertEquals("count", sqlTypes.length, TYPES.length);
260 for (int i = 0; i < TYPES.length; i++)
261 {
262 assertEquals("forSqlType", TYPES[i], DataType.forSqlType(sqlTypes[i]));
263 assertEquals("forSqlTypeName", TYPES[i], DataType.forSqlTypeName(TYPES[i].toString()));
264 assertEquals("getSqlType", sqlTypes[i], TYPES[i].getSqlType());
265 }
266 }
267
268 public void testForObject() throws Exception
269 {
270 assertEquals(DataType.NUMERIC, DataType.forObject(new BigDecimal((double)1234)));
271 }
272
273 public void testAsString() throws Exception
274 {
275 BigDecimal[] values = {
276 new BigDecimal("1234"),
277 };
278
279 String[] expected = {
280 "1234",
281 };
282
283 assertEquals("actual vs expected count", values.length, expected.length);
284
285 for (int i = 0; i < values.length; i++)
286 {
287 assertEquals("asString " + i, expected[i], DataType.asString(values[i]));
288 }
289 }
290
291 public void testGetSqlValue() throws Exception
292 {
293 BigDecimal[] expected = {
294 null,
295 new BigDecimal("12.34"),
296 };
297
298 ExtendedMockSingleRowResultSet resultSet = new ExtendedMockSingleRowResultSet();
299 resultSet.addExpectedIndexedValues(expected);
300
301 for (int i = 0; i < expected.length; i++)
302 {
303 Object expectedValue = expected[i];
304
305 for (int j = 0; j < TYPES.length; j++)
306 {
307 DataType dataType = TYPES[j];
308 Object actualValue = dataType.getSqlValue(i + 1, resultSet);
309 assertEquals("value " + j, expectedValue, actualValue);
310 }
311 }
312 }
313
314 }