KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > dataset > datatype > DoubleDataTypeTest


1 /*
2  *
3  * The DbUnit Database Testing Framework
4  * Copyright (C)2002-2004, DbUnit.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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 JavaDoc;
28 import java.sql.Types JavaDoc;
29
30 /**
31  * @author Manuel Laflamme
32  * @version $Revision: 1.13 $
33  */

34
35 public class DoubleDataTypeTest extends AbstractDataTypeTest
36 {
37     private final static DataType[] TYPES = {DataType.FLOAT, DataType.DOUBLE};
38
39     public DoubleDataTypeTest(String JavaDoc name)
40     {
41         super(name);
42     }
43
44     public void testToString() throws Exception JavaDoc
45     {
46         String JavaDoc[] 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 JavaDoc
56     {
57         for (int i = 0; i < TYPES.length; i++)
58         {
59             assertEquals("class", Double JavaDoc.class, TYPES[i].getTypeClass());
60         }
61     }
62
63     public void testIsNumber() throws Exception JavaDoc
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 JavaDoc
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 JavaDoc
80     {
81         Object JavaDoc[] values = {
82             null,
83             "5.555",
84             new Float JavaDoc(Float.MAX_VALUE),
85             new Double JavaDoc(Double.MIN_VALUE),
86             "-7500",
87             "2.34E23",
88             new Double JavaDoc(0.666),
89             new Double JavaDoc(5.49879),
90             "-99.9",
91             new BigDecimal JavaDoc(1234),
92         };
93
94         Double JavaDoc[] expected = {
95             null,
96             new Double JavaDoc(5.555),
97             new Double JavaDoc(Float.MAX_VALUE),
98             new Double JavaDoc(Double.MIN_VALUE),
99             new Double JavaDoc(-7500),
100             Double.valueOf("2.34E23"),
101             new Double JavaDoc(0.666),
102             new Double JavaDoc(5.49879),
103             new Double JavaDoc(-99.9),
104             new Double JavaDoc(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 JavaDoc
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 JavaDoc
130     {
131         Object JavaDoc[] values = {
132             new Object JavaDoc(),
133             "bla",
134             new java.util.Date JavaDoc()};
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 JavaDoc
153     {
154         Object JavaDoc[] values1 = {
155             null,
156             "5.555",
157             new Float JavaDoc(Float.MAX_VALUE),
158             new Double JavaDoc(Double.MIN_VALUE),
159             "-7500",
160             "2.34E23",
161             new Double JavaDoc(0.666),
162             new Double JavaDoc(5.49879),
163             "-99.9",
164             new BigDecimal JavaDoc(1234),
165             "123",
166         };
167
168         Object JavaDoc[] values2 = {
169             null,
170             new Double JavaDoc(5.555),
171             new Double JavaDoc(Float.MAX_VALUE),
172             new Double JavaDoc(Double.MIN_VALUE),
173             new Double JavaDoc(-7500),
174             Double.valueOf("2.34E23"),
175             new Double JavaDoc(0.666),
176             new Double JavaDoc(5.49879),
177             new Double JavaDoc(-99.9),
178             new Double JavaDoc(1234),
179             new Double JavaDoc(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 JavaDoc
195     {
196         Object JavaDoc[] values1 = {
197             new Object JavaDoc(),
198             "bla",
199             new java.util.Date JavaDoc()
200         };
201         Object JavaDoc[] 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 JavaDoc
235     {
236         Object JavaDoc[] less = {
237             null,
238             "-7500",
239             new Double JavaDoc(Float.MIN_VALUE),
240         };
241
242         Object JavaDoc[] greater = {
243             "0",
244             "5.555",
245             new Float JavaDoc(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 JavaDoc
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 JavaDoc
274     {
275         assertEquals(DataType.DOUBLE, DataType.forObject(new Double JavaDoc(1234)));
276     }
277
278     public void testAsString() throws Exception JavaDoc
279     {
280         Object JavaDoc[] values = {
281             new Double JavaDoc("1234"),
282             new Double JavaDoc("12.34"),
283         };
284
285         String JavaDoc[] 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 JavaDoc
299     {
300         Double JavaDoc[] expected = {
301             null,
302             new Double JavaDoc(5.555),
303             new Double JavaDoc(Float.MAX_VALUE),
304             new Double JavaDoc(Double.MIN_VALUE),
305             new Double JavaDoc(-7500),
306             Double.valueOf("2.34E23"),
307             new Double JavaDoc(0.666),
308             new Double JavaDoc(5.49879),
309             new Double JavaDoc(-99.9),
310             new Double JavaDoc(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 JavaDoc expectedValue = expected[i];
319
320             for (int j = 0; j < TYPES.length; j++)
321             {
322                 DataType dataType = TYPES[j];
323                 Object JavaDoc actualValue = dataType.getSqlValue(i + 1, resultSet);
324                 assertEquals("value " + j, expectedValue, actualValue);
325             }
326         }
327     }
328
329 }
330
Popular Tags