KickJava   Java API By Example, From Geeks To Geeks.

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


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.12 $
33  */

34
35 public class IntegerDataTypeTest extends AbstractDataTypeTest
36 {
37     private final static DataType[] TYPES = {
38         DataType.TINYINT,
39         DataType.SMALLINT,
40         DataType.INTEGER,
41     };
42
43     public IntegerDataTypeTest(String JavaDoc name)
44     {
45         super(name);
46     }
47
48     /**
49      *
50      */

51     public void testToString() throws Exception JavaDoc
52     {
53         String JavaDoc[] expected = {
54             "TINYINT",
55             "SMALLINT",
56             "INTEGER",
57         };
58
59         assertEquals("type count", expected.length, TYPES.length);
60         for (int i = 0; i < TYPES.length; i++)
61         {
62             assertEquals("name", expected[i], TYPES[i].toString());
63         }
64     }
65
66     public void testGetTypeClass() throws Exception JavaDoc
67     {
68         for (int i = 0; i < TYPES.length; i++)
69         {
70             assertEquals("class", Integer JavaDoc.class, TYPES[i].getTypeClass());
71         }
72     }
73
74     public void testIsNumber() throws Exception JavaDoc
75     {
76         for (int i = 0; i < TYPES.length; i++)
77         {
78             assertEquals("is number", true, TYPES[i].isNumber());
79         }
80     }
81
82     public void testIsDateTime() throws Exception JavaDoc
83     {
84         for (int i = 0; i < TYPES.length; i++)
85         {
86             assertEquals("is date/time", false, TYPES[i].isDateTime());
87         }
88     }
89
90     public void testTypeCast() throws Exception JavaDoc
91     {
92         Object JavaDoc[] values = {
93             null,
94             "5",
95             new Long JavaDoc(Integer.MAX_VALUE - 1),
96             new Double JavaDoc(Integer.MIN_VALUE + 1),
97             "-7500",
98             new Long JavaDoc(Integer.MAX_VALUE),
99             new Double JavaDoc(Integer.MIN_VALUE),
100             new Float JavaDoc(0.666),
101             new Double JavaDoc(0.666),
102             new Double JavaDoc(5.49),
103             "-99.9",
104             new Double JavaDoc(1.5E2),
105             new BigDecimal JavaDoc(1234),
106         };
107
108         Integer JavaDoc[] expected = {
109             null,
110             new Integer JavaDoc(5),
111             new Integer JavaDoc(Integer.MAX_VALUE - 1),
112             new Integer JavaDoc(Integer.MIN_VALUE + 1),
113             new Integer JavaDoc(-7500),
114             new Integer JavaDoc(Integer.MAX_VALUE),
115             new Integer JavaDoc(Integer.MIN_VALUE),
116             new Integer JavaDoc(0),
117             new Integer JavaDoc(0),
118             new Integer JavaDoc(5),
119             new Integer JavaDoc(-99),
120             new Integer JavaDoc(150),
121             new Integer JavaDoc(1234),
122
123         };
124
125         assertEquals("actual vs expected count", values.length, expected.length);
126
127         for (int i = 0; i < TYPES.length; i++)
128         {
129             for (int j = 0; j < values.length; j++)
130             {
131                 assertEquals("typecast " + j, expected[j],
132                         TYPES[i].typeCast(values[j]));
133             }
134         }
135     }
136
137     public void testTypeCastNone() throws Exception JavaDoc
138     {
139         for (int i = 0; i < TYPES.length; i++)
140         {
141             DataType type = TYPES[i];
142             assertEquals("typecast " + type, null, type.typeCast(ITable.NO_VALUE));
143         }
144     }
145
146     public void testTypeCastInvalid() throws Exception JavaDoc
147     {
148         Object JavaDoc[] values = {
149             new Object JavaDoc(),
150             "bla",
151             new java.util.Date JavaDoc()
152         };
153
154         for (int i = 0; i < TYPES.length; i++)
155         {
156             for (int j = 0; j < values.length; j++)
157             {
158                 try
159                 {
160                     TYPES[i].typeCast(values[j]);
161                     fail("Should throw TypeCastException");
162                 }
163                 catch (TypeCastException e)
164                 {
165                 }
166             }
167         }
168     }
169
170     public void testCompareEquals() throws Exception JavaDoc
171     {
172         Object JavaDoc[] values1 = {
173             null,
174             "5",
175             new Long JavaDoc(Integer.MAX_VALUE - 1),
176             new Double JavaDoc(Integer.MIN_VALUE + 1),
177             "-7500",
178             new Long JavaDoc(Integer.MAX_VALUE),
179             new Double JavaDoc(Integer.MIN_VALUE),
180             new Float JavaDoc(0.666),
181             new Double JavaDoc(0.666),
182             new Double JavaDoc(5.49),
183             "-99.9",
184             new Double JavaDoc(1.5E2),
185             new BigDecimal JavaDoc(1234),
186         };
187
188         Object JavaDoc[] values2 = {
189             null,
190             new Integer JavaDoc(5),
191             new Integer JavaDoc(Integer.MAX_VALUE - 1),
192             new Integer JavaDoc(Integer.MIN_VALUE + 1),
193             new Integer JavaDoc(-7500),
194             new Integer JavaDoc(Integer.MAX_VALUE),
195             new Integer JavaDoc(Integer.MIN_VALUE),
196             new Integer JavaDoc(0),
197             new Integer JavaDoc(0),
198             new Integer JavaDoc(5),
199             new Integer JavaDoc(-99),
200             new Integer JavaDoc(150),
201             new Integer JavaDoc(1234),
202         };
203
204         assertEquals("values count", values1.length, values2.length);
205
206         for (int i = 0; i < TYPES.length; i++)
207         {
208             for (int j = 0; j < values1.length; j++)
209             {
210                 assertEquals("compare1 " + j, 0, TYPES[i].compare(values1[j], values2[j]));
211                 assertEquals("compare2 " + j, 0, TYPES[i].compare(values2[j], values1[j]));
212             }
213         }
214     }
215
216     public void testCompareInvalid() throws Exception JavaDoc
217     {
218         Object JavaDoc[] values1 = {
219             new Object JavaDoc(),
220             "bla",
221             new java.util.Date JavaDoc()
222         };
223         Object JavaDoc[] values2 = {
224             null,
225             null,
226             null
227         };
228
229         assertEquals("values count", values1.length, values2.length);
230
231         for (int i = 0; i < TYPES.length; i++)
232         {
233             for (int j = 0; j < values1.length; j++)
234             {
235                 try
236                 {
237                     TYPES[i].compare(values1[j], values2[j]);
238                     fail("Should throw TypeCastException");
239                 }
240                 catch (TypeCastException e)
241                 {
242                 }
243
244                 try
245                 {
246                     TYPES[i].compare(values2[j], values1[j]);
247                     fail("Should throw TypeCastException");
248                 }
249                 catch (TypeCastException e)
250                 {
251                 }
252             }
253         }
254     }
255
256     public void testCompareDifferent() throws Exception JavaDoc
257     {
258         Object JavaDoc[] less = {
259             null,
260             "-7500",
261             new Double JavaDoc(Float.MIN_VALUE),
262         };
263
264         Object JavaDoc[] greater = {
265             "0",
266             "5.555",
267             new Float JavaDoc(Float.MAX_VALUE),
268         };
269
270         assertEquals("values count", less.length, greater.length);
271
272         for (int i = 0; i < TYPES.length; i++)
273         {
274             for (int j = 0; j < less.length; j++)
275             {
276                 assertTrue("less " + j, TYPES[i].compare(less[j], greater[j]) < 0);
277                 assertTrue("greater " + j, TYPES[i].compare(greater[j], less[j]) > 0);
278             }
279         }
280     }
281
282     public void testSqlType() throws Exception JavaDoc
283     {
284         int[] sqlTypes = {
285             Types.TINYINT,
286             Types.SMALLINT,
287             Types.INTEGER,
288         };
289
290         assertEquals("count", sqlTypes.length, TYPES.length);
291         for (int i = 0; i < TYPES.length; i++)
292         {
293             assertEquals("forSqlType", TYPES[i], DataType.forSqlType(sqlTypes[i]));
294             assertEquals("forSqlTypeName", TYPES[i], DataType.forSqlTypeName(TYPES[i].toString()));
295             assertEquals("getSqlType", sqlTypes[i], TYPES[i].getSqlType());
296         }
297     }
298
299     /**
300      *
301      */

302     public void testForObject() throws Exception JavaDoc
303     {
304         assertEquals(DataType.INTEGER, DataType.forObject(new Integer JavaDoc(1234)));
305     }
306
307     public void testAsString() throws Exception JavaDoc
308     {
309         Object JavaDoc[] values = {
310             new Integer JavaDoc("1234"),
311         };
312
313         String JavaDoc[] expected = {
314             "1234",
315         };
316
317         assertEquals("actual vs expected count", values.length, expected.length);
318
319         for (int i = 0; i < values.length; i++)
320         {
321             assertEquals("asString " + i, expected[i], DataType.asString(values[i]));
322         }
323     }
324
325     public void testGetSqlValue() throws Exception JavaDoc
326     {
327         Integer JavaDoc[] expected = {
328             null,
329             new Integer JavaDoc(5),
330             new Integer JavaDoc(Integer.MAX_VALUE - 1),
331             new Integer JavaDoc(Integer.MIN_VALUE + 1),
332             new Integer JavaDoc(-7500),
333         };
334
335         ExtendedMockSingleRowResultSet resultSet = new ExtendedMockSingleRowResultSet();
336         resultSet.addExpectedIndexedValues(expected);
337
338         for (int i = 0; i < expected.length; i++)
339         {
340             Object JavaDoc expectedValue = expected[i];
341
342             for (int j = 0; j < TYPES.length; j++)
343             {
344                 DataType dataType = TYPES[j];
345                 Object JavaDoc actualValue = dataType.getSqlValue(i + 1, resultSet);
346                 assertEquals("value " + j, expectedValue, actualValue);
347             }
348         }
349     }
350
351 }
352
Popular Tags