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.sql.Types;
29
30
31
32
33
34
35 public class DoubleDataTypeTest extends AbstractDataTypeTest
36 {
37 private final static DataType[] TYPES = {DataType.FLOAT, DataType.DOUBLE};
38
39 public DoubleDataTypeTest(String name)
40 {
41 super(name);
42 }
43
44 public void testToString() throws Exception
45 {
46 String[] expected = {"FLOAT", "DOUBLE"};
47
48 assertEquals("type count", expected.length, TYPES.length);
49 for (int i = 0; i < TYPES.length; i++)
50 {
51 assertEquals("name", expected[i], TYPES[i].toString());
52 }
53 }
54
55 public void testGetTypeClass() throws Exception
56 {
57 for (int i = 0; i < TYPES.length; i++)
58 {
59 assertEquals("class", Double.class, TYPES[i].getTypeClass());
60 }
61 }
62
63 public void testIsNumber() throws Exception
64 {
65 for (int i = 0; i < TYPES.length; i++)
66 {
67 assertEquals("is number", true, TYPES[i].isNumber());
68 }
69 }
70
71 public void testIsDateTime() throws Exception
72 {
73 for (int i = 0; i < TYPES.length; i++)
74 {
75 assertEquals("is date/time", false, TYPES[i].isDateTime());
76 }
77 }
78
79 public void testTypeCast() throws Exception
80 {
81 Object[] values = {
82 null,
83 "5.555",
84 new Float(Float.MAX_VALUE),
85 new Double(Double.MIN_VALUE),
86 "-7500",
87 "2.34E23",
88 new Double(0.666),
89 new Double(5.49879),
90 "-99.9",
91 new BigDecimal((double)1234),
92 };
93
94 Double[] expected = {
95 null,
96 new Double(5.555),
97 new Double(Float.MAX_VALUE),
98 new Double(Double.MIN_VALUE),
99 new Double(-7500),
100 Double.valueOf("2.34E23"),
101 new Double(0.666),
102 new Double(5.49879),
103 new Double(-99.9),
104 new Double(1234),
105 };
106
107 assertEquals("actual vs expected count", values.length, expected.length);
108
109 for (int i = 0; i < TYPES.length; i++)
110 {
111 for (int j = 0; j < values.length; j++)
112 {
113 assertEquals("typecast " + j, expected[j],
114 TYPES[i].typeCast(values[j]));
115 }
116 }
117 }
118
119
120 public void testTypeCastNone() throws Exception
121 {
122 for (int i = 0; i < TYPES.length; i++)
123 {
124 DataType type = TYPES[i];
125 assertEquals("typecast " + type, null, type.typeCast(ITable.NO_VALUE));
126 }
127 }
128
129 public void testTypeCastInvalid() throws Exception
130 {
131 Object[] values = {
132 new Object(),
133 "bla",
134 new java.util.Date()};
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 "5.555",
157 new Float(Float.MAX_VALUE),
158 new Double(Double.MIN_VALUE),
159 "-7500",
160 "2.34E23",
161 new Double(0.666),
162 new Double(5.49879),
163 "-99.9",
164 new BigDecimal((double)1234),
165 "123",
166 };
167
168 Object[] values2 = {
169 null,
170 new Double(5.555),
171 new Double(Float.MAX_VALUE),
172 new Double(Double.MIN_VALUE),
173 new Double(-7500),
174 Double.valueOf("2.34E23"),
175 new Double(0.666),
176 new Double(5.49879),
177 new Double(-99.9),
178 new Double(1234),
179 new Double(123.0)
180 };
181
182 assertEquals("values count", values1.length, values2.length);
183
184 for (int i = 0; i < TYPES.length; i++)
185 {
186 for (int j = 0; j < values1.length; j++)
187 {
188 assertEquals("compare1 " + j, 0, TYPES[i].compare(values1[j], values2[j]));
189 assertEquals("compare2 " + j, 0, TYPES[i].compare(values2[j], values1[j]));
190 }
191 }
192 }
193
194 public void testCompareInvalid() throws Exception
195 {
196 Object[] values1 = {
197 new Object(),
198 "bla",
199 new java.util.Date()
200 };
201 Object[] values2 = {
202 null,
203 null,
204 null
205 };
206
207 assertEquals("values count", values1.length, values2.length);
208
209 for (int i = 0; i < TYPES.length; i++)
210 {
211 for (int j = 0; j < values1.length; j++)
212 {
213 try
214 {
215 TYPES[i].compare(values1[j], values2[j]);
216 fail("Should throw TypeCastException");
217 }
218 catch (TypeCastException e)
219 {
220 }
221
222 try
223 {
224 TYPES[i].compare(values2[j], values1[j]);
225 fail("Should throw TypeCastException");
226 }
227 catch (TypeCastException e)
228 {
229 }
230 }
231 }
232 }
233
234 public void testCompareDifferent() throws Exception
235 {
236 Object[] less = {
237 null,
238 "-7500",
239 new Double(Float.MIN_VALUE),
240 };
241
242 Object[] greater = {
243 "0",
244 "5.555",
245 new Float(Float.MAX_VALUE),
246 };
247
248 assertEquals("values count", less.length, greater.length);
249
250 for (int i = 0; i < TYPES.length; i++)
251 {
252 for (int j = 0; j < less.length; j++)
253 {
254 assertTrue("less " + j, TYPES[i].compare(less[j], greater[j]) < 0);
255 assertTrue("greater " + j, TYPES[i].compare(greater[j], less[j]) > 0);
256 }
257 }
258 }
259
260 public void testSqlType() throws Exception
261 {
262 int[] sqlTypes = {Types.FLOAT, Types.DOUBLE};
263
264 assertEquals("count", sqlTypes.length, TYPES.length);
265 for (int i = 0; i < TYPES.length; i++)
266 {
267 assertEquals("forSqlType", TYPES[i], DataType.forSqlType(sqlTypes[i]));
268 assertEquals("forSqlTypeName", TYPES[i], DataType.forSqlTypeName(TYPES[i].toString()));
269 assertEquals("getSqlType", sqlTypes[i], TYPES[i].getSqlType());
270 }
271 }
272
273 public void testForObject() throws Exception
274 {
275 assertEquals(DataType.DOUBLE, DataType.forObject(new Double(1234)));
276 }
277
278 public void testAsString() throws Exception
279 {
280 Object[] values = {
281 new Double("1234"),
282 new Double("12.34"),
283 };
284
285 String[] expected = {
286 "1234.0",
287 "12.34",
288 };
289
290 assertEquals("actual vs expected count", values.length, expected.length);
291
292 for (int i = 0; i < values.length; i++)
293 {
294 assertEquals("asString " + i, expected[i], DataType.asString(values[i]));
295 }
296 }
297
298 public void testGetSqlValue() throws Exception
299 {
300 Double[] expected = {
301 null,
302 new Double(5.555),
303 new Double(Float.MAX_VALUE),
304 new Double(Double.MIN_VALUE),
305 new Double(-7500),
306 Double.valueOf("2.34E23"),
307 new Double(0.666),
308 new Double(5.49879),
309 new Double(-99.9),
310 new Double(1234),
311 };
312
313 ExtendedMockSingleRowResultSet resultSet = new ExtendedMockSingleRowResultSet();
314 resultSet.addExpectedIndexedValues(expected);
315
316 for (int i = 0; i < expected.length; i++)
317 {
318 Object expectedValue = expected[i];
319
320 for (int j = 0; j < TYPES.length; j++)
321 {
322 DataType dataType = TYPES[j];
323 Object actualValue = dataType.getSqlValue(i + 1, resultSet);
324 assertEquals("value " + j, expectedValue, actualValue);
325 }
326 }
327 }
328
329 }