KickJava   Java API By Example, From Geeks To Geeks.

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


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 LongDataTypeTest extends AbstractDataTypeTest
36 {
37     private final static DataType THIS_TYPE = DataType.BIGINT;
38
39     public LongDataTypeTest(String JavaDoc name)
40     {
41         super(name);
42     }
43
44     /**
45      *
46      */

47     public void testToString() throws Exception JavaDoc
48     {
49         assertEquals("name", "BIGINT", THIS_TYPE.toString());
50     }
51
52     /**
53      *
54      */

55     public void testGetTypeClass() throws Exception JavaDoc
56     {
57         assertEquals("class", Long JavaDoc.class, THIS_TYPE.getTypeClass());
58     }
59
60     /**
61      *
62      */

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